use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class TestRelayCommandsLocal method testNoDataStreamCommand.
@Test
public void testNoDataStreamCommand() throws Exception {
LOG.debug("\n\nstarting testNoDataStreamCommand()\n");
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/stream?sources=100&size=1000&output=json&checkPoint={\"windowScn\":-1,\"windowOffset\":-1}");
SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
assertEquals("expected to get empty response", 0, respHandler.getReceivedBytes().length);
HttpResponse respObj = respHandler.getResponse();
assertNull("/stream returned unexpected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
assertNull("/stream returned unexpected error with cause", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CAUSE_CLASS_HEADER));
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project bagheera by mozilla-metrics.
the class RootResponse method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Object msg = e.getMessage();
if (msg instanceof HttpMessage) {
HttpRequest request = (HttpRequest) msg;
if (ROOT_PATH.equals(request.getUri()) || request.getUri().isEmpty()) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
} else {
Channels.fireMessageReceived(ctx, request, e.getRemoteAddress());
}
} else {
ctx.sendUpstream(e);
}
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project bagheera by mozilla-metrics.
the class SubmissionHandler method handleOptions.
private void handleOptions(MessageEvent e, BagheeraHttpRequest request) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "POST,PUT,DELETE");
response.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length");
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project NabAlive by jcheype.
the class TtsController method init.
@PostConstruct
void init() {
restHandler.get(new Route("/tts/:apikey/:voice") {
@Override
public void handle(Request request, final Response response, Map<String, String> map) throws Exception {
String text = StringEscapeUtils.escapeXml(checkNotNull(request.getParam("text")));
String voice = checkNotNull(map.get("voice"));
if (!text.startsWith("<s>")) {
text = "<s>" + text + "</s>";
}
final String key = text + "|" + voice;
byte[] bytes = ttsCache.asMap().get(key);
if (bytes != null) {
response.write(ChannelBuffers.copiedBuffer(bytes));
return;
}
asyncHttpClient.preparePost(frenchTtsUrl).setBody(text).execute(new AsyncCompletionHandler<com.ning.http.client.Response>() {
@Override
public com.ning.http.client.Response onCompleted(com.ning.http.client.Response asyncResponse) throws Exception {
InputStream inputStream = asyncResponse.getResponseBodyAsStream();
byte[] bytes = ByteStreams.toByteArray(inputStream);
ttsCache.asMap().put(key, bytes);
response.write(bytes);
return asyncResponse;
}
@Override
public void onThrowable(Throwable t) {
logger.error("TTS error", t);
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
response.write(httpResponse);
}
});
}
}).get(new Route("/tts/:voice") {
@Override
public void handle(Request request, final Response response, Map<String, String> map) throws Exception {
String text = StringEscapeUtils.escapeXml(checkNotNull(request.getParam("text")));
String voice = checkNotNull(map.get("voice"));
if (!text.startsWith("<s>")) {
text = "<s>" + text + "</s>";
}
final String key = text + "|" + voice;
byte[] bytes = ttsCache.asMap().get(key);
if (bytes != null) {
response.write(ChannelBuffers.copiedBuffer(bytes));
return;
}
asyncHttpClient.preparePost(frenchTtsUrl).setBody(text).execute(new AsyncCompletionHandler<com.ning.http.client.Response>() {
@Override
public com.ning.http.client.Response onCompleted(com.ning.http.client.Response asyncResponse) throws Exception {
InputStream inputStream = asyncResponse.getResponseBodyAsStream();
byte[] bytes = ByteStreams.toByteArray(inputStream);
ttsCache.asMap().put(key, bytes);
response.write(bytes);
return asyncResponse;
}
@Override
public void onThrowable(Throwable t) {
logger.error("TTS error", t);
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
response.write(httpResponse);
}
});
}
});
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project hadoop by apache.
the class TestShuffleHandler method testKeepAlive.
@Test(timeout = 10000)
public void testKeepAlive() throws Exception {
final ArrayList<Throwable> failures = new ArrayList<Throwable>(1);
Configuration conf = new Configuration();
conf.setInt(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY, 0);
conf.setBoolean(ShuffleHandler.SHUFFLE_CONNECTION_KEEP_ALIVE_ENABLED, true);
// try setting to -ve keep alive timeout.
conf.setInt(ShuffleHandler.SHUFFLE_CONNECTION_KEEP_ALIVE_TIME_OUT, -100);
ShuffleHandler shuffleHandler = new ShuffleHandler() {
@Override
protected Shuffle getShuffle(final Configuration conf) {
// replace the shuffle handler with one stubbed for testing
return new Shuffle(conf) {
@Override
protected MapOutputInfo getMapOutputInfo(String mapId, int reduce, String jobId, String user) throws IOException {
return null;
}
@Override
protected void verifyRequest(String appid, ChannelHandlerContext ctx, HttpRequest request, HttpResponse response, URL requestUri) throws IOException {
}
@Override
protected void populateHeaders(List<String> mapIds, String jobId, String user, int reduce, HttpRequest request, HttpResponse response, boolean keepAliveParam, Map<String, MapOutputInfo> infoMap) throws IOException {
// Send some dummy data (populate content length details)
ShuffleHeader header = new ShuffleHeader("attempt_12345_1_m_1_0", 5678, 5678, 1);
DataOutputBuffer dob = new DataOutputBuffer();
header.write(dob);
dob = new DataOutputBuffer();
for (int i = 0; i < 100000; ++i) {
header.write(dob);
}
long contentLength = dob.getLength();
// disable connectinKeepAliveEnabled if keepAliveParam is available
if (keepAliveParam) {
connectionKeepAliveEnabled = false;
}
super.setResponseHeaders(response, keepAliveParam, contentLength);
}
@Override
protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch, String user, String mapId, int reduce, MapOutputInfo info) throws IOException {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
// send a shuffle header and a lot of data down the channel
// to trigger a broken pipe
ShuffleHeader header = new ShuffleHeader("attempt_12345_1_m_1_0", 5678, 5678, 1);
DataOutputBuffer dob = new DataOutputBuffer();
header.write(dob);
ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
dob = new DataOutputBuffer();
for (int i = 0; i < 100000; ++i) {
header.write(dob);
}
return ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
}
@Override
protected void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
if (failures.size() == 0) {
failures.add(new Error());
ctx.getChannel().close();
}
}
@Override
protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) {
if (failures.size() == 0) {
failures.add(new Error());
ctx.getChannel().close();
}
}
};
}
};
shuffleHandler.init(conf);
shuffleHandler.start();
String shuffleBaseURL = "http://127.0.0.1:" + shuffleHandler.getConfig().get(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY);
URL url = new URL(shuffleBaseURL + "/mapOutput?job=job_12345_1&reduce=1&" + "map=attempt_12345_1_m_1_0");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
conn.connect();
DataInputStream input = new DataInputStream(conn.getInputStream());
Assert.assertEquals(HttpHeader.KEEP_ALIVE.asString(), conn.getHeaderField(HttpHeader.CONNECTION.asString()));
Assert.assertEquals("timeout=1", conn.getHeaderField(HttpHeader.KEEP_ALIVE.asString()));
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
ShuffleHeader header = new ShuffleHeader();
header.readFields(input);
input.close();
// For keepAlive via URL
url = new URL(shuffleBaseURL + "/mapOutput?job=job_12345_1&reduce=1&" + "map=attempt_12345_1_m_1_0&keepAlive=true");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
conn.connect();
input = new DataInputStream(conn.getInputStream());
Assert.assertEquals(HttpHeader.KEEP_ALIVE.asString(), conn.getHeaderField(HttpHeader.CONNECTION.asString()));
Assert.assertEquals("timeout=1", conn.getHeaderField(HttpHeader.KEEP_ALIVE.asString()));
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
header = new ShuffleHeader();
header.readFields(input);
input.close();
}
Aggregations