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

Commit

Permalink
ZNTA-1271 - migrate JAX-RS client back to resteasy
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Sep 20, 2016
1 parent e48e996 commit b1ae356
Show file tree
Hide file tree
Showing 38 changed files with 641 additions and 832 deletions.
63 changes: 40 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
<zanata.api.version>4.0.0-SNAPSHOT</zanata.api.version>
<zanata.common.version>4.0.0-SNAPSHOT</zanata.common.version>
<powermock.version>1.6.2</powermock.version>
<resteasy.version>3.0.12.Final</resteasy.version>
<jersey.version>1.17.1</jersey.version>
<resteasy.version>3.0.13.Final</resteasy.version>
</properties>


Expand Down Expand Up @@ -80,22 +79,6 @@
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
<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>
Expand Down Expand Up @@ -147,11 +130,6 @@
<version>2.32</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>${resteasy.version}</version>
</dependency>
<!-- core library -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
Expand Down Expand Up @@ -200,6 +178,45 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>${resteasy.version}</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>${resteasy.version}</version>
<exclusions>
<exclusion>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
25 changes: 2 additions & 23 deletions stub-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,13 @@
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
</dependency>
<dependency>
<groupId>org.zanata</groupId>
<artifactId>zanata-common-api</artifactId>
<exclusions>
<!-- mock server uses RESTEasy -->
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
Expand Down Expand Up @@ -83,26 +78,10 @@
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<exclusions>
<exclusion>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.scannotation</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void test503ResponseHandling() throws IOException {
Mockito.verify(mockAbortStrategy).abort(throwableCapture.capture());
assertThat("Client will display meaningful message for 503",
throwableCapture.getValue().getMessage()
.contains("503 Service Unavailable"));
.contains("unavailable"));
}
}
}
39 changes: 4 additions & 35 deletions zanata-client-commands/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,10 @@
<dependency>
<groupId>org.zanata</groupId>
<artifactId>zanata-common-api</artifactId>
<exclusions>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
</dependency>
<dependency>
<groupId>org.zanata</groupId>
Expand All @@ -55,28 +45,12 @@
<dependency>
<groupId>org.zanata</groupId>
<artifactId>zanata-rest-client</artifactId>
<exclusions>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.zanata</groupId>
<artifactId>zanata-adapter-glossary</artifactId>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
Expand All @@ -94,11 +68,6 @@
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@
*/
package org.zanata.client.commands;

import static org.zanata.client.commands.ConsoleInteractorImpl.AnswerValidator;
import static org.zanata.client.commands.Messages.get;
import static org.zanata.util.VersionUtility.getVersionInfo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.io.output.FileWriterWithEncoding;
import org.fedorahosted.openprops.Properties;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Weeks;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;

import static org.zanata.client.commands.ConsoleInteractorImpl.AnswerValidator;
import static org.zanata.client.commands.Messages.get;
import static org.zanata.util.VersionUtility.getVersionInfo;

/**
* This class checks whether there is newer version of client available. It will
Expand Down Expand Up @@ -200,23 +200,19 @@ public void checkNewerVersion() {
* @return latest version of client in sonatype oss
*/
private Optional<String> checkLatestVersion(ConsoleInteractor console) {
ClientResponse response;
Response response;
try {
DefaultClientConfig clientConfig =
new DefaultClientConfig();

Client client = com.sun.jersey.api.client.Client.create(
clientConfig);
WebResource target =
client.resource(sonatypeRestUrl)
Client client = ResteasyClientBuilder.newClient();
WebTarget target =
client.target(sonatypeRestUrl)
.path("artifact/maven/resolve")
.queryParam("g", "org.zanata")
.queryParam("a", "client")
.queryParam("p", "pom")
.queryParam("v", "LATEST")
.queryParam("r", "releases");
response = target.get(ClientResponse.class);
if (response.getClientResponseStatus() != ClientResponse.Status.OK) {
response = target.request(MediaType.APPLICATION_XML_TYPE).get();
if (response.getStatusInfo() != Response.Status.OK) {
log.debug(
"Failed to resolve latest client artifact [status {}]. Ignored",
response.getStatus());
Expand All @@ -230,14 +226,14 @@ private Optional<String> checkLatestVersion(ConsoleInteractor console) {
}
// cheap xml parsing
String payload =
response.getEntity(String.class).replaceAll("\\n", "");
response.readEntity(String.class).replaceAll("\\n", "");
Pattern pattern = Pattern.compile("^.+<version>(.+)</version>.+");
Matcher matcher = pattern.matcher(payload);
return matcher.matches() ? Optional.of(matcher.group(1)) : Optional
.<String> absent();
return matcher.matches() ? Optional.of(matcher.group(1)) :
Optional.absent();
}

private static enum Frequency {
private enum Frequency {
weekly, monthly, daily;
static Frequency from(String value) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -37,7 +40,6 @@

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.sun.jersey.api.client.ClientResponse;

/**
*
Expand Down Expand Up @@ -88,23 +90,22 @@ public void run() throws Exception {
: client.getProjectQualifiedName(project);

log.info("Pulling glossary from server");
ClientResponse response =
client.downloadFile(fileType, transLang, qualifiedName);

if (response
.getClientResponseStatus() == ClientResponse.Status.NOT_FOUND) {
Response response;
try {
response =
client.downloadFile(fileType, transLang, qualifiedName);
} catch (NotFoundException e) {
log.info("No glossary file in server");
return;
}

ClientUtil.checkResult(response);
InputStream glossaryFile = response.getEntity(InputStream.class);
InputStream glossaryFile = response.readEntity(InputStream.class);
if (glossaryFile == null) {
log.info("No glossary file in server");
return;
}
String fileName =
ClientUtil.getFileNameFromHeader(response.getHeaders());
ClientUtil.getFileNameFromHeader(response.getStringHeaders());
File file = new File(fileName);
PathUtil.makeDirs(file.getParentFile());
try (OutputStream out = new FileOutputStream(file)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.sun.jersey.api.client.UniformInterfaceException;

/**
* @author Patrick Huang <a
Expand Down Expand Up @@ -127,7 +126,7 @@ protected void createNewVersion() {
try {
clientFactory.getProjectIterationClient(opts.getProj(), versionId)
.put(iteration);
} catch (UniformInterfaceException ex) {
} catch (Exception ex) {
InitCommand.offerRetryOnServerError(ex, consoleInteractor);
createNewVersion();
}
Expand Down
Loading

0 comments on commit b1ae356

Please sign in to comment.