Author: nilsga Date: Thu Aug 9 10:15:28 2007 New Revision: 564282 URL: http://svn.apache.org/viewvc?view=rev&rev=564282 Log: WW-2097 New "aware" interfaces and interceptor. The PortletAwareInterceptor replaces the PortletPreferencesInterceptor.
Added: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/ struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java Modified: struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml Added: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java?view=auto&rev=564282 ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java (added) +++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java Thu Aug 9 10:15:28 2007 @@ -0,0 +1,84 @@ +/* + * $Id: $ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.portlet.interceptor; + +import javax.portlet.PortletContext; +import javax.portlet.PortletRequest; +import javax.portlet.PortletResponse; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.struts2.StrutsStatics; +import org.apache.struts2.interceptor.PrincipalAware; +import org.apache.struts2.portlet.PortletActionConstants; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.interceptor.AbstractInterceptor; + +public class PortletAwareInterceptor extends AbstractInterceptor implements PortletActionConstants, StrutsStatics { + + private static final long serialVersionUID = 2476509721059587700L; + + private static final Log LOG = LogFactory.getLog(PortletAwareInterceptor.class); + + /** + * Sets action properties based on the interfaces an action implements. Things like application properties, + * parameters, session attributes, etc are set based on the implementing interface. + * + * @param invocation an encapsulation of the action execution state. + * @throws Exception if an error occurs when setting action properties. + */ + public String intercept(ActionInvocation invocation) throws Exception { + final Object action = invocation.getAction(); + final ActionContext context = invocation.getInvocationContext(); + + if (action instanceof PortletRequestAware) { + PortletRequest request = (PortletRequest) context.get(REQUEST); + ((PortletRequestAware) action).setPortletRequest(request); + } + + if (action instanceof PortletResponseAware) { + PortletResponse response = (PortletResponse) context.get(RESPONSE); + ((PortletResponseAware) action).setPortletResponse(response); + } + if (action instanceof PrincipalAware) { + PortletRequest request = (PortletRequest) context.get(REQUEST); + ((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy(request)); + } + if (action instanceof PortletContextAware) { + PortletContext portletContext = (PortletContext) context.get(STRUTS_PORTLET_CONTEXT); + ((PortletContextAware) action).setPortletContext(portletContext); + } + if (action instanceof PortletPreferencesAware) { + PortletRequest request = (PortletRequest) context.get(REQUEST); + + // Check if running in a servlet environment + if (request == null) { + LOG.warn("This portlet preferences implementation should only be used during development"); + ((PortletPreferencesAware)action).setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession())); + } else { + ((PortletPreferencesAware)action).setPortletPreferences(request.getPreferences()); + } + } + return invocation.invoke(); + } +} Added: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java?view=auto&rev=564282 ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java (added) +++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java Thu Aug 9 10:15:28 2007 @@ -0,0 +1,29 @@ +/* + * $Id: $ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.portlet.interceptor; + +import javax.portlet.PortletContext; + +public interface PortletContextAware { + + void setPortletContext(PortletContext portletContext); + +} Added: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java?view=auto&rev=564282 ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java (added) +++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java Thu Aug 9 10:15:28 2007 @@ -0,0 +1,29 @@ +/* + * $Id: $ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.portlet.interceptor; + +import javax.portlet.PortletRequest; + +public interface PortletRequestAware { + + void setPortletRequest(PortletRequest request); + +} Added: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java?view=auto&rev=564282 ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java (added) +++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java Thu Aug 9 10:15:28 2007 @@ -0,0 +1,29 @@ +/* + * $Id: $ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.portlet.interceptor; + +import javax.portlet.PortletResponse; + +public interface PortletResponseAware { + + void setPortletResponse(PortletResponse response); + +} Modified: struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml?view=diff&rev=564282&r1=564281&r2=564282 ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml (original) +++ struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml Thu Aug 9 10:15:28 2007 @@ -40,11 +40,11 @@ </result-types> <interceptors> - <interceptor name="portlet-preferences" class="org.apache.struts2.portlet.interceptor.PortletPreferencesInterceptor"/> - <interceptor name="servletConfig" class="org.apache.struts2.portlet.interceptor.PortletConfigInterceptor"/> + <interceptor name="portletAware" class="org.apache.struts2.portlet.interceptor.PortletAwareInterceptor"/> + <interceptor-stack name="portletDefaultStack"> <interceptor-ref name="defaultStack"/> - <interceptor-ref name="portlet-preferences" /> + <interceptor-ref name="portletAware" /> </interceptor-stack> </interceptors> Added: struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java?view=auto&rev=564282 ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java (added) +++ struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java Thu Aug 9 10:15:28 2007 @@ -0,0 +1,47 @@ +package org.apache.struts2.portlet.interceptor; + +import java.util.HashMap; +import java.util.Map; + +import javax.portlet.PortletRequest; + +import junit.framework.TestCase; + +import org.apache.struts2.portlet.PortletActionConstants; +import org.easymock.EasyMock; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; + +public class PortletAwareInterceptorTest extends TestCase implements PortletActionConstants { + + private PortletAwareInterceptor interceptor; + + protected void setUp() throws Exception { + super.setUp(); + interceptor = new PortletAwareInterceptor(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testPortletRequestIsSet() throws Exception { + PortletRequest request = EasyMock.createMock(PortletRequest.class); + Map<String, Object> ctx = new HashMap<String, Object>(); + ctx.put(REQUEST, request); + PortletRequestAware action = EasyMock.createMock(PortletRequestAware.class); + action.setPortletRequest(request); + + ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); + EasyMock.expect(invocation.getInvocationContext()).andReturn(new ActionContext(ctx)); + EasyMock.expect(invocation.getAction()).andReturn(action); + + EasyMock.replay(action); + EasyMock.replay(invocation); + + interceptor.intercept(invocation); + + EasyMock.verify(action); + } +}