You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2013/08/06 00:07:36 UTC
svn commit: r1510776 - in /chemistry/opencmis/trunk: ./
chemistry-opencmis-android/chemistry-opencmis-android-client/
chemistry-opencmis-android/chemistry-opencmis-android-client/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/
che...
Author: fmui
Date: Mon Aug 5 22:07:36 2013
New Revision: 1510776
URL: http://svn.apache.org/r1510776
Log:
Apache HTTP Client support for Java SE and Android (not ready for productive use)
Added:
chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java (with props)
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/AbstractApacheClientHttpInvoker.java (with props)
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java (with props)
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/pom.xml
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/pom.xml
chemistry/opencmis/trunk/pom.xml
Modified: chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/pom.xml
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/pom.xml?rev=1510776&r1=1510775&r2=1510776&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/pom.xml (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/pom.xml Mon Aug 5 22:07:36 2013
@@ -87,6 +87,7 @@
<exclude name="**/spi/LTPAWSSecurityAuthenticationProvider.*" />
<exclude name="**/spi/local/**" />
<exclude name="**/spi/http/DefaultHttpInvoker.*" />
+ <exclude name="**/spi/http/ApacheClientHttpInvoker.*" />
<exclude name="**/spi/webservices/**" />
<exclude name="**/spi/atompub/**" />
</fileset>
Added: chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java?rev=1510776&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java Mon Aug 5 22:07:36 2013
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.client.bindings.spi.http;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
+
+import org.apache.chemistry.opencmis.client.bindings.impl.CmisBindingsHelper;
+import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
+import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
+import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.conn.params.ConnManagerPNames;
+import org.apache.http.conn.params.ConnPerRouteBean;
+import org.apache.http.conn.scheme.LayeredSocketFactory;
+import org.apache.http.conn.scheme.PlainSocketFactory;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.ProxySelectorRoutePlanner;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+
+/**
+ * A {@link HttpInvoker} that uses The Apache HTTP client.
+ */
+public class ApacheClientHttpInvoker extends AbstractApacheClientHttpInvoker {
+
+ protected DefaultHttpClient createHttpClient(UrlBuilder url, BindingSession session) {
+ // set params
+ HttpParams params = createDefaultHttpParams(session);
+
+ // set max connection
+ String maxConnStr = System.getProperty("http.maxConnections", "5");
+ int maxConn = 5;
+ try {
+ maxConn = Integer.parseInt(maxConnStr);
+ } catch (NumberFormatException nfe) {
+ // ignore
+ }
+ params.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, maxConn * 4);
+ params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(maxConn));
+
+ // set up scheme registry
+ SchemeRegistry registry = new SchemeRegistry();
+ registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
+ registry.register(new Scheme("https", getSSLSocketFactory(url, session), 443));
+
+ // set up connection manager
+ ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, registry);
+
+ // set up proxy
+ ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(registry, null);
+
+ // set up client
+ DefaultHttpClient httpclient = new DefaultHttpClient(connManager, params);
+ httpclient.setRoutePlanner(routePlanner);
+
+ return httpclient;
+ }
+
+ /**
+ * Builds a SSL Socket Factory for the Apache HTTP Client.
+ */
+ private LayeredSocketFactory getSSLSocketFactory(final UrlBuilder url, final BindingSession session) {
+ // get authentication provider
+ AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(session);
+
+ // check SSL Socket Factory
+ final SSLSocketFactory sf = authProvider.getSSLSocketFactory();
+ if (sf == null) {
+ // no custom factory -> return default factory
+ return org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory();
+ }
+
+ // check hostame verifier and use default if not set
+ final HostnameVerifier hv = (authProvider.getHostnameVerifier() == null ? new BrowserCompatHostnameVerifier()
+ : authProvider.getHostnameVerifier());
+
+ // build new socket factory
+ return new LayeredSocketFactory() {
+
+ public boolean isSecure(Socket sock) throws IllegalArgumentException {
+ return true;
+ }
+
+ public Socket createSocket() throws IOException {
+ return (SSLSocket) sf.createSocket();
+ }
+
+ public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
+ UnknownHostException {
+ SSLSocket sslSocket = (SSLSocket) sf.createSocket(socket, host, port, autoClose);
+ verify(hv, host, sslSocket);
+
+ return sslSocket;
+ }
+
+ public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
+ HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
+ SSLSocket sslSocket = (SSLSocket) (sock != null ? sock : createSocket());
+
+ if (localAddress != null || localPort > 0) {
+ if (localPort < 0) {
+ localPort = 0;
+ }
+
+ InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
+ sslSocket.bind(isa);
+ }
+
+ InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
+
+ int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
+ int soTimeout = HttpConnectionParams.getSoTimeout(params);
+
+ try {
+ sock.setSoTimeout(soTimeout);
+ sock.connect(remoteAddress, connTimeout);
+ } catch (SocketTimeoutException ex) {
+ closeSocket(sock);
+ throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out!");
+ }
+
+ verify(hv, host, sslSocket);
+
+ return sslSocket;
+ }
+ };
+ }
+}
Propchange: chemistry/opencmis/trunk/chemistry-opencmis-android/chemistry-opencmis-android-client/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/pom.xml
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/pom.xml?rev=1510776&r1=1510775&r2=1510776&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/pom.xml (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/pom.xml Mon Aug 5 22:07:36 2013
@@ -102,6 +102,12 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>${apacheclient.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
Added: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/AbstractApacheClientHttpInvoker.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/AbstractApacheClientHttpInvoker.java?rev=1510776&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/AbstractApacheClientHttpInvoker.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/AbstractApacheClientHttpInvoker.java Mon Aug 5 22:07:36 2013
@@ -0,0 +1,363 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.client.bindings.spi.http;
+
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.math.BigInteger;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.zip.GZIPOutputStream;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLSocket;
+
+import org.apache.chemistry.opencmis.client.bindings.impl.ClientVersion;
+import org.apache.chemistry.opencmis.client.bindings.impl.CmisBindingsHelper;
+import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
+import org.apache.chemistry.opencmis.commons.SessionParameter;
+import org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException;
+import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
+import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.conn.ssl.X509HostnameVerifier;
+import org.apache.http.entity.AbstractHttpEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A {@link HttpInvoker} that uses The Apache HTTP client.
+ */
+public abstract class AbstractApacheClientHttpInvoker implements HttpInvoker {
+
+ protected static final Logger LOG = LoggerFactory.getLogger(AbstractApacheClientHttpInvoker.class);
+
+ protected static final String HTTP_CLIENT = "org.apache.chemistry.opencmis.client.bindings.spi.http.ApacheClientHttpInvoker.httpClient";
+ protected static final int BUFFER_SIZE = 2 * 1024 * 1024;
+
+ public Response invokeGET(UrlBuilder url, BindingSession session) {
+ return invoke(url, "GET", null, null, null, session, null, null);
+ }
+
+ public Response invokeGET(UrlBuilder url, BindingSession session, BigInteger offset, BigInteger length) {
+ return invoke(url, "GET", null, null, null, session, offset, length);
+ }
+
+ public Response invokePOST(UrlBuilder url, String contentType, Output writer, BindingSession session) {
+ return invoke(url, "POST", contentType, null, writer, session, null, null);
+ }
+
+ public Response invokePUT(UrlBuilder url, String contentType, Map<String, String> headers, Output writer,
+ BindingSession session) {
+ return invoke(url, "PUT", contentType, headers, writer, session, null, null);
+ }
+
+ public Response invokeDELETE(UrlBuilder url, BindingSession session) {
+ return invoke(url, "DELETE", null, null, null, session, null, null);
+ }
+
+ protected Response invoke(UrlBuilder url, String method, String contentType, Map<String, String> headers,
+ final Output writer, final BindingSession session, BigInteger offset, BigInteger length) {
+ try {
+ // log before connect
+ if (LOG.isDebugEnabled()) {
+ LOG.debug(method + " " + url);
+ }
+
+ // get HTTP client object from session
+ DefaultHttpClient httpclient = (DefaultHttpClient) session.get(HTTP_CLIENT);
+ if (httpclient == null) {
+ session.writeLock();
+ try {
+ httpclient = (DefaultHttpClient) session.get(HTTP_CLIENT);
+ if (httpclient == null) {
+ httpclient = createHttpClient(url, session);
+ session.put(HTTP_CLIENT, httpclient, true);
+ }
+ } finally {
+ session.writeUnlock();
+ }
+ }
+
+ HttpRequestBase request = null;
+
+ if ("GET".equals(method)) {
+ request = new HttpGet(url.toString());
+ } else if ("POST".equals(method)) {
+ request = new HttpPost(url.toString());
+ } else if ("PUT".equals(method)) {
+ request = new HttpPut(url.toString());
+ } else if ("DELETE".equals(method)) {
+ request = new HttpDelete(url.toString());
+ }
+
+ // set content type
+ if (contentType != null) {
+ request.setHeader("Content-Type", contentType);
+ }
+ // set other headers
+ if (headers != null) {
+ for (Map.Entry<String, String> header : headers.entrySet()) {
+ request.addHeader(header.getKey(), header.getValue());
+ }
+ }
+
+ // authenticate
+ AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(session);
+ if (authProvider != null) {
+ Map<String, List<String>> httpHeaders = authProvider.getHTTPHeaders(url.toString());
+ if (httpHeaders != null) {
+ for (Map.Entry<String, List<String>> header : httpHeaders.entrySet()) {
+ if (header.getKey() != null && header.getValue() != null && !header.getValue().isEmpty()) {
+ String key = header.getKey();
+ if (key.equalsIgnoreCase("user-agent")) {
+ request.setHeader("User-Agent", header.getValue().get(0));
+ } else {
+ for (String value : header.getValue()) {
+ if (value != null) {
+ request.addHeader(key, value);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // range
+ if ((offset != null) || (length != null)) {
+ StringBuilder sb = new StringBuilder("bytes=");
+
+ if ((offset == null) || (offset.signum() == -1)) {
+ offset = BigInteger.ZERO;
+ }
+
+ sb.append(offset.toString());
+ sb.append("-");
+
+ if ((length != null) && (length.signum() == 1)) {
+ sb.append(offset.add(length.subtract(BigInteger.ONE)).toString());
+ }
+
+ request.setHeader("Range", sb.toString());
+ }
+
+ // compression
+ Object compression = session.get(SessionParameter.COMPRESSION);
+ if ((compression != null) && Boolean.parseBoolean(compression.toString())) {
+ request.setHeader("Accept-Encoding", "gzip,deflate");
+ }
+
+ // locale
+ if (session.get(CmisBindingsHelper.ACCEPT_LANGUAGE) instanceof String) {
+ request.setHeader("Accept-Language", session.get(CmisBindingsHelper.ACCEPT_LANGUAGE).toString());
+ }
+
+ // send data
+ if (writer != null) {
+ Object clientCompression = session.get(SessionParameter.CLIENT_COMPRESSION);
+ final boolean clientCompressionFlag = (clientCompression != null)
+ && Boolean.parseBoolean(clientCompression.toString());
+ if (clientCompressionFlag) {
+ request.setHeader("Content-Encoding", "gzip");
+ }
+
+ AbstractHttpEntity streamEntity = new AbstractHttpEntity() {
+ @Override
+ public boolean isChunked() {
+ return true;
+ }
+
+ public boolean isRepeatable() {
+ return false;
+ }
+
+ public long getContentLength() {
+ return -1;
+ }
+
+ public boolean isStreaming() {
+ return false;
+ }
+
+ public InputStream getContent() throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void writeTo(final OutputStream outstream) throws IOException {
+ OutputStream connOut = null;
+
+ if (clientCompressionFlag) {
+ connOut = new GZIPOutputStream(outstream, 4096);
+ } else {
+ connOut = outstream;
+ }
+
+ OutputStream out = new BufferedOutputStream(connOut, BUFFER_SIZE);
+ try {
+ writer.write(out);
+ } catch (IOException ioe) {
+ throw ioe;
+ } catch (Exception e) {
+ new IOException(e);
+ }
+ out.flush();
+
+ if (connOut instanceof GZIPOutputStream) {
+ ((GZIPOutputStream) connOut).finish();
+ }
+ }
+ };
+ ((HttpEntityEnclosingRequestBase) request).setEntity(streamEntity);
+ }
+
+ // connect
+ HttpResponse response = httpclient.execute(request);
+ HttpEntity entity = response.getEntity();
+
+ // get stream, if present
+ int respCode = response.getStatusLine().getStatusCode();
+ InputStream inputStream = null;
+ InputStream errorStream = null;
+
+ if ((respCode == 200) || (respCode == 201) || (respCode == 203) || (respCode == 206)) {
+ if (entity != null) {
+ inputStream = entity.getContent();
+ } else {
+ inputStream = new ByteArrayInputStream(new byte[0]);
+ }
+ } else {
+ if (entity != null) {
+ errorStream = entity.getContent();
+ } else {
+ errorStream = new ByteArrayInputStream(new byte[0]);
+ }
+ }
+
+ // collect headers
+ Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
+ for (Header header : response.getAllHeaders()) {
+ List<String> values = responseHeaders.get(header.getName());
+ if (values == null) {
+ values = new ArrayList<String>();
+ responseHeaders.put(header.getName(), values);
+ }
+ values.add(header.getValue());
+ }
+
+ // log after connect
+ if (LOG.isTraceEnabled()) {
+ LOG.trace(method + " " + url + " > Headers: " + responseHeaders);
+ }
+
+ // forward response HTTP headers
+ if (authProvider != null) {
+ authProvider.putResponseHeaders(url.toString(), respCode, responseHeaders);
+ }
+
+ // get the response
+ return new Response(respCode, response.getStatusLine().getReasonPhrase(), responseHeaders, inputStream,
+ errorStream);
+ } catch (Exception e) {
+ throw new CmisConnectionException("Cannot access " + url + ": " + e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Creates default params for the Apache HTTP Client.
+ */
+ protected HttpParams createDefaultHttpParams(BindingSession session) {
+ HttpParams params = new BasicHttpParams();
+
+ HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
+ HttpProtocolParams.setUserAgent(params, ClientVersion.OPENCMIS_CLIENT);
+ HttpProtocolParams.setContentCharset(params, "UTF-8");
+ HttpProtocolParams.setUseExpectContinue(params, true);
+
+ HttpConnectionParams.setStaleCheckingEnabled(params, false);
+
+ int connectTimeout = session.get(SessionParameter.CONNECT_TIMEOUT, -1);
+ if (connectTimeout >= 0) {
+ HttpConnectionParams.setConnectionTimeout(params, connectTimeout);
+ }
+
+ int readTimeout = session.get(SessionParameter.READ_TIMEOUT, -1);
+ if (readTimeout >= 0) {
+ HttpConnectionParams.setSoTimeout(params, readTimeout);
+ }
+
+ return params;
+ }
+
+ /**
+ * Verifies a hostname with the given verifier.
+ */
+ protected void verify(HostnameVerifier verifier, String host, SSLSocket sslSocket) throws IOException {
+ try {
+ if (verifier instanceof X509HostnameVerifier) {
+ ((X509HostnameVerifier) verifier).verify(host, sslSocket);
+ } else {
+ if (!verifier.verify(host, sslSocket.getSession())) {
+ throw new SSLException("Hostname in certificate didn't match: <" + host + ">");
+ }
+ }
+ } catch (IOException ioe) {
+ closeSocket(sslSocket);
+ throw ioe;
+ }
+ }
+
+ /**
+ * Closes the given socket and ignores exceptions.
+ */
+ protected void closeSocket(Socket socket) {
+ try {
+ socket.close();
+ } catch (IOException ioe) {
+ // ignore
+ }
+ }
+
+ /**
+ * Creates the {@link HttpClient} instance.
+ */
+ protected abstract DefaultHttpClient createHttpClient(UrlBuilder url, BindingSession session);
+}
Propchange: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/AbstractApacheClientHttpInvoker.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java?rev=1510776&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java Mon Aug 5 22:07:36 2013
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.client.bindings.spi.http;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
+
+import org.apache.chemistry.opencmis.client.bindings.impl.CmisBindingsHelper;
+import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
+import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
+import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
+import org.apache.http.client.params.ClientPNames;
+import org.apache.http.client.params.CookiePolicy;
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.conn.HttpInetSocketAddress;
+import org.apache.http.conn.scheme.PlainSocketFactory;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeLayeredSocketFactory;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
+import org.apache.http.conn.ssl.X509HostnameVerifier;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.PoolingClientConnectionManager;
+import org.apache.http.impl.conn.ProxySelectorRoutePlanner;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+
+/**
+ * A {@link HttpInvoker} that uses The Apache HTTP client.
+ */
+public class ApacheClientHttpInvoker extends AbstractApacheClientHttpInvoker {
+
+ protected DefaultHttpClient createHttpClient(UrlBuilder url, BindingSession session) {
+ // set params
+ HttpParams params = createDefaultHttpParams(session);
+ params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
+
+ // set up scheme registry and connection manager
+ SchemeRegistry registry = new SchemeRegistry();
+ registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
+ registry.register(new Scheme("https", 443, getSSLSocketFactory(url, session)));
+
+ // set up connection manager
+ PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(registry);
+
+ // set max connection a
+ String keepAliveStr = System.getProperty("http.keepAlive", "true");
+ if ("true".equalsIgnoreCase(keepAliveStr)) {
+ String maxConnStr = System.getProperty("http.maxConnections", "5");
+ int maxConn = 5;
+ try {
+ maxConn = Integer.parseInt(maxConnStr);
+ } catch (NumberFormatException nfe) {
+ // ignore
+ }
+ connManager.setDefaultMaxPerRoute(maxConn);
+ connManager.setMaxTotal(4 * maxConn);
+ }
+
+ // set up proxy
+ ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(registry, null);
+
+ // set up client
+ DefaultHttpClient httpclient = new DefaultHttpClient(connManager, params);
+ httpclient.setRoutePlanner(routePlanner);
+
+ return httpclient;
+ }
+
+ /**
+ * Builds a SSL Socket Factory for the Apache HTTP Client.
+ */
+ private SchemeLayeredSocketFactory getSSLSocketFactory(final UrlBuilder url, final BindingSession session) {
+ // get authentication provider
+ AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(session);
+
+ // check SSL Socket Factory
+ final SSLSocketFactory sf = authProvider.getSSLSocketFactory();
+ if (sf == null) {
+ // no custom factory -> return default factory
+ return org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory();
+ }
+
+ // check hostame verifier and use default if not set
+ final HostnameVerifier hv = (authProvider.getHostnameVerifier() == null ? new BrowserCompatHostnameVerifier()
+ : authProvider.getHostnameVerifier());
+
+ if (hv instanceof X509HostnameVerifier) {
+ return new org.apache.http.conn.ssl.SSLSocketFactory(sf, (X509HostnameVerifier) hv);
+ }
+
+ // build new socket factory
+ return new SchemeLayeredSocketFactory() {
+
+ public boolean isSecure(Socket sock) throws IllegalArgumentException {
+ return true;
+ }
+
+ public Socket createSocket(HttpParams params) throws IOException {
+ return (SSLSocket) sf.createSocket();
+ }
+
+ public Socket connectSocket(final Socket socket, final InetSocketAddress remoteAddress,
+ final InetSocketAddress localAddress, final HttpParams params) throws IOException,
+ UnknownHostException, ConnectTimeoutException {
+
+ Socket sock = (socket != null ? socket : createSocket(params));
+ if (localAddress != null) {
+ sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
+ sock.bind(localAddress);
+ }
+
+ int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
+ int soTimeout = HttpConnectionParams.getSoTimeout(params);
+
+ try {
+ sock.setSoTimeout(soTimeout);
+ sock.connect(remoteAddress, connTimeout);
+ } catch (SocketTimeoutException ex) {
+ closeSocket(sock);
+ throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out!");
+ }
+
+ String host;
+ if (remoteAddress instanceof HttpInetSocketAddress) {
+ host = ((HttpInetSocketAddress) remoteAddress).getHttpHost().getHostName();
+ } else {
+ host = remoteAddress.getHostName();
+ }
+
+ SSLSocket sslSocket;
+ if (sock instanceof SSLSocket) {
+ sslSocket = (SSLSocket) sock;
+ } else {
+ int port = remoteAddress.getPort();
+ sslSocket = (SSLSocket) sf.createSocket(sock, host, port, true);
+ }
+ verify(hv, host, sslSocket);
+
+ return sslSocket;
+ }
+
+ public Socket createLayeredSocket(final Socket socket, final String host, final int port,
+ final HttpParams params) throws IOException, UnknownHostException {
+ SSLSocket sslSocket = (SSLSocket) sf.createSocket(socket, host, port, true);
+ verify(hv, host, sslSocket);
+
+ return sslSocket;
+ }
+ };
+ }
+}
Propchange: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/ApacheClientHttpInvoker.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: chemistry/opencmis/trunk/pom.xml
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/pom.xml?rev=1510776&r1=1510775&r2=1510776&view=diff
==============================================================================
--- chemistry/opencmis/trunk/pom.xml (original)
+++ chemistry/opencmis/trunk/pom.xml Mon Aug 5 22:07:36 2013
@@ -201,7 +201,7 @@
<module>chemistry-opencmis-osgi/chemistry-opencmis-osgi-client</module>
<module>chemistry-opencmis-osgi/chemistry-opencmis-osgi-server</module>
<!-- <module>chemistry-opencmis-workbench/chemistry-opencmis-workbench-webstart</module> -->
- <module>chemistry-opencmis-android/chemistry-opencmis-android-client</module>
+ <module>chemistry-opencmis-android/chemistry-opencmis-android-client</module>
<module>chemistry-opencmis-dist</module>
</modules>
@@ -234,6 +234,7 @@
<junit.version>4.11</junit.version>
<slf4j.version>1.7.5</slf4j.version>
<log4j.version>1.2.17</log4j.version>
+ <apacheclient.version>4.2.5</apacheclient.version>
<cxf.version>2.7.5</cxf.version>
<axis2.version>1.6.2</axis2.version>
</properties>