Skip to content

Commit

Permalink
JAVACLIENT-147: Fix server error deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinleturc committed Dec 18, 2017
1 parent aba6f2c commit a6eae11
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
15 changes: 15 additions & 0 deletions nuxeo-java-client-test/src/test/java/org/nuxeo/client/ITBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@ public void itCanFetchWorkflowModelsFromRepository() {
assertEquals("SerialDocumentReview", workflow.getName());
}

@Test
public void itCanFailServerSide() {
NuxeoClient client = createClient();
// create a document under a non existent parent
Document doc = Document.createWithName("file", "File");
doc.setPropertyValue("dc:title", "File");
try {
client.repository().createDocumentByPath("/absent", doc);
fail("Previous call should have failed.");
} catch (NuxeoClientRemoteException e) {
assertEquals(404, e.getStatus());
assertEquals("/absent", e.getMessage());
}
}

/**
* @return A {@link NuxeoClient} filled with Nuxeo Server URL and default basic authentication.
*/
Expand Down
20 changes: 15 additions & 5 deletions nuxeo-java-client/src/main/java/org/nuxeo/client/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.nio.charset.Charset;
import java.util.Locale;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -104,8 +105,14 @@ public boolean equalsTypeSubType(MediaType mediaType) {
return equalsType(mediaType) && StringUtils.equals(subtype, mediaType.subtype);
}

public boolean equalsTypeStartsWithSubType(MediaType mediaType) {
return equalsType(mediaType) && subtype.startsWith(mediaType.subtype);
public boolean equalsTypeSubTypeWithoutSuffix(MediaType mediaType) {
if (equalsType(mediaType)) {
UnaryOperator<String> removeSuffix = s -> s.replaceAll("\\+.*", "");
String thisSubtype = removeSuffix.apply(subtype);
String givenSubtype = removeSuffix.apply(mediaType.subtype);
return thisSubtype.equals(givenSubtype);
}
return false;
}

/**
Expand Down Expand Up @@ -138,8 +145,9 @@ public static MediaType fromOkHttpMediaType(okhttp3.MediaType mediaType) {
*/
public static MediaType parse(String string) {
Matcher typeSubtype = TYPE_SUBTYPE.matcher(string);
if (!typeSubtype.lookingAt())
if (!typeSubtype.lookingAt()) {
return null;
}
String type = typeSubtype.group(1).toLowerCase(Locale.US);
String subtype = typeSubtype.group(2).toLowerCase(Locale.US);

Expand All @@ -148,12 +156,14 @@ public static MediaType parse(String string) {
Matcher parameter = PARAMETER.matcher(string);
for (int s = typeSubtype.end(); s < string.length(); s = parameter.end()) {
parameter.region(s, string.length());
if (!parameter.lookingAt())
if (!parameter.lookingAt()) {
return null; // This is not a well-formed media type.
}

String name = parameter.group(1);
if (name == null)
if (name == null) {
continue;
}
if (name.equalsIgnoreCase("nuxeo-entity")) {
String nuxeoEntityParameter = parameter.group(2) != null ? parameter.group(2) // Value is a token.
: parameter.group(3); // Value is a quoted string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ protected <T> retrofit2.Response<T> handleResponse(Call<T> call, retrofit2.Respo
// content type could be null
MediaType mediaType = MediaType.fromOkHttpMediaType(response.raw().body().contentType());
if (!StringUtils.EMPTY.equals(errorBody)
&& MediaTypes.APPLICATION_JSON.equalsTypeStartsWithSubType(mediaType)) {
&& MediaTypes.APPLICATION_JSON.equalsTypeSubTypeWithoutSuffix(mediaType)) {
throw converterFactory.readJSON(errorBody, NuxeoClientRemoteException.class);
}
throw new NuxeoClientRemoteException(httpCode, httpMessage, errorBody, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.nuxeo.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.nio.charset.Charset;

Expand All @@ -44,4 +45,12 @@ public void testTypeSubTypeCharset() {
assertEquals(Charset.forName("UTF-8"), mediaType.charset());
}

@Test
public void testEqualsTypeSubTypeWithoutSuffix() {
MediaType mediaType1 = MediaType.parse("application/json; charset=UTF-8");
MediaType mediaType2 = MediaType.parse("application/json+nxentity; charset=UTF-8");
assertTrue(mediaType1.equalsTypeSubTypeWithoutSuffix(mediaType2));
assertTrue(mediaType2.equalsTypeSubTypeWithoutSuffix(mediaType1));
}

}

0 comments on commit a6eae11

Please sign in to comment.