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