use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project tomee by apache.
the class HTTPConduit method prepare.
/**
* Prepare to send an outbound HTTP message over this http conduit to a
* particular endpoint.
* <P>
* If the Message.PATH_INFO property is set it gets appended
* to the Conduit's endpoint URL. If the Message.QUERY_STRING
* property is set, it gets appended to the resultant URL following
* a "?".
* <P>
* If the Message.HTTP_REQUEST_METHOD property is NOT set, the
* Http request method defaults to "POST".
* <P>
* If the Message.PROTOCOL_HEADERS is not set on the message, it is
* initialized to an empty map.
* <P>
* This call creates the OutputStream for the content of the message.
* It also assigns the created Http(s)URLConnection to the Message
* Map.
*
* @param message The message to be sent.
*/
public void prepare(Message message) throws IOException {
// This call can possibly change the conduit endpoint address and
// protocol from the default set in EndpointInfo that is associated
// with the Conduit.
Address currentAddress;
try {
currentAddress = setupAddress(message);
} catch (URISyntaxException e) {
throw new IOException(e);
}
// The need to cache the request is off by default
boolean needToCacheRequest = false;
HTTPClientPolicy csPolicy = getClient(message);
setupConnection(message, currentAddress, csPolicy);
// If the HTTP_REQUEST_METHOD is not set, the default is "POST".
String httpRequestMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
if (httpRequestMethod == null) {
httpRequestMethod = "POST";
message.put(Message.HTTP_REQUEST_METHOD, "POST");
}
boolean isChunking = false;
int chunkThreshold = 0;
final AuthorizationPolicy effectiveAuthPolicy = getEffectiveAuthPolicy(message);
if (this.authSupplier == null) {
this.authSupplier = createAuthSupplier(effectiveAuthPolicy);
}
if (this.proxyAuthSupplier == null) {
this.proxyAuthSupplier = createAuthSupplier(proxyAuthorizationPolicy);
}
if (this.authSupplier.requiresRequestCaching()) {
needToCacheRequest = true;
isChunking = false;
LOG.log(Level.FINE, "Auth Supplier, but no Preemptive User Pass or Digest auth (nonce may be stale)" + " We must cache request.");
}
if (csPolicy.isAutoRedirect()) {
needToCacheRequest = true;
LOG.log(Level.FINE, "AutoRedirect is turned on.");
}
if (csPolicy.getMaxRetransmits() > 0) {
needToCacheRequest = true;
LOG.log(Level.FINE, "MaxRetransmits is set > 0.");
}
// TODO : ensure chunking can be enabled for non-empty PUTs - if requested
if (csPolicy.isAllowChunking() && isChunkingSupported(message, httpRequestMethod)) {
// TODO: The chunking mode be configured or at least some
// documented client constant.
// use -1 and allow the URL connection to pick a default value
isChunking = true;
chunkThreshold = csPolicy.getChunkingThreshold();
}
cookies.writeToMessageHeaders(message);
if (certConstraints != null) {
message.put(CertConstraints.class.getName(), certConstraints);
message.getInterceptorChain().add(CertConstraintsInterceptor.INSTANCE);
}
setHeadersByAuthorizationPolicy(message, currentAddress.getURI());
new Headers(message).setFromClientPolicy(getClient(message));
// set the OutputStream on the ProxyOutputStream
ProxyOutputStream pos = message.getContent(ProxyOutputStream.class);
if (pos != null && message.getContent(OutputStream.class) != null) {
pos.setWrappedOutputStream(createOutputStream(message, needToCacheRequest, isChunking, chunkThreshold));
} else {
message.setContent(OutputStream.class, createOutputStream(message, needToCacheRequest, isChunking, chunkThreshold));
}
// We are now "ready" to "send" the message.
}
use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project dubbo by alibaba.
the class WebServiceProtocol method doRefer.
@Override
@SuppressWarnings("unchecked")
protected <T> T doRefer(final Class<T> serviceType, URL url) throws RpcException {
ClientProxyFactoryBean proxyFactoryBean = new ClientProxyFactoryBean();
String servicePathPrefix = url.getParameter(SERVICE_PATH_PREFIX);
if (!StringUtils.isEmpty(servicePathPrefix) && PROTOCOL_SERVER_SERVLET.equals(url.getParameter(PROTOCOL_SERVER))) {
url = url.setPath(servicePathPrefix + "/" + url.getPath());
}
proxyFactoryBean.setAddress(url.setProtocol("http").toIdentityString());
proxyFactoryBean.setServiceClass(serviceType);
proxyFactoryBean.setBus(bus);
T ref = (T) proxyFactoryBean.create();
Client proxy = ClientProxy.getClient(ref);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(url.getParameter(Constants.CONNECT_TIMEOUT_KEY, Constants.DEFAULT_CONNECT_TIMEOUT));
policy.setReceiveTimeout(url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT));
conduit.setClient(policy);
return ref;
}
use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project cxf by apache.
the class AsyncHTTPConduitTest method testCall.
@Test
public void testCall() throws Exception {
updateAddressPort(g, PORT);
assertEquals("Hello " + request, g.greetMe(request));
HTTPConduit c = (HTTPConduit) ClientProxy.getClient(g).getConduit();
HTTPClientPolicy cp = new HTTPClientPolicy();
cp.setAllowChunking(false);
c.setClient(cp);
assertEquals("Hello " + request, g.greetMe(request));
}
use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project cxf by apache.
the class AsyncHTTPConduitTest method testCallAsyncWithFullWorkQueue.
@Test
public void testCallAsyncWithFullWorkQueue() throws Exception {
Bus bus = BusFactory.getThreadDefaultBus();
WorkQueueManager workQueueManager = bus.getExtension(WorkQueueManager.class);
AutomaticWorkQueueImpl automaticWorkQueue1 = (AutomaticWorkQueueImpl) workQueueManager.getAutomaticWorkQueue();
updateAddressPort(g, PORT);
Client client = ClientProxy.getClient(g);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
int asyncExecuteTimeout = 500;
httpClientPolicy.setAsyncExecuteTimeout(asyncExecuteTimeout);
http.setClient(httpClientPolicy);
long repeat = automaticWorkQueue1.getHighWaterMark() + automaticWorkQueue1.getMaxSize() + 1;
CountDownLatch initialThreadsLatch = new CountDownLatch(automaticWorkQueue1.getHighWaterMark());
CountDownLatch doneLatch = new CountDownLatch((int) repeat);
AtomicInteger threadCount = new AtomicInteger();
for (long i = 0; i < repeat; i++) {
g.greetMeLaterAsync(-50, res -> {
try {
int myCount = threadCount.getAndIncrement();
if (myCount < automaticWorkQueue1.getHighWaterMark()) {
// Sleep long enough so that the workqueue will fill up and then
// handleResponseOnWorkqueue will fail for the calls from both
// responseReceived and consumeContent
Thread.sleep(3L * asyncExecuteTimeout);
initialThreadsLatch.countDown();
} else {
Thread.sleep(50);
}
if (!initialThreadsLatch.await(30, TimeUnit.SECONDS)) {
throw new TimeoutException("The initial threads latch timeout exceeded," + " exception in JaxwsClientCallback?");
}
doneLatch.countDown();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
doneLatch.await(30, TimeUnit.SECONDS);
assertEquals("All responses should be handled eventually", 0, doneLatch.getCount());
}
use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project cxf by apache.
the class AsyncHTTPConduitTest method testCallAsyncWithFullWorkQueue.
@Test
public void testCallAsyncWithFullWorkQueue() throws Exception {
Bus bus = BusFactory.getThreadDefaultBus();
WorkQueueManager workQueueManager = bus.getExtension(WorkQueueManager.class);
AutomaticWorkQueueImpl automaticWorkQueue1 = (AutomaticWorkQueueImpl) workQueueManager.getAutomaticWorkQueue();
updateAddressPort(g, PORT);
Client client = ClientProxy.getClient(g);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
int asyncExecuteTimeout = 500;
httpClientPolicy.setAsyncExecuteTimeout(asyncExecuteTimeout);
http.setClient(httpClientPolicy);
long repeat = automaticWorkQueue1.getHighWaterMark() + automaticWorkQueue1.getMaxSize() + 1;
CountDownLatch initialThreadsLatch = new CountDownLatch(automaticWorkQueue1.getHighWaterMark());
CountDownLatch doneLatch = new CountDownLatch((int) repeat);
AtomicInteger threadCount = new AtomicInteger();
for (long i = 0; i < repeat; i++) {
g.greetMeLaterAsync(-50, res -> {
try {
int myCount = threadCount.getAndIncrement();
if (myCount < automaticWorkQueue1.getHighWaterMark()) {
// Sleep long enough so that the workqueue will fill up and then
// handleResponseOnWorkqueue will fail for the calls from both
// responseReceived and consumeContent
Thread.sleep(3L * asyncExecuteTimeout);
initialThreadsLatch.countDown();
} else {
Thread.sleep(50);
}
initialThreadsLatch.await();
doneLatch.countDown();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
doneLatch.await(30, TimeUnit.SECONDS);
assertEquals("All responses should be handled eventually", 0, doneLatch.getCount());
}
Aggregations