use of io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class YarMessageHandlerWarpperTest method testAbnormal.
@Test
public void testAbnormal() throws Exception {
final String errmsg = "rpc process error";
YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {
@Override
public Object handle(Channel channel, Object message) {
throw new RuntimeException(errmsg);
}
});
// yar协议无法解析
FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(null, uri));
assertNotNull(httpResponse);
assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
YarResponse retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertNotNull(retYarResponse.getError());
// yar协议可以正常解析,但后续处理异常
YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
assertNotNull(httpResponse);
assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertEquals(errmsg, retYarResponse.getError());
}
use of io.netty.handler.codec.http.FullHttpResponse in project flink by apache.
the class StaticFileServerHandler method sendError.
// ------------------------------------------------------------------------
// Utilities to encode headers and responses
// ------------------------------------------------------------------------
/**
* Writes a simple error response message.
*
* @param ctx The channel context to write the response to.
* @param status The response status.
*/
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
// close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
use of io.netty.handler.codec.http.FullHttpResponse in project flink by apache.
the class JobCancellationWithSavepointHandlersTest method testTriggerNewRequest.
/**
* Tests triggering a new request and monitoring it.
*/
@Test
public void testTriggerNewRequest() throws Exception {
JobID jobId = new JobID();
ExecutionGraphHolder holder = mock(ExecutionGraphHolder.class);
ExecutionGraph graph = mock(ExecutionGraph.class);
CheckpointCoordinator coord = mock(CheckpointCoordinator.class);
when(holder.getExecutionGraph(eq(jobId), any(ActorGateway.class))).thenReturn(graph);
when(graph.getCheckpointCoordinator()).thenReturn(coord);
JobCancellationWithSavepointHandlers handlers = new JobCancellationWithSavepointHandlers(holder, EC);
JobCancellationWithSavepointHandlers.TriggerHandler trigger = handlers.getTriggerHandler();
JobCancellationWithSavepointHandlers.InProgressHandler progress = handlers.getInProgressHandler();
Map<String, String> params = new HashMap<>();
params.put("jobid", jobId.toString());
params.put("targetDirectory", "custom-directory");
ActorGateway jobManager = mock(ActorGateway.class);
// Successful
Promise<Object> promise = new Promise.DefaultPromise<>();
when(jobManager.ask(any(Object.class), any(FiniteDuration.class))).thenReturn(promise);
// Trigger
FullHttpResponse response = trigger.handleRequest(params, Collections.<String, String>emptyMap(), jobManager);
verify(jobManager).ask(eq(new CancelJobWithSavepoint(jobId, "custom-directory")), any(FiniteDuration.class));
String location = String.format("/jobs/%s/cancel-with-savepoint/in-progress/1", jobId);
assertEquals(HttpResponseStatus.ACCEPTED, response.getStatus());
assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
assertEquals(location, response.headers().get(HttpHeaders.Names.LOCATION));
String json = response.content().toString(Charset.forName("UTF-8"));
JsonNode root = new ObjectMapper().readTree(json);
assertEquals("accepted", root.get("status").getValueAsText());
assertEquals("1", root.get("request-id").getValueAsText());
assertEquals(location, root.get("location").getValueAsText());
// Trigger again
response = trigger.handleRequest(params, Collections.<String, String>emptyMap(), jobManager);
assertEquals(HttpResponseStatus.ACCEPTED, response.getStatus());
assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
assertEquals(location, response.headers().get(HttpHeaders.Names.LOCATION));
json = response.content().toString(Charset.forName("UTF-8"));
root = new ObjectMapper().readTree(json);
assertEquals("accepted", root.get("status").getValueAsText());
assertEquals("1", root.get("request-id").getValueAsText());
assertEquals(location, root.get("location").getValueAsText());
// Only single actual request
verify(jobManager).ask(eq(new CancelJobWithSavepoint(jobId, "custom-directory")), any(FiniteDuration.class));
// Query progress
params.put("requestId", "1");
response = progress.handleRequest(params, Collections.<String, String>emptyMap(), jobManager);
assertEquals(HttpResponseStatus.ACCEPTED, response.getStatus());
assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
json = response.content().toString(Charset.forName("UTF-8"));
root = new ObjectMapper().readTree(json);
assertEquals("in-progress", root.get("status").getValueAsText());
assertEquals("1", root.get("request-id").getValueAsText());
// Complete
promise.success(new CancellationSuccess(jobId, "_path-savepoint_"));
response = progress.handleRequest(params, Collections.<String, String>emptyMap(), jobManager);
assertEquals(HttpResponseStatus.CREATED, response.getStatus());
assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
json = response.content().toString(Charset.forName("UTF-8"));
root = new ObjectMapper().readTree(json);
assertEquals("success", root.get("status").getValueAsText());
assertEquals("1", root.get("request-id").getValueAsText());
assertEquals("_path-savepoint_", root.get("savepoint-path").getValueAsText());
// Query again, keep recent history
response = progress.handleRequest(params, Collections.<String, String>emptyMap(), jobManager);
assertEquals(HttpResponseStatus.CREATED, response.getStatus());
assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
json = response.content().toString(Charset.forName("UTF-8"));
root = new ObjectMapper().readTree(json);
assertEquals("success", root.get("status").getValueAsText());
assertEquals("1", root.get("request-id").getValueAsText());
assertEquals("_path-savepoint_", root.get("savepoint-path").getValueAsText());
// Query for unknown request
params.put("requestId", "9929");
response = progress.handleRequest(params, Collections.<String, String>emptyMap(), jobManager);
assertEquals(HttpResponseStatus.BAD_REQUEST, response.getStatus());
assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
json = response.content().toString(Charset.forName("UTF-8"));
root = new ObjectMapper().readTree(json);
assertEquals("failed", root.get("status").getValueAsText());
assertEquals("9929", root.get("request-id").getValueAsText());
assertEquals("Unknown job/request ID", root.get("cause").getValueAsText());
}
use of io.netty.handler.codec.http.FullHttpResponse in project moco by dreamhead.
the class MocoHandler method handleRequest.
private FullHttpResponse handleRequest(final FullHttpRequest message) {
HttpRequest request = DefaultHttpRequest.newRequest(message);
DefaultMutableHttpResponse httpResponse = getHttpResponse(request);
FullHttpResponse response = httpResponse.toFullResponse();
prepareForKeepAlive(message, response);
monitor.onMessageLeave(httpResponse);
return response;
}
use of io.netty.handler.codec.http.FullHttpResponse in project moco by dreamhead.
the class AbstractProxyResponseHandler method setupNormalResponse.
private HttpResponse setupNormalResponse(final org.apache.http.HttpResponse remoteResponse) throws IOException {
HttpVersion httpVersion = HttpVersion.valueOf(remoteResponse.getProtocolVersion().toString());
HttpResponseStatus status = HttpResponseStatus.valueOf(remoteResponse.getStatusLine().getStatusCode());
FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, status);
response.setStatus(status);
Header[] allHeaders = remoteResponse.getAllHeaders();
for (Header header : allHeaders) {
if (isResponseHeader(header)) {
response.headers().set(header.getName(), header.getValue());
}
}
HttpEntity entity = remoteResponse.getEntity();
if (entity != null) {
byte[] content = toByteArray(entity);
if (content.length > 0) {
ByteBuf buffer = Unpooled.copiedBuffer(content);
response.content().writeBytes(buffer);
}
}
return newResponse(response);
}
Aggregations