1 17 package org.alfresco.web.app.portlet; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import javax.faces.application.ViewHandler; 25 import javax.faces.component.UIViewRoot; 26 import javax.faces.context.FacesContext; 27 import javax.portlet.ActionRequest; 28 import javax.portlet.ActionResponse; 29 import javax.portlet.PortletException; 30 import javax.portlet.PortletRequestDispatcher; 31 import javax.portlet.PortletSession; 32 import javax.portlet.RenderRequest; 33 import javax.portlet.RenderResponse; 34 import javax.portlet.UnavailableException; 35 36 import org.alfresco.i18n.I18NUtil; 37 import org.alfresco.repo.security.authentication.AuthenticationException; 38 import org.alfresco.service.cmr.security.AuthenticationService; 39 import org.alfresco.util.TempFileProvider; 40 import org.alfresco.web.app.Application; 41 import org.alfresco.web.app.servlet.AuthenticationHelper; 42 import org.alfresco.web.app.servlet.AuthenticationStatus; 43 import org.alfresco.web.bean.ErrorBean; 44 import org.alfresco.web.bean.FileUploadBean; 45 import org.alfresco.web.bean.LoginBean; 46 import org.alfresco.web.bean.repository.User; 47 import org.apache.commons.fileupload.FileItem; 48 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 49 import org.apache.commons.fileupload.portlet.PortletFileUpload; 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 import org.apache.myfaces.portlet.MyFacesGenericPortlet; 53 import org.apache.myfaces.portlet.PortletUtil; 54 import org.springframework.web.context.WebApplicationContext; 55 56 63 public class AlfrescoFacesPortlet extends MyFacesGenericPortlet 64 { 65 private static final String PREF_ALF_USERNAME = "_alfUserName"; 66 public static final String INSTANCE_NAME = "AlfrescoClientInstance"; 67 public static final String MANAGED_BEAN_PREFIX = "javax.portlet.p." + INSTANCE_NAME + "?"; 68 69 private static final String ERROR_PAGE_PARAM = "error-page"; 70 private static final String ERROR_OCCURRED = "error-occurred"; 71 72 private static Log logger = LogFactory.getLog(AlfrescoFacesPortlet.class); 73 74 private String loginPage = null; 75 private String errorPage = null; 76 77 78 81 public void processAction(ActionRequest request, ActionResponse response) 82 throws PortletException, IOException 83 { 84 Application.setInPortalServer(true); 85 86 I18NUtil.setLocale(Application.getLanguage(request.getPortletSession())); 88 89 boolean isMultipart = PortletFileUpload.isMultipartContent(request); 90 91 try 92 { 93 98 if (isMultipart) 99 { 100 if (logger.isDebugEnabled()) 101 logger.debug("Handling multipart request..."); 102 103 PortletSession session = request.getPortletSession(); 104 105 DiskFileItemFactory factory = new DiskFileItemFactory(); 107 PortletFileUpload upload = new PortletFileUpload(factory); 108 List <FileItem> fileItems = upload.parseRequest(request); 109 Iterator <FileItem> iter = fileItems.iterator(); 110 FileUploadBean bean = new FileUploadBean(); 111 while(iter.hasNext()) 112 { 113 FileItem item = iter.next(); 114 String filename = item.getName(); 115 if(item.isFormField() == false) 116 { 117 if (logger.isDebugEnabled()) 118 logger.debug("Processing uploaded file: " + filename); 119 120 int idx = filename.lastIndexOf('\\'); 123 124 if (idx == -1) 125 { 126 idx = filename.lastIndexOf('/'); 128 } 129 130 if (idx != -1) 131 { 132 filename = filename.substring(idx + File.separator.length()); 133 } 134 135 File tempFile = TempFileProvider.createTempFile("alfresco", ".upload"); 136 item.write(tempFile); 137 bean.setFile(tempFile); 138 bean.setFileName(filename); 139 bean.setFilePath(tempFile.getAbsolutePath()); 140 session.setAttribute(FileUploadBean.FILE_UPLOAD_BEAN_NAME, bean, 141 PortletSession.PORTLET_SCOPE); 142 } 143 } 144 145 response.setRenderParameter(VIEW_ID, "a-jsf-page"); 150 } 151 else 152 { 153 User user = (User)request.getPortletSession().getAttribute(AuthenticationHelper.AUTHENTICATION_USER); 154 if (user != null) 155 { 156 try 158 { 159 WebApplicationContext ctx = (WebApplicationContext)getPortletContext().getAttribute( 160 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 161 AuthenticationService auth = (AuthenticationService)ctx.getBean("authenticationService"); 162 auth.validate(user.getTicket()); 163 164 LoginBean loginBean = (LoginBean)request.getPortletSession().getAttribute(AuthenticationHelper.LOGIN_BEAN); 166 if (loginBean != null) 167 { 168 String oldValue = request.getPreferences().getValue(PREF_ALF_USERNAME, null); 174 if (oldValue == null || oldValue.equals(loginBean.getUsernameInternal()) == false) 175 { 176 request.getPreferences().setValue(PREF_ALF_USERNAME, loginBean.getUsernameInternal()); 177 request.getPreferences().store(); 178 } 179 } 180 181 super.processAction(request, response); 183 } 184 catch (AuthenticationException authErr) 185 { 186 request.getPortletSession().removeAttribute(AuthenticationHelper.AUTHENTICATION_USER); 188 } 189 } 190 else 191 { 192 super.processAction(request, response); 194 } 195 } 196 } 197 catch (Throwable e) 198 { 199 if (getErrorPage() != null) 200 { 201 handleError(request, response, e); 202 } 203 else 204 { 205 logger.warn("No error page configured, re-throwing exception"); 206 207 if (e instanceof PortletException) 208 { 209 throw (PortletException)e; 210 } 211 else if (e instanceof IOException ) 212 { 213 throw (IOException )e; 214 } 215 else 216 { 217 throw new PortletException(e); 218 } 219 } 220 } 221 } 222 223 226 protected void facesRender(RenderRequest request, RenderResponse response) 227 throws PortletException, IOException 228 { 229 Application.setInPortalServer(true); 230 231 I18NUtil.setLocale(Application.getLanguage(request.getPortletSession())); 233 234 if (request.getParameter(ERROR_OCCURRED) != null) 235 { 236 String errorPage = Application.getErrorPage(getPortletContext()); 237 238 if (logger.isDebugEnabled()) 239 logger.debug("An error has occurred, redirecting to error page: " + errorPage); 240 241 response.setContentType("text/html"); 242 PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(errorPage); 243 dispatcher.include(request, response); 244 } 245 else 246 { 247 WebApplicationContext ctx = (WebApplicationContext)getPortletContext().getAttribute( 248 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 249 AuthenticationService auth = (AuthenticationService)ctx.getBean("AuthenticationService"); 250 251 PortletSession session = request.getPortletSession(); 254 String viewId = request.getParameter(VIEW_ID); 255 User user = (User)request.getPortletSession().getAttribute(AuthenticationHelper.AUTHENTICATION_USER); 256 if (user == null && (viewId == null || viewId.equals(getLoginPage()) == false)) 257 { 258 if (AuthenticationHelper.portalGuestAuthenticate(ctx, session, auth) == AuthenticationStatus.Guest) 259 { 260 if (logger.isDebugEnabled()) 261 logger.debug("Guest access successful."); 262 263 response.setContentType("text/html"); 265 request.getPortletSession().setAttribute(PortletUtil.PORTLET_REQUEST_FLAG, "true"); 266 nonFacesRequest(request, response, "/jsp/browse/browse.jsp"); 267 } 268 else 269 { 270 if (logger.isDebugEnabled()) 271 logger.debug("No valid User login, requesting login page. ViewId: " + viewId); 272 273 session.setAttribute(AuthenticationHelper.SESSION_USERNAME, 275 request.getPreferences().getValue(PREF_ALF_USERNAME, null)); 276 277 response.setContentType("text/html"); 279 request.getPortletSession().setAttribute(PortletUtil.PORTLET_REQUEST_FLAG, "true"); 280 nonFacesRequest(request, response); 281 } 282 } 283 else 284 { 285 if (session.getAttribute(AuthenticationHelper.SESSION_INVALIDATED) != null) 286 { 287 request.getPreferences().reset(PREF_ALF_USERNAME); 289 session.removeAttribute(AuthenticationHelper.SESSION_INVALIDATED); 290 } 291 292 try 293 { 294 if (user != null) 295 { 296 if (logger.isDebugEnabled()) 297 logger.debug("Validating ticket: " + user.getTicket()); 298 299 auth.validate(user.getTicket()); 301 } 302 303 super.facesRender(request, response); 305 } 306 catch (AuthenticationException authErr) 307 { 308 if (logger.isDebugEnabled()) 310 logger.debug("Invalid ticket, requesting login page."); 311 312 request.getPortletSession().removeAttribute(AuthenticationHelper.AUTHENTICATION_USER); 314 315 response.setContentType("text/html"); 317 request.getPortletSession().setAttribute(PortletUtil.PORTLET_REQUEST_FLAG, "true"); 318 nonFacesRequest(request, response); 319 } 320 catch (Throwable e) 321 { 322 if (getErrorPage() != null) 323 { 324 handleError(request, response, e); 325 } 326 else 327 { 328 logger.warn("No error page configured, re-throwing exception"); 329 330 if (e instanceof PortletException) 331 { 332 throw (PortletException)e; 333 } 334 else if (e instanceof IOException ) 335 { 336 throw (IOException )e; 337 } 338 else 339 { 340 throw new PortletException(e); 341 } 342 } 343 } 344 } 345 } 346 } 347 348 351 private void handleError(ActionRequest request, ActionResponse response, Throwable error) 352 throws PortletException, IOException 353 { 354 PortletSession session = request.getPortletSession(); 356 ErrorBean errorBean = (ErrorBean)session.getAttribute(ErrorBean.ERROR_BEAN_NAME, 357 PortletSession.PORTLET_SCOPE); 358 if (errorBean == null) 359 { 360 errorBean = new ErrorBean(); 361 session.setAttribute(ErrorBean.ERROR_BEAN_NAME, errorBean, PortletSession.PORTLET_SCOPE); 362 } 363 errorBean.setLastError(error); 364 365 response.setRenderParameter(ERROR_OCCURRED, "true"); 366 } 367 368 371 private void handleError(RenderRequest request, RenderResponse response, Throwable error) 372 throws PortletException, IOException 373 { 374 PortletSession session = request.getPortletSession(); 376 ErrorBean errorBean = (ErrorBean)session.getAttribute(ErrorBean.ERROR_BEAN_NAME, 377 PortletSession.PORTLET_SCOPE); 378 if (errorBean == null) 379 { 380 errorBean = new ErrorBean(); 381 session.setAttribute(ErrorBean.ERROR_BEAN_NAME, errorBean, PortletSession.PORTLET_SCOPE); 382 } 383 errorBean.setLastError(error); 384 385 FacesContext context = FacesContext.getCurrentInstance(); 390 if (context != null) 391 { 392 ViewHandler viewHandler = context.getApplication().getViewHandler(); 393 UIViewRoot view = viewHandler.createView(context, "/jsp/browse/browse.jsp"); 395 context.setViewRoot(view); 396 } 397 398 String errorPage = getErrorPage(); 400 401 if (logger.isDebugEnabled()) 402 logger.debug("An error has occurred, redirecting to error page: " + errorPage); 403 404 response.setContentType("text/html"); 405 PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(errorPage); 406 dispatcher.include(request, response); 407 } 408 409 412 protected void setDefaultViewSelector() throws UnavailableException 413 { 414 super.setDefaultViewSelector(); 415 if (this.defaultViewSelector == null) 416 { 417 this.defaultViewSelector = new AlfrescoDefaultViewSelector(); 418 } 419 } 420 421 424 private String getLoginPage() 425 { 426 if (this.loginPage == null) 427 { 428 this.loginPage = Application.getLoginPage(getPortletContext()); 429 } 430 431 return this.loginPage; 432 } 433 434 437 private String getErrorPage() 438 { 439 if (this.errorPage == null) 440 { 441 this.errorPage = Application.getErrorPage(getPortletContext()); 442 } 443 444 return this.errorPage; 445 } 446 } 447 | Popular Tags |