Merge pull request #1689 from xhh/java-json-mime

[Java] Improve the checking of JSON MIME
This commit is contained in:
wing328
2015-12-10 17:22:40 +08:00
9 changed files with 251 additions and 70 deletions

View File

@@ -337,6 +337,17 @@ public class ApiClient {
return params;
}
/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
*/
public boolean isJsonMime(String mime) {
return mime != null && mime.matches("(?i)application\\/json(;.*)?");
}
/**
* Select the Accept header's value from the given accepts array:
* if JSON exists in the given array, use it;
@@ -347,8 +358,14 @@ public class ApiClient {
* null will be returned (not to set the Accept header explicitly).
*/
public String selectHeaderAccept(String[] accepts) {
if (accepts.length == 0) return null;
if (StringUtil.containsIgnoreCase(accepts, "application/json")) return "application/json";
if (accepts.length == 0) {
return null;
}
for (String accept : accepts) {
if (isJsonMime(accept)) {
return accept;
}
}
return StringUtil.join(accepts, ",");
}
@@ -362,8 +379,14 @@ public class ApiClient {
* JSON will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) return "application/json";
if (StringUtil.containsIgnoreCase(contentTypes, "application/json")) return "application/json";
if (contentTypes.length == 0) {
return "application/json";
}
for (String contentType : contentTypes) {
if (isJsonMime(contentType)) {
return contentType;
}
}
return contentTypes[0];
}
@@ -383,7 +406,7 @@ public class ApiClient {
* Content-Type (only JSON is supported for now).
*/
public String serialize(Object obj, String contentType) throws ApiException {
if (contentType.startsWith("application/json")) {
if (isJsonMime(contentType)) {
return json.serialize(obj);
} else {
throw new ApiException(400, "can not serialize object into Content-Type: " + contentType);
@@ -407,7 +430,7 @@ public class ApiClient {
else
body = "";
if (contentType.startsWith("application/json")) {
if (isJsonMime(contentType)) {
return json.deserialize(body, returnType);
} else if (returnType.getType().equals(String.class)) {
// Expecting string, return the raw response body.

View File

@@ -344,6 +344,17 @@ public class ApiClient {
return params;
}
/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
*/
public boolean isJsonMime(String mime) {
return mime != null && mime.matches("(?i)application\\/json(;.*)?");
}
/**
* Select the Accept header's value from the given accepts array:
* if JSON exists in the given array, use it;
@@ -354,8 +365,14 @@ public class ApiClient {
* null will be returned (not to set the Accept header explicitly).
*/
public String selectHeaderAccept(String[] accepts) {
if (accepts.length == 0) return null;
if (StringUtil.containsIgnoreCase(accepts, "application/json")) return "application/json";
if (accepts.length == 0) {
return null;
}
for (String accept : accepts) {
if (isJsonMime(accept)) {
return accept;
}
}
return StringUtil.join(accepts, ",");
}
@@ -369,8 +386,14 @@ public class ApiClient {
* JSON will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) return "application/json";
if (StringUtil.containsIgnoreCase(contentTypes, "application/json")) return "application/json";
if (contentTypes.length == 0) {
return "application/json";
}
for (String contentType : contentTypes) {
if (isJsonMime(contentType)) {
return contentType;
}
}
return contentTypes[0];
}
@@ -390,7 +413,7 @@ public class ApiClient {
* Content-Type (only JSON is supported for now).
*/
public Entity<String> serialize(Object obj, String contentType) throws ApiException {
if (contentType.startsWith("application/json")) {
if (isJsonMime(contentType)) {
return Entity.json(json.serialize(obj));
} else {
throw new ApiException(400, "can not serialize object into Content-Type: " + contentType);
@@ -414,7 +437,7 @@ public class ApiClient {
else
body = "";
if (contentType.startsWith("application/json")) {
if (isJsonMime(contentType)) {
return json.deserialize(body, returnType);
} else if (returnType.getType().equals(String.class)) {
// Expecting string, return the raw response body.

View File

@@ -549,6 +549,17 @@ public class ApiClient {
return params;
}
/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
*/
public boolean isJsonMime(String mime) {
return mime != null && mime.matches("(?i)application\\/json(;.*)?");
}
/**
* Select the Accept header's value from the given accepts array:
* if JSON exists in the given array, use it;
@@ -559,8 +570,14 @@ public class ApiClient {
* null will be returned (not to set the Accept header explicitly).
*/
public String selectHeaderAccept(String[] accepts) {
if (accepts.length == 0) return null;
if (StringUtil.containsIgnoreCase(accepts, "application/json")) return "application/json";
if (accepts.length == 0) {
return null;
}
for (String accept : accepts) {
if (isJsonMime(accept)) {
return accept;
}
}
return StringUtil.join(accepts, ",");
}
@@ -574,8 +591,14 @@ public class ApiClient {
* JSON will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) return "application/json";
if (StringUtil.containsIgnoreCase(contentTypes, "application/json")) return "application/json";
if (contentTypes.length == 0) {
return "application/json";
}
for (String contentType : contentTypes) {
if (isJsonMime(contentType)) {
return contentType;
}
}
return contentTypes[0];
}
@@ -626,7 +649,7 @@ public class ApiClient {
// ensuring a default content type
contentType = "application/json";
}
if (contentType.startsWith("application/json")) {
if (isJsonMime(contentType)) {
return json.deserialize(respBody, returnType);
} else if (returnType.equals(String.class)) {
// Expecting string, return the raw response body.
@@ -650,7 +673,7 @@ public class ApiClient {
* @throws ApiException If fail to serialize the given object
*/
public String serialize(Object obj, String contentType) throws ApiException {
if (contentType.startsWith("application/json")) {
if (isJsonMime(contentType)) {
if (obj != null)
return json.serialize(obj);
else
@@ -822,7 +845,9 @@ public class ApiClient {
String contentType = (String) headerParams.get("Content-Type");
// ensuring a default content type
if (contentType == null) contentType = "application/json";
if (contentType == null) {
contentType = "application/json";
}
RequestBody reqBody;
if (!HttpMethod.permitsRequestBody(method)) {