本文整理汇总了Java中com.ning.http.client.AsyncHttpClient.BoundRequestBuilder类的典型用法代码示例。如果您正苦于以下问题:Java BoundRequestBuilder类的具体用法?Java BoundRequestBuilder怎么用?Java BoundRequestBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BoundRequestBuilder类属于com.ning.http.client.AsyncHttpClient包,在下文中一共展示了BoundRequestBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: forwardEvent
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
public void forwardEvent(Event event, Position position) {
BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url);
requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8");
if (!header.equals("")) {
String[] headerLines = header.split("\\r?\\n");
for (String headerLine: headerLines) {
String[] splitedLine = headerLine.split(":", 2);
if (splitedLine.length == 2) {
requestBuilder.setHeader(splitedLine[0].trim(), splitedLine[1].trim());
}
}
}
requestBuilder.setBody(preparePayload(event, position));
requestBuilder.execute();
}
示例2: sendBaragonRequest
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
private SingularityLoadBalancerUpdate sendBaragonRequest(LoadBalancerRequestId loadBalancerRequestId, BaragonRequest loadBalancerRequest, LoadBalancerMethod method) {
try {
LOG.trace("Preparing to send request {}", loadBalancerRequest);
final BoundRequestBuilder requestBuilder = httpClient.preparePost(loadBalancerUri)
.addHeader(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON)
.setBody(objectMapper.writeValueAsBytes(loadBalancerRequest));
if (loadBalancerQueryParams.isPresent()) {
addAllQueryParams(requestBuilder, loadBalancerQueryParams.get());
}
return sendRequestWrapper(loadBalancerRequestId, method, requestBuilder.build(), BaragonRequestState.FAILED);
} catch (IOException e) {
return new SingularityLoadBalancerUpdate(BaragonRequestState.UNKNOWN, loadBalancerRequestId, Optional.of(e.getMessage()), System.currentTimeMillis(), method, Optional.of(loadBalancerUri));
}
}
示例3: makeAccessTokenRequest
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
private BoundRequestBuilder makeAccessTokenRequest() {
final BoundRequestBuilder requestBuilder = HttpRequestTask.ASYNC_HTTP_CLIENT.preparePost(
HttpRequestTask.createBaseURL(ServerProtocol.AUTH_AUTHORITY, ServerProtocol.ACCESS_TOKEN_PATH));
final Entry<String,String> entry = HttpRequestTask.KA_HEADER.entrySet().iterator().next();
requestBuilder.addHeader(entry.getKey(), entry.getValue());
if (accessTokenRequest.isAccessTokenRequestWithAuthCode()) {
requestBuilder.addQueryParameter(ServerProtocol.GRANT_TYPE_KEY, ServerProtocol.GRANT_TYPE_AUTHORIZATION_CODE);
requestBuilder.addQueryParameter(ServerProtocol.CODE_KEY, accessTokenRequest.getAuthorizationCode());
requestBuilder.addQueryParameter(ServerProtocol.REDIRECT_URI_KEY, accessTokenRequest.getRedirectURI());
} else { //if(request.isAccessTokenRequestWithRefreshToken()) {
requestBuilder.addQueryParameter(ServerProtocol.GRANT_TYPE_KEY, ServerProtocol.REFRESH_TOKEN_KEY);
requestBuilder.addQueryParameter(ServerProtocol.REFRESH_TOKEN_KEY, accessTokenRequest.getRefreshToken());
}
requestBuilder.addQueryParameter(ServerProtocol.CLIENT_ID_KEY, accessTokenRequest.getAppKey());
requestBuilder.addQueryParameter(ServerProtocol.ANDROID_KEY_HASH, accessTokenRequest.getKeyHash());
return requestBuilder;
}
示例4: addQueryParams
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
/**
* String/Boolean/Number만 지원.
*/
public static void addQueryParams(BoundRequestBuilder requestBuilder, Bundle parameters) {
if(parameters == null)
return;
Set<String> keys = parameters.keySet();
for (String key : keys) {
Object value = parameters.get(key);
String valueStr = null;
if (value == null) {
value = "";
}
if (isSupportedParameterType(value)) {
valueStr = parameterToString(value);
} else {
throw new IllegalArgumentException(String.format("Unsupported parameter type for GET request: %s",
value.getClass().getSimpleName()));
}
requestBuilder.addQueryParameter(key, valueStr);
}
}
示例5: replay
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
public ListenableFuture<Response> replay(final LogLineData logLineData) throws IOException {
BoundRequestBuilder req = asyncHttpClient.prepareGet(host + logLineData.getRequest());
req.setFollowRedirects(followRedirects);
String usedHostHeader = null;
if (logLineData.getHost() == null) {
if (hostHeader != null) {
usedHostHeader = hostHeader;
}
} else {
usedHostHeader = logLineData.getHost();
}
req = req.setVirtualHost(usedHostHeader);
for (final Header header : headers) {
req = req.setHeader(header.getName(), header.getValue());
}
if (logLineData.getUserAgent() != null) {
req.setHeader("user-agent", logLineData.getUserAgent());
}
LOG.info("Executing request {}: {} with host headers {}", req, host + logLineData.getRequest(), usedHostHeader);
return req.execute(new LoggingAsyncCompletionHandler(logLineData, resultDataLogger));
}
示例6: execute
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
private Promise<HttpResponse> execute(BoundRequestBuilder builder) {
try {
final Promise<HttpResponse> smartFuture = new Promise<HttpResponse>();
prepare(builder).execute(new AsyncCompletionHandler<HttpResponse>() {
@Override
public HttpResponse onCompleted(Response response) throws Exception {
HttpResponse httpResponse = new HttpAsyncResponse(response);
smartFuture.invoke(httpResponse);
return httpResponse;
}
@Override
public void onThrowable(Throwable t) {
// An error happened - must "forward" the exception to the one waiting for the result
smartFuture.invokeWithException(t);
}
});
return smartFuture;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例7: addAllHeadersFromHeaderMetadataMap
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
/**
* 20131215
* Called in httpWorker
* @param builder
* @param protocol
*/
public static void addAllHeadersFromHeaderMetadataMap(BoundRequestBuilder builder,
String httpHeaderType) {
if (builder != null && httpHeaderType != null) {
AgentDataProvider adp = AgentDataProvider.getInstance();
HttpHeaderMetadata httpHeaderMetadata = adp.headerMetadataMap.get(httpHeaderType);
addHeaders(builder, httpHeaderMetadata.getHeaderMap());
// add dynamic headers
addDynamicHeaders(builder, httpHeaderType);
} else {
models.utils.LogUtils.printLogError
("Failed to build request; builder is null, unknown method in createRequest()");
}
}
示例8: addDynamicHeaders
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
public static void addDynamicHeaders(BoundRequestBuilder builder,
String httpHeaderType) {
if (builder != null && httpHeaderType != null) {
/**
* TODO: for further extension
*/
// if (httpHeaderType.equalsIgnoreCase(MyHttpUtils.TBC)
// ){
// builder.addHeader("DynamicHeaderKey", HeaderUtils.addDynamicHeader(arg));
// }
} else {
models.utils.LogUtils.printLogError
("Failed to build request; builder is null, unknown method in createRequest()");
}
}
示例9: getContents
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
public Optional<String> getContents(String user, String repo, String path, Optional<String> ref) {
try {
final BoundRequestBuilder builder = asyncHttpClient.prepareGet(String.format("%s/repos/%s/%s/contents/%s", baseUrl, user, repo, path))
.addHeader("Accept", "application/vnd.github.v3.raw");
if (ref.isPresent()) {
builder.addQueryParameter("ref", ref.get());
}
if (accessToken.isPresent()) {
builder.addQueryParameter("access_token", accessToken.get());
}
final Response response = builder.execute().get();
if (response.getStatusCode() == 200) {
return Optional.of(response.getResponseBody());
} else if (response.getStatusCode() == 404) {
return Optional.absent();
} else {
throw new RuntimeException(response.getResponseBody()); // TODO: better exception
}
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
示例10: postRequest
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
public Promise<WSResponse> postRequest(URL url, String contentType, byte[] body,
RequestPreparer postRequestPreparer) throws IOException {
final scala.concurrent.Promise<WSResponse> scalaPromise =
scala.concurrent.Promise$.MODULE$.apply();
BoundRequestBuilder builder = asyncHttpClient.preparePost(url.toString())
.setHeader("Content-Type", contentType)
.setContentLength(body.length)
.setBody(body);
if (postRequestPreparer != null) {
postRequestPreparer.prepare(builder, contentType, url);
}
builder.execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) {
scalaPromise.success(new NingWSResponse(response));
return response;
}
@Override
public void onThrowable(Throwable t) {
scalaPromise.failure(t);
}
});
return Promise.wrap(scalaPromise.future());
}
示例11: expectZergCall
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private ListenableFuture<Response> expectZergCall() throws Exception {
BoundRequestBuilder requestBuilder = createMock(BoundRequestBuilder.class);
final com.ning.http.client.ListenableFuture<Response> future = createMock(com.ning.http.client.ListenableFuture.class);
expect(client.prepareGet("/path")).andReturn(requestBuilder);
expect(requestBuilder.execute()).andReturn(future);
final Capture<Runnable> chainingListenableFutureCapture = new Capture<Runnable>();
expect(future.addListener(capture(chainingListenableFutureCapture), isA(Executor.class))).andStubAnswer(new IAnswer<ListenableFuture<Response>>() {
@Override
public ListenableFuture<Response> answer() throws Throwable {
// Invoking this ChainingListenableFuture is necessary or the tests will hang.
chainingListenableFutureCapture.getValue().run();
return future;
}
});
return future;
}
示例12: copyHeadersAndParams
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
private void copyHeadersAndParams(BoundRequestBuilder requestBuilder, HttpServletRequest request) {
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
requestBuilder.addHeader(headerName, request.getHeader(headerName));
LOG.trace("Copied header {}:{}", headerName, request.getHeader(headerName));
}
}
Enumeration<String> parameterNames = request.getParameterNames();
if (parameterNames != null) {
while (parameterNames.hasMoreElements()) {
String parameterName = parameterNames.nextElement();
requestBuilder.addQueryParameter(parameterName, request.getParameter(parameterName));
LOG.trace("Copied query param {}={}", parameterName, request.getParameter(parameterName));
}
}
}
示例13: internalSendCommand
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
private void internalSendCommand(String machineIdentifier, String url) throws IOException {
logger.debug("Calling url {}", url);
BoundRequestBuilder builder = client.prepareGet(url).setHeaders(getDefaultHeaders())
.setHeader("X-Plex-Target-Client-Identifier", machineIdentifier);
builder.execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) throws Exception {
if (response.getStatusCode() != 200) {
logger.error("Error while sending command to Plex: {}\r\n{}", response.getStatusText(),
response.getResponseBody());
}
return response;
}
@Override
public void onThrowable(Throwable t) {
logger.error("Error sending command to Plex", t);
}
});
}
示例14: buildAgentRequest
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
private AsyncHttpClient.BoundRequestBuilder buildAgentRequest(String url, AgentRequestType requestType) {
final BoundRequestBuilder builder;
switch (requestType) {
case APPLY:
builder = asyncHttpClient.preparePost(url);
break;
case REVERT:
case CANCEL:
builder = asyncHttpClient.prepareDelete(url);
break;
default:
throw new RuntimeException("Don't know how to send requests for " + requestType);
}
if (baragonAuthKey.isPresent()) {
builder.addQueryParameter("authkey", baragonAuthKey.get());
}
return builder;
}
示例15: getState
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; //导入依赖的package包/类
@Override
public SingularityLoadBalancerUpdate getState(LoadBalancerRequestId loadBalancerRequestId) {
final String uri = getLoadBalancerUri(loadBalancerRequestId);
final BoundRequestBuilder requestBuilder = httpClient.prepareGet(uri);
if (loadBalancerQueryParams.isPresent()) {
addAllQueryParams(requestBuilder, loadBalancerQueryParams.get());
}
return sendRequestWrapper(loadBalancerRequestId, LoadBalancerMethod.CHECK_STATE, requestBuilder.build(), BaragonRequestState.UNKNOWN);
}