KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > ResponseTag


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.jsp.tag;
37
38 import javax.servlet.*;
39 import javax.servlet.http.*;
40 import java.util.*;
41
42 import com.micronova.util.*;
43
44 /** controls HTTP response */
45
46 public class ResponseTag extends MapTag
47 {
48     public static final String JavaDoc COOKIE = "cookie";
49     public static final String JavaDoc HEADER = "header";
50     public static final String JavaDoc ERROR = "error";
51     public static final String JavaDoc ERRORMESSAGE = "errorMessage";
52     public static final String JavaDoc REDIRECT = "redirect";
53
54     protected void init()
55     {
56         super.init();
57
58         _export = null;
59     }
60
61     protected Cookie makeCookie(String JavaDoc name, Object JavaDoc object) throws Exception JavaDoc
62     {
63         if (object instanceof Cookie)
64         {
65             return (Cookie)object;
66         }
67         else if (object != null)
68         {
69             Cookie cookie = new Cookie(name, null);
70
71             if (object instanceof Map)
72             {
73                 BeanUtil.fill(cookie, (Map)object);
74             }
75             else
76             {
77                 cookie.setValue(object.toString());
78             }
79
80             return cookie;
81         }
82         else
83         {
84             return null;
85         }
86     }
87
88     protected Object JavaDoc processValue(Object JavaDoc tagValue) throws Exception JavaDoc
89     {
90         HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
91
92         Map map = _map;
93
94         // process cookies
95

96         Object JavaDoc cookieMap = map.get(COOKIE);
97
98         if (cookieMap instanceof Map)
99         {
100             Iterator iterator = ((Map)cookieMap).entrySet().iterator();
101
102             while (iterator.hasNext())
103             {
104                 Map.Entry entry = (Map.Entry)iterator.next();
105
106                 Object JavaDoc cookieName = entry.getKey();
107                 Object JavaDoc cookieValue = entry.getValue();
108
109
110                 if (cookieName!= null)
111                 {
112                     Cookie cookie = makeCookie(cookieName.toString(), cookieValue);
113                     
114                     response.addCookie(cookie);
115                 }
116             }
117         }
118
119         // process headers
120

121         Object JavaDoc headerMap = map.get(HEADER);
122
123         if (headerMap instanceof Map)
124         {
125             Iterator iterator = ((Map)headerMap).entrySet().iterator();
126
127             while (iterator.hasNext())
128             {
129                 Map.Entry entry = (Map.Entry)iterator.next();
130
131                 String JavaDoc headerKey = (String JavaDoc)entry.getKey();
132                 Object JavaDoc headerValue = entry.getValue();
133
134                 if (headerKey != null)
135                 {
136                     if (headerValue instanceof String JavaDoc)
137                     {
138                         response.addHeader(headerKey, headerValue.toString());
139                     }
140                     else
141                     {
142                         List list = (List)TypeUtil.isList(headerValue);
143
144                         if (list != null)
145                         {
146                             Iterator listIterator = list.iterator();
147                         
148                             while (listIterator.hasNext())
149                             {
150                                 Object JavaDoc element = listIterator.next();
151                                 
152                                 response.addHeader(headerKey, element.toString());
153                             }
154                         }
155                     }
156                 }
157             }
158         }
159
160         // process error
161

162         String JavaDoc error = (String JavaDoc)map.get(ERROR);
163         String JavaDoc errorMessage = (String JavaDoc)map.get(ERRORMESSAGE);
164
165         if (error != null)
166         {
167             int errorCode = Integer.parseInt(error);
168
169             if (errorMessage == null)
170             {
171                 response.sendError(errorCode);
172             }
173             else
174             {
175                 response.sendError(errorCode, errorMessage);
176             }
177         }
178
179         // process redirect
180

181         String JavaDoc redirect = (String JavaDoc)map.get(REDIRECT);
182
183         if (redirect != null)
184         {
185             response.sendRedirect(redirect);
186         }
187
188         return tagValue;
189     }
190 }
191
Popular Tags