Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz1124630 - use explicit path construction
Browse files Browse the repository at this point in the history
- jersey can not handle path template with regex in path param
- extra url prefix (i.e. rest) will get appended twice using previous method
  • Loading branch information
Patrick Huang committed Dec 5, 2014
1 parent ffcc07c commit eec5567
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 86 deletions.
Expand Up @@ -24,7 +24,6 @@
import java.net.URI;
import java.util.Set;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.UriBuilder;

import org.zanata.common.LocaleId;
import org.zanata.rest.dto.ProcessStatus;
Expand All @@ -34,8 +33,6 @@
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

import static org.zanata.rest.client.ClientUtil.resolvePath;

/**
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
Expand Down Expand Up @@ -64,13 +61,12 @@ public ProcessStatus startSourceDocCreationOrUpdate(String idNoSlash,
Client client = factory.getClient();
CacheResponseFilter filter = new CacheResponseFilter();
client.addFilter(filter);
WebResource webResource = client.resource(baseUri);
String path =
resolvePath(webResource, AsynchronousProcessResource.class,
"startSourceDocCreationOrUpdate", projectSlug,
iterationSlug, idNoSlash);

webResource.path(path)
WebResource webResource = client.resource(baseUri)
.path(AsynchronousProcessResource.SERVICE_PATH)
.path("projects").path("p").path(projectSlug)
.path("iterations").path("i").path(iterationSlug)
.path("r").path(idNoSlash);
webResource
.queryParams(ClientUtil.asMultivaluedMap("ext", extensions))
.queryParam("copyTrans", String.valueOf(copytrans))
.put(resource);
Expand All @@ -86,12 +82,13 @@ public ProcessStatus startTranslatedDocCreationOrUpdate(String idNoSlash,
Client client = factory.getClient();
CacheResponseFilter filter = new CacheResponseFilter();
client.addFilter(filter);
WebResource webResource = client.resource(baseUri);
String path =
resolvePath(webResource, AsynchronousProcessResource.class,
"startTranslatedDocCreationOrUpdate", projectSlug,
iterationSlug, idNoSlash, locale);
webResource.path(path)
WebResource webResource = client.resource(baseUri)
.path(AsynchronousProcessResource.SERVICE_PATH)
.path("projects").path("p").path(projectSlug)
.path("iterations").path("i").path(iterationSlug)
.path("r").path(idNoSlash)
.path("translations").path(locale.toString());
webResource
.queryParams(ClientUtil.asMultivaluedMap("ext", extensions))
.queryParam("merge", merge)
.put(translatedDoc);
Expand Down
Expand Up @@ -21,8 +21,6 @@

package org.zanata.rest.client;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -32,9 +30,7 @@
import javax.ws.rs.core.Response;

import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

/**
Expand All @@ -43,48 +39,6 @@
*/
public class ClientUtil {

/**
* Resolve a REST resource path to a method.
*
* @param webResource
* web resource
* @param resourceInterface
* Zanata REST api interface
* @param methodName
* the method name we want to call which is annotated by @Path
* @param pathParams
* path param values
* @param <T>
* interface type
* @return resolved path
*/
static <T> String resolvePath(WebResource webResource,
Class<T> resourceInterface, String methodName,
Object... pathParams) {
try {
// Zanata API always define SERVICE_PATH field
Field servicePathField = resourceInterface.getField("SERVICE_PATH");
String servicePath = servicePathField.get(null).toString();
Method method = getMethod(resourceInterface, methodName);
return webResource.getUriBuilder().path(servicePath).path(method)
.build(pathParams).getPath();
} catch (NoSuchFieldException | IllegalAccessException e) {
throw Throwables.propagate(e);
}
}

private static <T> Method getMethod(Class<T> resourceClass,
String methodName) {
Method[] methods = resourceClass.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
return method;
}
}
throw new IllegalArgumentException(methodName + " not found in "
+ resourceClass);
}

static MultivaluedMap<String, String> asMultivaluedMap(
String paramKey, Iterable<String> values) {
MultivaluedMapImpl map = new MultivaluedMapImpl();
Expand Down
Expand Up @@ -28,8 +28,6 @@
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

import static org.zanata.rest.client.ClientUtil.resolvePath;

/**
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
Expand Down Expand Up @@ -58,11 +56,11 @@ public CopyTransStatus startCopyTrans(String projectSlug,
private WebResource webResource(Client client, String projectSlug,
String iterationSlug,
String docId) {
WebResource webResource = client.resource(baseUri);
String path =
resolvePath(webResource, CopyTransResource.class,
"startCopyTrans", projectSlug, iterationSlug, docId);
return webResource.path(path);
return client.resource(baseUri)
.path(CopyTransResource.SERVICE_PATH)
.path("/proj").path(projectSlug)
.path("iter").path(iterationSlug)
.path("doc").path(docId);
}

@Override
Expand Down
Expand Up @@ -21,8 +21,6 @@

package org.zanata.rest.client;

import static org.zanata.rest.client.ClientUtil.resolvePath;

import java.net.URI;

import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -133,26 +131,20 @@ public ChunkUploadResponse uploadTranslationFile(
public ClientResponse downloadSourceFile(String projectSlug,
String iterationSlug,
String fileType, String docId) {
WebResource webResource = factory.getClient().resource(baseUri);
String path = resolvePath(webResource, FileResource.class,
"downloadSourceFile", projectSlug, iterationSlug, fileType);
return webResource.path(path)
.queryParam("docId", docId)
.get(ClientResponse.class);
WebResource webResource = factory.getClient().resource(baseUri)
.path(FileResource.SERVICE_PATH).path("source")
.path(projectSlug).path(iterationSlug).path(fileType);
return webResource.queryParam("docId", docId).get(ClientResponse.class);
}

public ClientResponse downloadTranslationFile(String projectSlug,
String iterationSlug, String locale, String fileExtension,
String docId) {
WebResource webResource = factory.getClient().resource(baseUri);
String path =
resolvePath(webResource, FileResource.class,
"downloadTranslationFile",
projectSlug, iterationSlug, locale, fileExtension);
return webResource.path(path)
.queryParam("docId", docId)
.get(ClientResponse.class);

WebResource webResource = factory.getClient().resource(baseUri)
.path(FileResource.SERVICE_PATH).path("translation")
.path(projectSlug).path(iterationSlug).path(locale)
.path(fileExtension);
return webResource.queryParam("docId", docId).get(ClientResponse.class);
}

private static <T> FormDataMultiPart addBodyPartIfPresent(
Expand Down

0 comments on commit eec5567

Please sign in to comment.