KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > response > SendErrorTag


1 /*
2  * Copyright 1999,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.taglibs.response;
18
19 import java.io.*;
20 import java.lang.*;
21 import java.lang.reflect.*;
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25 import javax.servlet.jsp.*;
26 import javax.servlet.jsp.tagext.*;
27
28 /**
29  * JSP Tag <b>sendError</b>, used to send error as the HTTP Response.
30  * <p>
31  * The <b>sendError</b> tag can use an optional error message that it
32  * gets from the body of the tag.
33  * <p>
34  * The required attribute <b>error</b> must be set to an HTTP error
35  * code such as <b>SC_UNAUTHORIZED</b>.
36  * <p>
37  * If the optional attribute <b>reset</b> is set to <b>true</b> the
38  * buffer, headers, and status code will be reset.
39  * <p>
40  * JSP Tag Lib Descriptor
41  * <p><pre>
42  * &lt;name&gt;sendError&lt;/name&gt;
43  * &lt;tagclass&gt;org.apache.taglibs.response.SendErrorTag&lt;/tagclass&gt;
44  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
45  * &lt;info&gt;Return an HTTP error as the HTTP Response.&lt;/info&gt;
46  * &lt;attribute&gt;
47  * &lt;name&gt;error&lt;/name&gt;
48  * &lt;required&gt;true&lt;/required&gt;
49  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
50  * &lt;/attribute&gt;
51  * &lt;attribute&gt;
52  * &lt;name&gt;reset&lt;/name&gt;
53  * &lt;required&gt;false&lt;/required&gt;
54  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
55  * &lt;/attribute&gt;
56  * </pre>
57  *
58  * @author Glenn Nielsen
59  */

60
61 public class SendErrorTag extends BodyTagSupport
62 {
63     private String JavaDoc error;
64     private boolean reset = false;
65
66     /**
67      * Method called at start of tag, just returns EVAL_BODY_TAG
68      *
69      * @return EVAL_BODY_TAG
70      */

71     public final int doStartTag() throws JspException
72     {
73     return EVAL_BODY_TAG;
74     }
75
76     /**
77      * Read the body of the sendError tag to obtain the error message.
78      *
79      * @return SKIP_BODY
80      */

81     public final int doAfterBody() throws JspException
82     {
83         // Use the body of the tag as header value
84
int error_code = 0;
85         BodyContent body = getBodyContent();
86         String JavaDoc s = body.getString().trim();
87         // Clear the body since we only used it as input for the header
88
// value
89
body.clearBody();
90
91     if( reset )
92         pageContext.getResponse().reset();
93
94     try {
95             HttpServletResponse resp = null;
96             Field ec = null;
97             resp = (HttpServletResponse)pageContext.getResponse();
98             Class JavaDoc rc = resp.getClass();
99             ec = rc.getField(error);
100             error_code = ec.getInt(resp);
101         } catch(Exception JavaDoc e) {
102             throw new JspException(
103                 "Response sendError tag could not find error code: " + error);
104         }
105
106         try {
107         if( s.length() > 0 )
108             ((HttpServletResponse)pageContext.getResponse()).sendError(error_code,s);
109         else
110         ((HttpServletResponse)pageContext.getResponse()).sendError(error_code);
111     } catch(IOException e) {
112         throw new JspException(
113         "Response sendError tag failed: " + e.getMessage());
114     } catch(IllegalStateException JavaDoc e) {
115         throw new JspException(
116         "Response sendError tag failed " + e.getMessage());
117     }
118
119         return SKIP_BODY;
120     }
121
122     /**
123      * Method called at end of Tag used to send an error code,
124      * always returns SKIP_PAGE.
125      *
126      * @return SKIP_PAGE
127      */

128     public final int doEndTag() throws JspException
129     {
130     return SKIP_PAGE;
131     }
132
133     /**
134      * Required attribute that sets the error code to return.
135      *
136      * The error must be a text string for an error code as
137      * defined in java class HttpServletResponse. For example,
138      * <b>SC_UNATHORIZED</b> to return HTTP error code 401.
139      *
140      * @param String error code, i.e. <b>SC_UNATHORIZED</b>
141      */

142     public final void setError(String JavaDoc err)
143     {
144         error = err;
145     }
146
147     /**
148      * Optional attribute that flags whether the buffer, headers,
149      * and status code should be reset.
150      *
151      * @param boolean <b>true</b> or <b>false</b>
152      */

153     public final void setReset(boolean reset)
154     {
155         this.reset = reset;
156     }
157 }
158
Popular Tags