1 17 package org.alfresco.web.app.servlet; 18 19 import java.io.IOException ; 20 import java.io.UnsupportedEncodingException ; 21 import java.net.URLDecoder ; 22 import java.util.ArrayList ; 23 import java.util.HashSet ; 24 import java.util.List ; 25 import java.util.Set ; 26 27 import javax.faces.context.FacesContext; 28 import javax.servlet.ServletContext ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import org.alfresco.service.ServiceRegistry; 34 import org.alfresco.service.cmr.model.FileFolderService; 35 import org.alfresco.service.cmr.model.FileInfo; 36 import org.alfresco.service.cmr.model.FileNotFoundException; 37 import org.alfresco.service.cmr.repository.NodeRef; 38 import org.alfresco.web.app.Application; 39 import org.alfresco.web.bean.LoginBean; 40 import org.alfresco.web.bean.repository.Repository; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 import org.springframework.web.context.WebApplicationContext; 44 import org.springframework.web.context.support.WebApplicationContextUtils; 45 import org.springframework.web.jsf.FacesContextUtils; 46 47 52 public abstract class BaseServlet extends HttpServlet 53 { 54 public static final String FACES_SERVLET = "/faces"; 55 56 57 private static final String ARG_TICKET = "ticket"; 58 59 60 private static final String ARG_GUEST = "guest"; 61 62 63 private static Set <String > validRedirectJSPs = new HashSet <String >(); 65 static 66 { 67 validRedirectJSPs.add("/jsp/browse/browse.jsp"); 68 validRedirectJSPs.add("/jsp/browse/dashboard.jsp"); 69 validRedirectJSPs.add("/jsp/admin/admin-console.jsp"); 70 validRedirectJSPs.add("/jsp/admin/node-browser.jsp"); 71 validRedirectJSPs.add("/jsp/admin/store-browser.jsp"); 72 validRedirectJSPs.add("/jsp/categories/categories.jsp"); 73 validRedirectJSPs.add("/jsp/dialog/about.jsp"); 74 validRedirectJSPs.add("/jsp/dialog/advanced-search.jsp"); 75 validRedirectJSPs.add("/jsp/dialog/system-info.jsp"); 76 validRedirectJSPs.add("/jsp/forums/forums.jsp"); 77 validRedirectJSPs.add("/jsp/users/users.jsp"); 78 } 79 80 private static Log logger = LogFactory.getLog(BaseServlet.class); 81 82 83 90 public static ServiceRegistry getServiceRegistry(ServletContext sc) 91 { 92 WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); 93 return (ServiceRegistry)wc.getBean(ServiceRegistry.SERVICE_REGISTRY); 94 } 95 96 104 public AuthenticationStatus servletAuthenticate(HttpServletRequest req, HttpServletResponse res) 105 throws IOException 106 { 107 AuthenticationStatus status; 108 109 String ticket = req.getParameter(ARG_TICKET); 111 if (ticket != null && ticket.length() != 0) 112 { 113 status = AuthenticationHelper.authenticate(getServletContext(), req, res, ticket); 114 } 115 else 116 { 117 boolean forceGuest = false; 118 String guest = req.getParameter(ARG_GUEST); 119 if (guest != null) 120 { 121 forceGuest = Boolean.parseBoolean(guest); 122 } 123 status = AuthenticationHelper.authenticate(getServletContext(), req, res, forceGuest); 124 } 125 if (status == AuthenticationStatus.Failure) 126 { 127 redirectToLoginPage(req, res, getServletContext()); 129 } 130 131 return status; 132 } 133 134 138 public static void redirectToLoginPage(HttpServletRequest req, HttpServletResponse res, ServletContext sc) 139 throws IOException 140 { 141 res.sendRedirect(req.getContextPath() + FACES_SERVLET + Application.getLoginPage(sc)); 144 String uri = req.getRequestURI(); 145 if (uri.indexOf(req.getContextPath() + FACES_SERVLET) != -1) 146 { 147 int jspIndex = uri.indexOf(BaseServlet.FACES_SERVLET) + BaseServlet.FACES_SERVLET.length(); 150 if (uri.length() > jspIndex && BaseServlet.validRedirectJSP(uri.substring(jspIndex))) 151 { 152 req.getSession().setAttribute(LoginBean.LOGIN_REDIRECT_KEY, uri); 153 } 154 } 155 else 156 { 157 req.getSession().setAttribute(LoginBean.LOGIN_REDIRECT_KEY, uri); 158 } 159 } 160 161 172 public static boolean validRedirectJSP(String jsp) 173 { 174 return validRedirectJSPs.contains(jsp); 175 } 176 177 183 public static NodeRef resolveWebDAVPath(FacesContext context, String [] args) 184 { 185 NodeRef nodeRef = null; 186 187 List <String > paths = new ArrayList <String >(args.length-1); 188 189 FileInfo file = null; 190 try 191 { 192 for (int x = 1; x < args.length; x++) 194 { 195 paths.add(URLDecoder.decode(args[x], "UTF-8")); 196 } 197 198 if (logger.isDebugEnabled()) 199 logger.debug("Attempting to resolve webdav path: " + paths); 200 201 NodeRef companyHome = new NodeRef(Repository.getStoreRef(), 203 Application.getCompanyRootId()); 204 205 WebApplicationContext wc = FacesContextUtils.getRequiredWebApplicationContext(context); 206 FileFolderService ffs = (FileFolderService)wc.getBean("FileFolderService"); 207 file = ffs.resolveNamePath(companyHome, paths); 208 nodeRef = file.getNodeRef(); 209 210 if (logger.isDebugEnabled()) 211 logger.debug("Resolved webdav path to NodeRef: " + nodeRef); 212 } 213 catch (UnsupportedEncodingException uee) 214 { 215 if (logger.isWarnEnabled()) 216 logger.warn("Failed to resolve webdav path", uee); 217 218 nodeRef = null; 219 } 220 catch (FileNotFoundException fne) 221 { 222 if (logger.isWarnEnabled()) 223 logger.warn("Failed to resolve webdav path", fne); 224 225 nodeRef = null; 226 } 227 228 return nodeRef; 229 } 230 } 231 | Popular Tags |