DynamicRequestServlet.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.yiidata.katta.ui.handle;
  2. import com.alibaba.fastjson.JSON;
  3. import com.yiidata.katta.ui.annaotion.Path;
  4. import org.apache.commons.lang.exception.ExceptionUtils;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletOutputStream;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. import java.io.OutputStreamWriter;
  12. import java.io.PrintWriter;
  13. import java.lang.reflect.Method;
  14. import java.nio.charset.Charset;
  15. import java.util.Collection;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. /**
  19. * <pre>
  20. *
  21. * Created by zhenqin.
  22. * User: zhenqin
  23. * Date: 17/12/13
  24. * Time: 17:51
  25. * Vendor: yiidata.com
  26. * To change this template use File | Settings | File Templates.
  27. *
  28. * </pre>
  29. *
  30. * @author zhenqin
  31. */
  32. public class DynamicRequestServlet extends HttpServlet {
  33. /**
  34. * Action Object
  35. */
  36. private final Object instance;
  37. /**
  38. * Action Method
  39. */
  40. private final Method method;
  41. /**
  42. * 当前方法上 Path
  43. */
  44. protected final Path pathAnno;
  45. /**
  46. * 输出的字符编码
  47. */
  48. protected Charset utf8 = Charset.forName("utf-8");
  49. public DynamicRequestServlet(Object instance, Method method, Path pathAnno) {
  50. this.instance = instance;
  51. this.method = method;
  52. this.pathAnno = pathAnno;
  53. }
  54. @Override
  55. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  56. doPost(req, resp);
  57. }
  58. @Override
  59. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  60. HashMap<String, Object> params = new HashMap<>();
  61. params.put("request", req);
  62. ServletOutputStream out = resp.getOutputStream();
  63. try {
  64. Object r = method.invoke(instance, params, req, resp);
  65. if(r != null) {
  66. if(r instanceof String) {
  67. resp.setCharacterEncoding(utf8.name());
  68. resp.setContentType(pathAnno.contentType() + "; charset=" + utf8);
  69. resp.setHeader("Content-type", pathAnno.contentType()+ ";charset=" + utf8);
  70. new FreemarkerView((String)r).out(new OutputStreamWriter(out, utf8), params);
  71. } else if((r instanceof Map) || (r instanceof Collection)) {
  72. resp.setCharacterEncoding("utf-8");
  73. resp.setContentType("application/json");
  74. resp.setHeader("Content-type", "application/json;charset=" + utf8);
  75. out.write(JSON.toJSONString(r).getBytes(utf8));
  76. } else {
  77. throw new IllegalArgumentException("unkown action to print " + r);
  78. }
  79. }
  80. } catch (Exception e) {
  81. resp.reset();
  82. resp.setCharacterEncoding(utf8.name());
  83. resp.setContentType("text/html; charset=" + utf8);
  84. resp.setHeader("Content-type", "text/html;charset=" + utf8);
  85. out.write(ExceptionUtils.getFullStackTrace(e).getBytes(utf8));
  86. throw new ServletException(e);
  87. } finally {
  88. out.flush();
  89. }
  90. }
  91. }