| 1 | |
package org.webslinger; |
| 2 | |
|
| 3 | |
import java.io.ByteArrayOutputStream; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.StringReader; |
| 6 | |
import java.io.StringWriter; |
| 7 | |
import java.util.HashMap; |
| 8 | |
import java.util.Map; |
| 9 | |
import javax.servlet.RequestDispatcher; |
| 10 | |
import javax.servlet.ServletException; |
| 11 | |
import javax.servlet.ServletRequest; |
| 12 | |
import javax.servlet.ServletResponse; |
| 13 | |
import javax.servlet.http.HttpServletRequest; |
| 14 | |
import javax.servlet.http.HttpServletResponse; |
| 15 | |
|
| 16 | |
import org.webslinger.collections.CollectionUtil; |
| 17 | |
import org.webslinger.io.IOUtil; |
| 18 | |
import org.webslinger.json.JSON; |
| 19 | |
import org.webslinger.json.JSONWriter; |
| 20 | |
|
| 21 | |
public final class WebslingerRequestDispatcher implements RequestDispatcher { |
| 22 | |
private final WebslingerServletContext sc; |
| 23 | |
private final PathContext pc; |
| 24 | |
private final Map<String, Object> context; |
| 25 | |
|
| 26 | 307 | protected WebslingerRequestDispatcher(WebslingerServletContext sc, PathContext pc, Map<String, Object> context) { |
| 27 | 307 | this.sc = sc; |
| 28 | 307 | this.pc = pc; |
| 29 | 307 | this.context = context; |
| 30 | 307 | } |
| 31 | |
|
| 32 | |
public PathContext getPathContext() { |
| 33 | 439 | return pc; |
| 34 | |
} |
| 35 | |
|
| 36 | |
public Map<String, Object> getContext() { |
| 37 | 0 | return context; |
| 38 | |
} |
| 39 | |
|
| 40 | |
public void forward(ServletRequest request, ServletResponse response) throws IOException, ServletException { |
| 41 | 283 | if (!"text/x-json".equals(request.getContentType())) { |
| 42 | 283 | run(request, response, "forward", null); |
| 43 | 283 | return; |
| 44 | |
} |
| 45 | 0 | if (!(request instanceof HttpServletRequest)) new Exception("dispatcher: not http request").printStackTrace(); |
| 46 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 47 | 0 | IOUtil.copy(request.getInputStream(), true, baos, true); |
| 48 | 0 | Map<String, Object> result = new HashMap<String, Object>(); |
| 49 | |
try { |
| 50 | 0 | Object value = new JSON(new StringReader(baos.toString())).allowResolve(true).JSONValue(); |
| 51 | 0 | if (!(value instanceof Map)) { |
| 52 | 0 | ((HttpServletResponse) response).sendError(500); |
| 53 | |
return; |
| 54 | |
} |
| 55 | 0 | StringWriter savedOutput = new StringWriter(); |
| 56 | 0 | HttpServletResponse tmpResponse = sc.alterPaths(pc, (HttpServletRequest) request, (HttpServletResponse) response, savedOutput); |
| 57 | 0 | Map<String, Object> inMap = (Map<String, Object>) value; |
| 58 | 0 | Map<String, Object> context = (Map<String, Object>) inMap.get("context"); |
| 59 | 0 | context.putAll(getContext()); |
| 60 | 0 | Object payload = inMap.get("payload"); |
| 61 | 0 | context.put("RequestPath", request.getAttribute("RequestPath")); |
| 62 | 0 | Object eventResult = sc.run(pc, sc.alterPaths(pc, (HttpServletRequest) request), (HttpServletResponse) tmpResponse, context, "json", payload); |
| 63 | 0 | if (eventResult != null) { |
| 64 | 0 | result.put("result", eventResult); |
| 65 | |
} else { |
| 66 | 0 | result.put("result", savedOutput.toString()); |
| 67 | |
} |
| 68 | 0 | } catch (Throwable t) { |
| 69 | 0 | result.put("error", CollectionUtil.toMap("type", "exception", "exc", t.getMessage())); |
| 70 | |
} finally { |
| 71 | 0 | result.put("hub", sc.getInvoker().getJSONHub()); |
| 72 | 0 | } |
| 73 | 0 | response.setContentType("text/x-json"); |
| 74 | 0 | new JSONWriter(response.getWriter()).write(result); |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
public void include(ServletRequest request, ServletResponse response) throws IOException, ServletException { |
| 78 | 0 | run(request, response, "include", null); |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
public void comet(CometEventType eventType, ServletRequest request, ServletResponse response) throws IOException, ServletException { |
| 82 | 0 | run(request, response, "comet", eventType); |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
protected void run(ServletRequest request, ServletResponse response, String command, Object payload) throws IOException, ServletException { |
| 86 | 283 | if (!(request instanceof HttpServletRequest)) new Exception("dispatcher: not http request").printStackTrace(); |
| 87 | |
|
| 88 | |
|
| 89 | 283 | response = sc.alterPaths(pc, (HttpServletRequest) request, (HttpServletResponse) response); |
| 90 | 283 | HashMap<String, Object> context = new HashMap<String, Object>(this.context); |
| 91 | 283 | context.put("RequestPath", request.getAttribute("RequestPath")); |
| 92 | 283 | sc.run(pc, sc.alterPaths(pc, (HttpServletRequest) request), (HttpServletResponse) response, context, command, payload); |
| 93 | 283 | } |
| 94 | |
} |