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 socket.io-netty by ibdknox.
the class PollingIOClient method _write.
private void _write(String message) {
if (!this.open)
return;
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
res.addHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Credentials", "true");
res.addHeader("Connection", "keep-alive");
res.setContent(ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8));
setContentLength(res, res.getContent().readableBytes());
// Send the response and close the connection if necessary.
Channel chan = ctx.getChannel();
if (chan.isOpen()) {
ChannelFuture f = chan.write(res);
if (!isKeepAlive(req) || res.getStatus().getCode() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
this.connected = false;
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class DummyHttpRequestHandler method testSetResponseCode.
@Test
public void testSetResponseCode() {
LOG.info("Start: Testing response code with headers with empty body");
setupClient();
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("header1", "value1");
headers.put("header2", "value2");
HashMap<String, String> footers = new HashMap<String, String>();
setupServer(HttpResponseStatus.BAD_GATEWAY, chunks, headers, footers);
ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", connectFuture.isSuccess());
HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
Channel requestChannel = connectFuture.getChannel();
ChannelFuture writeFuture = requestChannel.write(request);
writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", writeFuture.isSuccess());
HttpResponse response = _responseHandler.getResponse();
assertEquals("response code", HttpResponseStatus.BAD_GATEWAY, response.getStatus());
assertEquals("Checking header1 value", "value1", response.getHeader("header1"));
assertEquals("Checking header2 value", "value2", response.getHeader("header2"));
assertEquals("Checking header3 value", null, response.getHeader("header3"));
byte[] responseBody = _responseHandler.getReceivedBytes();
assertTrue("empty response", null == responseBody || responseBody.length == 0);
LOG.info("Done: Testing response code with headers with empty body");
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class DummyHttpRequestHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
//HttpRequest request = (HttpRequest) e.getMessage();
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
ChunkedBodyWritableByteChannel writeChannel = new ChunkedBodyWritableByteChannel(e.getChannel(), response);
for (String key : _headers.keySet()) {
writeChannel.setMetadata(key, _headers.get(key));
}
for (byte[] chunk : _responseChunks) {
writeChannel.write(ByteBuffer.wrap(chunk));
}
writeChannel.setResponseCode(_responseCode);
for (String key : _footers.keySet()) {
writeChannel.setMetadata(key, _footers.get(key));
}
writeChannel.close();
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class DummyHttpRequestHandler method testWriteOneChunk.
@Test
public void testWriteOneChunk() {
LOG.info("Start: Testing headers with one chunk");
setupClient();
String chunk1 = "hello";
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
chunks.add(chunk1.getBytes(Charset.defaultCharset()));
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("header1", "value1");
headers.put("header2", "value2");
HashMap<String, String> footers = new HashMap<String, String>();
setupServer(HttpResponseStatus.OK, chunks, headers, footers);
ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", connectFuture.isSuccess());
HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
Channel requestChannel = connectFuture.getChannel();
ChannelFuture writeFuture = requestChannel.write(request);
writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", writeFuture.isSuccess());
HttpResponse response = _responseHandler.getResponse();
assertEquals("response code", Integer.toString(HttpResponseStatus.OK.getCode()), response.getHeader(ChunkedBodyWritableByteChannel.RESPONSE_CODE_FOOTER_NAME));
assertEquals("Checking header1 value", "value1", response.getHeader("header1"));
assertEquals("Checking header2 value", "value2", response.getHeader("header2"));
byte[] responseBody = _responseHandler.getReceivedBytes();
assertEquals("response length", chunk1.getBytes(Charset.defaultCharset()).length, responseBody.length);
assertTrue("response content", Arrays.equals(chunk1.getBytes(Charset.defaultCharset()), responseBody));
LOG.info("Done: Testing headers with one chunk");
}
Aggregations