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 java.io.IOException;
20  
21  import org.apache.commons.jelly.JellyTagException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  
25  import org.mortbay.http.HttpResponse;
26  import org.mortbay.util.ByteArrayISO8859Writer;
27  
28  /***
29   * Set the response body in a response handler for a Jetty http server
30   *
31   * @author  rtl
32   */
33  public class ResponseBodyTag extends TagSupport {
34  
35      /***
36       * Perform the tag functionality. In this case, set the body of a
37       * http response found in the jelly context
38       *
39       * @param xmlOutput where to send output
40       * @throws Exception when an error occurs
41       */
42      public void doTag(XMLOutput xmlOutput) throws JellyTagException {
43  
44          // get the response from the context
45          HttpResponse httpResponse = (HttpResponse) getContext().getVariable("response");
46          if (null == httpResponse) {
47              throw new JellyTagException("HttpResponse variable not available in Jelly context");
48          }
49  
50          ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
51          try {
52               writer.write(getBodyText());
53               writer.flush();
54               httpResponse.setContentLength(writer.size());
55               writer.writeTo(httpResponse.getOutputStream());
56          }
57          catch (IOException e) {
58              throw new JellyTagException(e);
59          }
60      }
61  }
62