1 16 17 package org.apache.jetspeed.services.webpage; 18 19 20 import java.io.IOException ; 22 import java.io.FileOutputStream ; 23 24 import java.util.StringTokenizer ; 26 import java.util.Date ; 27 import java.text.SimpleDateFormat ; 28 29 import java.net.InetAddress ; 31 import java.net.UnknownHostException ; 32 33 import javax.servlet.http.Cookie ; 35 36 37 43 public class WebPageHelper 44 { 45 46 public static final int CT_TEXT = 0; 47 public static final int CT_BINARY = 1; 48 public static final int CT_APPLICATION = 2; 49 public static final int CT_HTML = 3; 50 public static final int CT_IMAGE = 4; 51 public static final int CT_CSS = 5; 52 public static final int CT_JS = 6; 53 54 55 61 62 public static int getContentType(String typeString, String resource) 63 { 64 int contentType = CT_HTML; 65 66 if (null == typeString) { 67 68 if (null == resource) 69 { 70 return contentType; 71 } 72 73 if (resource.endsWith(".js")) 74 { 75 return CT_JS; 76 } 77 else if (resource.endsWith(".gif") || 78 resource.endsWith(".jpg") || 79 resource.endsWith(".png")) 80 { 81 return CT_IMAGE; 82 } 83 else if (resource.endsWith(".css") ) 84 { 85 return CT_CSS; 86 } 87 return contentType; 88 } 89 if (typeString.equalsIgnoreCase("text/html")) 90 contentType = CT_HTML; 91 else if (typeString.startsWith("image")) 92 contentType = CT_IMAGE; 93 else if (typeString.startsWith("text/css")) 94 contentType = CT_CSS; 95 else if (typeString.startsWith("text")) 96 contentType = CT_TEXT; 97 else if (typeString.startsWith("binary")) 98 contentType = CT_BINARY; 99 else if (typeString.equals("application/x-javascript") ) 100 contentType = CT_JS; 101 else if (typeString.startsWith("application")) 102 contentType = CT_APPLICATION; 103 104 return contentType; 105 } 106 107 108 115 public static String buildCookieString(Cookie cookie) 116 { 117 StringBuffer buffer = new StringBuffer (); 118 119 int version = cookie.getVersion(); 120 if (version != -1) { 121 buffer.append("$Version=\""); 122 buffer.append(cookie.getVersion()); 123 buffer.append("\"; "); 124 } 125 buffer.append(cookie.getName()); 127 buffer.append("="); 129 buffer.append(cookie.getValue()); 130 132 String path = cookie.getPath(); 134 if (path != null) { 135 buffer.append("; path="); 137 buffer.append(path); 138 } 140 141 String cookieHeader = buffer.toString(); 142 143 return cookieHeader; 144 } 145 146 154 155 public static boolean parseCookies(String cookieHeader, SiteSession session) 156 { 157 StringTokenizer st = new StringTokenizer (cookieHeader, " =;"); 158 String token, value; 159 boolean firstTime = true; Cookie cookie = null; 161 162 while (st.hasMoreTokens()) 163 { 164 token = st.nextToken(); 165 if (firstTime) { 166 value = st.nextToken(); 167 cookie = new Cookie (token, value); 168 cookie.setVersion(-1); 169 firstTime = false; 170 } 171 else if (token.equalsIgnoreCase("path")) { 172 cookie.setPath(st.nextToken()); 173 } 174 else if (token.equalsIgnoreCase("version")) { 175 cookie.setVersion(Integer.getInteger(st.nextToken()).intValue()); 176 } 177 else if (token.equalsIgnoreCase("max-age")) { 178 cookie.setMaxAge( Integer.getInteger(st.nextToken()).intValue() ); 179 } 180 else if (token.equalsIgnoreCase("domain")) { 181 cookie.setDomain( st.nextToken() ); 182 } 183 else if (token.equalsIgnoreCase("secure")) { 184 cookie.setSecure(true); 185 } 186 else 187 { 188 if (null != cookie) 189 session.addCookieToSession(cookie); 190 if (!st.hasMoreTokens()) { 191 break; 192 } 193 value = st.nextToken(); 194 cookie = new Cookie (token, value); 195 cookie.setVersion(-1); 196 } 197 } 198 if (null != cookie) 199 session.addCookieToSession(cookie); 200 201 return (null != cookie); } 203 204 212 public static StringBuffer replaceAll(StringBuffer buffer, 213 String find, 214 String replacement) 215 { 216 217 int bufidx = buffer.length() - 1; 218 int offset = find.length(); 219 while( bufidx > -1 ) { 220 int findidx = offset -1; 221 while( findidx > -1 ) { 222 if( bufidx == -1 ) { 223 return buffer; 225 } 226 if( buffer.charAt( bufidx ) == find.charAt( findidx ) ) { 227 findidx--; bufidx--; 229 } else { 230 findidx = offset - 1; bufidx--; 232 if( bufidx == -1 ) { 233 return buffer; 235 } 236 continue; 237 } 238 } 239 buffer.replace( bufidx+1, 240 bufidx+1+offset, 241 replacement); 242 } 244 return buffer; 246 247 } 248 249 258 public static String concatURLs(String base, String path) 259 { 260 String result = ""; 261 if (base.endsWith("/")) 262 { 263 if (path.startsWith("/")) 264 { 265 result = base.concat( path.substring(1)); 266 return result; 267 } 268 269 } 270 else 271 { 272 if (!path.startsWith("/")) 273 { 274 result = base.concat("/").concat(path); 275 return result; 276 } 277 } 278 return base.concat(path); 279 } 280 281 287 public static String getAvailabilityStatus(int status) 288 { 289 switch(status) 290 { 291 case 0: return "Not Initialized"; 292 case 1: return "Online"; 293 default: return "Offline"; 294 } 295 } 296 297 298 304 private static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss"; 305 private static final String CONTENT_LOG_HEADER = 306 "------------------------------------------------------" ; 307 308 public static void writeHeader(FileOutputStream fos, String resource) 309 throws IOException 310 { 311 fos.write(13); 312 fos.write(10); 313 fos.write(CONTENT_LOG_HEADER.getBytes()); 314 fos.write(13); 315 fos.write(10); 316 SimpleDateFormat sdf = new SimpleDateFormat (DATE_PATTERN); 317 fos.write(sdf.format(new Date ()).getBytes()); 318 fos.write(13); 319 fos.write(10); 320 fos.write(resource.getBytes()); 321 fos.write(13); 322 fos.write(10); 323 } 324 325 331 public static String getIP(String hostname) 332 { 333 String ip = null; 334 335 try 336 { 337 InetAddress computer = InetAddress.getByName(hostname); 338 ip = computer.getHostAddress(); 339 } 340 catch (UnknownHostException ex) 341 { 342 } 343 return ip; 344 } 345 346 private static int id = 0; 347 348 public static synchronized long generateId() 349 { 350 id = id + 1; 351 return id; 352 } 353 } 354 | Popular Tags |