1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| import static com.banksteel.openerp.commons.permission.PermissionContext.checkIsLegalReq; import static com.banksteel.openerp.commons.permission.PermissionContext.checkIsUrlReq;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.DispatcherServlet;
import com.alibaba.fastjson.JSONObject; import com.banksteel.openerp.commons.framework.entiy.RequestEntiy; import com.banksteel.openerp.commons.framework.entiy.RequestEntiyFace; import com.banksteel.openerp.commons.framework.entiy.ResponseEntity; import com.banksteel.openerp.commons.framework.exception.CommondNotFoundException; import com.banksteel.openerp.commons.permission.PermissionContext; import com.banksteel.openerp.commons.permission.PermissionRequest;
public class CustomDispatcherServlet extends DispatcherServlet { public static final String QUOT = """; private static final long serialVersionUID = -3983568685367864966L; public static final String REQUEST_COMMAND = "request_command"; private static Logger LOGGER = LoggerFactory.getLogger(CustomDispatcherServlet.class); private static final List<String> PARAMS = new ArrayList<String>();
static { PARAMS.add("announcementContent"); PARAMS.add("xmlFilePath"); }
@Override protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception { String requestUrl = request.getRequestURI(); String method = request.getMethod(); try{ request.setCharacterEncoding("UTF-8"); if (!checkIsUrlReq(PermissionContext.getApiPath(requestUrl) , method) && checkIsLegalReq(requestUrl)) { RequestEntiyFace requestEntiyface = null; RequestEntiy requestEntiy = getRequestEntiy(request); if (requestEntiy != null) { request.setAttribute(REQUEST_COMMAND, requestEntiy.getCommand()); String command = requestEntiy.getCommand(); LOGGER.info("请求命令:" + command); if (StringUtils.isBlank(command)){ throw new CommondNotFoundException(String.format("请求[%s,%s]不为url直接访问的请求,请求参数中不存在command",method,requestUrl)); }
if (!PermissionContext.containsCommand(command)){ throw new CommondNotFoundException("命令:"+command); } requestEntiyface = doRequestPath(requestEntiy); if (requestEntiyface != null) { request = new HttpResquestWrapper(request, requestEntiyface); } } } super.doService(request, response); }catch(Exception e) { LOGGER.error(String.format("请求[%s,%s]分发失败",method,requestUrl),e); ResponseEntity result=returnException("4001","请求分发失败"+","+e.getMessage(),""); response.setContentType("application/json; charset=UTF-8"); try { response.getWriter().write(JSONObject.toJSONString(result)); } catch (Exception e2) { throw new IOException(e2.getMessage()); } } }
protected RequestEntiyFace doRequestPath(RequestEntiy requestEntiy) { RequestEntiyFace requestEntiyface = new RequestEntiyFace(); String command = requestEntiy.getCommand();
PermissionRequest request = PermissionContext.getPermissionByCommand(command);
if (request.getRequestMethod() != null){ requestEntiyface.setMethod(request.getRequestMethod().toString()); }
requestEntiyface.setURI(request.getRequestPath()); Map<String, String> heads = new HashMap<String, String>(); heads.put("x-openerp-token", requestEntiy.getAccessToken()); requestEntiyface.setHeads(heads);
if (StringUtils.startsWith(requestEntiy.getData(),"{")){ JSONObject json = JSONObject.parseObject(requestEntiy.getData()); json = (JSONObject) objectFilter(json); requestEntiy.setData(json.toJSONString()); Map<String, String[]> params = new HashMap<String, String[]>(); for (String key : json.keySet()) { String[] param = new String[1]; param[0] = json.get(key) + ""; params.put(key, param); } requestEntiyface.setGetParam(params); } requestEntiyface.setBodyData(requestEntiy.getData()); return requestEntiyface; }
private RequestEntiy getRequestEntiy(HttpServletRequest request) throws Exception { String encode = "UTF-8"; InputStream inputStream = request.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] tmp = new byte[2048]; int i = inputStream.read(tmp); while (i > 0) { baos.write(tmp, 0, i); i = inputStream.read(tmp); } String value = new String(baos.toByteArray(), encode); if(value != null && !"".equals(value)){ RequestEntiy requestEntiy = JSONObject.parseObject(value, RequestEntiy.class); requestEntiy.setData(requestEntiy.getData()); return requestEntiy; } return null; }
public static String formatHtml(String needToFormat) { if (needToFormat != null && needToFormat.length() > 0) { needToFormat = needToFormat.replace("<script>", ""); needToFormat = needToFormat.replace("&", "&"); needToFormat = needToFormat.replace("\"", QUOT); needToFormat = needToFormat.replace("“", QUOT); needToFormat = needToFormat.replace("”", QUOT); needToFormat = needToFormat.replace("<", "<"); needToFormat = needToFormat.replace(">", ">"); needToFormat = needToFormat.replace("'", "'"); needToFormat = needToFormat.replace("\r\n", ""); return needToFormat; } return ""; }
private Object objectFilter(Object json) { Object result = null; if (json == null) { result = ""; } else if (json instanceof JSONObject) { JSONObject jsono = (JSONObject) json; Set<String> keys = jsono.keySet(); if (!keys.isEmpty()) { for (String k : keys) { if (PARAMS.indexOf(k) != -1) { continue; } jsono.put(k, objectFilter(jsono.get(k))); } } result = jsono; } else if (json instanceof String) { result = formatHtml(json.toString()); } else { result = json; } return result; } private ResponseEntity returnException(String code, String mess, Object data) { ResponseEntity responseEntity = new ResponseEntity(); responseEntity.setCode(Integer.parseInt(code)); Calendar calendar = Calendar.getInstance(Locale.CHINA); responseEntity.setTimeStamp(calendar.getTimeInMillis()); responseEntity.setResult("exception"); responseEntity.setMess(mess); responseEntity.setData(data); return responseEntity; }
}
|