001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 * ataillefer 012 */ 013package org.nuxeo.ecm.automation.client.jaxrs.impl; 014 015import org.apache.http.HttpHost; 016import org.apache.http.client.HttpClient; 017import org.apache.http.conn.params.ConnRoutePNames; 018import org.apache.http.impl.client.DefaultHttpClient; 019import org.apache.http.impl.conn.PoolingClientConnectionManager; 020import org.nuxeo.ecm.automation.client.adapters.BusinessServiceFactory; 021import org.nuxeo.ecm.automation.client.adapters.DocumentServiceFactory; 022import org.nuxeo.ecm.automation.client.jaxrs.spi.AsyncAutomationClient; 023import org.nuxeo.ecm.automation.client.jaxrs.spi.Connector; 024import org.nuxeo.ecm.automation.client.rest.api.RestClient; 025 026/** 027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 028 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a> 029 */ 030public class HttpAutomationClient extends AsyncAutomationClient { 031 032 protected HttpClient http; 033 034 protected int httpConnectionTimeout; 035 036 /** 037 * Instantiates a new {@link HttpAutomationClient} with no timeout for the HTTP connection and the default timeout 038 * for the wait of the asynchronous thread pool termination: 2 seconds. 039 */ 040 public HttpAutomationClient(String url) { 041 this(url, 0); 042 } 043 044 /** 045 * Instantiates a new {@link HttpAutomationClient} with the given timeout in milliseconds for the HTTP connection 046 * and the default timeout for the wait of the asynchronous thread pool termination: 2 seconds. 047 * 048 * @since 5.7 049 */ 050 public HttpAutomationClient(String url, int httpConnectionTimeout) { 051 super(url); 052 init(httpConnectionTimeout); 053 } 054 055 /** 056 * Instantiates a new {@link HttpAutomationClient} with the given timeout in milliseconds for the HTTP connection 057 * and the given timeout in milliseconds for the wait of the asynchronous thread pool termination. 058 * 059 * @since 5.7 060 */ 061 public HttpAutomationClient(String url, int httpConnectionTimeout, long asyncAwaitTerminationTimeout) { 062 super(url, asyncAwaitTerminationTimeout); 063 init(httpConnectionTimeout); 064 } 065 066 private void init(int httpConnectionTimeout) { 067 http = new DefaultHttpClient(new PoolingClientConnectionManager()); 068 this.httpConnectionTimeout = httpConnectionTimeout; 069 // http.setCookieSpecs(null); 070 // http.setCookieStore(null); 071 registerAdapter(new DocumentServiceFactory()); 072 registerAdapter(new BusinessServiceFactory()); 073 } 074 075 public void setProxy(String host, int port) { 076 // httpclient.getCredentialsProvider().setCredentials( 077 // new AuthScope(PROXY, PROXY_PORT), 078 // new UsernamePasswordCredentials("username", "password")); 079 080 http.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(host, port)); 081 } 082 083 public HttpClient http() { 084 return http; 085 } 086 087 @Override 088 public synchronized void shutdown() { 089 super.shutdown(); 090 http.getConnectionManager().shutdown(); 091 http = null; 092 } 093 094 @Override 095 protected Connector newConnector() { 096 return new HttpConnector(http, httpConnectionTimeout); 097 } 098 099 /** 100 * Returns the {@link RestClient} associated to this 101 * {@link org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient}. 102 * 103 * @since 5.8 104 */ 105 public RestClient getRestClient() { 106 return new RestClient(this); 107 } 108}