import java.io.IOException;
import java.nio.charset.Charset; import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper; import org.apache.log4j.Logger;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yundaex.common.basic.vo.RequestVO;
import com.yundaex.common.basic.vo.UserContext;
import com.yundaex.common.bidiboxing.constants.CommunicationConstants;
import com.yundaex.common.bidiboxing.convert.PayloadConvertException;
import com.yundaex.common.exception.WrappedIOException;
import com.yundaex.utility.json.YDJSONConvertException;
import com.yundaex.utility.protobuf.ProtobufUtils;
import com.yundaex.utility.xml.XMLUtils; public class HttpHelper { //logger for class HttpHelper
private static final Logger logger =
Logger.getLogger(HttpHelper.class); /**
* generate requestVO from http servlet request
* */
public static RequestVO generateRequestVO(HttpServletRequest request) {
RequestVO requestVO = null;
// get request vo from request
Object attribute = request.getAttribute("requestVO");
if (attribute != null && RequestVO.class.isInstance(attribute)) {
requestVO = (RequestVO) attribute;
} else {
String payloadFormat = getPayloadFormat(request);
String voStr = request.getParameter("payload");
if (null == voStr && !CommunicationConstants.DATA_DIALECT_PROTOBUF.equals(payloadFormat)) {
try {
Charset charset = Charset.forName("UTF-8");
ServletInputStream inputStream = request.getInputStream();
voStr = StreamUtils.copyToString(inputStream, charset);
} catch (IOException e) {
throw new YDJSONConvertException("payload convert exception,couldn't convert to bean");
}
}
if (null != voStr) {
if (logger.isDebugEnabled()) {
logger.debug(voStr);
}
if (StringUtils.isEmpty(voStr)) {
requestVO = new RequestVO();
}
if ( !StringUtils.isEmpty(voStr) && CommunicationConstants.DATA_DIALECT_JSON.equals(payloadFormat)) {
try{
Feature[] features = {Feature.SortFeidFastMatch};
requestVO = JSONObject.parseObject(voStr, RequestVO.class, features);
} catch(Exception e) {
throw new PayloadConvertException("JSON数据不合法,无法解析。");
} }
if (!StringUtils.isEmpty(voStr) && CommunicationConstants.DATA_DIALECT_XML.equals(payloadFormat)) {
requestVO = XMLUtils.unmarshall(voStr, RequestVO.class);
}
}
if (null == voStr && CommunicationConstants.DATA_DIALECT_PROTOBUF.equals(payloadFormat)) {
ServletInputStream inputStream = null;
try {
inputStream = request.getInputStream();
} catch (IOException e) {
throw new WrappedIOException();
}
requestVO = ProtobufUtils.unmarshal(inputStream, RequestVO.class);
}
if (requestVO != null) {
requestVO.setFormat(payloadFormat);
if (null == requestVO.getUserContext()) {
requestVO.setUserContext(UserContext.constructEmptyUserContext());
}
requestVO.getUserContext().setDialect(payloadFormat);
request.setAttribute("requestVO", requestVO);
} else {
throw new YDJSONConvertException("payload is blank,couldn't convert to bean");
}
}
return requestVO;
} private static String getPayloadFormat(HttpServletRequest request) {
if (request.getContentType() != null) {
// System.out.println(request.getContentType());
if (request.getContentType().contains("application/json")) {
return CommunicationConstants.DATA_DIALECT_JSON;
} else if (request.getContentType().contains("application/xml")) {
return CommunicationConstants.DATA_DIALECT_XML;
} else if (request.getContentType().contains("application/x-protobuf")) {
return CommunicationConstants.DATA_DIALECT_PROTOBUF;
} else if (request.getContentType().contains("multipart/form-data")) {
if(logger.isDebugEnabled()) {
logger.debug(request.getClass());
}
if(HttpServletRequestWrapper.class.isAssignableFrom(request.getClass())) {
HttpServletRequestWrapper httpServletRequestWrapper = (HttpServletRequestWrapper) request;
request = (HttpServletRequest) httpServletRequestWrapper.getRequest();
}
if (MultipartHttpServletRequest.class.isAssignableFrom(request.getClass())) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
String format = multiRequest.getParameter("format");
if (format == null || format.equalsIgnoreCase(CommunicationConstants.DATA_DIALECT_JSON)) {
return CommunicationConstants.DATA_DIALECT_JSON;
}
if (format.equalsIgnoreCase(CommunicationConstants.DATA_DIALECT_XML)) {
return CommunicationConstants.DATA_DIALECT_XML;
}
if (format.equalsIgnoreCase(CommunicationConstants.DATA_DIALECT_PROTOBUF)) {
return CommunicationConstants.DATA_DIALECT_PROTOBUF;
}
}
}
}
return CommunicationConstants.DATA_DIALECT_JSON;
}
}
import java.util.Date;

