001: /*
002: * $Id: ReloadResource.java 3423 2007-07-05 09:47:56Z hengels $
003: * Copyright 2000,2005 wingS development team.
004: *
005: * This file is part of wingS (http://wingsframework.org).
006: *
007: * wingS is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.wings.resource;
015:
016: import java.beans.PropertyChangeEvent;
017: import java.beans.PropertyChangeListener;
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.Date;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.wings.Resource;
027: import org.wings.SFrame;
028: import org.wings.io.Device;
029:
030: /**
031: * Traverses the component hierarchy of a frame and lets the CGs compose
032: * the document.
033: *
034: * @author Holger Engels
035: * @version $Revision: 3423 $
036: */
037: public class ReloadResource extends DynamicResource {
038: private final static Log log = LogFactory
039: .getLog(ReloadResource.class);
040:
041: private static final ArrayList<HttpHeader> DEFAULT_CODE_HEADER = new ArrayList<HttpHeader>();
042: private final PropertyChangeListener changeListener;
043:
044: static {
045: DEFAULT_CODE_HEADER.add(new Resource.HeaderEntry("Expires",
046: new Date(1000)));
047: DEFAULT_CODE_HEADER
048: .add(new Resource.HeaderEntry("Cache-Control",
049: "no-store, no-cache, must-revalidate"));
050: DEFAULT_CODE_HEADER.add(new Resource.HeaderEntry(
051: "Cache-Control", "post-check=0, pre-check=0"));
052: DEFAULT_CODE_HEADER.add(new Resource.HeaderEntry("Pragma",
053: "no-cache"));
054: }
055:
056: /**
057: * Create a code resource for the specified frame.
058: * <p>The MIME-type for this frame will be <code>text/html; charset=<i>current encoding</i></code>
059: */
060: public ReloadResource(final SFrame f) {
061: super (f, "html", provideMimeType(f));
062: // update session encoding if manually updated in the session.
063: changeListener = new PropertyChangeListener() {
064: public void propertyChange(PropertyChangeEvent evt) {
065: mimeType = provideMimeType(f);
066: }
067: };
068: f.getSession().addPropertyChangeListener(changeListener);
069: }
070:
071: /**
072: * The MIME-type for this {@link Resource}.
073: */
074: private static String provideMimeType(SFrame frame) {
075: return "text/html; charset="
076: + frame.getSession().getCharacterEncoding();
077: }
078:
079: /**
080: * Renders and writes the code of the {@link SFrame} attached to this <code>ReloadResource</code>.
081: */
082: public void write(Device out) throws IOException {
083: try {
084: getFrame().write(out);
085: } catch (IOException e) {
086: throw e;
087: } catch (Exception e) {
088: log.fatal("resource: " + getId(), e);
089: throw new IOException(e.getMessage()); // UndeclaredThrowable
090: }
091: }
092:
093: /**
094: * The HTTP header parameteres attached to this dynamic code ressource.
095: * This <b>static</b> list will by default contain entries to disable caching
096: * on the server side. Call <code>getHeaders().clear()</code> to avoid this
097: * i.e. if you want to enable back buttons.
098: *
099: * @return A <code>Collection</code> of {@link org.wings.Resource.HeaderEntry} objects.
100: */
101: public Collection<HttpHeader> getHeaders() {
102: if (getFrame().isNoCaching())
103: return DEFAULT_CODE_HEADER;
104: else
105: return Collections.EMPTY_SET;
106: }
107: }
|