use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestMIMEChainingMultipleSources method generateServerAResponseCallback.
private Callback<StreamResponse> generateServerAResponseCallback(final StreamRequest incomingRequest, final Callback<StreamResponse> incomingRequestCallback) {
return new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(StreamResponse result) {
final MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(result);
_serverAMultiPartCallback = new ServerAMultiPartCallback(incomingRequest, incomingRequestCallback);
reader.registerReaderCallback(_serverAMultiPartCallback);
}
};
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class ServerQueryTunnelFilter method onStreamRequest.
@Override
public void onStreamRequest(final StreamRequest req, final RequestContext requestContext, final Map<String, String> wireAttrs, final NextFilter<StreamRequest, StreamResponse> nextFilter) {
Callback<StreamRequest> callback = new Callback<StreamRequest>() {
@Override
public void onError(Throwable e) {
if (e instanceof MessagingException || e instanceof URISyntaxException) {
RestResponse errorResponse = RestStatus.responseForStatus(RestStatus.BAD_REQUEST, e.toString());
nextFilter.onResponse(Messages.toStreamResponse(errorResponse), requestContext, wireAttrs);
} else {
nextFilter.onError(e, requestContext, wireAttrs);
}
}
@Override
public void onSuccess(StreamRequest newReq) {
nextFilter.onRequest(newReq, requestContext, wireAttrs);
}
};
// the entire request would be buffered in memory if decoding is needed
// this usually is not a problem as request with extremely query parameters is usually get request with no body
QueryTunnelUtil.decode(req, requestContext, callback);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class SimpleLoggingFilter method trace.
private void trace(final String method, final StreamResponse response, final Map<String, String> wireAttrs, final RequestContext requestContext) {
final URI requestUri = (URI) requestContext.getLocalAttr(REQUEST_URI);
final String requestMethod = (String) requestContext.getLocalAttr(REQUEST_METHOD);
Callback<Integer> callback = new Callback<Integer>() {
@Override
public void onError(Throwable e) {
_log.warn("Cannot get the length of the response", e);
}
@Override
public void onSuccess(Integer result) {
_log.debug(buildLogMessage(method, "response", formatResponse(response, result, requestUri, requestMethod), wireAttrs, requestContext));
}
};
response.getEntityStream().addObserver(new LengthObserver(callback));
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testRestRequests.
@Test(dataProvider = "requestResponseParameters", expectedExceptions = UnsupportedOperationException.class)
public void testRestRequests(AbstractNettyStreamClient client, String method, int requestSize, int responseSize, boolean isFullRequest) throws Exception {
Server server = new HttpServerBuilder().responseSize(responseSize).build();
try {
server.start();
for (int i = 0; i < REQUEST_COUNT; i++) {
RestRequest request = new RestRequestBuilder(new URI(URL)).setMethod(method).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).setEntity(ByteString.copy(new byte[requestSize])).build();
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.IS_FULL_REQUEST, isFullRequest);
client.restRequest(request, context, new HashMap<>(), new TransportCallbackAdapter<>(new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse response) {
}
@Override
public void onError(Throwable e) {
}
}));
}
} finally {
server.stop();
}
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testCancelStreamRequests.
@Test(dataProvider = "requestResponseParameters", groups = TestGroupNames.TESTNG_GROUP_KNOWN_ISSUE)
public void testCancelStreamRequests(AbstractNettyStreamClient client, String method, int requestSize, int responseSize, boolean isFullRequest) throws Exception {
AtomicInteger succeeded = new AtomicInteger(0);
AtomicInteger failed = new AtomicInteger(0);
Server server = new HttpServerBuilder().responseSize(responseSize).build();
try {
server.start();
CountDownLatch latch = new CountDownLatch(REQUEST_COUNT);
for (int i = 0; i < REQUEST_COUNT; i++) {
StreamRequest request = new StreamRequestBuilder(new URI(URL)).setMethod(method).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[requestSize]))));
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.IS_FULL_REQUEST, isFullRequest);
client.streamRequest(request, context, new HashMap<>(), new TransportCallbackAdapter<>(new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse response) {
response.getEntityStream().setReader(new Reader() {
@Override
public void onDataAvailable(ByteString data) {
}
@Override
public void onDone() {
failed.incrementAndGet();
latch.countDown();
}
@Override
public void onError(Throwable e) {
failed.incrementAndGet();
latch.countDown();
}
@Override
public void onInit(ReadHandle rh) {
rh.cancel();
succeeded.incrementAndGet();
latch.countDown();
}
});
}
@Override
public void onError(Throwable e) {
failed.incrementAndGet();
latch.countDown();
}
}));
}
if (!latch.await(30, TimeUnit.SECONDS)) {
Assert.fail("Timeout waiting for responses. " + succeeded + " requests succeeded and " + failed + " requests failed out of total " + REQUEST_COUNT + " requests");
}
Assert.assertEquals(latch.getCount(), 0);
Assert.assertEquals(failed.get(), 0);
Assert.assertEquals(succeeded.get(), REQUEST_COUNT);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
} finally {
server.stop();
}
}
Aggregations