%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.jetty.JellyResourceHttpHandler |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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,v 1.3 2002/07/14 12:38:22 dion Exp $ |
|
44 | */ |
|
45 | 1 | class JellyResourceHttpHandler extends AbstractHttpHandler { |
46 | ||
47 | /** The Log to which logging calls will be made. */ |
|
48 | 1 | 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 | 3 | public JellyResourceHttpHandler( XMLOutput xmlOutput ) { |
61 | 3 | _tagMap = new HashMap(); |
62 | 3 | _xmlOutput = xmlOutput; |
63 | 3 | } |
64 | ||
65 | /* |
|
66 | * register this tag as the handler for the specified method |
|
67 | * |
|
68 | * @param tag the tag to be registered |
|
69 | * @param method the name of the http method which this tag processes |
|
70 | */ |
|
71 | public void registerTag(Tag tag, String method){ |
|
72 | 2 | _tagMap.put(method.toLowerCase(), tag); |
73 | 2 | } |
74 | ||
75 | /* |
|
76 | * handle an http request |
|
77 | * |
|
78 | * @param pathInContext the path of the http request |
|
79 | * @param pathParams the parameters (if any) of the http request |
|
80 | * @param request the actual http request |
|
81 | * @param response the place for any response |
|
82 | * |
|
83 | * @throws HttpException when an error occurs |
|
84 | * @throws IOException when an error occurs |
|
85 | */ |
|
86 | public void handle(String pathInContext, |
|
87 | String pathParams, |
|
88 | HttpRequest request, |
|
89 | HttpResponse response) |
|
90 | throws HttpException, IOException |
|
91 | { |
|
92 | 3 | Tag handlerTag = (Tag) _tagMap.get(request.getMethod().toLowerCase()); |
93 | 3 | if (null != handlerTag) { |
94 | // setup the parameters in the jelly context |
|
95 | 2 | JellyContext jellyContext = handlerTag.getContext(); |
96 | 2 | jellyContext.setVariable( "pathInContext", pathInContext); |
97 | 2 | jellyContext.setVariable( "pathParams", pathParams); |
98 | 2 | jellyContext.setVariable( "request", request); |
99 | 2 | jellyContext.setVariable( "requestBody", getRequestBody(request)); |
100 | 2 | jellyContext.setVariable( "response", response); |
101 | ||
102 | try { |
|
103 | 2 | handlerTag.invokeBody(_xmlOutput); |
104 | // only call set handled if tag has not requested an override |
|
105 | // if it has requested an override then reset the request |
|
106 | 2 | if (null == jellyContext.getVariable(OVERRIDE_SET_HANDLED_VAR)) { |
107 | 2 | request.setHandled(true); |
108 | 2 | response.commit(); |
109 | } else { |
|
110 | 0 | jellyContext.removeVariable(OVERRIDE_SET_HANDLED_VAR); |
111 | } |
|
112 | 2 | } catch (Exception ex ) { |
113 | 0 | throw new HttpException(HttpResponse.__500_Internal_Server_Error, |
114 | "Error invoking method handler tag: " + ex.getLocalizedMessage()); |
|
115 | 2 | } |
116 | } else { |
|
117 | 1 | log.info("No handler for request:" + |
118 | request.getMethod() + " path:" + |
|
119 | response.getHttpContext().getContextPath() + |
|
120 | pathInContext); |
|
121 | } |
|
122 | ||
123 | 3 | return; |
124 | } |
|
125 | ||
126 | public String getRequestBody(HttpRequest request) throws IOException { |
|
127 | ||
128 | // read the body as a string from the input stream |
|
129 | 2 | InputStream is = request.getInputStream(); |
130 | 2 | InputStreamReader isr = new InputStreamReader(is); |
131 | 2 | BufferedReader br = new BufferedReader(isr); |
132 | 2 | StringBuffer sb = new StringBuffer(); |
133 | 2 | char[] buffer = new class="keyword">char[1024]; |
134 | int len; |
|
135 | ||
136 | 5 | while ((len = isr.read(buffer, 0, 1024)) != -1) |
137 | 1 | sb.append(buffer, 0, len); |
138 | ||
139 | 2 | if (sb.length() > 0) |
140 | 1 | return sb.toString(); |
141 | else |
|
142 | 1 | return null; |
143 | ||
144 | } |
|
145 | } |
|
146 |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |