org.wings.resource.ReloadResource Maven / Gradle / Ivy
/*
* $Id$
* Copyright 2000,2005 wingS development team.
*
* This file is part of wingS (http://wingsframework.org).
*
* wingS is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* Please see COPYING for the complete licence.
*/
package org.wings.resource;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wings.Resource;
import org.wings.SFrame;
import org.wings.io.Device;
/**
* Traverses the component hierarchy of a frame and lets the CGs compose
* the document.
*
* @author Holger Engels
* @version $Revision$
*/
public class ReloadResource extends DynamicResource {
private final static Logger log = LoggerFactory.getLogger(ReloadResource.class);
private static final ArrayList DEFAULT_CODE_HEADER = new ArrayList<>();
private final PropertyChangeListener changeListener;
static {
DEFAULT_CODE_HEADER.add(new Resource.HeaderEntry("Expires", new Date(1000)));
DEFAULT_CODE_HEADER.add(new Resource.HeaderEntry("Cache-Control", "no-store, no-cache, must-revalidate"));
DEFAULT_CODE_HEADER.add(new Resource.HeaderEntry("Cache-Control", "post-check=0, pre-check=0"));
DEFAULT_CODE_HEADER.add(new Resource.HeaderEntry("Pragma", "no-cache"));
}
/**
* Create a code resource for the specified frame.
* The MIME-type for this frame will be text/html; charset=current encoding
*/
public ReloadResource(final SFrame f) {
super(f, "html", provideMimeType(f));
// update session encoding if manually updated in the session.
changeListener = evt -> mimeType = provideMimeType(f);
f.getSession().addPropertyChangeListener(changeListener);
}
/**
* The MIME-type for this {@link Resource}.
*/
private static String provideMimeType(SFrame frame) {
return "text/html; charset=" + frame.getSession().getCharacterEncoding();
}
/**
* Renders and writes the code of the {@link SFrame} attached to this ReloadResource
.
*/
@Override
public void write(Device out) throws IOException {
try {
getFrame().write(out);
} catch (IOException e) {
throw e;
} catch (Exception e) {
log.error("resource: " + getId(), e);
throw new IOException(e.getMessage()); // UndeclaredThrowable
}
}
/**
* The HTTP header parameteres attached to this dynamic code ressource.
* This static list will by default contain entries to disable caching
* on the server side. Call getHeaders().clear()
to avoid this
* i.e. if you want to enable back buttons.
*
* @return A Collection
of {@link org.wings.Resource.HeaderEntry} objects.
*/
@Override
public Collection getHeaders() {
if (getFrame().isNoCaching())
return DEFAULT_CODE_HEADER;
else
return Collections.emptySet();
}
}