Java Code Examples for org.tio.http.common.HttpResponse#setBody()

The following examples show how to use org.tio.http.common.HttpResponse#setBody() . 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: Resps.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
	 * 创建字符串输出
	 * @param request
	 * @param bodyString
	 * @param charset
	 * @param Content_Type
	 * @return
	 * @author tanyaowu
	 */
	public static HttpResponse string(HttpRequest request, String bodyString, String charset, String Content_Type) {
		HttpResponse ret = new HttpResponse(request);
//
//		//处理jsonp
//		String jsonp = request.getParam(request.httpConfig.getJsonpParamName());
//		if (StrUtil.isNotBlank(jsonp)) {
//			bodyString = jsonp + "(" + bodyString + ")";
//		}

		if (bodyString != null) {
			if (charset == null) {
				ret.setBody(bodyString.getBytes());
			} else {
				try {
					ret.setBody(bodyString.getBytes(charset));
				} catch (UnsupportedEncodingException e) {
					log.error(e.toString(), e);
				}
			}
		}
		ret.addHeader(HeaderName.Content_Type, HeaderValue.Content_Type.from(Content_Type));
		return ret;
	}
 
Example 2
Source File: HttpGzipUtils.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param response
 * @author tanyaowu
 */
public static void gzip(HttpResponse response) {
	if (response == null) {
		return;
	}

	// 已经gzip过了,就不必再压缩了
	if (response.isHasGzipped()) {
		return;
	}

	byte[] bs = response.getBody();
	if (bs != null && bs.length >= 300) {
		byte[] bs2 = ZipUtil.gzip(bs);
		if (bs2.length < bs.length) {
			response.setBody(bs2);
			response.setHasGzipped(true);
			response.addHeader(HeaderName.Content_Encoding, HeaderValue.Content_Encoding.gzip);
		}
	}
}
 
Example 3
Source File: FileCache.java    From t-io with Apache License 2.0 5 votes vote down vote up
public HttpResponse cloneResponse(HttpRequest request) {
	//		HttpResponse responseInCache = fileCache.getResponse();
	HttpResponse ret = new HttpResponse(request);
	ret.setBody(response.getBody());
	ret.setHasGzipped(response.isHasGzipped());
	ret.addHeaders(response.getHeaders());
	return ret;
}
 
Example 4
Source File: Resps.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param request
 * @param bodyBytes
 * @param contentType 形如:application/octet-stream等
 * @return
 * @author tanyaowu
 */
public static HttpResponse bytesWithContentType(HttpRequest request, byte[] bodyBytes, String contentType) {
	HttpResponse ret = new HttpResponse(request);
	ret.setBody(bodyBytes);

	if (StrUtil.isBlank(contentType)) {
		ret.addHeader(HeaderName.Content_Type, HeaderValue.Content_Type.DEFAULT_TYPE);
	} else {
		ret.addHeader(HeaderName.Content_Type, HeaderValue.Content_Type.from(contentType));
	}
	return ret;
}
 
Example 5
Source File: TestController.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@RequestPath(value = "json")
	public HttpResponse json(HttpRequest request) throws Exception {
		//更高性能的写法
		HttpResponse ret = new HttpResponse(request);
		ret.setBody(Json.toJson(new Message(HELLO_WORLD)).getBytes());
		ret.addHeader(HeaderName.Content_Type, HeaderValue.Content_Type.TEXT_PLAIN_JSON);
		return ret;
		
		//简便写法
//		return Resps.json(request, new Message(HELLO_WORLD));
	}
 
Example 6
Source File: TestController.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@RequestPath(value = "plaintext")
	public HttpResponse plaintext(HttpRequest request) throws Exception {
		//更高性能的写法
		HttpResponse ret = new HttpResponse(request);
		ret.setBody(HELLO_WORLD_BYTES);
		ret.addHeader(HeaderName.Content_Type, HeaderValue.Content_Type.TEXT_PLAIN_TXT);
		return ret;
		
		//简便写法
//		return Resps.bytesWithContentType(request, HELLO_WORLD_BYTES, MimeType.TEXT_PLAIN_TXT.getType());
	}
 
