Java Code Examples for org.asynchttpclient.BoundRequestBuilder#addHeader()

The following examples show how to use org.asynchttpclient.BoundRequestBuilder#addHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CarreraAsyncRequest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private void addPropertiesToHeader(final BoundRequestBuilder builder, final HttpParam httpParam) {
    if (builder == null || httpParam == null || httpParam.key == null) {
        return;
    }

    if (httpParam.key.equals(CARRERA_PROPERTIES)) {
        Map<String, String> properties = JsonUtils.fromJsonString(httpParam.value, Map.class);
        if (properties != null && properties.size() > 0) {
            if (properties.containsKey(CARRERA_HEADERS)) {
                Map<String, String> headers = JsonUtils.fromJsonString(properties.get(CARRERA_HEADERS), Map.class);
                if (headers != null && headers.size() > 0) {
                    for (Map.Entry<String, String> entry : headers.entrySet()) {
                        builder.addHeader(entry.getKey(), entry.getValue());
                    }
                }
            }
        }
    }
}
 
Example 2
Source File: CarreraAsyncRequest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private void addPropertiesToHeader(final BoundRequestBuilder builder, final HttpParam httpParam) {
    if (builder == null || httpParam == null || httpParam.key == null) {
        return;
    }

    if (httpParam.key.equals(CARRERA_PROPERTIES)) {
        Map<String, String> properties = JsonUtils.fromJsonString(httpParam.value, Map.class);
        if (properties != null && properties.size() > 0) {
            if (properties.containsKey(CARRERA_HEADERS)) {
                Map<String, String> headers = JsonUtils.fromJsonString(properties.get(CARRERA_HEADERS), Map.class);
                if (headers != null && headers.size() > 0) {
                    for (Map.Entry<String, String> entry : headers.entrySet()) {
                        builder.addHeader(entry.getKey(), entry.getValue());
                    }
                }
            }
        }
    }
}
 
Example 3
Source File: RecurlyClient.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
BoundRequestBuilder setupCommonSettings(BoundRequestBuilder builder, @Nullable String apiVersion) {
    builder.addHeader(HttpHeaders.AUTHORIZATION, APIKey)
            .addHeader(HttpHeaders.ACCEPT, APPLICATION_XML_NO_CHARSET)
            .addHeader(HttpHeaders.USER_AGENT, USER_AGENT);

    // Must include the "X-Api-Version" header for certain APIs (e.g. "Lookup Account Balance") that were added in a
    // later release, or else Recurly will return a 400 error.  For now, we include the header lazily, only for those
    // APIs that require it, until we have enough QA time in a future release to test a universal migration to the
    // latest Recurly version for all our calls.
    if (!isEmpty(apiVersion)) {
        builder.addHeader(HEADER_API_VERSION, apiVersion);
    }

    return builder;
}
 
