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.BusinessServiceFactory;
028import org.nuxeo.ecm.automation.client.adapters.DocumentServiceFactory;
029import org.nuxeo.ecm.automation.client.jaxrs.spi.AsyncAutomationClient;
030import org.nuxeo.ecm.automation.client.jaxrs.spi.Connector;
031import org.nuxeo.ecm.automation.client.rest.api.RestClient;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
036 */
037public class HttpAutomationClient extends AsyncAutomationClient {
038
039    protected HttpClient http;
040
041    protected int httpConnectionTimeout;
042
043    /**
044     * Instantiates a new {@link HttpAutomationClient} with no timeout for the HTTP connection and the default timeout
045     * for the wait of the asynchronous thread pool termination: 2 seconds.
046     */
047    public HttpAutomationClient(String url) {
048        this(url, 0);
049    }
050
051    /**
052     * Instantiates a new {@link HttpAutomationClient} with the given timeout in milliseconds for the HTTP connection
053     * and the default timeout for the wait of the asynchronous thread pool termination: 2 seconds.
054     *
055     * @since 5.7
056     */
057    public HttpAutomationClient(String url, int httpConnectionTimeout) {
058        super(url);
059        init(httpConnectionTimeout);
060    }
061
062    /**
063     * Instantiates a new {@link HttpAutomationClient} with the given timeout in milliseconds for the HTTP connection
064     * and the given timeout in milliseconds for the wait of the asynchronous thread pool termination.
065     *
066     * @since 5.7
067     */
068    public HttpAutomationClient(String url, int httpConnectionTimeout, long asyncAwaitTerminationTimeout) {
069        super(url, asyncAwaitTerminationTimeout);
070        init(httpConnectionTimeout);
071    }
072
073    private void init(int httpConnectionTimeout) {
074        http = new DefaultHttpClient(new PoolingClientConnectionManager());
075        this.httpConnectionTimeout = httpConnectionTimeout;
076        // http.setCookieSpecs(null);
077        // http.setCookieStore(null);
078        registerAdapter(new DocumentServiceFactory());
079        registerAdapter(new BusinessServiceFactory());
080    }
081
082    public void setProxy(String host, int port) {
083        // httpclient.getCredentialsProvider().setCredentials(
084        // new AuthScope(PROXY, PROXY_PORT),
085        // new UsernamePasswordCredentials("username", "password"));
086
087        http.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(host, port));
088    }
089
090    public HttpClient http() {
091        return http;
092    }
093
094    @Override
095    public synchronized void shutdown() {
096        super.shutdown();
097        http.getConnectionManager().shutdown();
098        http = null;
099    }
100
101    @Override
102    protected Connector newConnector() {
103        return new HttpConnector(http, httpConnectionTimeout);
104    }
105
106    /**
107     * Returns the {@link RestClient} associated to this
108     * {@link org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient}.
109     *
110     * @since 5.8
111     */
112    public RestClient getRestClient() {
113        return new RestClient(this);
114    }
115}