com.fitbur.glassfish.jersey.client.ClientRuntime Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012-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.
*/
package com.fitbur.glassfish.com.fitbur.client;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedMap;
import com.fitbur.glassfish.com.fitbur.client.internal.LocalizationMessages;
import com.fitbur.glassfish.com.fitbur.client.spi.AsyncConnectorCallback;
import com.fitbur.glassfish.com.fitbur.client.spi.Connector;
import com.fitbur.glassfish.com.fitbur.internal.Version;
import com.fitbur.glassfish.com.fitbur.internal.inject.Injections;
import com.fitbur.glassfish.com.fitbur.internal.inject.Providers;
import com.fitbur.glassfish.com.fitbur.message.MessageBodyWorkers;
import com.fitbur.glassfish.com.fitbur.process.internal.ChainableStage;
import com.fitbur.glassfish.com.fitbur.process.internal.RequestScope;
import com.fitbur.glassfish.com.fitbur.process.internal.Stage;
import com.fitbur.glassfish.com.fitbur.process.internal.Stages;
import com.fitbur.glassfish.hk2.api.ServiceLocator;
import com.fitbur.repackaged.com.fitbur.google.com.fitburmon.util.concurrent.SettableFuture;
/**
* Client-side request processing runtime.
*
* @author Marek Potociar (marek.potociar at oracle.com.fitbur)
*/
class ClientRuntime implements JerseyClient.ShutdownHook {
private static final Logger LOG = Logger.getLogger(ClientRuntime.class.getName());
private final Stage requestProcessingRoot;
private final Stage responseProcessingRoot;
private final Connector connector;
private final ClientConfig config;
private final RequestScope requestScope;
private final ClientAsyncExecutorFactory asyncExecutorsFactory;
private final ServiceLocator locator;
private final Iterable lifecycleListeners;
/**
* Create new client request processing runtime.
*
* @param config client runtime configuration.
* @param connector client transport connector.
* @param locator HK2 service locator.
*/
public ClientRuntime(final ClientConfig config, final Connector connector, final ServiceLocator locator) {
final Stage.Builder requestingChainBuilder = Stages
.chain(locator.createAndInitialize(RequestProcessingInitializationStage.class));
final ChainableStage requestFilteringStage = ClientFilteringStages.createRequestFilteringStage(locator);
this.requestProcessingRoot = requestFilteringStage != null ?
requestingChainBuilder.build(requestFilteringStage) : requestingChainBuilder.build();
final ChainableStage responseFilteringStage = ClientFilteringStages.createResponseFilteringStage(locator);
this.responseProcessingRoot = responseFilteringStage != null ?
responseFilteringStage : Stages.identity();
this.config = config;
this.connector = connector;
this.requestScope = locator.getService(RequestScope.class);
int asyncThreadPoolSize = ClientProperties.getValue(config.getProperties(), ClientProperties.ASYNC_THREADPOOL_SIZE, 0);
asyncThreadPoolSize = (asyncThreadPoolSize < 0) ? 0 : asyncThreadPoolSize;
this.asyncExecutorsFactory = new ClientAsyncExecutorFactory(locator, asyncThreadPoolSize);
this.locator = locator;
this.lifecycleListeners = Providers.getAllProviders(locator, ClientLifecycleListener.class);
for (final ClientLifecycleListener listener : lifecycleListeners) {
try {
listener.onInit();
} catch (final Throwable t) {
LOG.log(Level.WARNING, LocalizationMessages.ERROR_LISTENER_INIT(listener.getClass().getName()), t);
}
}
}
/**
* Submit a {@link ClientRequest client request} for asynchronous processing.
*
* Both, the request processing as well as response callback invocation will be executed
* in a context of an active {@link RequestScope.Instance request scope instance}.
*
*
* @param request client request to be sent.
* @param callback asynchronous response callback.
*/
public void submit(final ClientRequest request, final ResponseCallback callback) {
submit(asyncExecutorsFactory.getExecutor(), new Runnable() {
@Override
public void run() {
ClientRequest processedRequest;
try {
processedRequest = Stages.process(request, requestProcessingRoot);
processedRequest = addUserAgent(processedRequest, connector.getName());
} catch (final AbortException aborted) {
processResponse(aborted.getAbortResponse(), callback);
return;
}
try {
final SettableFuture responseFuture = SettableFuture.create();
final AsyncConnectorCallback connectorCallback = new AsyncConnectorCallback() {
@Override
public void response(final ClientResponse response) {
responseFuture.set(response);
}
@Override
public void failure(final Throwable failure) {
responseFuture.setException(failure);
}
};
connector.apply(processedRequest, connectorCallback);
processResponse(responseFuture.get(), callback);
} catch (final ExecutionException e) {
processFailure(e.getCause(), callback);
} catch (final Throwable throwable) {
processFailure(throwable, callback);
}
}
});
}
private void processResponse(final ClientResponse response, final ResponseCallback callback) {
final ClientResponse processedResponse;
try {
processedResponse = Stages.process(response, responseProcessingRoot);
} catch (final Throwable throwable) {
processFailure(throwable, callback);
return;
}
callback.com.fitburpleted(processedResponse, requestScope);
}
private void processFailure(final Throwable failure, final ResponseCallback callback) {
callback.failed(failure instanceof ProcessingException ?
(ProcessingException) failure : new ProcessingException(failure));
}
private Future> submit(final ExecutorService executor, final Runnable task) {
return executor.submit(new Runnable() {
@Override
public void run() {
requestScope.runInScope(task);
}
});
}
private ClientRequest addUserAgent(final ClientRequest clientRequest, final String connectorName) {
final MultivaluedMap headers = clientRequest.getHeaders();
if (headers.containsKey(HttpHeaders.USER_AGENT)) {
// Check for explicitly set null value and if set, then remove the header - see JERSEY-2189
if (clientRequest.getHeaderString(HttpHeaders.USER_AGENT) == null) {
headers.remove(HttpHeaders.USER_AGENT);
}
} else if (!clientRequest.ignoreUserAgent()) {
if (connectorName != null && !connectorName.isEmpty()) {
headers.put(HttpHeaders.USER_AGENT,
Arrays.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy