io.netty.handler.codec.http.HttpResponse # setStatus ( ) 源码实例Demo

下面列出了 io.netty.handler.codec.http.HttpResponse # setStatus ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


/**
 * 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);
   }
 }
 

@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);
    }
  };
}
 

@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"));
}
 
源代码4 项目: termd   文件: HttpRequestHandler.java

@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);
    }
  }
}
 

@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);
        }
    }
}
 
源代码6 项目: termd   文件: HttpRequestHandler.java

@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);
    }
  }
}