本文整理汇总了Java中com.ning.http.client.AsyncHandler类的典型用法代码示例。如果您正苦于以下问题:Java AsyncHandler类的具体用法?Java AsyncHandler怎么用?Java AsyncHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AsyncHandler类属于com.ning.http.client包,在下文中一共展示了AsyncHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
@Test
public void test() throws Exception {
AsyncHttpClient client = new AsyncHttpClient();
try {
Future<Response> f = client.preparePost(webServer.getCallHttpUrl()).addParameter("param1", "value1").execute();
Response response = f.get();
} finally {
client.close();
}
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
String destinationId = webServer.getHostAndPort();
String httpUrl = webServer.getCallHttpUrl();
verifier.verifyTrace(event("ASYNC_HTTP_CLIENT", AsyncHttpClient.class.getMethod("executeRequest", Request.class, AsyncHandler.class), null, null, destinationId,
annotation("http.url", httpUrl)));
verifier.verifyTraceCount(0);
}
示例2: onBodyPartReceived
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
public com.ning.http.client.AsyncHandler.STATE onBodyPartReceived(
HttpResponseBodyPart bodyPart) throws Exception {
String contType = testCase.getExpectedResContentType();
if(isMatchesContentType(MediaType.APPLICATION_JSON_TYPE, contType) || isMatchesContentType(MediaType.APPLICATION_XML_TYPE, contType)
|| isMatchesContentType(MediaType.TEXT_PLAIN_TYPE, contType) || isMatchesContentType(MediaType.TEXT_HTML_TYPE, contType)
|| isMatchesContentType(MediaType.TEXT_XML_TYPE, contType))
{
builder.accumulate(bodyPart);
}
else
{
bodyPart.writeTo(NullOutputStream.NULL_OUTPUT_STREAM);
}
testCaseReport.setResponseContentType(contType);
return STATE.CONTINUE;
}
示例3: onStatusReceived
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
public com.ning.http.client.AsyncHandler.STATE onStatusReceived(
HttpResponseStatus responseStatus) throws Exception {
int statusCode = responseStatus.getStatusCode();
testCaseReport.setResponseStatusCode(statusCode);
if (statusCode != testCase.getExpectedResCode()) {
testCaseReport.setError("Expected status code ["+testCase.getExpectedResCode()
+"] does not match actual status code ["+statusCode+"]");
testCaseReport.setStatus(TestStatus.Failed.status);
testCaseReport.setFailureReason(TestFailureReason.InvalidStatusCode.status);
if(testCase.isAbortOnInvalidStatusCode())
{
return STATE.ABORT;
}
}
builder.accumulate(responseStatus);
return STATE.CONTINUE;
}
示例4: onHeadersReceived
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
public com.ning.http.client.AsyncHandler.STATE onHeadersReceived(
HttpResponseHeaders headers) throws Exception {
if(testCase.getExpectedResContentType()!=null)
{
if(testCase.getExpectedResContentType()!=null && !testCase.getExpectedResContentType().trim().isEmpty()
&& headers.getHeaders().getFirstValue(HttpHeaders.CONTENT_TYPE)!=null
&& headers.getHeaders().getFirstValue(HttpHeaders.CONTENT_TYPE)
.indexOf(testCase.getExpectedResContentType())!=0)
{
testCaseReport.setError("Expected content type ["+testCase.getExpectedResContentType()
+"] does not match actual content type ["+headers.getHeaders().getFirstValue(HttpHeaders.CONTENT_TYPE)+"]");
testCaseReport.setStatus(TestStatus.Failed.status);
testCaseReport.setFailureReason(TestFailureReason.InvalidContentType.status);
if(testCase.isAbortOnInvalidContentType())
{
return STATE.ABORT;
}
}
}
else if(headers.getHeaders().getFirstValue(HttpHeaders.CONTENT_TYPE)!=null)
{
testCase.setExpectedResContentType(headers.getHeaders().getFirstValue(HttpHeaders.CONTENT_TYPE));
}
builder.accumulate(headers);
return STATE.CONTINUE;
}
示例5: testCreatesCorrectRequest
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void testCreatesCorrectRequest() throws Exception {
FluentStringsMap queryParams = new FluentStringsMap()
.add("test", "foo");
RequestParams params = RequestParams.get(endpoint, queryParams);
ArgumentCaptor<Request> captor = ArgumentCaptor.forClass(Request.class);
when(http.executeRequest(captor.capture(), any(AsyncHandler.class)))
.thenReturn(null);
client.getResponse(params, type);
Request request = captor.getValue();
assertEquals("GET", request.getMethod());
URL url = new URL(request.getUrl());
assertEquals("api.orgsync.com", url.getHost());
assertEquals("/api/v2" + endpoint, url.getPath());
Set<String> qParams = new HashSet<String>(Arrays.asList(url.getQuery()
.split("&")));
assertEquals(
new HashSet<String>(Arrays.asList("key=" + apiKey, "test=foo")),
qParams);
}
示例6: ParsecHttpRequestRetryCallable
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
/**
* Constructor.
*
* @param client client
* @param request request
* @param asyncHandler async handler
*/
public ParsecHttpRequestRetryCallable(
final AsyncHttpClient client, final ParsecAsyncHttpRequest request, final AsyncHandler<T> asyncHandler) {
this.client = client;
this.request = request;
this.asyncHandler = asyncHandler;
responses = new ArrayList<>();
}
示例7: criticalExecute
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
/**
* Critical execute a request (will not lookup in cache nor load into cache).
*
* @param request Request to critical execute
* @param asyncHandler Request async handler
* @param <T> Response type
* @return {@literal CompletableFuture<T>}
*/
public <T> CompletableFuture<T> criticalExecute(
final ParsecAsyncHttpRequest request,
AsyncHandler<T> asyncHandler
) {
ParsecAsyncHandlerWrapper<T> asyncHandlerWrapper =
new ParsecAsyncHandlerWrapper<>(asyncHandler, request.getNingRequest());
if (!request.getRetryStatusCodes().isEmpty() ||
!request.getRetryExceptions().isEmpty()) {
return new ParsecCompletableFuture<>(
executorService.submit(
new ParsecHttpRequestRetryCallable<>(
client,
request,
asyncHandlerWrapper
)
)
);
} else {
return new ParsecCompletableFuture<>(
client.executeRequest(
request.getNingRequest(),
asyncHandlerWrapper
)
);
}
}
示例8: AsyncHttpUrlConnection
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
public AsyncHttpUrlConnection(HttpURLConnection urlConnection, Request request, AsyncHandler<T> asyncHandler, ListenableFuture<T> future) {
this.urlConnection = urlConnection;
this.request = request;
this.asyncHandler = asyncHandler;
this.future = future;
this.request = request;
}
示例9: SimpleFuture
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
public SimpleFuture(AsyncHandler<V> asyncHandler, int responseTimeoutInMs, HttpURLConnection urlConnection) {
this.asyncHandler = asyncHandler;
this.responseTimeoutInMs = responseTimeoutInMs;
this.urlConnection = urlConnection;
writeHeaders = true;
writeBody = true;
}
示例10: transactionMarker
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
@Override
public void transactionMarker() throws Exception {
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger statusCode = new AtomicInteger();
asyncHttpClient.prepareGet("http://localhost:" + getPort() + "/hello3/")
.execute(new AsyncHandler<Response>() {
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart part) {
return null;
}
@Override
public Response onCompleted() throws Exception {
latch.countDown();
return null;
}
@Override
public STATE onHeadersReceived(HttpResponseHeaders headers) {
return null;
}
@Override
public STATE onStatusReceived(HttpResponseStatus status) {
statusCode.set(status.getStatusCode());
return null;
}
@Override
public void onThrowable(Throwable t) {}
});
latch.await();
asyncHttpClient.close();
if (statusCode.get() != 200) {
throw new IllegalStateException("Unexpected status code: " + statusCode);
}
}
示例11: testHttpConnectorPostsReportToTargetUrl
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
@Test
public void testHttpConnectorPostsReportToTargetUrl() throws IOException {
final Map<String, String> map = new HashMap<String, String>();
map.put(HttpConnector.PROPERTY_URL, "https://www.example.com");
unit.init(map);
unit.sendErrorReport(ConnectorOutputProviderBuilder.createConnectorOutputProvider(null, "{'abc':'def'}"));
verify(asyncHttpClient).preparePost("https://www.example.com");
verify(requestBuilder).setBody(eq("{'abc':'def'}"));
verify(requestBuilder).execute(Mockito.any(AsyncHandler.class));
}
示例12: testGetResponseFailsForHttpException
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test(expected = ApiClientException.class)
public void testGetResponseFailsForHttpException() throws Exception {
RequestParams params = RequestParams.get(endpoint);
when(http.executeRequest(any(Request.class), any(AsyncHandler.class)))
.thenThrow(new IOException("bang"));
client.getResponse(params, type);
}
示例13: testSucessfulRequest
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void testSucessfulRequest() throws Exception {
ListenableFuture<ApiResponse<String>> expected = mock(ListenableFuture.class);
RequestParams params = RequestParams.get(endpoint);
when(http.executeRequest(any(Request.class), any(AsyncHandler.class)))
.thenReturn(expected);
ListenableFuture<ApiResponse<Object>> result = client.getResponse(
params, type);
assertEquals(expected, result);
}
示例14: onBodyPartReceived
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
public AsyncHandler.STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
// body arrived, flush headers
if (!responseSet) {
response = responseBuilder.build();
responseSet = true;
headersArrived.countDown();
}
bodyPart.writeTo(output);
return AsyncHandler.STATE.CONTINUE;
}
示例15: execute
import com.ning.http.client.AsyncHandler; //导入依赖的package包/类
public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler) throws IOException {
return execute(request, handler, null);
}