public class RequestVO {

    private Object data;

    private String validation;

    private String format;

    private String username;

    private Date dateTime;

    private UserContext userContext;

    public String getFormat() {
return format;
} public void setFormat(String format) {
this.format = format;
} public String getValidation() {
return validation;
} public void setValidation(String validation) {
this.validation = validation;
} /**
* @return the userContext
*/
public UserContext getUserContext() {
return userContext;
} /**
* @param userContext the userContext to set
*/
public void setUserContext(UserContext userContext) {
this.userContext = userContext;
} /**
* @return the username
*/
public String getUsername() {
return username;
} /**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
} /**
* @return the data
*/
public Object getData() {
return data;
} /**
* @param data the data to set
*/
public void setData(Object data) {
this.data = data;
} public Date getDateTime() {
return dateTime;
} public void setDateTime(Date dateTime) {
this.dateTime = dateTime;
} }
public interface CommunicationConstants {

    public static final String DATA_DIALECT_JSON = "json";

    public static final String DATA_DIALECT_XML = "xml";

    public static final String DATA_DIALECT_PROTOBUF = "protobuf";

}
public class UserContext {

    //auto calculate
private Long userId; //copy form request vo
private String username; private String userProp; private Long domainId; //auto calculate
private String userType; //auto calculate;from contentTypoe;never transform
private String dialect; //optional ;support for multi-timezone
private Integer timeZone; //required;transform
private Integer curOrganization; //optional;support gb_rbac;transform
private Integer curGroup; //auto calculate. default is RBAC,optional.
private String pmModel; private String msg; private String appKey; // API 接口名称
private String method; //设备类型
private String deviceType; /**
* @return the dialect
*/
public String getDialect() {
return dialect;
} /**
* @param dialect the dialect to set
*/
public void setDialect(String dialect) {
this.dialect = dialect;
} /**
* @return the timeZone
*/
public Integer getTimeZone() {
return timeZone;
} /**
* @param timeZone the timeZone to set
*/
public void setTimeZone(Integer timeZone) {
this.timeZone = timeZone;
} /**
* @return the userId
*/
public Long getUserId() {
return userId;
} /**
* @param userId the userId to set
*/
public void setUserId(Long userId) {
this.userId = userId;
} public static UserContext constructEmptyUserContext(){
UserContext emptyContext = new UserContext();
return emptyContext;
} /**
* @return the username
*/
public String getUsername() {
return username;
} /**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
} public String getUserProp() {
return userProp;
} public void setUserProp(String userProp) {
this.userProp = userProp;
} /**
* @return the userType
*/
public String getUserType() {
return userType;
} /**
* @param userType the userType to set
*/
public void setUserType(String userType) {
this.userType = userType;
} /**
* @return the pmModel
*/
public String getPmModel() {
return pmModel;
} /**
* @param pmModel the pmModel to set
*/
public void setPmModel(String pmModel) {
this.pmModel = pmModel;
} /**
* @return the curGroup
*/
public Integer getCurGroup() {
return curGroup;
} /**
* @param curGroup the curGroup to set
*/
public void setCurGroup(Integer curGroup) {
this.curGroup = curGroup;
} /**
* @return the curOrganization
*/
public Integer getCurOrganization() {
return curOrganization;
} /**
* @param curOrganization the curOrganization to set
*/
public void setCurOrganization(Integer curOrganization) {
this.curOrganization = curOrganization;
} /**
* @return the msg
*/
public String getMsg() {
return msg;
} /**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this.msg = msg;
} /**
* @return the appKey
*/
public String getAppKey() {
return appKey;
} /**
* @param appKey the appKey to set
*/
public void setAppKey(String appKey) {
this.appKey = appKey;
} /**
* @return the method
*/
public String getMethod() {
return method;
} /**
* @param method the method to set
*/
public void setMethod(String method) {
this.method = method;
} /**
* @return the domainId
*/
public Long getDomainId() {
return domainId;
} /**
* @param domainId the domainId to set
*/
public void setDomainId(Long domainId) {
this.domainId = domainId;
} public String getDeviceType() {
return deviceType;
} public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
}

