This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Patrick Huang
committed
Nov 6, 2014
1 parent
8dbf5ec
commit 3a731e9
Showing
32 changed files
with
1,773 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?xml version="1.0"?> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.zanata</groupId> | ||
<artifactId>client</artifactId> | ||
<version>3.6.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>rest-client</artifactId> | ||
<name>rest-client</name> | ||
<url>http://maven.apache.org</url> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.zanata</groupId> | ||
<artifactId>zanata-common-api</artifactId> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-jaxrs</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-jaxb-provider</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>jaxrs-api</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-multipart-provider</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-core</artifactId> | ||
<version>1.17.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-client</artifactId> | ||
<version>1.17.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey.contribs</groupId> | ||
<artifactId>jersey-multipart</artifactId> | ||
<version>1.17.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-codec</groupId> | ||
<artifactId>commons-codec</artifactId> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-library</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<testResources> | ||
<testResource> | ||
<directory>src/test/resources</directory> | ||
</testResource> | ||
</testResources> | ||
</build> | ||
</project> |
63 changes: 63 additions & 0 deletions
63
rest-client/src/main/java/org/zanata/rest/client/ApiKeyHeaderFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2014, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.zanata.rest.client; | ||
|
||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.zanata.rest.RestConstant; | ||
|
||
import com.sun.jersey.api.client.ClientHandlerException; | ||
import com.sun.jersey.api.client.ClientRequest; | ||
import com.sun.jersey.api.client.ClientResponse; | ||
import com.sun.jersey.api.client.filter.ClientFilter; | ||
|
||
@Provider | ||
public class ApiKeyHeaderFilter extends ClientFilter { | ||
private String apiKey; | ||
private String username; | ||
private String ver; | ||
|
||
public ApiKeyHeaderFilter(String username, String apiKey, String ver) { | ||
this.username = username; | ||
this.apiKey = apiKey; | ||
this.ver = ver; | ||
} | ||
|
||
@Override | ||
public ClientResponse handle(ClientRequest cr) | ||
throws ClientHandlerException { | ||
MultivaluedMap<String, Object> headers = cr.getHeaders(); | ||
headers.add(RestConstant.HEADER_USERNAME, username); | ||
headers.add(RestConstant.HEADER_API_KEY, apiKey); | ||
headers.add(RestConstant.HEADER_VERSION_NO, ver); | ||
return getNext().handle(cr); | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
rest-client/src/main/java/org/zanata/rest/client/CacheResponseFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2014, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.zanata.rest.client; | ||
|
||
import com.google.common.base.Optional; | ||
import com.google.common.base.Preconditions; | ||
import com.sun.jersey.api.client.ClientHandlerException; | ||
import com.sun.jersey.api.client.ClientRequest; | ||
import com.sun.jersey.api.client.ClientResponse; | ||
import com.sun.jersey.api.client.GenericType; | ||
import com.sun.jersey.api.client.filter.ClientFilter; | ||
|
||
/** | ||
* This is a workaround that jersey client don't support put/post returning | ||
* response. | ||
* | ||
* @author Patrick Huang <a | ||
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
public class CacheResponseFilter extends ClientFilter { | ||
private Optional<ClientResponse> cachedClientResponse = Optional.absent(); | ||
|
||
@Override | ||
public ClientResponse handle(ClientRequest cr) | ||
throws ClientHandlerException { | ||
ClientResponse response = getNext().handle(cr); | ||
response.bufferEntity(); | ||
cachedClientResponse = Optional.of(response); | ||
return response; | ||
} | ||
|
||
public <T> T getEntity(Class<T> type) { | ||
checkState(); | ||
return cachedClientResponse.get().getEntity(type); | ||
} | ||
|
||
public <T> T getEntity(GenericType<T> genericType) { | ||
checkState(); | ||
return cachedClientResponse.get().getEntity(genericType); | ||
} | ||
|
||
private void checkState() { | ||
Preconditions.checkState(cachedClientResponse.isPresent(), | ||
"No cached ClientResponse. Did you forget to add this filter?"); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
rest-client/src/main/java/org/zanata/rest/client/FileResourceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright 2014, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.zanata.rest.client; | ||
|
||
import java.net.URI; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.zanata.rest.DocumentFileUploadForm; | ||
import org.zanata.rest.StringSet; | ||
import org.zanata.rest.dto.ChunkUploadResponse; | ||
import com.sun.jersey.api.client.Client; | ||
import com.sun.jersey.api.client.WebResource; | ||
import com.sun.jersey.multipart.FormDataMultiPart; | ||
|
||
/** | ||
* @author Patrick Huang <a | ||
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
public class FileResourceClient { | ||
private final RestClientFactory factory; | ||
|
||
private final URI baseUri; | ||
|
||
public FileResourceClient(RestClientFactory restClientFactory) { | ||
this.factory = restClientFactory; | ||
baseUri = restClientFactory.getBaseUri(); | ||
|
||
} | ||
|
||
public StringSet acceptedFileTypes() { | ||
String types = factory.getClient() | ||
.resource(baseUri) | ||
.path("file").path("accepted_types") | ||
.get(String.class); | ||
return new StringSet(types); | ||
} | ||
|
||
public ChunkUploadResponse uploadSourceFile( | ||
String projectSlug, | ||
String iterationSlug, String docId, | ||
DocumentFileUploadForm documentFileUploadForm) { | ||
CacheResponseFilter filter = new CacheResponseFilter(); | ||
Client client = factory.getClient(); | ||
client.addFilter(filter); | ||
WebResource.Builder builder = client | ||
.resource(baseUri) | ||
.path("file").path("source").path(projectSlug) | ||
.path(iterationSlug) | ||
.queryParam("docId", docId) | ||
.type(MediaType.MULTIPART_FORM_DATA_TYPE); | ||
FormDataMultiPart form = | ||
prepareFormDataMultiPart(documentFileUploadForm); | ||
|
||
builder.post(form); | ||
ChunkUploadResponse chunkUploadResponse = | ||
filter.getEntity(ChunkUploadResponse.class); | ||
client.removeFilter(filter); | ||
return chunkUploadResponse; | ||
} | ||
|
||
private FormDataMultiPart prepareFormDataMultiPart( | ||
DocumentFileUploadForm documentFileUploadForm) { | ||
FormDataMultiPart form = | ||
new FormDataMultiPart() | ||
.field("file", documentFileUploadForm | ||
.getFileStream(), | ||
MediaType.APPLICATION_OCTET_STREAM_TYPE); | ||
addBodyPartIfPresent(form, "adapterParams", | ||
documentFileUploadForm.getAdapterParams()); | ||
addBodyPartIfPresent(form, "type", documentFileUploadForm.getFileType()); | ||
addBodyPartIfPresent(form, "first", documentFileUploadForm.getFirst()); | ||
addBodyPartIfPresent(form, "hash", documentFileUploadForm.getHash()); | ||
addBodyPartIfPresent(form, "last", documentFileUploadForm.getLast()); | ||
addBodyPartIfPresent(form, "size", documentFileUploadForm.getSize()); | ||
addBodyPartIfPresent(form, "uploadId", | ||
documentFileUploadForm.getUploadId()); | ||
return form; | ||
} | ||
|
||
public ChunkUploadResponse uploadTranslationFile( | ||
String projectSlug, | ||
String iterationSlug, String locale, String docId, | ||
String mergeType, | ||
DocumentFileUploadForm documentFileUploadForm) { | ||
CacheResponseFilter filter = new CacheResponseFilter(); | ||
Client client = factory.getClient(); | ||
client.addFilter(filter); | ||
WebResource.Builder builder = client.resource(baseUri) | ||
.path("file").path("translation") | ||
.path(projectSlug) | ||
.path(iterationSlug) | ||
.path(locale) | ||
.queryParam("docId", docId) | ||
.queryParam("merge", mergeType) | ||
.type(MediaType.MULTIPART_FORM_DATA_TYPE); | ||
FormDataMultiPart form = | ||
prepareFormDataMultiPart(documentFileUploadForm); | ||
|
||
builder.post(form); | ||
ChunkUploadResponse chunkUploadResponse = | ||
filter.getEntity(ChunkUploadResponse.class); | ||
client.removeFilter(filter); | ||
return chunkUploadResponse; | ||
} | ||
|
||
private static <T> FormDataMultiPart addBodyPartIfPresent( | ||
FormDataMultiPart form, String field, T value) { | ||
if (value != null) { | ||
return form.field(field, value.toString()); | ||
} | ||
return form; | ||
} | ||
} |
Oops, something went wrong.