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 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
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