private static final FullHttpResponse http404() { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
private static final FullHttpResponse http404() { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
private static final FullHttpResponse http200(String result) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(result.getBytes())); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
private static final FullHttpResponse http200(String result) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(result.getBytes())); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
private static final FullHttpResponse http500(String errorMessage) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR , Unpooled.wrappedBuffer(errorMessage.getBytes())); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
private static final FullHttpResponse http500(String errorMessage) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR , Unpooled.wrappedBuffer(errorMessage.getBytes())); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
public Netty4ClientHttpResponse(ChannelHandlerContext context, FullHttpResponse nettyResponse) { Assert.notNull(context, "ChannelHandlerContext must not be null"); Assert.notNull(nettyResponse, "FullHttpResponse must not be null"); this.context = context; this.nettyResponse = nettyResponse; this.body = new ByteBufInputStream(this.nettyResponse.content()); this.nettyResponse.retain(); }
private String readRequestBody(FullHttpResponse fullHttpResponse) { if (fullHttpResponse.content().readableBytes() > 0) { byte[] bodyBytes = new byte[fullHttpResponse.content().readableBytes()]; fullHttpResponse.content().readBytes(bodyBytes); Charset requestCharset = ContentTypeMapper.getCharsetFromContentTypeHeader(fullHttpResponse.headers().get(CONTENT_TYPE)); return new String(bodyBytes, requestCharset); } return ""; }
static void sendError(Channel ch, HttpResponseStatus status, CharSequence err) { FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); if (status.code() == METHOD_NOT_ALLOWED.code()) { // SockJS requires this resp.headers().set(io.vertx.core.http.HttpHeaders.ALLOW, io.vertx.core.http.HttpHeaders.GET); } if (err != null) { resp.content().writeBytes(err.toString().getBytes(CharsetUtil.UTF_8)); HttpUtil.setContentLength(resp, err.length()); } else { HttpUtil.setContentLength(resp, 0); } ch.writeAndFlush(resp); }
private void outputContent(ChannelHandlerContext ctx, FullHttpRequest request, int code, String content, String mimeType) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code), Unpooled.wrappedBuffer(content.getBytes(Charset.forName("UTF-8")))); response.headers().set(Names.CONTENT_TYPE, mimeType); response.headers().set(Names.CONTENT_LENGTH, response.content().readableBytes()); response.headers().set(Names.SERVER, SERVER_VS); ChannelFuture future = ctx.writeAndFlush(response); if (!HttpHeaders.isKeepAlive(request)) { future.addListener(ChannelFutureListener.CLOSE); } }
public Netty4ClientHttpResponse(ChannelHandlerContext context, FullHttpResponse nettyResponse) { Assert.notNull(context, "ChannelHandlerContext must not be null"); Assert.notNull(nettyResponse, "FullHttpResponse must not be null"); this.context = context; this.nettyResponse = nettyResponse; this.body = new ByteBufInputStream(this.nettyResponse.content()); this.nettyResponse.retain(); }
@SuppressWarnings({"rawtypes", "unchecked"}) protected FullHttpResponse buildHttpResponse(Response response, boolean keepAlive) throws Exception { Object value = response.getValue(); byte[] responseBytes = null; if (value instanceof Message) { Marshaller marshaller = ProtoUtils.jsonMarshaller((Message) value.getClass().getMethod("getDefaultInstance", null).invoke(null, null)); InputStream is = marshaller.stream(value); responseBytes = new byte[is.available()]; is.read(responseBytes); } else { // TODO not pb } FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes)); httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/x-www-form-urlencoded"); httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpResponse.content().readableBytes()); if (keepAlive) { httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } else { httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); } return httpResponse; }
private void setBody(HttpResponse httpResponse, FullHttpResponse fullHttpResponse) { if (fullHttpResponse.content().readableBytes() > 0) { byte[] bodyBytes = new byte[fullHttpResponse.content().readableBytes()]; fullHttpResponse.content().readBytes(bodyBytes); if (bodyBytes.length > 0) { if (ContentTypeMapper.isBinary(fullHttpResponse.headers().get(CONTENT_TYPE))) { httpResponse.withBody(new BinaryBody(bodyBytes)); } else { Charset requestCharset = ContentTypeMapper.getCharsetFromContentTypeHeader(fullHttpResponse.headers().get(CONTENT_TYPE)); httpResponse.withBody(new String(bodyBytes, requestCharset)); } } } } }
private void setContentLengthForKeepAlive(final FullHttpResponse response) { if (!isContentLengthSet(response)) { setContentLength(response, response.content().writerIndex()); } }
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
public FullHttpResponse toFullResponse() { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.valueOf(this.version.text()), HttpResponseStatus.valueOf(this.status)); for (Map.Entry<String, String[]> entry : getHeaders().entrySet()) { String key = entry.getKey(); for (String value : entry.getValue()) { response.headers().add(key, value); } } if (this.content != null) { response.content().writeBytes(this.content.getContent()); } return response; } }
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); }
@Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception { FullHttpResponse response = (FullHttpResponse) msg; if (httpRequiresSessionId && !active) { final List<String> setCookieHeaderValues = response.headers().getAll(HttpHeaderNames.SET_COOKIE); for (String setCookieHeaderValue : setCookieHeaderValues) { final Cookie cookie = ClientCookieDecoder.LAX.decode(setCookieHeaderValue); if ("JSESSIONID".equals(cookie.name())) { this.cookie = setCookieHeaderValue; break; } } active = true; handShakeFuture.run(); } waitingGet = false; ctx.fireChannelRead(response.content()); }
response.content().writeBytes(((HttpContent) msg).content()); if (msg instanceof LastHttpContent) { response.trailingHeaders().add(((LastHttpContent) msg).trailingHeaders());
public static HttpResponse newResponse(final FullHttpResponse response) { return builder() .withVersion(toHttpProtocolVersion(response.protocolVersion())) .withStatus(response.status().code()) .withHeaders(toHeaders(response)) .withContent(content() .withContent(new ByteBufInputStream(response.content())) .build()) .build(); }