1 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 45 46 public class ResponseTag extends MapTag 47 { 48 public static final String COOKIE = "cookie"; 49 public static final String HEADER = "header"; 50 public static final String ERROR = "error"; 51 public static final String ERRORMESSAGE = "errorMessage"; 52 public static final String REDIRECT = "redirect"; 53 54 protected void init() 55 { 56 super.init(); 57 58 _export = null; 59 } 60 61 protected Cookie makeCookie(String name, Object object) throws Exception 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 processValue(Object tagValue) throws Exception 89 { 90 HttpServletResponse response = (HttpServletResponse)pageContext.getResponse(); 91 92 Map map = _map; 93 94 96 Object 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 cookieName = entry.getKey(); 107 Object 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 121 Object 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 headerKey = (String )entry.getKey(); 132 Object headerValue = entry.getValue(); 133 134 if (headerKey != null) 135 { 136 if (headerValue instanceof String ) 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 element = listIterator.next(); 151 152 response.addHeader(headerKey, element.toString()); 153 } 154 } 155 } 156 } 157 } 158 } 159 160 162 String error = (String )map.get(ERROR); 163 String errorMessage = (String )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 181 String redirect = (String )map.get(REDIRECT); 182 183 if (redirect != null) 184 { 185 response.sendRedirect(redirect); 186 } 187 188 return tagValue; 189 } 190 } 191 | Popular Tags |