diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletPreferencesAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletPreferencesAware.java new file mode 100644 index 000000000..da20c023b --- /dev/null +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletPreferencesAware.java @@ -0,0 +1,39 @@ +/* + * 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.action; + +import javax.portlet.PortletPreferences; + + +/** + * All Actions that want to have access to the portlet preferences should + * implement this interface. If running in a servlet environment, an + * appropriate testing implementation will be provided. + * + * @since 2.6 + */ +public interface PortletPreferencesAware { + + /** + * Sets the HTTP request object in implementing classes. + * + * @param preferences the portlet preferences. + */ + void withPortletPreferences(PortletPreferences preferences); +} diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java index 8fedf5d51..a2be6a7c5 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java @@ -21,8 +21,8 @@ package org.apache.struts2.portlet.interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.struts2.StrutsStatics; import org.apache.struts2.interceptor.PrincipalAware; import org.apache.struts2.portlet.PortletConstants; @@ -33,11 +33,11 @@ import javax.portlet.PortletResponse; public class PortletAwareInterceptor extends AbstractInterceptor implements StrutsStatics { - private static final long serialVersionUID = 2476509721059587700L; - - private static final Logger LOG = LogManager.getLogger(PortletAwareInterceptor.class); + private static final long serialVersionUID = 2476509721059587700L; - /** + private static final Logger LOG = LogManager.getLogger(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. * @@ -78,16 +78,29 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Stru } if (action instanceof PortletPreferencesAware) { - PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); - + PortletRequest request = (PortletRequest) context.get(PortletConstants.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())); + ((PortletPreferencesAware) action).setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession())); } else { - ((PortletPreferencesAware)action).setPortletPreferences(request.getPreferences()); + ((PortletPreferencesAware) action).setPortletPreferences(request.getPreferences()); } } + + if (action instanceof org.apache.struts2.portlet.action.PortletPreferencesAware) { + PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); + + // Check if running in a servlet environment + if (request == null) { + LOG.warn("This portlet preferences implementation should only be used during development"); + ((org.apache.struts2.portlet.action.PortletPreferencesAware) action).withPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession())); + } else { + ((org.apache.struts2.portlet.action.PortletPreferencesAware) action).withPortletPreferences(request.getPreferences()); + } + } + return invocation.invoke(); } } diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletPreferencesAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletPreferencesAware.java index e7b61d9c4..de39b25eb 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletPreferencesAware.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletPreferencesAware.java @@ -25,13 +25,18 @@ import javax.portlet.PortletPreferences; * All Actions that want to have access to the portlet preferences should * implement this interface. If running in a servlet environment, an * appropriate testing implementation will be provided. + * + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletPreferencesAware} instead */ +@Deprecated public interface PortletPreferencesAware { /** * Sets the HTTP request object in implementing classes. * * @param prefs the portlet preferences. + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletPreferencesAware#withPortletPreferences(PortletPreferences)} instead */ - public void setPortletPreferences(PortletPreferences prefs); + @Deprecated + void setPortletPreferences(PortletPreferences prefs); }