项目业务背景:该项目是XX公司为YY能源公司创建的企业内容管理(ECM,Enterprise Content Manager)解决方案。该项目分为两大部分,CM和BPM,其中CM创建并保存YY公司在生产及运营过程中产生的文档,而BPM则负责处理YY公司中的业务流程。同时,该项目还与YY公司现有的企业系统EAM进行集成,将EAM系统中的一些文档ECM进行存储。
public abstract class AbstractRestController { private static final int POST = 0; private static final int GET = 1; private static final int PUT = 2; //使用Log4j记录日志 protected static final Logger logger = Logger .getLogger(AbstractRestController.class); protected ThreadLocal<HttpServletRequest> request = new ThreadLocal<HttpServletRequest>(); protected String body; private BackLoginModule module = null; /** * 使用注解处理POST请求 */ @RequestMapping(method = RequestMethod.POST) public void handlePOSTRequest(@RequestBody String body, HttpServletRequest request, HttpServletResponse response) throws ServletException { this.body = body; process(request, response, POST); } /** * 使用注解处理GET请求 */ @RequestMapping(method = RequestMethod.GET) public void handleGetRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException { process(request, response, GET); } /** * 使用注解处理PUT请求 */ @RequestMapping(method = RequestMethod.PUT) public void handlePUTRequest(@RequestBody String body, HttpServletRequest request, HttpServletResponse response) throws ServletException { this.body = body; process(request, response, PUT); } private void process(HttpServletRequest request, HttpServletResponse response, int type) { this.request.set(request); //使用事务,发生错误时可以回滚 UserTransaction tx = null; try { tx = TransactionUtil.getUserTransaction(); tx.begin(); Object result = null; if(type == POST) result = processPost(); else if(type == PUT) result = processPut(); else if(type == GET) result = processGet(); writeResponse(result, response); tx.commit(); } catch (EcmException ex) { try { tx.rollback(); } catch (Exception e) { e.printStackTrace(); } logger.error("Error occurred when invoking remoting services.", ex); BizResponse br = new BizResponse(); br.setErrorCode(ex.getCode()); br.setErrorDescription(ex.getErrorDescrption()); Map<String, String> map = new HashMap<String, String>(); map.put(RestConstants.RESPONSE_BODY_CONTENT, body); br.setItem(map); writeResponse(br, response); } catch (Exception ex) { try { tx.rollback(); } catch (Exception e) { e.printStackTrace(); } logger.error("Error occurred when invoking remoting services.", ex); BizResponse br = new BizResponse(); br.setErrorCode(ErrorCode.UNKNOW_ERROR); br.setErrorDescription(ErrorMessageHelper .getErrMessage(ErrorCode.UNKNOW_ERROR)); Map<String, String> map = new HashMap<String, String>(); map.put(RestConstants.RESPONSE_BODY_CONTENT, body); br.setItem(map); writeResponse(br, response); } finally { this.request.set(null); if(module != null){ try { module.logout(); } catch (Exception e) { // TODO: handle exception } } // if (EcmConfig.useTomcat()) if (EcmConfig.useSSO == false) { UserContextUtils.popSubject(); } // SubjectUtil.clear(); ObjectStoreHelper.clear(); } } protected Object processPost() throws EcmException { return null; } protected Object processGet() throws EcmException { return null; } protected Object processPut() throws EcmException { return null; } protected void writeResponse(Object bmpResp, HttpServletResponse response) { String body = null; if (bmpResp instanceof String) { body = (String) bmpResp; } else { body = JsonUtil.toJson(bmpResp); } response.setContentType("application/json"); try { PrintWriter out = response.getWriter(); out.print(body); } catch (IOException ioe) { logger.error("Error occurred when setting HTTP response via JSON.", ioe); } } protected HttpServletRequest getRequest(){ return request.get(); } }
@Controller @RequestMapping("/borrowProcessUpdate.do") public class UpdateBorrowStatusController extends AbstractRestController { //此处使用PUT方式 @Override public Object processPut() throws EcmException { UpdateBorrowReq req = JsonUtil.fromJson(body, UpdateBorrowReq.class); BorrowDocument[] borrowDocumentList = new BorrowDocument[req.getBorrowDocumentList().length]; //...此处省去部分代码 boolean flag = IntegrateBorrowProcess.executeProcess(obj,borrowDocumentList); ResetUniversalResponse results = new ResetUniversalResponse(); results.setBsuccessed(flag); return results; }