Search in sources :

Example 1 with InitiatorBuilder

use of org.jocean.http.client.HttpClient.InitiatorBuilder in project jocean-http by isdom.

the class MessageUtil method interact.

public static Interact interact(final HttpClient client) {
    final InitiatorBuilder _initiatorBuilder = client.initiator();
    final AtomicBoolean _isSSLEnabled = new AtomicBoolean(false);
    final AtomicReference<Observable<Object>> _obsreqRef = new AtomicReference<>(fullRequestWithoutBody(HttpVersion.HTTP_1_1, HttpMethod.GET));
    final List<String> _nvs = new ArrayList<>();
    final AtomicReference<URI> _uriRef = new AtomicReference<>();
    return new Interact() {

        private void updateObsRequest(final Action1<Object> action) {
            _obsreqRef.set(_obsreqRef.get().doOnNext(action));
        }

        private void addQueryParams() {
            if (!_nvs.isEmpty()) {
                updateObsRequest(MessageUtil.addQueryParam(_nvs.toArray(new String[0])));
            }
        }

        private void extractUriWithHost(final Object... reqbeans) {
            if (null == _uriRef.get()) {
                for (Object bean : reqbeans) {
                    try {
                        final Path path = bean.getClass().getAnnotation(Path.class);
                        if (null != path) {
                            final URI uri = new URI(path.value());
                            if (null != uri.getHost()) {
                                uri(path.value());
                                return;
                            }
                        }
                    } catch (Exception e) {
                        LOG.warn("exception when extract uri from bean {}, detail: {}", bean, ExceptionUtils.exception2detail(e));
                    }
                }
            }
        }

        private void checkAddr() {
            if (null == _uriRef.get()) {
                throw new RuntimeException("remote address not set.");
            }
        }

        private InitiatorBuilder addSSLFeatureIfNeed(final InitiatorBuilder builder) {
            if (_isSSLEnabled.get()) {
                return builder;
            } else if ("https".equals(_uriRef.get().getScheme())) {
                return builder.feature(F_SSL);
            } else {
                return builder;
            }
        }

        @Override
        public Interact method(final HttpMethod method) {
            updateObsRequest(MessageUtil.setMethod(method));
            return this;
        }

        @Override
        public Interact uri(final String uriAsString) {
            try {
                final URI uri = new URI(uriAsString);
                _uriRef.set(uri);
                _initiatorBuilder.remoteAddress(uri2addr(uri));
                updateObsRequest(MessageUtil.setHost(uri));
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
            return this;
        }

        @Override
        public Interact path(final String path) {
            updateObsRequest(MessageUtil.setPath(path));
            return this;
        }

        @Override
        public Interact paramAsQuery(final String name, final String value) {
            _nvs.add(name);
            _nvs.add(value);
            return this;
        }

        @Override
        public Interact reqbean(final Object... reqbeans) {
            updateObsRequest(MessageUtil.toRequest(reqbeans));
            extractUriWithHost(reqbeans);
            return this;
        }

        @Override
        public Interact body(final Observable<? extends MessageBody> body) {
            _obsreqRef.set(_obsreqRef.get().compose(MessageUtil.addBody(body)));
            return this;
        }

        @Override
        public Interact body(final Object bean, final ContentEncoder contentEncoder) {
            return body(toBody(bean, contentEncoder.contentType(), contentEncoder.encoder()));
        }

        // @Override
        // public Interact disposeBodyOnTerminate(final boolean doDispose) {
        // _doDisposeBody.set(doDispose);
        // return this;
        // }
        @Override
        public Interact onrequest(final Action1<Object> action) {
            updateObsRequest(action);
            return this;
        }

        @Override
        public Interact feature(final Feature... features) {
            _initiatorBuilder.feature(features);
            if (isSSLEnabled(features)) {
                _isSSLEnabled.set(true);
            }
            return this;
        }

        private boolean isSSLEnabled(final Feature... features) {
            for (Feature f : features) {
                if (f instanceof Feature.ENABLE_SSL) {
                    return true;
                }
            }
            return false;
        }

        private Observable<? extends Object> hookDisposeBody(final Observable<Object> obsreq, final HttpInitiator initiator) {
            return obsreq.doOnNext(DisposableWrapperUtil.disposeOnForAny(initiator));
        }

        private Observable<? extends DisposableWrapper<HttpObject>> defineInteraction(final HttpInitiator initiator) {
            return initiator.defineInteraction(hookDisposeBody(_obsreqRef.get(), initiator));
        }

        @Override
        public Observable<? extends Interaction> execution() {
            checkAddr();
            addQueryParams();
            return addSSLFeatureIfNeed(_initiatorBuilder).build().map(new Func1<HttpInitiator, Interaction>() {

                @Override
                public Interaction call(final HttpInitiator initiator) {
                    final Observable<? extends DisposableWrapper<HttpObject>> interaction = defineInteraction(initiator);
                    return new Interaction() {

                        @Override
                        public HttpInitiator initiator() {
                            return initiator;
                        }

                        @Override
                        public Observable<? extends DisposableWrapper<HttpObject>> execute() {
                            return interaction;
                        }
                    };
                }
            });
        }
    };
}
Also used : DisposableWrapper(org.jocean.idiom.DisposableWrapper) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpObject(io.netty.handler.codec.http.HttpObject) Path(javax.ws.rs.Path) Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) Observable(rx.Observable) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpObject(io.netty.handler.codec.http.HttpObject) InitiatorBuilder(org.jocean.http.client.HttpClient.InitiatorBuilder) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpObject (io.netty.handler.codec.http.HttpObject)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Path (javax.ws.rs.Path)1 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)1 InitiatorBuilder (org.jocean.http.client.HttpClient.InitiatorBuilder)1 DisposableWrapper (org.jocean.idiom.DisposableWrapper)1 Observable (rx.Observable)1 Action1 (rx.functions.Action1)1