Search in sources :

Example 46 with HTTPClientPolicy

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.
}
Also used : ProxyAuthorizationPolicy(org.apache.cxf.configuration.security.ProxyAuthorizationPolicy) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy) CertConstraints(org.apache.cxf.transport.https.CertConstraints) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 47 with HTTPClientPolicy

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;
}
Also used : HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) DEFAULT_TIMEOUT(org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT) PROTOCOL_SERVER_SERVLET(org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_SERVER_SERVLET) ClientProxyFactoryBean(org.apache.cxf.frontend.ClientProxyFactoryBean) HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy) Client(org.apache.cxf.endpoint.Client)

Example 48 with HTTPClientPolicy

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));
}
Also used : HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy) Test(org.junit.Test)

Example 49 with HTTPClientPolicy

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());
}
Also used : Bus(org.apache.cxf.Bus) AutomaticWorkQueueImpl(org.apache.cxf.workqueue.AutomaticWorkQueueImpl) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(javax.xml.ws.Endpoint) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy) Client(org.apache.cxf.endpoint.Client) WorkQueueManager(org.apache.cxf.workqueue.WorkQueueManager) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 50 with HTTPClientPolicy

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());
}
Also used : HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) Bus(org.apache.cxf.Bus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy) AutomaticWorkQueueImpl(org.apache.cxf.workqueue.AutomaticWorkQueueImpl) Client(org.apache.cxf.endpoint.Client) CountDownLatch(java.util.concurrent.CountDownLatch) WorkQueueManager(org.apache.cxf.workqueue.WorkQueueManager) Endpoint(javax.xml.ws.Endpoint) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

HTTPClientPolicy (org.apache.cxf.transports.http.configuration.HTTPClientPolicy)78 HTTPConduit (org.apache.cxf.transport.http.HTTPConduit)53 Client (org.apache.cxf.endpoint.Client)31 Test (org.junit.Test)27 URL (java.net.URL)12 Bus (org.apache.cxf.Bus)10 IOException (java.io.IOException)8 AuthorizationPolicy (org.apache.cxf.configuration.security.AuthorizationPolicy)8 WebClient (org.apache.cxf.jaxrs.client.WebClient)7 ClientPolicyCalculator (org.apache.cxf.transport.http.policy.impl.ClientPolicyCalculator)7 QName (javax.xml.namespace.QName)6 ProxyAuthorizationPolicy (org.apache.cxf.configuration.security.ProxyAuthorizationPolicy)6 ClientConfiguration (org.apache.cxf.jaxrs.client.ClientConfiguration)6 TLSClientParameters (org.apache.cxf.configuration.jsse.TLSClientParameters)5 Greeter (org.apache.hello_world.Greeter)5 SOAPService (org.apache.hello_world.services.SOAPService)5 Map (java.util.Map)4 BindingProvider (javax.xml.ws.BindingProvider)4 Endpoint (org.apache.cxf.endpoint.Endpoint)4 HashMap (java.util.HashMap)3