Example 4
Source File: AsyncHttpCaller.java    From pampas with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Response> asyncCall(AsyncHttpRequest req, ServerInstance serverInstance) {
    final FullHttpRequest httpRequest = req.getFullHttpRequest();
    try {
        final AsyncHttpClient httpClient = this.client;
        BoundRequestBuilder requestBuilder = new BoundRequestBuilder(httpClient,
                httpRequest.method().name(), true);

        requestBuilder.setUri(Uri.create(serverInstance.toUri() + req.getRequestPath()));
        for (Map.Entry<String, String> headerEntry : httpRequest.headers()) {
            if (StringUtils.isNotEmpty(headerEntry.getKey())
                    && !"Host".equals(headerEntry.getKey())) {
                requestBuilder.addHeader(headerEntry.getKey(), headerEntry.getValue());
            }

        }
        requestBuilder.addHeader(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

        if (httpRequest.content() != null && httpRequest.content().isReadable()) {
            //请求body转换为ByteBuffer,并且设置为只读,ByteBuf复用 堆内存中的数据 zero copy
            ByteBuffer readOnlyBuffer = httpRequest.content().nioBuffer().asReadOnlyBuffer();
            requestBuilder.setBody(readOnlyBuffer);
        }
        ListenableFuture<Response> listenableFuture = requestBuilder.execute();
        return listenableFuture.toCompletableFuture();

    } catch (Exception ex) {
        log.warn("执行调用报错:{}", ex);
        CompletableFuture<Response> exceptionFuture = CompletableFuture.completedFuture(null);
        exceptionFuture.completeExceptionally(ex);
        return exceptionFuture;
    }
}
 
Example 5
Source File: CarreraAsyncRequest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private void doRequest() {
    if (job.isTerminated()) {
        LOGGER.info("job is terminated! request:{}", this);
        job.terminate();
        return;
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("doRequest...request:{},httpForm={}", this, job.getHttpParams());
    }
    MetricUtils.maxRetryCount(job.getGroupId(), job.getTopic(), job.getQid(), requestCnt);

    BoundRequestBuilder builder;
    if (StringUtils.equalsIgnoreCase(job.getUpstreamTopic().getHttpMethod(), UpstreamTopic.HTTP_POST)) {
        builder = PushService.getInstance().getClient()
                .preparePost(getUrl())
                .setRequestTimeout(getTimeout());
    } else { //GET
        builder = PushService.getInstance().getClient()
                .prepareGet(getUrl())
                .setRequestTimeout(getTimeout());
    }

    if (job.getHttpParams() != null) {
        for (HttpParam httpParam : job.getHttpParams()) {
            addPropertiesToHeader(builder, httpParam);

            switch (httpParam.type) {
                case FORM:
                    builder.addFormParam(httpParam.key, httpParam.value);
                    break;
                case QUERY:
                    builder.addQueryParam(httpParam.key, httpParam.value);
                    break;
                case HEADER:
                    builder.addHeader(httpParam.key, httpParam.value);
                    break;
                default:
                    LogUtils.logErrorInfo("CarreraAsyncRequest_error", "unknown httpType, httpParam:{},job:{}", httpParam, job);
            }
        }
    }

    // http请求次数
    String carreraReqCnt = String.valueOf(requestCnt);
    if (job.isFromChronos()) {
        InternalKey internalKey = new InternalKey(job.getCommonMessage().getKey());
        if (internalKey != null) {
            if (internalKey.getType() == MsgTypes.LOOP_DELAY.getValue()
                    || internalKey.getType() == MsgTypes.LOOP_EXPONENT_DELAY.getValue()) {
                carreraReqCnt = String.valueOf(internalKey.getTimed());
            }
        }
    }
    builder.addFormParam(FormParamsExtractAction.CARRERA_REQ_CNT, carreraReqCnt);

    startTime = TimeUtils.getCurTime();
    job.setState(requestCnt == 0 ? "HTTP.RequestOnFly" : "HTTP.RetryOnFly#" + requestCnt);
    inflightRequests.add(this);
    builder.execute(this);
    requestCnt++;
}
 
Example 6
Source File: CarreraAsyncRequest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private void doRequest() {
    if (job.isTerminated()) {
        LOGGER.info("job is terminated! request:{}", this);
        job.terminate();
        return;
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("doRequest...request:{},httpForm={}", this, job.getHttpParams());
    }
    MetricUtils.maxRetryCount(job.getGroupId(), job.getTopic(), job.getQid(), requestCnt);

    BoundRequestBuilder builder;
    if (StringUtils.equalsIgnoreCase(job.getUpstreamTopic().getHttpMethod(), UpstreamTopic.HTTP_POST)) {
        builder = PushService.getInstance().getClient()
                .preparePost(getUrl())
                .setRequestTimeout(getTimeout());
    } else { //GET
        builder = PushService.getInstance().getClient()
                .prepareGet(getUrl())
                .setRequestTimeout(getTimeout());
    }

    if (job.getHttpParams() != null) {
        for (HttpParam httpParam : job.getHttpParams()) {
            addPropertiesToHeader(builder, httpParam);

            switch (httpParam.type) {
                case FORM:
                    builder.addFormParam(httpParam.key, httpParam.value);
                    break;
                case QUERY:
                    builder.addQueryParam(httpParam.key, httpParam.value);
                    break;
                case HEADER:
                    builder.addHeader(httpParam.key, httpParam.value);
                    break;
                default:
                    LogUtils.logErrorInfo("CarreraAsyncRequest_error", "unknown httpType, httpParam:{},job:{}", httpParam, job);
            }
        }
    }

    // http请求次数
    String carreraReqCnt = String.valueOf(requestCnt);
    if (job.isFromChronos()) {
        InternalKey internalKey = new InternalKey(job.getCommonMessage().getKey());
        if (internalKey != null) {
            if (internalKey.getType() == MsgTypes.LOOP_DELAY.getValue()
                    || internalKey.getType() == MsgTypes.LOOP_EXPONENT_DELAY.getValue()) {
                carreraReqCnt = String.valueOf(internalKey.getTimed());
            }
        }
    }
    builder.addFormParam(FormParamsExtractAction.CARRERA_REQ_CNT, carreraReqCnt);

    startTime = TimeUtils.getCurTime();
    job.setState(requestCnt == 0 ? "HTTP.RequestOnFly" : "HTTP.RetryOnFly#" + requestCnt);
    inflightRequests.add(this);
    builder.execute(this);
    requestCnt++;
}
 
Example 7
Source File: HttpClientWrapper.java    From flummi with Apache License 2.0 4 votes vote down vote up
private BoundRequestBuilder withBasicAuth(final BoundRequestBuilder boundRequestBuilder) {
    if (nonNull(username) && nonNull(password)) {
        boundRequestBuilder.addHeader(HttpHeaderNames.AUTHORIZATION, getAuthorizationHeaderValue(username, password));
    }
    return boundRequestBuilder;
}