Search in sources :

Example 56 with HTTPClientPolicy

use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project cxf by apache.

the class HttpConduitConfigApplier method applyClientPolicies.

private void applyClientPolicies(Dictionary<String, String> d, HTTPConduit c) {
    Enumeration<String> keys = d.keys();
    HTTPClientPolicy p = c.getClient();
    while (keys.hasMoreElements()) {
        String k = keys.nextElement();
        if (k.startsWith("client.")) {
            if (p == null) {
                p = new HTTPClientPolicy();
                c.setClient(p);
            }
            String v = d.get(k);
            k = k.substring("client.".length());
            if ("ConnectionTimeout".equals(k)) {
                p.setConnectionTimeout(Long.parseLong(v.trim()));
            } else if ("ReceiveTimeout".equals(k)) {
                p.setReceiveTimeout(Long.parseLong(v.trim()));
            } else if ("AsyncExecuteTimeout".equals(k)) {
                p.setAsyncExecuteTimeout(Long.parseLong(v.trim()));
            } else if ("AsyncExecuteTimeoutRejection".equals(k)) {
                p.setAsyncExecuteTimeoutRejection(Boolean.parseBoolean(v.trim()));
            } else if ("AutoRedirect".equals(k)) {
                p.setAutoRedirect(Boolean.parseBoolean(v.trim()));
            } else if ("MaxRetransmits".equals(k)) {
                p.setMaxRetransmits(Integer.parseInt(v.trim()));
            } else if ("AllowChunking".equals(k)) {
                p.setAllowChunking(Boolean.parseBoolean(v.trim()));
            } else if ("ChunkingThreshold".equals(k)) {
                p.setChunkingThreshold(Integer.parseInt(v.trim()));
            } else if ("ChunkLength".equals(k)) {
                p.setChunkLength(Integer.parseInt(v.trim()));
            } else if ("Connection".equals(k)) {
                p.setConnection(ConnectionType.valueOf(v));
            } else if ("DecoupledEndpoint".equals(k)) {
                p.setDecoupledEndpoint(v);
            } else if ("ProxyServer".equals(k)) {
                p.setProxyServer(v);
            } else if ("ProxyServerPort".equals(k)) {
                p.setProxyServerPort(Integer.parseInt(v.trim()));
            } else if ("ProxyServerType".equals(k)) {
                p.setProxyServerType(ProxyServerType.fromValue(v));
            } else if ("NonProxyHosts".equals(k)) {
                p.setNonProxyHosts(v);
            }
        }
    }
}
Also used : HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy)

Example 57 with HTTPClientPolicy

use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project cxf by apache.

the class ClientPolicyCalculator method intersect.

/**
 * Returns a new HTTPClientPolicy that is compatible with the two specified
 * policies or null if no compatible policy can be determined.
 *
 * @param p1 one policy
 * @param p2 another policy
 * @return the compatible policy
 */
public HTTPClientPolicy intersect(HTTPClientPolicy p1, HTTPClientPolicy p2) {
    if (!compatible(p1, p2)) {
        return null;
    }
    // ok - compute compatible policy
    HTTPClientPolicy p = new HTTPClientPolicy();
    p.setAccept(StringUtils.combine(p1.getAccept(), p2.getAccept()));
    p.setAcceptEncoding(StringUtils.combine(p1.getAcceptEncoding(), p2.getAcceptEncoding()));
    p.setAcceptLanguage(StringUtils.combine(p1.getAcceptLanguage(), p2.getAcceptLanguage()));
    if (p1.isSetAllowChunking()) {
        p.setAllowChunking(p1.isAllowChunking());
    } else if (p2.isSetAllowChunking()) {
        p.setAllowChunking(p2.isAllowChunking());
    }
    if (p1.isSetAutoRedirect()) {
        p.setAutoRedirect(p1.isAutoRedirect());
    } else if (p2.isSetAutoRedirect()) {
        p.setAutoRedirect(p2.isAutoRedirect());
    }
    p.setBrowserType(StringUtils.combine(p1.getBrowserType(), p2.getBrowserType()));
    if (p1.isSetCacheControl()) {
        p.setCacheControl(p1.getCacheControl());
    } else if (p2.isSetCacheControl()) {
        p.setCacheControl(p2.getCacheControl());
    }
    if (p1.isSetConnection()) {
        p.setConnection(p1.getConnection());
    } else if (p2.isSetConnection()) {
        p.setConnection(p2.getConnection());
    }
    if (p1.isSetContentType()) {
        p.setContentType(p1.getContentType());
    } else if (p2.isSetContentType()) {
        p.setContentType(p2.getContentType());
    }
    p.setCookie(StringUtils.combine(p1.getCookie(), p2.getCookie()));
    p.setDecoupledEndpoint(StringUtils.combine(p1.getDecoupledEndpoint(), p2.getDecoupledEndpoint()));
    p.setHost(StringUtils.combine(p1.getHost(), p2.getHost()));
    p.setProxyServer(StringUtils.combine(p1.getProxyServer(), p2.getProxyServer()));
    if (p1.isSetProxyServerPort()) {
        p.setProxyServerPort(p1.getProxyServerPort());
    } else if (p2.isSetProxyServerPort()) {
        p.setProxyServerPort(p2.getProxyServerPort());
    }
    if (p1.isSetProxyServerType()) {
        p.setProxyServerType(p1.getProxyServerType());
    } else if (p2.isSetProxyServerType()) {
        p.setProxyServerType(p2.getProxyServerType());
    }
    p.setReferer(StringUtils.combine(p1.getReferer(), p2.getReferer()));
    if (p1.isSetConnectionTimeout()) {
        p.setConnectionTimeout(p1.getConnectionTimeout());
    } else if (p2.isSetConnectionTimeout()) {
        p.setConnectionTimeout(p2.getConnectionTimeout());
    }
    if (p1.isSetConnectionRequestTimeout()) {
        p.setConnectionRequestTimeout(p1.getConnectionRequestTimeout());
    } else if (p2.isSetConnectionRequestTimeout()) {
        p.setConnectionRequestTimeout(p2.getConnectionRequestTimeout());
    }
    if (p1.isSetReceiveTimeout()) {
        p.setReceiveTimeout(p1.getReceiveTimeout());
    } else if (p2.isSetReceiveTimeout()) {
        p.setReceiveTimeout(p2.getReceiveTimeout());
    }
    return p;
}
Also used : HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy)

