org.ajax4jsf.framework.resource.UserResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ajax4jsf Show documentation
Show all versions of ajax4jsf Show documentation
Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.
The newest version!
/**
*
*/
package org.ajax4jsf.framework.resource;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Date;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
import org.ajax4jsf.ajax.resource.ResourceComponent;
/**
* @author shura
*
*/
public class UserResource extends InternetResourceBase {
private String contentType;
/**
*
*/
public UserResource(boolean cacheable, boolean session, String mime) {
super();
setCacheable(cacheable);
setSessionAware(session);
setContentType(mime);
}
/**
* @return Returns the contentType.
*/
public String getContentType() {
return contentType;
}
/**
* @param contentType The contentType to set.
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}
/* (non-Javadoc)
* @see org.ajax4jsf.framework.resource.InternetResourceBase#getDataToStore(javax.faces.context.FacesContext, java.lang.Object)
*/
public Object getDataToStore(FacesContext context, Object data) {
UriData dataToStore = null;
if (data instanceof ResourceComponent) {
ResourceComponent resource = (ResourceComponent) data;
dataToStore = new UriData();
dataToStore.value = resource.getValue();
dataToStore.createContent = UIComponentBase.saveAttachedState(context,resource.getCreateContent());
if (data instanceof UIComponent) {
UIComponent component = (UIComponent) data;
ValueBinding expires = component.getValueBinding("expires");
if (null != expires) {
dataToStore.expires = UIComponentBase.saveAttachedState(context,expires);
}
ValueBinding lastModified = component.getValueBinding("lastModified");
if (null != expires) {
dataToStore.modified = UIComponentBase.saveAttachedState(context,lastModified);
}
}
}
return dataToStore;
}
/* (non-Javadoc)
* @see org.ajax4jsf.framework.resource.InternetResourceBase#send(org.ajax4jsf.framework.resource.ResourceContext)
*/
public void send(ResourceContext context) throws IOException {
UriData data = (UriData) restoreData(context);
FacesContext facesContext = FacesContext.getCurrentInstance();
if (null != data && null != facesContext ) {
// Send headers
if(data.expires != null){
ValueBinding binding = (ValueBinding) UIComponentBase.restoreAttachedState(facesContext,data.expires);
Date expires = (Date) binding.getValue(facesContext);
context.setDateHeader("Expires",expires.getTime());
}
if(data.modified != null){
ValueBinding binding = (ValueBinding) UIComponentBase.restoreAttachedState(facesContext,data.modified);
Date modified = (Date) binding.getValue(facesContext);
context.setDateHeader("Last-Modified",modified.getTime());
}
// Send content
OutputStream out = context.getOutputStream();
MethodBinding send = (MethodBinding) UIComponentBase.restoreAttachedState(facesContext,data.createContent);
send.invoke(facesContext,new Object[]{out,data.value});
}
}
/* (non-Javadoc)
* @see org.ajax4jsf.framework.resource.InternetResourceBase#requireFacesContext()
*/
public boolean requireFacesContext() {
// TODO Auto-generated method stub
return true;
}
public static class UriData implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1258987L;
private Object value;
private Object createContent;
private Object expires;
private Object modified;
}
}