diff options
Diffstat (limited to 'base/common')
4 files changed, 58 insertions, 92 deletions
diff --git a/base/common/src/CMakeLists.txt b/base/common/src/CMakeLists.txt index e4f999921..f48ef8786 100644 --- a/base/common/src/CMakeLists.txt +++ b/base/common/src/CMakeLists.txt @@ -104,6 +104,13 @@ find_file(RESTEASY_ATOM_PROVIDER_JAR ${RESTEASY_LIB} ) +find_file(RESTEASY_CLIENT_JAR + NAMES + resteasy-client.jar + PATHS + ${RESTEASY_LIB} +) + find_file(HTTPCLIENT_JAR NAMES httpclient.jar @@ -128,7 +135,7 @@ javac(pki-certsrv-classes ${JSS_JAR} ${COMMONS_CODEC_JAR} ${COMMONS_HTTPCLIENT_JAR} ${APACHE_COMMONS_LANG_JAR} ${TOMCAT_CATALINA_JAR} ${TOMCAT_UTIL_JAR} ${SYMKEY_JAR} - ${JAXRS_API_JAR} ${RESTEASY_JAXRS_JAR} ${RESTEASY_ATOM_PROVIDER_JAR} + ${JAXRS_API_JAR} ${RESTEASY_JAXRS_JAR} ${RESTEASY_ATOM_PROVIDER_JAR} ${RESTEASY_CLIENT_JAR} ${HTTPCLIENT_JAR} ${HTTPCORE_JAR} OUTPUT_DIR ${CMAKE_BINARY_DIR}/classes diff --git a/base/common/src/com/netscape/certsrv/base/ResourceMessage.java b/base/common/src/com/netscape/certsrv/base/ResourceMessage.java index 57b5539b8..e7ef44f4b 100644 --- a/base/common/src/com/netscape/certsrv/base/ResourceMessage.java +++ b/base/common/src/com/netscape/certsrv/base/ResourceMessage.java @@ -13,6 +13,8 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @@ -24,6 +26,7 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; * @author Ade Lee */ @XmlRootElement(name="ResourceMessage") +@XmlAccessorType(XmlAccessType.NONE) public class ResourceMessage { protected Map<String, String> attributes = new LinkedHashMap<String, String>(); diff --git a/base/common/src/com/netscape/certsrv/client/PKIConnection.java b/base/common/src/com/netscape/certsrv/client/PKIConnection.java index 6e281dd5a..1c3e58a1b 100644 --- a/base/common/src/com/netscape/certsrv/client/PKIConnection.java +++ b/base/common/src/com/netscape/certsrv/client/PKIConnection.java @@ -8,6 +8,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -21,7 +22,8 @@ import java.util.Enumeration; import java.util.HashSet; import java.util.List; -import javax.ws.rs.core.MediaType; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import org.apache.commons.httpclient.ConnectTimeoutException; @@ -53,14 +55,11 @@ import org.apache.http.impl.client.RequestWrapper; import org.apache.http.message.BasicHttpResponse; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HttpContext; -import org.jboss.resteasy.client.ClientExecutor; -import org.jboss.resteasy.client.ClientRequest; -import org.jboss.resteasy.client.ClientResponse; -import org.jboss.resteasy.client.ClientResponseFailure; -import org.jboss.resteasy.client.ProxyFactory; -import org.jboss.resteasy.client.core.BaseClientResponse; -import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor; -import org.jboss.resteasy.client.core.extractors.ClientErrorHandler; +import org.jboss.resteasy.client.jaxrs.ProxyBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine; import org.jboss.resteasy.spi.ResteasyProviderFactory; import org.mozilla.jss.CryptoManager; import org.mozilla.jss.CryptoManager.NotInitializedException; @@ -68,6 +67,8 @@ import org.mozilla.jss.crypto.X509Certificate; import org.mozilla.jss.ssl.SSLCertificateApprovalCallback; import org.mozilla.jss.ssl.SSLSocket; +import com.netscape.certsrv.base.PKIException; + public class PKIConnection { @@ -82,9 +83,9 @@ public class PKIConnection { DefaultHttpClient httpClient = new DefaultHttpClient(); + ApacheHttpClient4Engine engine; + ResteasyClient resteasyClient; ResteasyProviderFactory providerFactory; - ClientErrorHandler errorHandler; - ClientExecutor executor; int requestCounter; int responseCounter; @@ -194,10 +195,8 @@ public class PKIConnection { } }); - executor = new ApacheHttpClient4Executor(httpClient); - providerFactory = ResteasyProviderFactory.getInstance(); - providerFactory.addClientErrorInterceptor(new PKIErrorInterceptor()); - errorHandler = new ClientErrorHandler(providerFactory.getClientErrorInterceptors()); + engine = new ApacheHttpClient4Engine(httpClient); + resteasyClient = new ResteasyClientBuilder().httpEngine(engine).build(); } public void storeRequest(File file, HttpRequest request) throws IOException { @@ -511,29 +510,48 @@ public class PKIConnection { } public <T> T createProxy(URI uri, Class<T> clazz) throws URISyntaxException { - return ProxyFactory.create(clazz, uri, executor, providerFactory); + ResteasyWebTarget target = resteasyClient.target(uri); + return ProxyBuilder.builder(clazz, target).build(); } - @SuppressWarnings("unchecked") public <T> T getEntity(Response response, Class<T> clazz) { - BaseClientResponse<T> clientResponse = (BaseClientResponse<T>)response; - try { - clientResponse.checkFailureStatus(); - } catch (ClientResponseFailure e) { - errorHandler.clientErrorHandling((BaseClientResponse<T>) e.getResponse(), e); + // handle HTTP status code 4xx and 5xx only + int code = response.getStatus(); + if (code < 400) { + if (!response.hasEntity()) return null; + return response.readEntity(clazz); + } + + String contentType = response.getHeaderString("Content-Type"); + + if (contentType == null) + throw new PKIException("HTTP Error " + code); - } catch (RuntimeException e) { - errorHandler.clientErrorHandling(clientResponse, e); + PKIException.Data data = response.readEntity(PKIException.Data.class); + + Class<?> exceptionClass; + try { + exceptionClass = Class.forName(data.getClassName()); + } catch (ClassNotFoundException e) { + throw new PKIException(e.getMessage(), e); } - return clientResponse.getEntity(); + try { + throw (PKIException) exceptionClass.getConstructor(PKIException.Data.class).newInstance(data); + } catch (InstantiationException + | IllegalAccessException + | IllegalArgumentException + | InvocationTargetException + | NoSuchMethodException + | SecurityException e) { + throw new PKIException(e.getMessage(), e); + } } - public ClientResponse<String> post(String content) throws Exception { - ClientRequest request = executor.createRequest(config.getServerURI().toString()); - request.body(MediaType.APPLICATION_FORM_URLENCODED, content); - return request.post(String.class); + public String post(MultivaluedMap<String, String> form) throws Exception { + ResteasyWebTarget target = resteasyClient.target(config.getServerURI()); + return target.request().post(Entity.form(form), String.class); } public void addRejectedCertStatus(Integer rejectedCertStatus) { diff --git a/base/common/src/com/netscape/certsrv/client/PKIErrorInterceptor.java b/base/common/src/com/netscape/certsrv/client/PKIErrorInterceptor.java deleted file mode 100644 index abb892289..000000000 --- a/base/common/src/com/netscape/certsrv/client/PKIErrorInterceptor.java +++ /dev/null @@ -1,62 +0,0 @@ -// --- BEGIN COPYRIGHT BLOCK --- -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; version 2 of the License. -// -// This program 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 General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// (C) 2007 Red Hat, Inc. -// All rights reserved. -// --- END COPYRIGHT BLOCK --- -package com.netscape.certsrv.client; - -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; - -import org.jboss.resteasy.client.ClientResponse; -import org.jboss.resteasy.client.core.ClientErrorInterceptor; - -import com.netscape.certsrv.base.PKIException; - -public class PKIErrorInterceptor implements ClientErrorInterceptor { - - public void handle(ClientResponse<?> response) { - - // handle HTTP code 4xx and 5xx - int code = response.getResponseStatus().getStatusCode(); - if (code < 400) - return; - - MultivaluedMap<String, String> headers = response.getResponseHeaders(); - String contentType = headers.getFirst("Content-Type"); - - // handle XML content only - if (contentType == null || !contentType.startsWith(MediaType.APPLICATION_XML)) - return; - - PKIException exception; - - try { - // Requires RESTEasy 2.3.2 - // https://issues.jboss.org/browse/RESTEASY-652 - PKIException.Data data = response.getEntity(PKIException.Data.class); - - Class<?> clazz = Class.forName(data.getClassName()); - exception = (PKIException) clazz.getConstructor(PKIException.Data.class).newInstance(data); - - } catch (Exception e) { - e.printStackTrace(); - return; - } - - throw exception; - } - -} |