All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.codestory.http.payload.Payload Maven / Gradle / Ivy

/**
 * Copyright (C) 2013-2014 [email protected]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package net.codestory.http.payload;

import static net.codestory.http.constants.Headers.*;
import static net.codestory.http.constants.HttpStatus.*;

import java.net.*;
import java.util.*;

import net.codestory.http.*;
import net.codestory.http.convert.*;

public class Payload {
  private final String contentType;
  private final Object content;
  private final Map headers;
  private final List cookies;
  private int code;

  public Payload(Object content) {
    this(null, content);
  }

  public Payload(String contentType, Object content) {
    this(contentType, content, OK);
  }

  public Payload(int code) {
    this(null, null, code);
  }

  public Payload(String contentType, Object content, int code) {
    if (content instanceof Payload) {
      Payload wrapped = (Payload) content;
      this.contentType = (null == contentType) ? wrapped.contentType : contentType;
      this.content = wrapped.content;
      this.code = wrapped.code;
      this.headers = new LinkedHashMap<>(wrapped.headers);
      this.cookies = new ArrayList<>(wrapped.cookies);
      return;
    }

    if (content instanceof Optional) {
      Optional optional = (Optional) content;
      if (optional.isPresent()) {
        this.content = optional.get();
        this.code = code;
      } else {
        this.content = null;
        this.code = NOT_FOUND;
      }
    } else {
      this.content = content;
      this.code = code;
    }

    this.contentType = contentType;
    this.headers = new LinkedHashMap<>();
    this.cookies = new ArrayList<>();
  }

  public Payload withMaxAge(int maxAge) {
    return withHeader(ACCESS_CONTROL_MAX_AGE, Integer.toString(maxAge));
  }

  public Payload withAllowCredentials(boolean allowedCredentials) {
    return withHeader(ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.toString(allowedCredentials));
  }

  public Payload withAllowOrigin(String allowedOrigin) {
    return withHeader(ACCESS_CONTROL_ALLOW_ORIGIN, allowedOrigin);
  }

  public Payload withAllowMethods(String... allowedMethods) {
    return withHeader(ACCESS_CONTROL_ALLOW_METHODS, String.join(", ", allowedMethods));
  }

  public Payload withAllowMethods(List allowedMethods) {
    return withHeader(ACCESS_CONTROL_ALLOW_METHODS, String.join(", ", allowedMethods));
  }

  public Payload withAllowHeaders(String... allowedHeaders) {
    return withHeader(ACCESS_CONTROL_ALLOW_HEADERS, String.join(", ", allowedHeaders));
  }

  public Payload withExposeHeaders(String... allowedHeaders) {
    return withHeader(ACCESS_CONTROL_EXPOSE_HEADERS, String.join(", ", allowedHeaders));
  }

  public Payload withAllowHeaders(List allowedHeaders) {
    return withHeader(ACCESS_CONTROL_ALLOW_HEADERS, String.join(", ", allowedHeaders));
  }

  public Payload withExposeHeaders(List allowedHeaders) {
    return withHeader(ACCESS_CONTROL_EXPOSE_HEADERS, String.join(", ", allowedHeaders));
  }

  public Payload withHeader(String key, String value) {
    if (value != null) {
      headers.put(key, value);
    }
    return this;
  }

  public Payload withHeaders(Map headers) {
    this.headers.putAll(headers);
    return this;
  }

  public Payload withCookie(String name, int value) {
    return withCookie(name, Integer.toString(value));
  }

  public Payload withCookie(String name, long value) {
    return withCookie(name, Long.toString(value));
  }

  public Payload withCookie(String name, boolean value) {
    return withCookie(name, Boolean.toString(value));
  }

  public Payload withCookie(String name, String value) {
    return withCookie(new NewCookie(name, value, "/", true));
  }

  public Payload withCookie(String name, Object value) {
    return withCookie(name, TypeConvert.toJson(value));
  }

  public Payload withCookie(Cookie cookie) {
    cookies.add(cookie);
    return this;
  }

  public Payload withCookies(List cookies) {
    cookies.addAll(cookies);
    return this;
  }

  public Payload withCode(int code) {
    this.code = code;
    return this;
  }

  public String rawContentType() {
    return contentType;
  }

  public Object rawContent() {
    return content;
  }

  public Map headers() {
    return headers;
  }

  public List cookies() {
    return cookies;
  }

  public int code() {
    return code;
  }

  public boolean isSuccess() {
    return (code >= 200) && (code <= 299);
  }

  public boolean isError() {
    return (code >= 400) && (code <= 599);
  }

  public static Payload ok() {
    return new Payload(OK);
  }

  public static Payload created() {
    return new Payload(CREATED);
  }

  public static Payload created(String uri) {
    return new Payload(CREATED).withHeader(LOCATION, uri);
  }

  public static Payload movedPermanently(String uri) {
    return new Payload(MOVED_PERMANENTLY).withHeader(LOCATION, uri);
  }

  public static Payload seeOther(String uri) {
    return new Payload(SEE_OTHER).withHeader(LOCATION, uri);
  }

  public static Payload seeOther(URI uri) {
    return seeOther(uri.toString());
  }

  public static Payload temporaryRedirect(String uri) {
    return new Payload(TEMPORARY_REDIRECT).withHeader(LOCATION, uri);
  }

  public static Payload temporaryRedirect(URI uri) {
    return temporaryRedirect(uri.toString());
  }

  public static Payload notModified() {
    return new Payload(NOT_MODIFIED);
  }

  public static Payload unauthorized(String realm) {
    return new Payload(UNAUTHORIZED).withHeader(WWW_AUTHENTICATE, "Basic realm=\"" + realm + "\"");
  }

  public static Payload forbidden() {
    return new Payload(FORBIDDEN);
  }

  public static Payload notFound() {
    return new Payload(NOT_FOUND);
  }

  public static Payload methodNotAllowed() {
    return new Payload(METHOD_NOT_ALLOWED);
  }

  public static Payload badRequest() {
    return new Payload(BAD_REQUEST);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy