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.JellyTagException;
20  import org.apache.commons.jelly.TagSupport;
21  import org.apache.commons.jelly.XMLOutput;
22  
23  import org.mortbay.http.HttpResponse;
24  
25  /***
26   * Set the response code in the request handler of a Jetty http server
27   *
28   * @author  rtl
29   */
30  public class ResponseCodeTag extends TagSupport {
31  
32      /*** parameter value */
33      private int _value;
34  
35      /***
36       * Perform the tag functionality. In this case, set the response code in the
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          if (getValue() <= 100) {
45              throw new JellyTagException("<responseCode> tag must have a value of at least 100");
46          }
47  
48          // get the response from the context
49          HttpResponse httpResponse = (HttpResponse) getContext().getVariable("response");
50          if (null == httpResponse) {
51              throw new JellyTagException("HttpResponse variable not available in Jelly context");
52          }
53  
54          // set response code
55          httpResponse.setStatus(getValue());
56  
57      }
58  
59      //--------------------------------------------------------------------------
60      // Property accessors/mutators
61      //--------------------------------------------------------------------------
62  
63      /***
64       * Getter for property value.
65       *
66       * @return value of property value.
67       */
68      public int getValue() {
69          return _value;
70      }
71  
72      /***
73       * Setter for property value.
74       *
75       * @param value New value of property value.
76       */
77      public void setValue(int value) {
78          _value = value;
79      }
80  
81  }
82