1 14 package org.wings.externalizer; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.io.Device; 19 import org.wings.util.StringUtil; 20 21 import javax.servlet.http.HttpServletResponse ; 22 import java.io.IOException ; 23 import java.util.*; 24 25 29 public abstract class AbstractExternalizeManager { 30 protected final transient static Log log = LogFactory.getLog(AbstractExternalizeManager.class); 31 32 36 public static final String NOT_FOUND_IDENTIFIER = "0"; 37 38 39 54 55 58 public final int UNIQUE_TIMESLICE = 20; 59 60 63 public final long FINAL_EXPIRES = 64 (StringUtil.MAX_RADIX * StringUtil.MAX_RADIX - 1) * UNIQUE_TIMESLICE; 65 66 69 protected final long PREFIX_TIMESLICE = 70 ((System.currentTimeMillis() / 1000) % FINAL_EXPIRES) / UNIQUE_TIMESLICE; 71 72 75 protected final String PREFIX_TIMESLICE_STRING = 76 StringUtil.toShortestAlphaNumericString(PREFIX_TIMESLICE, 2); 77 78 80 85 public static final int FINAL = 8; 86 87 92 public static final int REQUEST = 1; 93 94 101 public static final int SESSION = 2; 102 103 108 public static final int GLOBAL = 4; 109 110 113 private long counter = 0; 114 115 120 protected final Map reverseExternalized; 121 122 128 protected String sessionEncoding = ""; 129 130 131 public AbstractExternalizeManager() { 132 log.info("final scope expires in " + FINAL_EXPIRES + " seconds"); 133 log.info("use prefix " + PREFIX_TIMESLICE_STRING); 134 135 reverseExternalized = Collections.synchronizedMap(new HashMap()); 136 } 137 138 public void setResponse(HttpServletResponse response) { 139 if (response != null) { 140 sessionEncoding = response.encodeURL("foo").substring(3); 141 } 142 } 143 144 145 protected final synchronized long getNextIdentifier() { 146 return ++counter; 147 } 148 149 150 protected String getPrefix() { 151 return PREFIX_TIMESLICE_STRING; 152 } 153 154 155 protected final String createIdentifier() { 156 return getPrefix() + StringUtil.toShortestAlphaNumericString(getNextIdentifier()); 157 } 158 159 164 protected abstract void storeExternalizedResource(String identifier, 165 ExternalizedResource extInfo); 166 167 172 public abstract ExternalizedResource getExternalizedResource(String identifier); 173 174 177 public abstract void removeExternalizedResource(String identifier); 178 179 186 public String externalize(Object obj, Externalizer externalizer) { 187 return externalize(obj, externalizer, SESSION); 188 } 189 190 199 public String externalize(Object obj, Externalizer externalizer, Collection headers) { 200 return externalize(obj, externalizer, headers, SESSION); 201 } 202 203 216 public String externalize(Object obj, Externalizer externalizer, int flags) { 217 if (obj == null || externalizer == null) 218 throw new IllegalStateException ("no externalizer"); 219 220 return externalize(obj, externalizer, externalizer.getMimeType(obj), 221 null, flags); 222 } 223 224 239 public String externalize(Object obj, Externalizer externalizer, 240 Collection headers, int flags) { 241 if (obj == null || externalizer == null) 242 throw new IllegalStateException ("no externalizer"); 243 244 return externalize(obj, externalizer, externalizer.getMimeType(obj), 245 headers, flags); 246 } 247 248 258 public String externalize(Object obj, Externalizer externalizer, 259 String mimeType) { 260 return externalize(obj, externalizer, mimeType, null, SESSION); 261 } 262 263 273 public String externalize(Object obj, Externalizer externalizer, 274 String mimeType, Collection headers) { 275 return externalize(obj, externalizer, mimeType, headers, SESSION); 276 } 277 278 296 public String externalize(Object obj, Externalizer externalizer, 297 String mimeType, Collection headers, int flags) { 298 if (externalizer == null) { 299 throw new IllegalStateException ("no externalizer"); 300 } 301 ExternalizedResource extInfo = new ExternalizedResource(obj, externalizer, 302 mimeType, headers, flags); 303 304 if ((flags & GLOBAL) > 0) { 305 return SystemExternalizeManager.getSharedInstance().externalize(extInfo); 307 } else { 308 return externalize(extInfo); 309 } 310 } 311 312 313 319 public String externalize(ExternalizedResource extInfo) { 320 String identifier = (String ) reverseExternalized.get(extInfo); 321 322 if (identifier == null) { 323 identifier = createIdentifier(); 324 extInfo.setId(identifier); 325 326 storeExternalizedResource(identifier, extInfo); 327 reverseExternalized.put(extInfo, identifier); 328 } 329 String extension = extInfo.getExtension(); 330 if (extension != null) { 331 identifier += ("." + extension); 332 } 333 334 return identifier + sessionEncoding; 335 } 336 337 343 public String getId(String url) { 344 if (url == null || url.length() == 0) { 345 return url; 346 } 347 String result; 348 if (url.charAt(0) == '-') { 349 result = url; 350 } else { 351 result = url.substring(0, url.length() - sessionEncoding.length()); 352 } 353 return result; 354 } 355 356 357 362 public void deliver(String identifier, HttpServletResponse response, 363 Device out) throws IOException { 364 ExternalizedResource extInfo = getExternalizedResource(identifier); 365 366 if (extInfo == null) { 367 log.warn("identifier " + identifier + " not found"); 368 response.sendError(HttpServletResponse.SC_NOT_FOUND); 369 return; 370 } 371 deliver(extInfo, response, out); 372 } 373 374 public void deliver(ExternalizedResource extInfo, HttpServletResponse response, 375 Device out) 376 throws IOException { 377 382 383 if (extInfo.getMimeType() != null) { 384 response.setContentType(extInfo.getMimeType()); 385 } 386 387 if (out.isSizePreserving()) { 391 int resourceLen = extInfo 392 .getExternalizer().getLength(extInfo.getObject()); 393 if (resourceLen > 0) { 394 log.debug(extInfo.getMimeType() + ": " + resourceLen); 395 response.setContentLength(resourceLen); 396 } 397 } 398 399 Collection headers = extInfo.getHeaders(); 400 if (headers != null) { 401 for (Iterator it = headers.iterator(); it.hasNext();) { 402 Map.Entry entry = (Map.Entry) it.next(); 403 if (entry.getValue() instanceof String ) { 404 response.addHeader((String ) entry.getKey(), 405 (String ) entry.getValue()); 406 } else if (entry.getValue() instanceof Date) { 407 response.addDateHeader((String ) entry.getKey(), 408 ((Date) entry.getValue()).getTime()); 409 410 } else if (entry.getValue() instanceof Integer ) { 411 response.addIntHeader((String ) entry.getKey(), 412 ((Integer ) entry.getValue()).intValue()); 413 414 } } 416 } 417 418 if (!response.containsHeader("Expires")) { 419 426 response.setDateHeader("Expires", 432 System.currentTimeMillis() 433 + (1000 * FINAL_EXPIRES)); 434 } 435 436 extInfo.getExternalizer().write(extInfo.getObject(), out); 437 } 438 439 public void clear() { 440 reverseExternalized.clear(); 441 } 442 } 443 444 445 | Popular Tags |