Java Code Examples for io.netty.handler.codec.http.HttpResponse#setStatus()

The following examples show how to use io.netty.handler.codec.http.HttpResponse#setStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ODataNettyHandlerImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the OData Response to Netty Response
 * @param response
 * @param odResponse
 */
static void convertToHttp(final HttpResponse response, final ODataResponse odResponse) {
   response.setStatus(HttpResponseStatus.valueOf(odResponse.getStatusCode()));

   for (Entry<String, List<String>> entry : odResponse.getAllHeaders().entrySet()) {
     for (String headerValue : entry.getValue()) {
       ((HttpMessage)response).headers().add(entry.getKey(), headerValue);
     }
   }

   if (odResponse.getContent() != null) {
     copyContent(odResponse.getContent(), response);
   } else if (odResponse.getODataContent() != null) {
     writeContent(odResponse, response);
   }
 }
 
Example 2
Source File: HttpDownHandleInterceptFactory.java    From proxyee-down with Apache License 2.0 5 votes vote down vote up
@Override
public HttpProxyIntercept create() {
  return new HttpProxyIntercept() {

    @Override
    public void afterResponse(Channel clientChannel, Channel proxyChannel,
        HttpResponse httpResponse,
        HttpProxyInterceptPipeline pipeline) throws Exception {
      HttpRequest httpRequest = pipeline.getHttpRequest();
      ProxyConfig proxyConfig = ContentManager.CONFIG.get().getSecProxyConfig();
      TaskInfo taskInfo = HttpDownUtil.getTaskInfo(httpRequest,
          httpResponse.headers(),
          proxyConfig,
          HttpDownConstant.clientSslContext,
          HttpDownConstant.clientLoopGroup);
      HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest, proxyConfig);
      ContentManager.DOWN.putBoot(httpDownInfo);
      httpResponse.setStatus(HttpResponseStatus.OK);
      httpResponse.headers().clear();
      httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=utf-8");
      byte[] content = ("<html></html>")
          .getBytes("utf-8");
      httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.length);
      clientChannel.writeAndFlush(httpResponse);
      HttpContent httpContent = new DefaultLastHttpContent();
      httpContent.content().writeBytes(content);
      clientChannel.writeAndFlush(httpContent);
      clientChannel.close();
      httpDownDispatch.dispatch(httpDownInfo);
    }
  };
}
 
Example 3
Source File: VerifyMiscellaneousFunctionalityComponentTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Override
public void handleDownstreamResponseFirstChunk(
    @NotNull HttpResponse downstreamResponseFirstChunk,
    @NotNull RequestInfo<?> origRequestInfo
) {
    downstreamResponseFirstChunk.headers().set(ORIG_HTTP_STATUS_CODE_RESPONSE_HEADER_KEY,
                                               String.valueOf(downstreamResponseFirstChunk.status().code()));
    downstreamResponseFirstChunk.setStatus(new HttpResponseStatus(MODIFIED_HTTP_STATUS_RESPONSE_CODE, "junk status code"));
}
 
Example 4
Source File: HttpRequestHandler.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
  if (wsUri.equalsIgnoreCase(request.getUri())) {
    ctx.fireChannelRead(request.retain());
  } else {
    if (HttpHeaders.is100ContinueExpected(request)) {
      send100Continue(ctx);
    }

    HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.INTERNAL_SERVER_ERROR);

    String path = new URI(request.getUri()).getPath();

    if ("/".equals(path)) {
      path = "/index.html";
    }
    URL res = HttpTtyConnection.class.getResource(httpResourcePath + path);
    try {
      if (res != null) {
        DefaultFullHttpResponse fullResp = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
        InputStream in = res.openStream();
        byte[] tmp = new byte[256];
        for (int l = 0; l != -1; l = in.read(tmp)) {
          fullResp.content().writeBytes(tmp, 0, l);
        }
        int li = path.lastIndexOf('.');
        if (li != -1 && li != path.length() - 1) {
          String ext = path.substring(li + 1, path.length());
          String contentType;
          if ("html".equals(ext)) {
            contentType = "text/html";
          } else if ("js".equals(ext)) {
            contentType = "application/javascript";
          } else if ("css".equals(ext)) {
              contentType = "text/css";
          } else {
            contentType = null;
          }

          if (contentType != null) {
            fullResp.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
          }
        }
        response = fullResp;
      } else {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      ctx.write(response);
      ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
      future.addListener(ChannelFutureListener.CLOSE);
    }
  }
}
 
Example 5
Source File: HttpRequestHandler.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    if (wsUri.equalsIgnoreCase(request.getUri())) {
        ctx.fireChannelRead(request.retain());
    } else {
        if (HttpHeaders.is100ContinueExpected(request)) {
            send100Continue(ctx);
        }

        HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.INTERNAL_SERVER_ERROR);

        String path = request.getUri();
        if ("/".equals(path)) {
            path = "/index.html";
        }
        URL res = HttpTtyConnection.class.getResource("/org/aesh/terminal/http" + path);
        try {
            if (res != null) {
                DefaultFullHttpResponse fullResp = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
                InputStream in = res.openStream();
                byte[] tmp = new byte[256];
                for (int l = 0; l != -1; l = in.read(tmp)) {
                    fullResp.content().writeBytes(tmp, 0, l);
                }
                int li = path.lastIndexOf('.');
                if (li != -1 && li != path.length() - 1) {
                    String ext = path.substring(li + 1, path.length());
                    String contentType;
                    switch (ext) {
                        case "html":
                            contentType = "text/html";
                            break;
                        case "js":
                            contentType = "application/javascript";
                            break;
                        default:
                            contentType = null;
                            break;
                    }
                    if (contentType != null) {
                        fullResp.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
                    }
                }
                response = fullResp;
            } else {
                response.setStatus(HttpResponseStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ctx.write(response);
            ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
}
 
Example 6
Source File: HttpRequestHandler.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
  if (wsUri.equalsIgnoreCase(request.getUri())) {
    ctx.fireChannelRead(request.retain());
  } else {
    if (HttpHeaders.is100ContinueExpected(request)) {
      send100Continue(ctx);
    }

    HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.INTERNAL_SERVER_ERROR);

    String path = request.getUri();
    if ("/".equals(path)) {
      path = "/index.html";
    }
    URL res = HttpTtyConnection.class.getResource("/io/termd/core/http" + path);
    try {
      if (res != null) {
        DefaultFullHttpResponse fullResp = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
        InputStream in = res.openStream();
        byte[] tmp = new byte[256];
        for (int l = 0; l != -1; l = in.read(tmp)) {
          fullResp.content().writeBytes(tmp, 0, l);
        }
        int li = path.lastIndexOf('.');
        if (li != -1 && li != path.length() - 1) {
          String ext = path.substring(li + 1, path.length());
          String contentType;
          switch (ext) {
            case "html":
              contentType = "text/html";
              break;
            case "js":
              contentType = "application/javascript";
              break;
            default:
              contentType = null;
              break;
          }
          if (contentType != null) {
            fullResp.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
          }
        }
        response = fullResp;
      } else {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      ctx.write(response);
      ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
      future.addListener(ChannelFutureListener.CLOSE);
    }
  }
}