Skip to content

Commit

Permalink
fix: handle old rest endpoint (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Aug 22, 2017
1 parent 5057f6f commit 7db38f9
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 97 deletions.
Expand Up @@ -27,6 +27,7 @@
import javax.ws.rs.DefaultValue;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.ResponseProcessingException;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -91,20 +92,22 @@ public ProcessStatus startSourceDocCreationOrUpdateWithDocId(
.path("projects").path("p").path(projectSlug)
.path("iterations").path("i").path(iterationSlug)
.path("resource");
Response response = webResource
.queryParam("docId", docId)
.queryParam("ext", extensions.toArray())
.request(MediaType.APPLICATION_XML_TYPE)
.put(Entity.xml(resource));
if (RestUtil.isNotFound(response)) {
response.close();
// fallback to old endpoint
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
return startSourceDocCreationOrUpdate(idNoSlash, projectSlug,
iterationSlug, resource, extensions, false);
} else {
try {
Response response = webResource
.queryParam("docId", docId)
.queryParam("ext", extensions.toArray())
.request(MediaType.APPLICATION_XML_TYPE)
.put(Entity.xml(resource));
response.bufferEntity();
return response.readEntity(ProcessStatus.class);
} catch (ResponseProcessingException e) {
if (RestUtil.isNotFound(e.getResponse())) {
// fallback to old endpoint
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
return startSourceDocCreationOrUpdate(idNoSlash, projectSlug,
iterationSlug, resource, extensions, false);
}
throw e;
}
}

Expand Down Expand Up @@ -145,23 +148,26 @@ public ProcessStatus startTranslatedDocCreationOrUpdateWithDocId(
.path("iterations").path("i").path(iterationSlug)
.path("resource")
.path("translations").path(locale.toString());
Response response = webResource
.queryParam("docId", docId)
.queryParam("ext", extensions.toArray())
.queryParam("merge", merge)
.queryParam("assignCreditToUploader", String.valueOf(assignCreditToUploader))
.request(MediaType.APPLICATION_XML_TYPE)
.put(Entity.xml(translatedDoc));
if (RestUtil.isNotFound(response)) {
// fallback to old endpoint
response.close();
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
return startTranslatedDocCreationOrUpdate(idNoSlash, projectSlug,
iterationSlug, locale, translatedDoc, extensions, merge,
assignCreditToUploader);
} else {
try {
Response response = webResource
.queryParam("docId", docId)
.queryParam("ext", extensions.toArray())
.queryParam("merge", merge)
.queryParam("assignCreditToUploader", String.valueOf(assignCreditToUploader))
.request(MediaType.APPLICATION_XML_TYPE)
.put(Entity.xml(translatedDoc));
response.bufferEntity();
return response.readEntity(ProcessStatus.class);
} catch (ResponseProcessingException e) {
if (RestUtil.isNotFound(e.getResponse())) {
// fallback to old endpoint
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
return startTranslatedDocCreationOrUpdate(idNoSlash,
projectSlug,
iterationSlug, locale, translatedDoc, extensions, merge,
assignCreditToUploader);
}
throw e;
}
}

Expand Down
Expand Up @@ -60,7 +60,7 @@ public void filter(ClientRequestContext requestContext,
// getLocation() can return null but it's acceptable
throw new RedirectionException(message, statusCode,
responseContext.getLocation());
} else if (statusCode >= 399) {
} else if (statusCode >= 400) {
URI uri = requestContext.getUri();
String entity = tryGetEntity(responseContext);
String msg =
Expand Down
Expand Up @@ -27,6 +27,7 @@

import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.ResponseProcessingException;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -85,21 +86,23 @@ public Resource getResource(String docId, Set<String> extensions) {
.path("resource")
.queryParam("docId", docId)
.queryParam("ext", extensions.toArray());
Response response = webResource.request(MediaType.APPLICATION_XML_TYPE)
.get();
if (RestUtil.isNotFound(response)) {
// fallback to old endpoint
response.close();
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
webResource =
getBaseServiceResource(client)
.path("r")
.path(idNoSlash)
.queryParam("ext", extensions.toArray());
try {
return webResource.request(MediaType.APPLICATION_XML_TYPE)
.get(Resource.class);
} catch (ResponseProcessingException e) {
if (RestUtil.isNotFound(e.getResponse())) {
// fallback to old endpoint
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
webResource =
getBaseServiceResource(client)
.path("r")
.path(idNoSlash)
.queryParam("ext", extensions.toArray());
return webResource.request(MediaType.APPLICATION_XML_TYPE)
.get(Resource.class);
}
throw e;
}
return response.readEntity(Resource.class);
}

public String putResource(String docId, Resource resource,
Expand All @@ -110,36 +113,42 @@ public String putResource(String docId, Resource resource,
.queryParam("ext", extensions.toArray())
.queryParam("copyTrans", String.valueOf(copyTrans));

Response response = webResource.request(MediaType.APPLICATION_XML_TYPE)
.put(Entity.entity(resource, MediaType.APPLICATION_XML_TYPE));
if (RestUtil.isNotFound(response)) {
// fallback to old endpoint
response.close();
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
webResource = getBaseServiceResource(client)
.path("r")
.path(idNoSlash)
.queryParam("ext", extensions.toArray())
.queryParam("copyTrans", String.valueOf(copyTrans));
response = webResource.request(MediaType.APPLICATION_XML_TYPE)
try {
Response response = webResource.request(MediaType.APPLICATION_XML_TYPE)
.put(Entity.entity(resource, MediaType.APPLICATION_XML_TYPE));
response.bufferEntity();
return response.readEntity(String.class);
} catch (ResponseProcessingException e) {
if (RestUtil.isNotFound(e.getResponse())) {
// fallback to old endpoint
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
webResource = getBaseServiceResource(client)
.path("r")
.path(idNoSlash)
.queryParam("ext", extensions.toArray())
.queryParam("copyTrans", String.valueOf(copyTrans));
Response response = webResource.request(MediaType.APPLICATION_XML_TYPE)
.put(Entity.entity(resource, MediaType.APPLICATION_XML_TYPE));
response.bufferEntity();
return response.readEntity(String.class);
}
throw e;
}
response.bufferEntity();
return response.readEntity(String.class);
}

public String deleteResource(String docId) {
Client client = factory.getClient();
WebTarget webResource = getBaseServiceResource(client);
Response response =
webResource.path("resource").queryParam("docId", docId).request()
.delete();
if (RestUtil.isNotFound(response)) {
response.close();
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
return webResource.path("r").path(idNoSlash).request()
.delete(String.class);
try {
return webResource.path("resource").queryParam("docId", docId)
.request().delete(String.class);
} catch (ResponseProcessingException e) {
if (RestUtil.isNotFound(e.getResponse())) {
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
return webResource.path("r").path(idNoSlash).request()
.delete(String.class);
}
throw e;
}
return response.readEntity(String.class);
}
}
Expand Up @@ -23,6 +23,7 @@

import java.net.URI;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.client.ResponseProcessingException;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -86,24 +87,27 @@ public ContainerTranslationStatistics getStatisticsWithDocId(
.queryParam("docId", docId)
.queryParam("word", String.valueOf(includeWordStats))
.queryParam("locale", (Object[]) locales);
Response response =
webResource.request(MediaType.APPLICATION_XML_TYPE).get();
if (RestUtil.isNotFound(response)) {
response.close();
webResource =
factory.getClient().target(baseUri).path("stats")
.path("proj")
.path(projectSlug)
.path("iter")
.path(iterationSlug)
.path("doc")
.path(docId)
.queryParam("word", String.valueOf(includeWordStats))
.queryParam("locale", (Object[]) locales);
try {
return webResource.request(MediaType.APPLICATION_XML_TYPE)
.get(ContainerTranslationStatistics.class);
} catch (ResponseProcessingException e) {
if (RestUtil.isNotFound(e.getResponse())) {
// fallback to old endpoint
webResource =
factory.getClient().target(baseUri).path("stats")
.path("proj")
.path(projectSlug)
.path("iter")
.path(iterationSlug)
.path("doc")
.path(docId)
.queryParam("word", String.valueOf(includeWordStats))
.queryParam("locale", (Object[]) locales);
return webResource.request(MediaType.APPLICATION_XML_TYPE)
.get(ContainerTranslationStatistics.class);
}
throw e;
}
return response.readEntity(ContainerTranslationStatistics.class);
}

@Override
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.net.URI;
import java.util.Set;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ResponseProcessingException;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -55,31 +56,33 @@ public class TransDocResourceClient {
public Response getTranslations(String docId, LocaleId locale,
Set<String> extensions, boolean createSkeletons, String eTag) {
Client client = factory.getClient();
Response response = getBaseServiceResource(client)
.path("resource")
.path("translations")
.path(locale.getId())
.queryParam("docId", docId)
.queryParam("ext", extensions.toArray())
.queryParam("skeletons", String.valueOf(createSkeletons))
.request(MediaType.APPLICATION_XML_TYPE)
.header(HttpHeaders.IF_NONE_MATCH, eTag)
.get();
if (RestUtil.isNotFound(response)) {
// fallback to old endpoint
response.close();
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
response = getBaseServiceResource(client)
.path("r")
.path(idNoSlash)
.path("translations").path(locale.getId())
try {
return getBaseServiceResource(client)
.path("resource")
.path("translations")
.path(locale.getId())
.queryParam("docId", docId)
.queryParam("ext", extensions.toArray())
.queryParam("skeletons", String.valueOf(createSkeletons))
.request(MediaType.APPLICATION_XML_TYPE)
.header(HttpHeaders.IF_NONE_MATCH, eTag)
.get();
} catch (ResponseProcessingException e) {
if (RestUtil.isNotFound(e.getResponse())) {
// fallback to old endpoint
String idNoSlash = RestUtil.convertToDocumentURIId(docId);
return getBaseServiceResource(client)
.path("r")
.path(idNoSlash)
.path("translations").path(locale.getId())
.queryParam("ext", extensions.toArray())
.queryParam("skeletons", String.valueOf(createSkeletons))
.request(MediaType.APPLICATION_XML_TYPE)
.header(HttpHeaders.IF_NONE_MATCH, eTag)
.get();
}
throw e;
}
return response;
}

private WebTarget getBaseServiceResource(Client client) {
Expand Down

0 comments on commit 7db38f9

Please sign in to comment.