}
if (jSessionId != null) {
builder.header("Cookie", "JSESSIONID=" + jSessionId);
}
ClientResponse response = null;
if ("GET".equals(method)) {
response = (ClientResponse) builder.get(ClientResponse.class);
} else if ("POST".equals(method)) {
builder.header("Lumify-CSRF-Token", this.csrfToken);
if (body == null) {
response = builder.post(ClientResponse.class, serialize(body));
} else if (body instanceof FormDataMultiPart) {
response = builder.type(contentType).post(ClientResponse.class, body);
} else {
response = builder.type(contentType).post(ClientResponse.class, serialize(body));
}
} else if ("PUT".equals(method)) {
builder.header("Lumify-CSRF-Token", this.csrfToken);
if (body == null) {
response = builder.put(ClientResponse.class, serialize(body));
} else {
if ("application/x-www-form-urlencoded".equals(contentType)) {
StringBuilder formParamBuilder = new StringBuilder();
// encode the form params
for (String key : formParams.keySet()) {
String value = formParams.get(key);
if (value != null && !"".equals(value.trim())) {
if (formParamBuilder.length() > 0) {
formParamBuilder.append("&");
}
try {
formParamBuilder.append(URLEncoder.encode(key, "utf8")).append("=").append(URLEncoder.encode(value, "utf8"));
} catch (Exception e) {
// move on to next
}
}
}
response = builder.type(contentType).put(ClientResponse.class, formParamBuilder.toString());
} else
response = builder.type(contentType).put(ClientResponse.class, serialize(body));
}
} else if ("DELETE".equals(method)) {
builder.header("Lumify-CSRF-Token", this.csrfToken);
if (body == null) {
response = builder.delete(ClientResponse.class, serialize(body));
} else {
response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
}
} else {
throw new ApiException(500, "unknown method type " + method);
}
if (response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
return null;
} else if (response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
return (String) response.getEntity(String.class);
} else {
throw new ApiException(
response.getClientResponseStatus().getStatusCode(),
response.getEntity(String.class));
}
}