Search in sources :

Example 1 with Address

use of org.apache.cxf.transport.http.Address in project cxf by apache.

the class AsyncHTTPConduit method setupConnection.

@Override
protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException {
    if (factory.isShutdown()) {
        message.put(USE_ASYNC, Boolean.FALSE);
        super.setupConnection(message, address, csPolicy);
        return;
    }
    propagateJaxwsSpecTimeoutSettings(message, csPolicy);
    boolean addressChanged = false;
    // need to do some clean up work on the URI address
    URI uri = address.getURI();
    String uriString = uri.toString();
    if (uriString.startsWith("hc://")) {
        uriString = uriString.substring(5);
        addressChanged = true;
    } else if (uriString.startsWith("hc5://")) {
        uriString = uriString.substring(6);
        addressChanged = true;
    }
    if (addressChanged) {
        try {
            uri = new URI(uriString);
        } catch (URISyntaxException ex) {
            throw new MalformedURLException("unsupport uri: " + uriString);
        }
    }
    String s = uri.getScheme();
    if (!"http".equals(s) && !"https".equals(s)) {
        throw new MalformedURLException("unknown protocol: " + s);
    }
    Object o = message.getContextualProperty(USE_ASYNC);
    if (o == null) {
        o = factory.getUseAsyncPolicy();
    }
    switch(UseAsyncPolicy.getPolicy(o)) {
        case ALWAYS:
            o = true;
            break;
        case NEVER:
            o = false;
            break;
        case ASYNC_ONLY:
        default:
            o = !message.getExchange().isSynchronous();
            break;
    }
    // check tlsClientParameters from message header
    TLSClientParameters clientParameters = message.get(TLSClientParameters.class);
    if (clientParameters == null) {
        clientParameters = tlsClientParameters;
    }
    if ("https".equals(uri.getScheme()) && clientParameters != null && clientParameters.getSSLSocketFactory() != null) {
        // if they configured in an SSLSocketFactory, we cannot do anything
        // with it as the NIO based transport cannot use socket created from
        // the SSLSocketFactory.
        o = false;
    }
    if (!PropertyUtils.isTrue(o)) {
        message.put(USE_ASYNC, Boolean.FALSE);
        super.setupConnection(message, addressChanged ? new Address(uriString, uri) : address, csPolicy);
        return;
    }
    if (StringUtils.isEmpty(uri.getPath())) {
        // hc needs to have the path be "/"
        uri = uri.resolve("/");
    }
    message.put(USE_ASYNC, Boolean.TRUE);
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Asynchronous connection to " + uri.toString() + " has been set up");
    }
    message.put("http.scheme", uri.getScheme());
    String httpRequestMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
    if (httpRequestMethod == null) {
        httpRequestMethod = "POST";
        message.put(Message.HTTP_REQUEST_METHOD, httpRequestMethod);
    }
    final CXFHttpRequest e = new CXFHttpRequest(httpRequestMethod, uri);
    final String contentType = (String) message.get(Message.CONTENT_TYPE);
    final MutableHttpEntity entity = new MutableHttpEntity(contentType, null, true) {

        public boolean isRepeatable() {
            return e.getOutputStream().retransmitable();
        }
    };
    e.setEntity(entity);
    final RequestConfig.Builder b = RequestConfig.custom().setConnectTimeout(Timeout.ofMilliseconds(csPolicy.getConnectionTimeout())).setResponseTimeout(Timeout.ofMilliseconds(csPolicy.getReceiveTimeout())).setConnectionRequestTimeout(Timeout.ofMilliseconds(csPolicy.getConnectionRequestTimeout()));
    final Proxy p = proxyFactory.createProxy(csPolicy, uri);
    if (p != null && p.type() != Proxy.Type.DIRECT) {
        InetSocketAddress isa = (InetSocketAddress) p.address();
        HttpHost proxy = new HttpHost(isa.getHostString(), isa.getPort());
        b.setProxy(proxy);
    }
    e.setConfig(b.build());
    message.put(CXFHttpRequest.class, e);
}
Also used : TLSClientParameters(org.apache.cxf.configuration.jsse.TLSClientParameters) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) MalformedURLException(java.net.MalformedURLException) InetSocketAddress(java.net.InetSocketAddress) Address(org.apache.cxf.transport.http.Address) InetSocketAddress(java.net.InetSocketAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Proxy(java.net.Proxy) HttpHost(org.apache.hc.core5.http.HttpHost)

Example 2 with Address

use of org.apache.cxf.transport.http.Address in project cxf by apache.

the class NettyHttpConduit method setupConnection.

// Using Netty API directly
protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException {
    URI uri = address.getURI();
    boolean addressChanged = false;
    // need to do some clean up work on the URI address
    String uriString = uri.toString();
    if (uriString.startsWith("netty://")) {
        try {
            uriString = uriString.substring(8);
            uri = new URI(uriString);
            addressChanged = true;
        } catch (URISyntaxException ex) {
            throw new MalformedURLException("unsupport uri: " + uriString);
        }
    }
    String s = uri.getScheme();
    if (!"http".equals(s) && !"https".equals(s)) {
        throw new MalformedURLException("unknown protocol: " + s);
    }
    Object o = message.getContextualProperty(USE_ASYNC);
    if (o == null) {
        o = factory.getUseAsyncPolicy();
    }
    switch(NettyHttpConduitFactory.UseAsyncPolicy.getPolicy(o)) {
        case ALWAYS:
            o = true;
            break;
        case NEVER:
            o = false;
            break;
        case ASYNC_ONLY:
        default:
            o = !message.getExchange().isSynchronous();
            break;
    }
    // check tlsClientParameters from message header
    TLSClientParameters clientParameters = message.get(TLSClientParameters.class);
    if (clientParameters == null) {
        clientParameters = tlsClientParameters;
    }
    if ("https".equals(uri.getScheme()) && clientParameters != null && clientParameters.getSSLSocketFactory() != null) {
        // if they configured in an SSLSocketFactory, we cannot do anything
        // with it as the NIO based transport cannot use socket created from
        // the SSLSocketFactory.
        o = false;
    }
    if (!PropertyUtils.isTrue(o)) {
        message.put(USE_ASYNC, Boolean.FALSE);
        super.setupConnection(message, addressChanged ? new Address(uriString, uri) : address, csPolicy);
        return;
    }
    message.put(USE_ASYNC, Boolean.TRUE);
    if (StringUtils.isEmpty(uri.getPath())) {
        // hc needs to have the path be "/"
        uri = uri.resolve("/");
    }
    message.put("http.scheme", uri.getScheme());
    String httpRequestMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
    if (httpRequestMethod == null) {
        httpRequestMethod = "POST";
        message.put(Message.HTTP_REQUEST_METHOD, httpRequestMethod);
    }
    // setup a new NettyHttpClientRequest
    final NettyHttpClientRequest request = new NettyHttpClientRequest(uri, httpRequestMethod);
    final int ctimeout = determineConnectionTimeout(message, csPolicy);
    final int rtimeout = determineReceiveTimeout(message, csPolicy);
    final int maxResponseContentLength = determineMaxResponseContentLength(message);
    request.setConnectionTimeout(ctimeout);
    request.setReceiveTimeout(rtimeout);
    request.setMaxResponseContentLength(maxResponseContentLength);
    message.put(NettyHttpClientRequest.class, request);
}
Also used : TLSClientParameters(org.apache.cxf.configuration.jsse.TLSClientParameters) MalformedURLException(java.net.MalformedURLException) InetSocketAddress(java.net.InetSocketAddress) Address(org.apache.cxf.transport.http.Address) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 3 with Address

use of org.apache.cxf.transport.http.Address in project cxf by apache.

the class AsyncHTTPConduit method setupConnection.

@Override
protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException {
    if (factory.isShutdown()) {
        message.put(USE_ASYNC, Boolean.FALSE);
        super.setupConnection(message, address, csPolicy);
        return;
    }
    propagateJaxwsSpecTimeoutSettings(message, csPolicy);
    boolean addressChanged = false;
    // need to do some clean up work on the URI address
    URI uri = address.getURI();
    String uriString = uri.toString();
    if (uriString.startsWith("hc://")) {
        try {
            uriString = uriString.substring(5);
            uri = new URI(uriString);
            addressChanged = true;
        } catch (URISyntaxException ex) {
            throw new MalformedURLException("unsupport uri: " + uriString);
        }
    }
    String s = uri.getScheme();
    if (!"http".equals(s) && !"https".equals(s)) {
        throw new MalformedURLException("unknown protocol: " + s);
    }
    Object o = message.getContextualProperty(USE_ASYNC);
    if (o == null) {
        o = factory.getUseAsyncPolicy();
    }
    switch(UseAsyncPolicy.getPolicy(o)) {
        case ALWAYS:
            o = true;
            break;
        case NEVER:
            o = false;
            break;
        case ASYNC_ONLY:
        default:
            o = !message.getExchange().isSynchronous();
            break;
    }
    // check tlsClientParameters from message header
    TLSClientParameters clientParameters = message.get(TLSClientParameters.class);
    if (clientParameters == null) {
        clientParameters = tlsClientParameters;
    }
    if ("https".equals(uri.getScheme()) && clientParameters != null && clientParameters.getSSLSocketFactory() != null) {
        // if they configured in an SSLSocketFactory, we cannot do anything
        // with it as the NIO based transport cannot use socket created from
        // the SSLSocketFactory.
        o = false;
    }
    if (!PropertyUtils.isTrue(o)) {
        message.put(USE_ASYNC, Boolean.FALSE);
        super.setupConnection(message, addressChanged ? new Address(uriString, uri) : address, csPolicy);
        return;
    }
    if (StringUtils.isEmpty(uri.getPath())) {
        // hc needs to have the path be "/"
        uri = uri.resolve("/");
    }
    message.put(USE_ASYNC, Boolean.TRUE);
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Asynchronous connection to " + uri.toString() + " has been set up");
    }
    message.put("http.scheme", uri.getScheme());
    String httpRequestMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
    if (httpRequestMethod == null) {
        httpRequestMethod = "POST";
        message.put(Message.HTTP_REQUEST_METHOD, httpRequestMethod);
    }
    final CXFHttpRequest e = new CXFHttpRequest(httpRequestMethod);
    BasicHttpEntity entity = new BasicHttpEntity() {

        public boolean isRepeatable() {
            return e.getOutputStream().retransmitable();
        }
    };
    entity.setChunked(true);
    entity.setContentType((String) message.get(Message.CONTENT_TYPE));
    e.setURI(uri);
    e.setEntity(entity);
    RequestConfig.Builder b = RequestConfig.custom().setConnectTimeout((int) csPolicy.getConnectionTimeout()).setSocketTimeout((int) csPolicy.getReceiveTimeout()).setConnectionRequestTimeout((int) csPolicy.getConnectionRequestTimeout());
    Proxy p = proxyFactory.createProxy(csPolicy, uri);
    if (p != null && p.type() != Proxy.Type.DIRECT) {
        InetSocketAddress isa = (InetSocketAddress) p.address();
        HttpHost proxy = new HttpHost(isa.getHostString(), isa.getPort());
        b.setProxy(proxy);
    }
    e.setConfig(b.build());
    message.put(CXFHttpRequest.class, e);
}
Also used : TLSClientParameters(org.apache.cxf.configuration.jsse.TLSClientParameters) RequestConfig(org.apache.http.client.config.RequestConfig) MalformedURLException(java.net.MalformedURLException) InetSocketAddress(java.net.InetSocketAddress) Address(org.apache.cxf.transport.http.Address) InetSocketAddress(java.net.InetSocketAddress) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Proxy(java.net.Proxy) HttpHost(org.apache.http.HttpHost)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)3 MalformedURLException (java.net.MalformedURLException)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 TLSClientParameters (org.apache.cxf.configuration.jsse.TLSClientParameters)3 Address (org.apache.cxf.transport.http.Address)3 Proxy (java.net.Proxy)2 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 HttpHost (org.apache.http.HttpHost)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)1