Example 7
Source File: HttpResponseDecoder.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
   * @param buffer
   * @param limit
   * @param position
   * @param readableLength
   * @param channelContext
   * @return
   * @throws AioDecodeException
   * @author tanyaowu
   */
  public static HttpResponse decode(
      ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext)
      throws AioDecodeException {
    Map<String, String> headers = new HashMap<>();
    int contentLength = 0;
    byte[] bodyBytes = null;
    StringBuilder headerSb = null; // new StringBuilder(512);
    ResponseLine firstLine = null;
    boolean appendRequestHeaderString = true;

    if (appendRequestHeaderString) {
      headerSb = new StringBuilder(512);
    }

    // request line connect
    firstLine = parseResponseLine(buffer, channelContext);
    if (firstLine == null) {
      return null;
    }
    // request line end

    // request header connect
    boolean headerCompleted = parseHeaderLine(buffer, headers, 0);
    if (!headerCompleted) {
      return null;
    }
    String contentLengthStr = headers.get(HttpConst.ResponseHeaderKey.Content_Length);

    if (StrUtil.isBlank(contentLengthStr)) {
      contentLength = 0;
    } else {
      contentLength = Integer.parseInt(contentLengthStr);
    }

    int headerLength = (buffer.position() - position);
    int allNeedLength = headerLength + contentLength; // 这个packet所需要的字节长度(含头部和体部)

    if (readableLength < allNeedLength) {
      channelContext.setPacketNeededLength(allNeedLength);
      return null;
    }
    // request header end

    // ----------------------------------------------- request body connect

    HttpResponse httpResponse = new HttpResponse();
    //    httpResponse.setChannelContext(channelContext);
    //		httpResponse.setHttpConfig((HttpConfig)
    // channelContext.tioConfig.getAttribute(TioConfigKey.HTTP_SERVER_CONFIG));

    if (appendRequestHeaderString) {
      httpResponse.setHeaderString(headerSb.toString());
    } else {
      httpResponse.setHeaderString("");
    }

    //    httpResponse.setResponseLine(firstLine);
    httpResponse.setStatus(HttpResponseStatus.getHttpStatus(firstLine.status));
    httpResponse.addHeaders(
        headers.entrySet().stream()
            .collect(
                Collectors.toMap(
                    entry -> HeaderName.from(entry.getKey()),
                    entry -> HeaderValue.from(entry.getValue()))));
//    httpResponse.setContentLength(contentLength);
    String connection = headers.get(HttpConst.ResponseHeaderKey.Connection);
    if (connection != null) {
//      httpResponse.setConnection(connection.toLowerCase());
    }

    if (contentLength == 0) {
      //			if (StrUtil.isNotBlank(firstLine.getQuery())) {
      //				decodeParams(httpResponse.getParams(), firstLine.getQuery(), httpResponse.getCharset(),
      // channelContext);
      //			}
    } else {
      bodyBytes = new byte[contentLength];
      buffer.get(bodyBytes);
      httpResponse.setBody(bodyBytes);
      // 解析消息体
      parseBody(httpResponse, bodyBytes, channelContext);
    }
    // ----------------------------------------------- request body end

    return httpResponse;
  }
 
Example 8
Source File: HttpResponseDecoder.java    From t-io with Apache License 2.0 3 votes vote down vote up
/**
 * 先粗暴地简单解析一下
 *
 * @param httpResponse
 * @param bodyBytes
 * @param channelContext
 * @throws AioDecodeException
 * @author tanyaowu
 */
private static void parseBody(
    HttpResponse httpResponse, byte[] bodyBytes, ChannelContext channelContext)
    throws AioDecodeException {
  if (bodyBytes != null) {
	httpResponse.setBody(bodyBytes);
}
}
 
Example 9
Source File: Resps.java    From t-io with Apache License 2.0 3 votes vote down vote up
/**
 *
 * @param request
 * @param bodyBytes
 * @param headers
 * @return
 * @author tanyaowu
 */
public static HttpResponse bytesWithHeaders(HttpRequest request, byte[] bodyBytes, Map<HeaderName, HeaderValue> headers) {
	HttpResponse ret = new HttpResponse(request);
	ret.setBody(bodyBytes);
	ret.addHeaders(headers);
	return ret;
}