Example 58 with HTTPClientPolicy

use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project cxf 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 59 with HTTPClientPolicy

use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project cxf by apache.

the class ProxyFactory method createSystemProxyConfiguration.

private static HTTPClientPolicy createSystemProxyConfiguration() {
    // Retrieve system properties (if any)
    HTTPClientPolicy systemProxyConfiguration = null;
    String proxyHost = SystemPropertyAction.getPropertyOrNull(HTTP_PROXY_HOST);
    if (StringUtils.isEmpty(proxyHost)) {
        proxyHost = null;
    }
    if (proxyHost != null) {
        // System is configured with a proxy, use it
        systemProxyConfiguration = new HTTPClientPolicy();
        systemProxyConfiguration.setProxyServer(proxyHost);
        systemProxyConfiguration.setProxyServerType(ProxyServerType.HTTP);
        // 8080 is the default proxy port value as per some documentation
        String proxyPort = SystemPropertyAction.getProperty(HTTP_PROXY_PORT, "8080");
        if (StringUtils.isEmpty(proxyPort)) {
            proxyPort = "8080";
        }
        systemProxyConfiguration.setProxyServerPort(Integer.parseInt(proxyPort));
        // Load non proxy hosts
        String nonProxyHosts = SystemPropertyAction.getPropertyOrNull(HTTP_NON_PROXY_HOSTS);
        if (!StringUtils.isEmpty(nonProxyHosts)) {
            systemProxyConfiguration.setNonProxyHosts(nonProxyHosts);
        }
    }
    return systemProxyConfiguration;
}
Also used : HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy)

Example 60 with HTTPClientPolicy

use of org.apache.cxf.transports.http.configuration.HTTPClientPolicy in project cxf by apache.

the class JAXRSSoapBookTest method doTestHelloSoapCustomDataBinding.

private void doTestHelloSoapCustomDataBinding(String address) throws Exception {
    final QName serviceName = new QName("http://hello.com", "HelloWorld");
    final QName portName = new QName("http://hello.com", "HelloWorldPort");
    Service service = Service.create(serviceName);
    service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, address);
    HelloWorld hw = service.getPort(HelloWorld.class);
    Client cl = ClientProxy.getClient(hw);
    HTTPConduit http = (HTTPConduit) cl.getConduit();
    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
    httpClientPolicy.setConnectionTimeout(0);
    httpClientPolicy.setReceiveTimeout(0);
    http.setClient(httpClientPolicy);
    User user = new UserImpl("Barry");
    User user2 = hw.echoUser(user);
    assertNotSame(user, user2);
    assertEquals("Barry", user2.getName());
}
Also used : HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) User(org.apache.cxf.systest.jaxrs.jaxws.User) QName(javax.xml.namespace.QName) HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy) UserImpl(org.apache.cxf.systest.jaxrs.jaxws.UserImpl) Service(javax.xml.ws.Service) BookSoapService(org.apache.cxf.systest.jaxrs.jaxws.BookSoapService) Client(org.apache.cxf.endpoint.Client) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) WebClient(org.apache.cxf.jaxrs.client.WebClient) HelloWorld(org.apache.cxf.systest.jaxrs.jaxws.HelloWorld)

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