001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 * ataillefer 019 */ 020package org.nuxeo.ecm.automation.client.jaxrs.impl; 021 022import org.apache.http.HttpHost; 023import org.apache.http.client.HttpClient; 024import org.apache.http.conn.params.ConnRoutePNames; 025import org.apache.http.impl.client.DefaultHttpClient; 026import org.apache.http.impl.conn.PoolingClientConnectionManager; 027import org.nuxeo.ecm.automation.client.adapters.AsyncSessionFactory; 028import org.nuxeo.ecm.automation.client.adapters.BusinessServiceFactory; 029import org.nuxeo.ecm.automation.client.adapters.DocumentServiceFactory; 030import org.nuxeo.ecm.automation.client.jaxrs.spi.AsyncAutomationClient; 031import org.nuxeo.ecm.automation.client.jaxrs.spi.Connector; 032import org.nuxeo.ecm.automation.client.rest.api.RestClient; 033 034/** 035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 036 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a> 037 */ 038public class HttpAutomationClient extends AsyncAutomationClient { 039 040 protected HttpClient http; 041 042 protected int httpConnectionTimeout; 043 044 /** 045 * Instantiates a new {@link HttpAutomationClient} with no timeout for the HTTP connection and the default timeout 046 * for the wait of the asynchronous thread pool termination: 2 seconds. 047 */ 048 public HttpAutomationClient(String url) { 049 this(url, 0); 050 } 051 052 /** 053 * Instantiates a new {@link HttpAutomationClient} with the given timeout in milliseconds for the HTTP connection 054 * and the default timeout for the wait of the asynchronous thread pool termination: 2 seconds. 055 * 056 * @since 5.7 057 */ 058 public HttpAutomationClient(String url, int httpConnectionTimeout) { 059 super(url); 060 init(httpConnectionTimeout); 061 } 062 063 /** 064 * Instantiates a new {@link HttpAutomationClient} with the given timeout in milliseconds for the HTTP connection 065 * and the given timeout in milliseconds for the wait of the asynchronous thread pool termination. 066 * 067 * @since 5.7 068 */ 069 public HttpAutomationClient(String url, int httpConnectionTimeout, long asyncAwaitTerminationTimeout) { 070 super(url, asyncAwaitTerminationTimeout); 071 init(httpConnectionTimeout); 072 } 073 074 private void init(int httpConnectionTimeout) { 075 http = new DefaultHttpClient(new PoolingClientConnectionManager()); 076 this.httpConnectionTimeout = httpConnectionTimeout; 077 // http.setCookieSpecs(null); 078 // http.setCookieStore(null); 079 registerAdapter(new DocumentServiceFactory()); 080 registerAdapter(new BusinessServiceFactory()); 081 registerAdapter(new AsyncSessionFactory()); 082 } 083 084 public void setProxy(String host, int port) { 085 // httpclient.getCredentialsProvider().setCredentials( 086 // new AuthScope(PROXY, PROXY_PORT), 087 // new UsernamePasswordCredentials("username", "password")); 088 089 http.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(host, port)); 090 } 091 092 public HttpClient http() { 093 return http; 094 } 095 096 @Override 097 public synchronized void shutdown() { 098 super.shutdown(); 099 http.getConnectionManager().shutdown(); 100 http = null; 101 } 102 103 @Override 104 protected Connector newConnector() { 105 return new HttpConnector(http, httpConnectionTimeout); 106 } 107 108 /** 109 * Returns the {@link RestClient} associated to this 110 * {@link org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient}. 111 * 112 * @since 5.8 113 */ 114 public RestClient getRestClient() { 115 return new RestClient(this); 116 } 117}