package dk.valtech.octools.util;
import javax.servlet.http.HttpServletRequest;
import org.opencms.file.CmsObject;
import org.opencms.main.A_CmsAuthorizationHandler;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.security.I_CmsAuthorizationHandler;
/**
* An implementation of {@link CmsObjectProvider} that supports returning the
* users CmsObject even if the request is not a cms request (i.e. some servlet
* other than cms servlet).<br />
* However, this is only possible if a CmsObject is associated with the session
* of the given servlet request.
*
* @author Stefan.grinsted
*
*/
public class SessionCmsObjectProvider extends A_CmsAuthorizationHandler implements CmsObjectProvider {
private boolean defaultToGuest = true;
/* (non-Javadoc)
* @see dk.valtech.octools.util.CmsObjectProvider#getCmsObject(javax.servlet.http.HttpServletRequest)
*/
public CmsObject getCmsObject(HttpServletRequest request) throws CmsException {
CmsObject cms = initCmsObjectFromSession(request);
if (cms == null) {
cms = initCmsObject(request);
}
if (cms == null && defaultToGuest) {
cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
}
return cms;
}
/* (non-Javadoc)
* @see org.opencms.security.I_CmsAuthorizationHandler#initCmsObject(javax.servlet.http.HttpServletRequest)
*/
public CmsObject initCmsObject(HttpServletRequest request) {
I_CmsAuthorizationHandler auth = OpenCms.getAuthorizationHandler();
CmsObject cms = null;
if (auth != this) {
cms = auth.initCmsObject(request);
}
return cms;
}
/* (non-Javadoc)
* @see org.opencms.security.I_CmsAuthorizationHandler#initCmsObject(javax.servlet.http.HttpServletRequest, java.lang.String, java.lang.String)
*/
public CmsObject initCmsObject(HttpServletRequest request, String userName, String pwd) throws CmsException {
I_CmsAuthorizationHandler auth = OpenCms.getAuthorizationHandler();
CmsObject cms = null;
if (auth != this) {
cms = auth.initCmsObject(request, userName, pwd);
}
return cms;
}
public boolean isDefaultToGuest() {
return defaultToGuest;
}
public void setDefaultToGuest(boolean defaultToGuest) {
this.defaultToGuest = defaultToGuest;
}
}