Improve checking of JSON MIME in Java okhttp-gson client

to support suffix like charset in "application/json; charset=UTF8"
This commit is contained in:
xhh 2015-12-09 16:09:15 +08:00
parent 2e81bf34ce
commit 921659be5c
3 changed files with 87 additions and 24 deletions

View File

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

View File

@ -548,6 +548,17 @@ public class ApiClient {
return params; 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: * Select the Accept header's value from the given accepts array:
* if JSON exists in the given array, use it; * if JSON exists in the given array, use it;
@ -558,8 +569,14 @@ public class ApiClient {
* null will be returned (not to set the Accept header explicitly). * null will be returned (not to set the Accept header explicitly).
*/ */
public String selectHeaderAccept(String[] accepts) { public String selectHeaderAccept(String[] accepts) {
if (accepts.length == 0) return null; if (accepts.length == 0) {
if (StringUtil.containsIgnoreCase(accepts, "application/json")) return "application/json"; return null;
}
for (String accept : accepts) {
if (isJsonMime(accept)) {
return accept;
}
}
return StringUtil.join(accepts, ","); return StringUtil.join(accepts, ",");
} }
@ -573,8 +590,14 @@ public class ApiClient {
* JSON will be used. * JSON will be used.
*/ */
public String selectHeaderContentType(String[] contentTypes) { public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) return "application/json"; if (contentTypes.length == 0) {
if (StringUtil.containsIgnoreCase(contentTypes, "application/json")) return "application/json"; return "application/json";
}
for (String contentType : contentTypes) {
if (isJsonMime(contentType)) {
return contentType;
}
}
return contentTypes[0]; return contentTypes[0];
} }
@ -625,7 +648,7 @@ public class ApiClient {
// ensuring a default content type // ensuring a default content type
contentType = "application/json"; contentType = "application/json";
} }
if (contentType.startsWith("application/json")) { if (isJsonMime(contentType)) {
return json.deserialize(respBody, returnType); return json.deserialize(respBody, returnType);
} else if (returnType.equals(String.class)) { } else if (returnType.equals(String.class)) {
// Expecting string, return the raw response body. // Expecting string, return the raw response body.
@ -649,7 +672,7 @@ public class ApiClient {
* @throws ApiException If fail to serialize the given object * @throws ApiException If fail to serialize the given object
*/ */
public String serialize(Object obj, String contentType) throws ApiException { public String serialize(Object obj, String contentType) throws ApiException {
if (contentType.startsWith("application/json")) { if (isJsonMime(contentType)) {
if (obj != null) if (obj != null)
return json.serialize(obj); return json.serialize(obj);
else else
@ -821,7 +844,9 @@ public class ApiClient {
String contentType = (String) headerParams.get("Content-Type"); String contentType = (String) headerParams.get("Content-Type");
// ensuring a default content type // ensuring a default content type
if (contentType == null) contentType = "application/json"; if (contentType == null) {
contentType = "application/json";
}
RequestBody reqBody; RequestBody reqBody;
if (!HttpMethod.permitsRequestBody(method)) { if (!HttpMethod.permitsRequestBody(method)) {

View File

@ -77,16 +77,29 @@ public class ApiClientTest {
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11"))); assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11")));
} }
@Test
public void testIsJsonMime() {
assertFalse(apiClient.isJsonMime(null));
assertFalse(apiClient.isJsonMime(""));
assertFalse(apiClient.isJsonMime("text/plain"));
assertFalse(apiClient.isJsonMime("application/xml"));
assertFalse(apiClient.isJsonMime("application/jsonp"));
assertTrue(apiClient.isJsonMime("application/json"));
assertTrue(apiClient.isJsonMime("application/json; charset=UTF8"));
assertTrue(apiClient.isJsonMime("APPLICATION/JSON"));
}
@Test @Test
public void testSelectHeaderAccept() { public void testSelectHeaderAccept() {
String[] accepts = {"APPLICATION/JSON", "APPLICATION/XML"}; String[] accepts = {"application/json", "application/xml"};
assertEquals("application/json", apiClient.selectHeaderAccept(accepts)); assertEquals("application/json", apiClient.selectHeaderAccept(accepts));
accepts = new String[]{"application/json", "application/xml"}; accepts = new String[]{"APPLICATION/XML", "APPLICATION/JSON"};
assertEquals("application/json", apiClient.selectHeaderAccept(accepts)); assertEquals("APPLICATION/JSON", apiClient.selectHeaderAccept(accepts));
accepts = new String[]{"application/xml", "application/json"}; accepts = new String[]{"application/xml", "application/json; charset=UTF8"};
assertEquals("application/json", apiClient.selectHeaderAccept(accepts)); assertEquals("application/json; charset=UTF8", apiClient.selectHeaderAccept(accepts));
accepts = new String[]{"text/plain", "application/xml"}; accepts = new String[]{"text/plain", "application/xml"};
assertEquals("text/plain,application/xml", apiClient.selectHeaderAccept(accepts)); assertEquals("text/plain,application/xml", apiClient.selectHeaderAccept(accepts));
@ -97,14 +110,14 @@ public class ApiClientTest {
@Test @Test
public void testSelectHeaderContentType() { public void testSelectHeaderContentType() {
String[] contentTypes = {"APPLICATION/JSON", "APPLICATION/XML"}; String[] contentTypes = {"application/json", "application/xml"};
assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes));
contentTypes = new String[]{"application/json", "application/xml"}; contentTypes = new String[]{"APPLICATION/JSON", "APPLICATION/XML"};
assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); assertEquals("APPLICATION/JSON", apiClient.selectHeaderContentType(contentTypes));
contentTypes = new String[]{"application/xml", "application/json"}; contentTypes = new String[]{"application/xml", "application/json; charset=UTF8"};
assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); assertEquals("application/json; charset=UTF8", apiClient.selectHeaderContentType(contentTypes));
contentTypes = new String[]{"text/plain", "application/xml"}; contentTypes = new String[]{"text/plain", "application/xml"};
assertEquals("text/plain", apiClient.selectHeaderContentType(contentTypes)); assertEquals("text/plain", apiClient.selectHeaderContentType(contentTypes));