001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.portlet.handler; 018 019import javax.portlet.ActionRequest; 020import javax.portlet.ActionResponse; 021import javax.portlet.EventPortlet; 022import javax.portlet.EventRequest; 023import javax.portlet.EventResponse; 024import javax.portlet.Portlet; 025import javax.portlet.PortletContext; 026import javax.portlet.RenderRequest; 027import javax.portlet.RenderResponse; 028import javax.portlet.ResourceRequest; 029import javax.portlet.ResourceResponse; 030import javax.portlet.ResourceServingPortlet; 031 032import org.springframework.web.portlet.HandlerAdapter; 033import org.springframework.web.portlet.ModelAndView; 034import org.springframework.web.portlet.context.PortletContextAware; 035import org.springframework.web.portlet.util.PortletUtils; 036 037/** 038 * Adapter to use the Portlet interface with the generic DispatcherPortlet. 039 * Calls the Portlet's {@code render} and {@code processAction} 040 * methods to handle a request. 041 * 042 * <p>This adapter is not activated by default; it needs to be defined as a 043 * bean in the DispatcherPortlet context. It will automatically apply to 044 * mapped handler beans that implement the Portlet interface then. 045 * 046 * <p>Note that Portlet instances defined as bean will not receive initialization 047 * and destruction callbacks, unless a special post-processor such as 048 * SimplePortletPostProcessor is defined in the DispatcherPortlet context. 049 * 050 * <p><b>Alternatively, consider wrapping a Portlet with Spring's 051 * PortletWrappingController.</b> This is particularly appropriate for 052 * existing Portlet classes, allowing to specify Portlet initialization 053 * parameters, etc. 054 * 055 * @author John A. Lewis 056 * @since 2.0 057 * @see javax.portlet.Portlet 058 * @see SimplePortletPostProcessor 059 * @see org.springframework.web.portlet.mvc.PortletWrappingController 060 */ 061public class SimplePortletHandlerAdapter implements HandlerAdapter, PortletContextAware { 062 063 private PortletContext portletContext; 064 065 066 @Override 067 public void setPortletContext(PortletContext portletContext) { 068 this.portletContext = portletContext; 069 } 070 071 072 @Override 073 public boolean supports(Object handler) { 074 return (handler instanceof Portlet); 075 } 076 077 @Override 078 public void handleAction(ActionRequest request, ActionResponse response, Object handler) 079 throws Exception { 080 081 ((Portlet) handler).processAction(request, response); 082 } 083 084 @Override 085 public ModelAndView handleRender(RenderRequest request, RenderResponse response, Object handler) 086 throws Exception { 087 088 ((Portlet) handler).render(request, response); 089 return null; 090 } 091 092 @Override 093 public ModelAndView handleResource(ResourceRequest request, ResourceResponse response, Object handler) 094 throws Exception { 095 096 if (handler instanceof ResourceServingPortlet) { 097 ((ResourceServingPortlet) handler).serveResource(request, response); 098 } 099 else { 100 // roughly equivalent to Portlet 2.0 GenericPortlet 101 PortletUtils.serveResource(request, response, this.portletContext); 102 } 103 return null; 104 } 105 106 @Override 107 public void handleEvent(EventRequest request, EventResponse response, Object handler) throws Exception { 108 if (handler instanceof EventPortlet) { 109 ((EventPortlet) handler).processEvent(request, response); 110 } 111 else { 112 // if no event processing method was found just keep render params 113 response.setRenderParameters(request); 114 } 115 } 116 117}