.append(escapeString(value));
}
}
String querystring = b.toString();
Builder builder = client.resource(host + path + querystring).accept("application/json");
// add basic header if not present
String user = null;
String password = null;
if (headerParams.containsKey("BASIC"))
{
user = headerParams.get("BASIC").split(":")[0];
password = headerParams.get("BASIC").split(":")[1];
headerParams.remove("BASIC");
client.addFilter(new HTTPBasicAuthFilter(user, password));
}
else if (!headerParams.containsKey("Authorization")) // add oauth header if not present
{
headerParams.put("Authorization", "Bearer " + findSystemDefaultAccessToken());
}
for (String key : headerParams.keySet()) {
builder.header(key, headerParams.get(key));
}
for (String key : defaultHeaderMap.keySet()) {
if (!headerParams.containsKey(key)) {
builder.header(key, defaultHeaderMap.get(key));
}
}
ClientResponse response = null;
if ("GET".equals(method)) {
response = (ClientResponse) builder.get(ClientResponse.class);
} else if ("POST".equals(method)) {
if (body == null)
response = builder.post(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).post(
ClientResponse.class, formParamBuilder.toString());
} else {
response = builder.type(contentType).post(ClientResponse.class,
serialize(body));
}
} else if ("PUT".equals(method)) {
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)) {
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) {