PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/xjt/ws/core/ServiceRequest.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 206 lines | 111 code | 23 blank | 72 comment | 6 complexity | 827c83a266c93c64b0c7c9ee407ed868 MD5 | raw file
  1. package xjt.ws.core;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.Map;
  6. import javax.servlet.http.HttpServletRequest;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import com.alibaba.fastjson.JSONObject;
  9. /**
  10. * 服务请求.
  11. * @author huzq
  12. * @author xjt
  13. */
  14. public class ServiceRequest {
  15. /**
  16. * 头参数:缓存的上次请求时间
  17. */
  18. public static final String HEADER_LAST_MODIFIED_TIME = "last_modified_time";
  19. /**
  20. * POST参数:服务名
  21. */
  22. public static final String POST_PARAM_SERVICE = "SERVICE";
  23. /**
  24. * POST参数:头
  25. */
  26. public static final String POST_PARAM_HEADER = "HEADER";
  27. /**
  28. * POST参数:接口参数
  29. */
  30. public static final String POST_PARAM_PARAMS = "PARAMS";
  31. /** Servlet 请求对象. */
  32. private HttpServletRequest servletRequest;
  33. /**
  34. * 完整的服务名
  35. */
  36. private String fullServiceName;
  37. /** 服务名称. */
  38. private String service;
  39. /** 方法名称. */
  40. private String method;
  41. /** 头信息. */
  42. private JSONObject headers;
  43. /** 应用参数. */
  44. private JSONObject params;
  45. /** 是否有附件上传. */
  46. private boolean isMultiPart;
  47. /** 附件列表. */
  48. private Map<String,MultipartFile> fileMap;
  49. public ServiceRequest(){
  50. }
  51. public ServiceRequest(HttpServletRequest servletRequest, String fullServiceName, String service,
  52. String method, JSONObject headers, JSONObject params,
  53. boolean isMultiPart,Map<String,MultipartFile> fileMap) {
  54. super();
  55. this.servletRequest = servletRequest;
  56. this.fullServiceName = fullServiceName;
  57. this.service = service;
  58. this.method = method;
  59. this.headers = headers;
  60. this.params = params;
  61. this.isMultiPart = isMultiPart;
  62. this.fileMap = fileMap;
  63. }
  64. /**
  65. * Gets the servlet 请求对象.
  66. *
  67. * @return the servlet 请求对象
  68. */
  69. public HttpServletRequest getServletRequest() {
  70. return servletRequest;
  71. }
  72. /**
  73. * Gets the 服务名称.
  74. *
  75. * @return the 服务名称
  76. */
  77. public String getService() {
  78. return service;
  79. }
  80. /**
  81. * Gets the 方法名称.
  82. *
  83. * @return the 方法名称
  84. */
  85. public String getMethod() {
  86. return method;
  87. }
  88. /**
  89. * Gets the 头信息.
  90. *
  91. * @return the 头信息
  92. */
  93. public JSONObject getHeaders() {
  94. if(headers==null){
  95. return new JSONObject();
  96. }
  97. return headers;
  98. }
  99. /**
  100. * Gets the 应用参数.
  101. *
  102. * @return the 应用参数
  103. */
  104. public JSONObject getParams() {
  105. if(params==null){
  106. return new JSONObject();
  107. }
  108. return params;
  109. }
  110. /**
  111. * Checks if is 是否有附件上传.
  112. *
  113. * @return the 是否有附件上传
  114. */
  115. public boolean isMultiPart() {
  116. return isMultiPart;
  117. }
  118. /**
  119. * 获取客户端的缓存时间
  120. *
  121. * @return
  122. */
  123. public String getLastModifiedTime(){
  124. return getHeader(HEADER_LAST_MODIFIED_TIME);
  125. }
  126. /**
  127. * 根据关键字获取头信息
  128. * @param name
  129. * @return
  130. */
  131. public String getHeader(String name){
  132. if(this.headers!=null && this.headers.containsKey(name)){
  133. return this.headers.getString(name);
  134. }else{
  135. return null;
  136. }
  137. }
  138. /**
  139. * Gets the 完整的服务名.
  140. *
  141. * @return the 完整的服务名
  142. */
  143. public String getFullServiceName() {
  144. return fullServiceName;
  145. }
  146. public void setServletRequest(HttpServletRequest servletRequest) {
  147. this.servletRequest = servletRequest;
  148. }
  149. public void setFullServiceName(String fullServiceName) {
  150. this.fullServiceName = fullServiceName;
  151. }
  152. public void setService(String service) {
  153. this.service = service;
  154. }
  155. public void setMethod(String method) {
  156. this.method = method;
  157. }
  158. public void setHeaders(JSONObject headers) {
  159. this.headers = headers;
  160. }
  161. public void setParams(JSONObject params) {
  162. this.params = params;
  163. }
  164. public void setMultiPart(boolean isMultiPart) {
  165. this.isMultiPart = isMultiPart;
  166. }
  167. public RequestFile getFile(String param) {
  168. if(this.fileMap!=null){
  169. return new RequestFile(fileMap.get(param));
  170. }
  171. return null;
  172. }
  173. public List<RequestFile> getFiles(){
  174. List<RequestFile> files = new ArrayList<RequestFile>();
  175. if(this.fileMap!=null){
  176. Iterator<MultipartFile> it = fileMap.values().iterator();
  177. while(it.hasNext()){
  178. files.add(new RequestFile(it.next()));
  179. }
  180. }
  181. return files;
  182. }
  183. }