View Javadoc

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 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       * 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          _tagMap.put(method.toLowerCase(), tag);
73      }
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          Tag handlerTag = (Tag) _tagMap.get(request.getMethod().toLowerCase());
93          if (null != handlerTag) {
94              // setup the parameters in the jelly context
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                 // only call set handled if tag has not requested an override
105                 // if it has requested an override then reset the request
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         // read the body as a string from the input stream
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