得到RequestVO的更多相关文章

  1. aop 幂等验证(二)

    1 创建IIdempotent @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNT ...

  2. C#对接----韵达开发平台--取电子面单

    引子 最近根据业务的一些需求,所以放弃从快递鸟对接去电子面单,转而直接对接韵达开发平台:http://open.yundasys.com/ ,中间踩了一些坑,借此做了一个小案例给大伙,瞅瞅,若有需改进 ...

  3. java接口入参模板化,适用于企业化服务远程调度模板化的场景,接口入参实现高度可配置化

    需求:远程服务接口模板化配置提供接入服务 模板接口分为三个模块:功能路由.参数校验.模板入库 路由:这里的实现方式很简单,就是根据业务标识找到对应的处理方法 参数校验: 参数校验这步涉及模板和校验类两 ...

  4. redis事件监听及在订单系统中的使用

    https://blog.csdn.net/qq_37334135/article/details/77717248 通常在网上买好物品,或者说手机扫码后,点击付款,这时就会向后台发送请求,生成订单信 ...

  5. java 调用 C# webapi

    最近项目需要 java调用  .net 的webapi. 对于get来说很简单,但是post方法遇到了些问题,最后也是曲线救国. 先看代码 Java  代码 public static void ma ...

  6. SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析

    SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析 一:问题demo展示 ...

  7. [Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard

    在之前的18次文章中,我们实现了广告系统的广告投放,广告检索业务功能,中间使用到了 服务发现Eureka,服务调用Feign,网关路由Zuul以及错误熔断Hystrix等Spring Cloud组件. ...

  8. [Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用

    上一节我们使用了Ribbon(基于Http/Tcp)进行微服务的调用,Ribbon的调用比较简单,通过Ribbon组件对请求的服务进行拦截,通过Eureka Server 获取到服务实例的IP:Por ...

  9. [Spring cloud 一步步实现广告系统] 10. 使用Ribbon 实现微服务调用

    在使用Ribbon调用广告投放系统API之前,我们需要先创建2个VO对象,AdPlanVO,AdPlanGetRequestVO. //数据请求对象 @Data @NoArgsConstructor ...

随机推荐

  1. 《Javascript高级程序设计》阅读记录(一):第二、三章

    <Javascript高级程序设计>阅读记录(一) 这个系列,我会把阅读<Javascript高级程序设计>之后,感觉讲的比较深入,而且实际使用价值较大的内容记录下来,并且注释 ...

  2. bzoj 2553: [BeiJing2011]禁忌 AC自动机+矩阵乘法

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=2553 题解: 利用AC自动机的dp求出所有的转移 然后将所有的转移储存到矩阵中,进行矩阵 ...

  3. P1030 求先序排列

    题目描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=8). 输入输出格式 输入格式: 2行,均为大写字母组成的字符串,表示一棵二叉树的中序与后序 ...

  4. Javascript常用的设计模式详解

    Javascript常用的设计模式详解 阅读目录 一:理解工厂模式 二:理解单体模式 三:理解模块模式 四:理解代理模式 五:理解职责链模式 六:命令模式的理解: 七:模板方法模式 八:理解javas ...

  5. Parallel Programming-Task Result && Continuation Task

    本文主要介绍带有返回值的Task和Continuation Task 带返回值的Task Continuation Task ContinueWhenAll即多任务延续 一.带返回值的Task 1.1 ...

  6. 洛谷 P4546 & bzoj 5020 在美妙的数学王国中畅游 —— LCT+泰勒展开

    题目:https://www.luogu.org/problemnew/show/P4546 先写了个55分的部分分,直接用LCT维护即可,在洛谷上拿了60分: 注意各处 pushup,而且 spla ...

  7. DOM基础知识整理

    --<JavaScript高级程序设计>Chapter10学习笔记 一.节点层次 1.Node类型 1)(Node).nodeType属性 节点nodeType属性有12种. 检测node ...

  8. samba server导出/datasmb/目录;samba client挂载/data/至本地的/mydata目录;本地的mysqld或mariadb服务的数据目录设置为/mydata, 要求服务能正常启动,且可正常 存储数据;

    实验环境:CentOS7 主机(mini2) :172.16.250.247  主机名::localhost 客户端(mini3):172.16.253.99  主机名:pxe99 #主机:配置文件的 ...

  9. .net 缓存之数据库缓存依赖

    当监听的指定数据库内容某张表变化时就更新缓存 先来配置数据库,启动监听服务(SQL2008下) 执行如下语句: ALTER DATABASE OumindBlog SET NEW_BROKER WIT ...

  10. JavaScript代码存在形式

    <!-- 方式一 --> <script type"text/javascript" src="JS文件"></script> ...