1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jelly.tags.jetty;
18
19 import org.apache.commons.jelly.JellyContext;
20 import org.apache.commons.jelly.Tag;
21 import org.apache.commons.jelly.XMLOutput;
22
23 import org.mortbay.http.HttpException;
24 import org.mortbay.http.HttpRequest;
25 import org.mortbay.http.HttpResponse;
26 import org.mortbay.http.handler.AbstractHttpHandler;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.lang.StringBuffer;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 /***
40 * The actual http handler implementation for an http context in an http server
41 *
42 * @author rtl
43 * @version $Id: JellyResourceHttpHandler.java 155420 2005-02-26 13:06:03Z dirkv $
44 */
45 class JellyResourceHttpHandler extends AbstractHttpHandler {
46
47 /*** The Log to which logging calls will be made. */
48 private static final Log log = LogFactory.getLog(JellyResourceHttpHandler.class);
49
50 /*** The name of the var to check if setHandled should not be set to true . */
51 private static final String OVERRIDE_SET_HANDLED_VAR = "overrideSetHandled";
52
53 /*** The list of tags registered to handle a request method */
54 private Map _tagMap;
55
56 /*** The place where to output the results of the tag body */
57 private XMLOutput _xmlOutput;
58
59 /*** Creates a new instance of JellyResourceHttpHandler */
60 public JellyResourceHttpHandler( XMLOutput xmlOutput ) {
61 _tagMap = new HashMap();
62 _xmlOutput = xmlOutput;
63 }
64
65
66
67
68
69
70
71 public void registerTag(Tag tag, String method){
72 _tagMap.put(method.toLowerCase(), tag);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86 public void handle(String pathInContext,
87 String pathParams,
88 HttpRequest request,
89 HttpResponse response)
90 throws HttpException, IOException
91 {
92 Tag handlerTag = (Tag) _tagMap.get(request.getMethod().toLowerCase());
93 if (null != handlerTag) {
94
95 JellyContext jellyContext = handlerTag.getContext();
96 jellyContext.setVariable( "pathInContext", pathInContext);
97 jellyContext.setVariable( "pathParams", pathParams);
98 jellyContext.setVariable( "request", request);
99 jellyContext.setVariable( "requestBody", getRequestBody(request));
100 jellyContext.setVariable( "response", response);
101
102 try {
103 handlerTag.invokeBody(_xmlOutput);
104
105
106 if (null == jellyContext.getVariable(OVERRIDE_SET_HANDLED_VAR)) {
107 request.setHandled(true);
108 response.commit();
109 } else {
110 jellyContext.removeVariable(OVERRIDE_SET_HANDLED_VAR);
111 }
112 } catch (Exception ex ) {
113 throw new HttpException(HttpResponse.__500_Internal_Server_Error,
114 "Error invoking method handler tag: " + ex.getLocalizedMessage());
115 }
116 } else {
117 log.info("No handler for request:" +
118 request.getMethod() + " path:" +
119 response.getHttpContext().getContextPath() +
120 pathInContext);
121 }
122
123 return;
124 }
125
126 public String getRequestBody(HttpRequest request) throws IOException {
127
128
129 InputStream is = request.getInputStream();
130 InputStreamReader isr = new InputStreamReader(is);
131 BufferedReader br = new BufferedReader(isr);
132 StringBuffer sb = new StringBuffer();
133 char[] buffer = new char[1024];
134 int len;
135
136 while ((len = isr.read(buffer, 0, 1024)) != -1)
137 sb.append(buffer, 0, len);
138
139 if (sb.length() > 0)
140 return sb.toString();
141 else
142 return null;
143
144 }
145 }
146