public HttpResponse sendResponse(HttpResponseStatus responseCode, String responseBody) { String actualResponseBody = responseBody + "\n"; HttpResponse response = new DefaultHttpResponse(HTTP_1_1, responseCode); response.setHeader(CONTENT_LENGTH, actualResponseBody.length()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { outputStream.write(actualResponseBody.getBytes()); } catch(IOException e) { logger.error("IOException while trying to write the outputStream for an admin response", e); throw new RuntimeException(e); } ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer(); responseContent.writeBytes(outputStream.toByteArray()); response.setContent(responseContent); if (logger.isDebugEnabled()) { logger.debug("Sent " + response); } return response; }
HttpResponseStatus.INTERNAL_SERVER_ERROR ); httpResponse.setContent(ChannelBuffers.buffer(0)); httpResponseHandler.handleResponse(httpResponse, null); return Futures.immediateFailedFuture(new RuntimeException("server error")); httpResponse.setContent(ChannelBuffers.buffer(0)); httpResponseHandler.handleResponse(httpResponse, null); try {
HttpResponseStatus.INTERNAL_SERVER_ERROR ); httpResponse.setContent(ChannelBuffers.buffer(0)); httpResponseHandler.handleResponse(httpResponse, null); return Futures.immediateFailedFuture(new RuntimeException("server error")); httpResponse.setContent(ChannelBuffers.buffer(0)); httpResponseHandler.handleResponse(httpResponse, null); try {
response.setContent(responseContent); response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
@Override public void sendResponse(StoreStats performanceStats, boolean isFromLocalZone, long startTimeInMs) throws Exception { String base64Key = RestUtils.encodeVoldemortKey(key.get()); String contentLocationKey = "/" + this.storeName + "/" + base64Key; List<VectorClock> vectorClocks = new ArrayList<VectorClock>(); for(Version versionedValue: versionedValues) { VectorClock vectorClock = (VectorClock) versionedValue; vectorClocks.add(vectorClock); numVectorClockEntries += vectorClock.getVersionMap().size(); } String eTags = RestUtils.getSerializedVectorClocks(vectorClocks); byte[] responseContent = eTags.getBytes(); ChannelBuffer responseContentBuffer = ChannelBuffers.dynamicBuffer(responseContent.length); responseContentBuffer.writeBytes(responseContent); // Create the Response object HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); // Set the right headers response.setHeader(CONTENT_TYPE, "binary"); response.setHeader(CONTENT_TRANSFER_ENCODING, "binary"); response.setHeader(CONTENT_LOCATION, contentLocationKey); // Copy the data into the payload response.setContent(responseContentBuffer); response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes()); // Write the response to the Netty Channel this.messageEvent.getChannel().write(response); }
protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.headers().add(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Put shuffle version into http header response.headers().add(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME); response.headers().add(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION); response.setContent( ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
/** * Writes all error responses to the client. * * TODO REST-Server 1. collect error stats * * @param messageEvent - for retrieving the channel details * @param status - error code * @param message - error message */ public static void writeErrorResponse(MessageEvent messageEvent, HttpResponseStatus status, String message) { // Create the Response object HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer("Failure: " + status.toString() + ". " + message + "\r\n", CharsetUtil.UTF_8)); response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes()); // Write the response to the Netty Channel messageEvent.getChannel().write(response); } }
response.setContent(responseContent); response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
httpResponse.setContent(ChannelBuffers.buffer(0)); httpResponseHandler.handleResponse(httpResponse, null); try {
response.setContent(responseContent); response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
@Test public void simpleSingleStreamTest() throws IOException { SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(false); response.setContent(new BigEndianHeapChannelBuffer(allBytes)); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); int read = 0; while (read < allBytes.length) { final byte[] expectedBytes = new byte[Math.min(RANDOM.nextInt(128), allBytes.length - read)]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); read += expectedBytes.length; } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test(expected = TesterException.class) public void testExceptionalSingleStream() throws IOException { SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(false); response.setContent( new BigEndianHeapChannelBuffer(allBytes) { @Override public void getBytes(int index, byte[] dst, int dstIndex, int length) { if (dstIndex + length >= allBytes.length) { throw new TesterException(); } super.getBytes(index, dst, dstIndex, length); } } ); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final byte[] buff = new byte[allBytes.length]; fillBuff(stream, buff); }
private ChannelFuture write(ChannelBuffer responseBuffer) { response.setContent(responseBuffer); return ctx.getChannel().write(response); }
input.writeInt(b); input.writeLong(c); res.setContent(WebSocketUtil.md5(input)); } else {
@Override public void prepareHandshakeResponse(NettyWebSocketConnection webSocketConnection) { webSocketConnection.setVersion("HIXIE-76"); res.setStatus(new HttpResponseStatus(101, "Web Socket Protocol Handshake")); res.addHeader(UPGRADE, WEBSOCKET); res.addHeader(CONNECTION, UPGRADE); res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req)); String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL); if (protocol != null) { res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol); } // Calculate the answer of the challenge. String key1 = req.getHeader(SEC_WEBSOCKET_KEY1); String key2 = req.getHeader(SEC_WEBSOCKET_KEY2); int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length()); int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length()); long c = req.getContent().readLong(); ChannelBuffer input = ChannelBuffers.buffer(16); input.writeInt(a); input.writeInt(b); input.writeLong(c); ChannelBuffer output = ChannelBuffers.wrappedBuffer(MD5.digest(input.array())); res.setContent(output); }
response.setContent(buffer);
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer( "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer( "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); } }
protected void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer( "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); } }
private void handle(HttpResponse message) throws Exception { State state = getState(State.class); // convert back to netty http response org.jboss.netty.handler.codec.http.HttpResponse nettyResponse = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(message.getStatusCode())); // @todo: copy the headers from the message HttpHeaders.setHeader(nettyResponse,HttpHeaders.Names.CONTENT_TYPE,message.getContentType()); nettyResponse.setContent(ChannelBuffers.wrappedBuffer(message.getContent())); // we don't support keep alive as of yet state.getResponseChannel().write(nettyResponse).addListener(ChannelFutureListener.CLOSE); // need to remove myself getSystem().stop(getSelf()); }