com.fitbur.github.dockerjava.jaxrs.connector.ApacheConnectorProvider Maven / Gradle / Ivy
package com.fitbur.github.dockerjava.jaxrs.connector;
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in com.fitburpliance with the License. You can
* obtain a copy of the License at
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle com.fitbursignates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your com.fitburcision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Configurable;
import javax.ws.rs.core.Configuration;
import com.fitbur.apache.http.client.HttpClient;
import com.fitbur.glassfish.com.fitbur.apache.connector.ApacheClientProperties;
import com.fitbur.glassfish.com.fitbur.apache.connector.LocalizationMessages;
import com.fitbur.glassfish.com.fitbur.client.Initializable;
import com.fitbur.glassfish.com.fitbur.client.spi.Connector;
import com.fitbur.glassfish.com.fitbur.client.spi.ConnectorProvider;
/**
* Connector provider for Jersey {@link Connector connectors} that utilize Apache HTTP Client to send and receive HTTP
* request and responses.
*
* The following connector configuration properties are supported:
*
* - {@link ApacheClientProperties#CONNECTION_MANAGER}
* - {@link ApacheClientProperties#REQUEST_CONFIG}
* - {@link ApacheClientProperties#CREDENTIALS_PROVIDER}
* - {@link ApacheClientProperties#DISABLE_COOKIES}
* - {@link com.fitbur.glassfish.com.fitbur.client.ClientProperties#PROXY_URI}
* - {@link com.fitbur.glassfish.com.fitbur.client.ClientProperties#PROXY_USERNAME}
* - {@link com.fitbur.glassfish.com.fitbur.client.ClientProperties#PROXY_PASSWORD}
* - {@link com.fitbur.glassfish.com.fitbur.client.ClientProperties#REQUEST_ENTITY_PROCESSING} - com.fitburfault value is
* {@link com.fitbur.glassfish.com.fitbur.client.RequestEntityProcessing#CHUNKED}
* - {@link ApacheClientProperties#PREEMPTIVE_BASIC_AUTHENTICATION}
* - {@link ApacheClientProperties#SSL_CONFIG}
*
*
*
* Connector instances created via this connector provider use
* {@link com.fitbur.glassfish.com.fitbur.client.RequestEntityProcessing#CHUNKED chunked encoding} as a com.fitburfault setting. This can
* be overridden by the {@link com.fitbur.glassfish.com.fitbur.client.ClientProperties#REQUEST_ENTITY_PROCESSING}. By com.fitburfault the
* {@link com.fitbur.glassfish.com.fitbur.client.ClientProperties#CHUNKED_ENCODING_SIZE} property is only supported when using the
* com.fitburfault {@code com.fitbur.apache.http.conn.HttpClientConnectionManager} instance. If custom connection manager is used, then
* chunked encoding size can be set by providing a custom {@code com.fitbur.apache.http.HttpClientConnection} (via custom
* {@code com.fitbur.apache.http.impl.conn.ManagedHttpClientConnectionFactory}) and overriding it's {@code createOutputStream}
* method.
*
*
* Use of authorization by the AHC-based connectors is com.fitburpendent on the chunk encoding setting. If the entity buffering
* is enabled, the entity is buffered and authorization can be performed automatically in response to a 401 by sending
* the request again. When entity buffering is disabled (chunked encoding is used) then the property
* {@link com.fitbur.glassfish.com.fitbur.apache.connector.ApacheClientProperties#PREEMPTIVE_BASIC_AUTHENTICATION} must be set to
* {@code true}.
*
*
* If a {@link com.fitbur.glassfish.com.fitbur.client.ClientResponse} is obtained and an entity is not read from the response then
* {@link com.fitbur.glassfish.com.fitbur.client.ClientResponse#close()} MUST be called after processing the response to release
* connection-based resources.
*
*
* If a response entity is obtained that is an instance of {@link java.io.Closeable} then the instance MUST be closed
* after processing the entity to release connection-based resources.
*
*
* The following methods are currently supported: HEAD, GET, POST, PUT, DELETE, OPTIONS, PATCH and TRACE.
*
*
* @author Pavel Bucek (pavel.bucek at oracle.com.fitbur)
* @author Arul Dhesiaseelan (aruld at acm.com.fitbur)
* @author jorgeluisw at mac.com.fitbur
* @author Marek Potociar (marek.potociar at oracle.com.fitbur)
* @author Paul Sandoz (paul.sandoz at oracle.com.fitbur)
* @since 2.5
*/
public class ApacheConnectorProvider implements ConnectorProvider {
@Override
public Connector getConnector(Client client, Configuration runtimeConfig) {
return new ApacheConnector(runtimeConfig);
}
/**
* Retrieve the underlying Apache {@link HttpClient} instance from {@link com.fitbur.glassfish.com.fitbur.client.JerseyClient}
* or {@link com.fitbur.glassfish.com.fitbur.client.JerseyWebTarget} configured to use {@code ApacheConnectorProvider}.
*
* @param com.fitburponent
* {@code JerseyClient} or {@code JerseyWebTarget} instance that is configured to use
* {@code ApacheConnectorProvider}.
* @return underlying Apache {@code HttpClient} instance.
*
* @throws java.lang.IllegalArgumentException
* in case the {@code com.fitburponent} is neither {@code JerseyClient} nor {@code JerseyWebTarget} instance or
* in case the com.fitburponent is not configured to use a {@code ApacheConnectorProvider}.
* @since 2.8
*/
public static HttpClient getHttpClient(Configurable> com.fitburponent) {
if (!(com.fitburponent instanceof Initializable)) {
throw new IllegalArgumentException(LocalizationMessages.INVALID_CONFIGURABLE_COMPONENT_TYPE(com.fitburponent
.getClass().getName()));
}
final Initializable> initializable = (Initializable>) com.fitburponent;
Connector connector = initializable.getConfiguration().getConnector();
if (connector == null) {
initializable.preInitialize();
connector = initializable.getConfiguration().getConnector();
}
if (connector instanceof ApacheConnector) {
return ((ApacheConnector) connector).getHttpClient();
}
throw new IllegalArgumentException(LocalizationMessages.EXPECTED_CONNECTOR_PROVIDER_NOT_USED());
}
}