com.logicbus.backend.server.http.HttpContext Maven / Gradle / Ivy
package com.logicbus.backend.server.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.anysoft.webloader.HttpClientTool;
import com.anysoft.webloader.HttpTraceTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.anysoft.util.IOTools;
import com.anysoft.util.Settings;
import com.logicbus.backend.Context;
/**
* Http请求的上下文
*
* @author duanyy
* @version 1.0.5 [20140412 duanyy]
* - 改进消息传递模型
*
* @version 1.4.0 [20141117 duanyy]
* - 将MessageDoc和Context进行合并整合
*
* @version 1.6.1.1 [20141117 duanyy]
* - 增加{@link #getMethod()}实现
* - 暴露InputStream和OutputStream
*
* @version 1.6.1.2 [20141118 duanyy]
* - 增加截取数据的功能
*
* @version 1.6.2.1 [20141223 duanyy]
* - 增加对Comet的支持
*
* @version 1.6.3.10 [20140324 duanyy]
* - 增加忽略本次输出的功能
*
* @version 1.6.4.22 [20160113 duanyy]
* - 当发生错误时,细化错误信息的输出
*
* @version 1.6.5.6 [20160523 duanyy]
* - 不再从MessageDoc上继承
* - 增加报文长度
* - 增加全局调用次序
*
* @version 1.6.7.1 [20170117 duanyy]
* - trace日志调用链中的调用次序采用xx.xx.xx.xx字符串模式
*
* @version 1.6.7.9 [20170201 duanyy]
* - 采用SLF4j日志框架输出日志
*
* @version 1.6.8.6 [20170410 duanyy]
* - 服务调用全局序列号采用随机64位数字(16进制)
*
* @version 1.6.9.3 [20170615 duanyy]
* - 修正tlog的全局序列号不规范问题
*
* @version 1.6.10.9 [20171124 duanyy]
* - 规范化URL和URI的取值
*
* @version 1.6.11.12 [20170123 duanyy]
* - http响应的缓存属性改成由服务来个性化控制
*
* @version 1.6.11.45 [duanyy 20180722]
* - 增加getHostDomain方法
*
* @version 1.6.11.48 [20180807 duanyy]
* - 优化缓存相关的http控制头的输出
*
* @version 1.6.11.50 [20180808 duanyy]
* - 从Http头中获取Sample信息
*
* @version 1.6.12.29 [20190409]
* - 优化底层的错误代码的处理;
*
* @version 1.6.13.7 [20200720 duanyy]
* - 可以从Header中获取替代的URI
*
* @version 1.6.13.23 [20201030 duanyy]
* - 将服务序列号注入到slf4j的MDC中
*
* @version 1.6.14.6 [20210415 duanyy]
* - 增加baseUrl的获取方法
*/
public class HttpContext extends Context {
/**
* a logger of log4j
*/
protected final Logger logger = LoggerFactory.getLogger(HttpContext.class);
/**
* request
*/
protected HttpServletRequest request = null;
/**
* 全局序列号
*/
private String globalSerial = null;
/**
* 调用次序
*/
private String globalSerialOrder = null;
/**
* response
*/
protected HttpServletResponse response = null;
/**
* constructor
* @param _request HttpServletRequest
* @param _response HttpServletResponse
* @param _encoding the encoding
*/
public HttpContext(HttpServletRequest _request,HttpServletResponse _response,String _encoding){
super(_encoding);
request = _request;
response = _response;
globalSerial = traceTool.getTraceId(request);
globalSerialOrder = traceTool.getTraceOrder(request);
traceTool.enableMDC(globalSerial);
}
/**
* constructor
* @param _request HttpServletRequest
* @param _response HttpServletResponse
* @param _encoding the encoding
* @param toIntercept whether or not to intercept data from input stream
*/
public HttpContext(HttpServletRequest _request,HttpServletResponse _response,String _encoding,boolean toIntercept){
this(_request,_response,_encoding);
if (toIntercept){
InputStream in = null;
try {
in = request.getInputStream();
requestRaw = readBytesFromInputStream(in);
}catch (Exception ex){
logger.error("Error when reading data from inputstream",ex);
}finally{
IOTools.close(in);
}
}
}
/**
* to get request
* @return HttpServletRequest
*/
public HttpServletRequest getRequest(){ return request;}
/**
* to get response
* @return HttpServletResponse
*/
public HttpServletResponse getResponse(){return response;}
@Override
public String _GetValue(String _name) {
String found = super._GetValue(_name);
if (found == null || found.length() <= 0){
if (request != null)
{
HttpSession session = request.getSession(false);
if (session != null){
Object obj = session.getAttribute(_name);
if (obj != null) return obj.toString();
}
String value = request.getParameter(_name);
if (value != null){
return value;
}
}
}
return found;
}
@Override
public String getClientIp() {
return clientTool.getClientIp(request);
}
@Override
public String getClientRealIp(){
return clientTool.getClientRealIp(request);
}
@Override
public String getHost() {
return clientTool.getHost(request);
}
@Override
public String getHostDomain(){
return clientTool.getHostDomain(request);
}
@Override
public String getUrlBase(){
return clientTool.getUrlBase(request);
}
@Override
public String getRequestURI() {
return clientTool.getRequestURI(request);
}
@Override
public String getRequestURL(){
return clientTool.getRequestURL(request);
}
@Override
public String getPathInfo(){
return clientTool.getPathInfo(request);
}
@Override
public String getGlobalSerial() {
return globalSerial;
}
@Override
public String getGlobalSerialOrder() {
return globalSerialOrder;
}
@Override
public long getContentLength() {
return msg == null ? 0 : msg.getContentLength();
}
@Override
public String getRequestHeader(String id) {
return clientTool.getRequestHeader(request,id);
}
@Override
public void setResponseHeader(String id, String value) {
clientTool.setResponseHeader(response,id,value);
}
@Override
public String getRequestContentType() {
return clientTool.getRequestContentType(request);
}
@Override
public String getMethod() {
return clientTool.getOriginalMethod(request);
}
@Override
public String getQueryString(){
return clientTool.getQueryString(request);
}
@Override
public void setResponseContentType(String contentType) {
clientTool.setResponseContentType(response,contentType);
}
@Override
public void setResponseContentLength(int contentLength) {
clientTool.setResponseContentLength(response,contentLength);
}
@Override
public InputStream getInputStream() throws IOException{
return request.getInputStream();
}
@Override
public OutputStream getOutputStream() throws IOException{
return response.getOutputStream();
}
/**
* requestRow,存放提前截取的输入数据
*/
private byte [] requestRaw = null;
@Override
public byte[] getRequestRaw() {
return requestRaw;
}
@Override
public void finish() {
String code = getReturnCode();
String codeMessage = getReason();
try {
if (msg == null){
if (code.equals("core.ok")){
clientTool.sendError(response,HttpServletResponse.SC_NOT_FOUND,"No message is found,check servant implemention.");
}else{
logger.error(String.format("[%s]%s-%s,query:[%s]",this.getClientIp(),code,codeMessage,getQueryString()));
clientTool.sendError(response,HttpServletResponse.SC_NOT_FOUND,String.format("%s:%s",code,codeMessage));
}
}else{
clientTool.setStatus(response,getStatus());
clientTool.setResponseEncoding(response,getEncoding());
if (enableClientCache()){
cacheTool.cacheEnable(response);
}else{
cacheTool.cacheDisable(response);
}
msg.finish(this,true);
}
}catch (Exception ex){
try {
logger.error(String.format("[%s]%s-%s,query:[%s]",this.getClientIp(),code,codeMessage,getQueryString()));
clientTool.sendError(response,HttpServletResponse.SC_NOT_FOUND,String.format("%s:%s",code,codeMessage));
}catch (Exception e){
logger.error("Error when writing result:" + e.getMessage());
}
}
}
public static HttpTraceTool traceTool = null;
public static HttpCacheTool cacheTool = null;
public static HttpClientTool clientTool = null;
static{
traceTool = Settings.getToolkit(HttpTraceTool.class);
cacheTool = Settings.getToolkit(HttpCacheTool.class);
clientTool = Settings.getToolkit(HttpClientTool.class);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy