
com.wedeploy.api.sdk.Base Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of Liferay, Inc. nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.wedeploy.api.sdk;
import com.wedeploy.api.serializer.Parser;
import com.wedeploy.api.serializer.Serializer;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*/
public abstract class Base {
public String body() {
return getBodyAsString();
}
public R body(byte[] body) {
setBody(body);
return (R) this;
}
public R body(Object body) {
setBodyObject(body);
return (R) this;
}
public R body(String body) {
setBody(body);
return (R) this;
}
public R body(String body, ContentType contentType) {
contentType(contentType);
body(body);
return (R) this;
}
public byte[] bodyBytes() {
return body;
}
public List bodyList(Class componentType) {
if (body == null) {
return null;
}
String contentTypeValue = contentType();
ContentType contentType = null;
if (contentTypeValue == null) {
contentType = ContentType.TEXT;
} else {
contentType = new ContentType(contentTypeValue);
}
return Parser
.get(contentType)
.parseAsList(getBodyAsString(), componentType);
}
public Map bodyMap(Class keyType, Class valueType) {
if (body == null) {
return null;
}
String contentTypeValue = contentType();
ContentType contentType = null;
if (contentTypeValue == null) {
contentType = ContentType.TEXT;
} else {
contentType = new ContentType(contentTypeValue);
}
return Parser
.get(contentType)
.parseAsMap(getBodyAsString(), keyType, valueType);
}
public T bodyValue() {
if (body == null) {
return null;
}
String contentTypeValue = contentType();
ContentType contentType = null;
if (contentTypeValue == null) {
contentType = ContentType.TEXT;
} else {
contentType = new ContentType(contentTypeValue);
}
return Parser
.get(contentType)
.parse(getBodyAsString());
}
public T bodyValue(Class type) {
if (body == null) {
return null;
}
String contentTypeValue = contentType();
ContentType contentType = null;
if (contentTypeValue == null) {
contentType = ContentType.TEXT;
} else {
contentType = new ContentType(contentTypeValue);
}
return Parser
.get(contentType)
.parse(getBodyAsString(), type);
}
public String contentType() {
return headers().get("Content-Type");
}
public R contentType(ContentType contentType) {
header("Content-Type", contentType.toString());
return (R) this;
}
public Context context() {
throw new UnsupportedOperationException();
}
public R cookie(Cookie cookie) {
cookies.put(cookie.name(), cookie);
return (R) this;
}
/**
* Gets cookie value by name.
*/
public Cookie cookie(String name) {
return this.cookies().get(name);
}
public Map cookies() {
return cookies;
}
public void end() {
}
/**
* Gets header value by name.
*/
public String header(String name) {
return this.headers().get(name);
}
public R header(String name, String value) {
headers.set(name, value);
return (R) this;
}
public MultiMap headers() {
return headers;
}
public boolean isContentType(ContentType contentType) {
return contentType.isSame(contentType());
}
protected String getBodyAsString() {
if (body == null) {
return null;
}
try {
return new String(body, BODY_ENCODING);
} catch (UnsupportedEncodingException uee) {
return null;
}
}
protected void setBody(byte[] value) {
this.body = value;
}
protected void setBody(String value) {
if (value == null) {
setBody((byte[]) null);
} else {
try {
setBody(value.getBytes(BODY_ENCODING));
} catch (UnsupportedEncodingException uee) {
setBody((byte[]) null);
}
}
}
protected R setBodyObject(Object body) {
if (body == null) {
setBody((byte[]) null);
return (R) this;
}
if (body instanceof String) {
setBody((String) body);
return (R) this;
}
String bodyJson = Serializer
.get(ContentType.JSON)
.serialize(body);
contentType(ContentType.JSON);
setBody(bodyJson);
return (R) this;
}
protected static String BODY_ENCODING = "UTF-8";
protected Map cookies = new HashMap<>();
protected MultiMap headers = MultiMap.newMultiMap();
private byte[] body;
}