1 17 18 19 package org.apache.jasper.tagplugins.jstl; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.io.UnsupportedEncodingException ; 26 import java.util.Locale ; 27 28 import javax.servlet.ServletOutputStream ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpServletResponseWrapper ; 32 import javax.servlet.jsp.JspException ; 33 import javax.servlet.jsp.JspTagException ; 34 import javax.servlet.jsp.PageContext ; 35 36 40 41 public class Util { 42 43 public static final String VALID_SCHEME_CHAR = 44 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+.-"; 45 46 public static final String DEFAULT_ENCODING = 47 "ISO-8859-1"; 48 49 public static final int HIGHEST_SPECIAL = '>'; 50 51 public static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][]; 52 53 static { 54 specialCharactersRepresentation['&'] = "&".toCharArray(); 55 specialCharactersRepresentation['<'] = "<".toCharArray(); 56 specialCharactersRepresentation['>'] = ">".toCharArray(); 57 specialCharactersRepresentation['"'] = """.toCharArray(); 58 specialCharactersRepresentation['\''] = "'".toCharArray(); 59 } 60 61 74 public static int getScope(String scope){ 75 int ret = PageContext.PAGE_SCOPE; 76 77 if("request".equalsIgnoreCase(scope)){ 78 ret = PageContext.REQUEST_SCOPE; 79 }else if("session".equalsIgnoreCase(scope)){ 80 ret = PageContext.SESSION_SCOPE; 81 }else if("application".equalsIgnoreCase(scope)){ 82 ret = PageContext.APPLICATION_SCOPE; 83 } 84 85 return ret; 86 } 87 88 93 public static boolean isAbsoluteUrl(String url){ 94 if(url == null){ 95 return false; 96 } 97 98 int colonPos = url.indexOf(":"); 99 if(colonPos == -1){ 100 return false; 101 } 102 103 for(int i=0;i<colonPos;i++){ 104 if(VALID_SCHEME_CHAR.indexOf(url.charAt(i)) == -1){ 105 return false; 106 } 107 } 108 109 return true; 110 } 111 112 117 public static String getContentTypeAttribute(String input, String name) { 118 int begin; 119 int end; 120 int index = input.toUpperCase().indexOf(name.toUpperCase()); 121 if (index == -1) return null; 122 index = index + name.length(); index = input.indexOf('=', index); if (index == -1) return null; 125 index += 1; input = input.substring(index).trim(); 127 128 if (input.charAt(0) == '"') { 129 begin = 1; 131 end = input.indexOf('"', begin); 132 if (end == -1) return null; 133 } else { 134 begin = 0; 135 end = input.indexOf(';'); 136 if (end == -1) end = input.indexOf(' '); 137 if (end == -1) end = input.length(); 138 } 139 return input.substring(begin, end).trim(); 140 } 141 142 150 public static String stripSession(String url) { 151 StringBuffer u = new StringBuffer (url); 152 int sessionStart; 153 while ((sessionStart = u.toString().indexOf(";jsessionid=")) != -1) { 154 int sessionEnd = u.toString().indexOf(";", sessionStart + 1); 155 if (sessionEnd == -1) 156 sessionEnd = u.toString().indexOf("?", sessionStart + 1); 157 if (sessionEnd == -1) sessionEnd = u.length(); 159 u.delete(sessionStart, sessionEnd); 160 } 161 return u.toString(); 162 } 163 164 165 179 public static String escapeXml(String buffer) { 180 int start = 0; 181 int length = buffer.length(); 182 char[] arrayBuffer = buffer.toCharArray(); 183 StringBuffer escapedBuffer = null; 184 185 for (int i = 0; i < length; i++) { 186 char c = arrayBuffer[i]; 187 if (c <= HIGHEST_SPECIAL) { 188 char[] escaped = specialCharactersRepresentation[c]; 189 if (escaped != null) { 190 if (start == 0) { 192 escapedBuffer = new StringBuffer (length + 5); 193 } 194 if (start < i) { 196 escapedBuffer.append(arrayBuffer,start,i-start); 197 } 198 start = i + 1; 199 escapedBuffer.append(escaped); 201 } 202 } 203 } 204 if (start == 0) { 206 return buffer; 207 } 208 if (start < length) { 210 escapedBuffer.append(arrayBuffer,start,length-start); 211 } 212 return escapedBuffer.toString(); 213 } 214 215 218 public static String resolveUrl( 219 String url, String context, PageContext pageContext) 220 throws JspException { 221 if (isAbsoluteUrl(url)) 223 return url; 224 225 HttpServletRequest request = 227 (HttpServletRequest ) pageContext.getRequest(); 228 if (context == null) { 229 if (url.startsWith("/")) 230 return (request.getContextPath() + url); 231 else 232 return url; 233 } else { 234 if (!context.startsWith("/") || !url.startsWith("/")) { 235 throw new JspTagException ( 236 "In URL tags, when the \"context\" attribute is specified, values of both \"context\" and \"url\" must start with \"/\"."); 237 } 238 if (context.equals("/")) { 239 return url; 243 } else { 244 return (context + url); 245 } 246 } 247 } 248 249 252 public static class ImportResponseWrapper extends HttpServletResponseWrapper { 253 254 private StringWriter sw = new StringWriter (); 255 private ByteArrayOutputStream bos = new ByteArrayOutputStream (); 256 private ServletOutputStream sos = new ServletOutputStream () { 257 public void write(int b) throws IOException { 258 bos.write(b); 259 } 260 }; 261 private boolean isWriterUsed; 262 private boolean isStreamUsed; 263 private int status = 200; 264 private String charEncoding; 265 266 public ImportResponseWrapper(HttpServletResponse arg0) { 267 super(arg0); 268 } 270 271 public PrintWriter getWriter() { 272 if (isStreamUsed) 273 throw new IllegalStateException ("Unexpected internal error during <import>: " + 274 "Target servlet called getWriter(), then getOutputStream()"); 275 isWriterUsed = true; 276 return new PrintWriter (sw); 277 } 278 279 public ServletOutputStream getOutputStream() { 280 if (isWriterUsed) 281 throw new IllegalStateException ("Unexpected internal error during <import>: " + 282 "Target servlet called getOutputStream(), then getWriter()"); 283 isStreamUsed = true; 284 return sos; 285 } 286 287 288 public void setContentType(String x) { 289 } 291 292 293 public void setLocale(Locale x) { 294 } 296 297 public void setStatus(int status) { 298 this.status = status; 299 } 300 301 public int getStatus() { 302 return status; 303 } 304 305 public String getCharEncoding(){ 306 return this.charEncoding; 307 } 308 309 public void setCharEncoding(String ce){ 310 this.charEncoding = ce; 311 } 312 313 public String getString() throws UnsupportedEncodingException { 314 if (isWriterUsed) 315 return sw.toString(); 316 else if (isStreamUsed) { 317 if (this.charEncoding != null && !this.charEncoding.equals("")) 318 return bos.toString(charEncoding); 319 else 320 return bos.toString("ISO-8859-1"); 321 } else 322 return ""; } 324 } 325 326 } 327 | Popular Tags |