This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch revert-670-issue/WW-5251 in repository https://gitbox.apache.org/repos/asf/struts.git
commit a4cf02ab96f4d0a02ff2bac3be51db57399db276 Author: Lukasz Lenart <lukasz.len...@gmail.com> AuthorDate: Tue Mar 26 09:02:54 2024 +0100 Revert "WW-5251 remove deprecated interfaces related to ServletConfigInterceptor" --- .../struts2/showcase/chat/ChatLoginAction.java | 13 +- .../struts2/showcase/chat/ChatLogoutAction.java | 13 +- .../struts2/showcase/chat/EnterRoomAction.java | 14 +- .../struts2/showcase/chat/ExitRoomAction.java | 13 +- .../showcase/chat/SendMessageToRoomAction.java | 12 +- .../showcase/hangman/GetUpdatedHangmanAction.java | 14 +- .../showcase/hangman/GuessCharacterAction.java | 14 +- .../showcase/hangman/StartHangmanAction.java | 13 +- .../apache/struts2/showcase/xslt/JVMAction.java | 13 +- .../apache/struts2/dispatcher/HttpParameters.java | 7 + .../struts2/interceptor/ApplicationAware.java | 45 +++++ .../struts2/interceptor/HttpParametersAware.java | 49 ++++++ .../apache/struts2/interceptor/ParameterAware.java | 50 ++++++ .../apache/struts2/interceptor/PrincipalAware.java | 38 ++++ .../apache/struts2/interceptor/RequestAware.java | 58 +++---- .../interceptor/ServletConfigInterceptor.java | 85 +++++++-- .../struts2/interceptor/ServletRequestAware.java | 49 ++++++ .../struts2/interceptor/ServletResponseAware.java | 47 +++++ .../apache/struts2/interceptor/SessionAware.java | 47 +++++ .../apache/struts2/util/ServletContextAware.java | 36 ++++ .../interceptor/ServletConfigInterceptorTest.java | 192 +++++++++++++++++++-- .../dispatcher/DirectRenderFromEventAction.java | 10 +- .../interceptor/PortletAwareInterceptor.java | 50 ++++-- .../portlet/interceptor/PortletContextAware.java | 34 ++++ .../interceptor/PortletPreferencesAware.java | 42 +++++ .../portlet/interceptor/PortletRequestAware.java | 35 ++++ .../portlet/interceptor/PortletResponseAware.java | 35 ++++ .../interceptor/PortletAwareInterceptorTest.java | 31 +++- .../interceptor/PortletStateInterceptorTest.java | 6 +- 29 files changed, 912 insertions(+), 153 deletions(-) diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLoginAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLoginAction.java index 0dcfd8943..a5fc15e73 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLoginAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLoginAction.java @@ -21,17 +21,16 @@ package org.apache.struts2.showcase.chat; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.SessionAware; import java.util.Map; -import org.apache.struts2.action.SessionAware; - public class ChatLoginAction extends ActionSupport implements SessionAware { private static final long serialVersionUID = 1L; private ChatService chatService; - private Map<String, Object> session; + private Map session; private String name; @@ -61,8 +60,8 @@ public class ChatLoginAction extends ActionSupport implements SessionAware { } - @Override - public void withSession(Map<String, Object> session) { - this.session = session; - } + // === SessionAware === + public void setSession(Map session) { + this.session = session; + } } diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLogoutAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLogoutAction.java index 452ce0d08..d0c3e6ba6 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLogoutAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLogoutAction.java @@ -21,18 +21,17 @@ package org.apache.struts2.showcase.chat; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.SessionAware; import java.util.Map; -import org.apache.struts2.action.SessionAware; - public class ChatLogoutAction extends ActionSupport implements SessionAware { private static final long serialVersionUID = 1L; private ChatService chatService; - private Map<String, Object> session; + private Map session; public ChatLogoutAction(ChatService chatService) { @@ -51,8 +50,8 @@ public class ChatLogoutAction extends ActionSupport implements SessionAware { } - @Override - public void withSession(Map<String, Object> session) { - this.session = session; - } + // === SessionAware === + public void setSession(Map session) { + this.session = session; + } } diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/EnterRoomAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/EnterRoomAction.java index 4b3939b0d..24a54531a 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/EnterRoomAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/EnterRoomAction.java @@ -21,17 +21,16 @@ package org.apache.struts2.showcase.chat; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.SessionAware; import java.util.Map; -import org.apache.struts2.action.SessionAware; - public class EnterRoomAction extends ActionSupport implements SessionAware { private static final long serialVersionUID = 1L; private ChatService chatService; - private Map<String, Object> session; + private Map session; private String roomName; public String getRoomName() { @@ -57,9 +56,10 @@ public class EnterRoomAction extends ActionSupport implements SessionAware { return SUCCESS; } - @Override - public void withSession(Map<String, Object> session) { - this.session = session; - } + + // === SessionAware === + public void setSession(Map session) { + this.session = session; + } } diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ExitRoomAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ExitRoomAction.java index 7dc14be1d..592f2d07f 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ExitRoomAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ExitRoomAction.java @@ -21,18 +21,17 @@ package org.apache.struts2.showcase.chat; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.SessionAware; import java.util.Map; -import org.apache.struts2.action.SessionAware; - public class ExitRoomAction extends ActionSupport implements SessionAware { private static final long serialVersionUID = 1L; private String roomName; - private Map<String, Object> session; + private Map session; public String getRoomName() { return roomName; @@ -55,9 +54,9 @@ public class ExitRoomAction extends ActionSupport implements SessionAware { return SUCCESS; } - @Override - public void withSession(Map<String, Object> session) { - this.session = session; - } + // === SessionAware === + public void setSession(Map session) { + this.session = session; + } } diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/SendMessageToRoomAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/SendMessageToRoomAction.java index 96bfe75c2..69a66609c 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/SendMessageToRoomAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/SendMessageToRoomAction.java @@ -21,11 +21,10 @@ package org.apache.struts2.showcase.chat; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.SessionAware; import java.util.Map; -import org.apache.struts2.action.SessionAware; - public class SendMessageToRoomAction extends ActionSupport implements SessionAware { private static final long serialVersionUID = 1L; @@ -34,7 +33,7 @@ public class SendMessageToRoomAction extends ActionSupport implements SessionAwa private String roomName; private String message; - private Map<String, Object> session; + private Map session; public SendMessageToRoomAction(ChatService chatService) { @@ -68,10 +67,9 @@ public class SendMessageToRoomAction extends ActionSupport implements SessionAwa return SUCCESS; } - @Override - public void withSession(Map<String, Object> session) { - this.session = session; - } + public void setSession(Map session) { + this.session = session; + } } diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/GetUpdatedHangmanAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/GetUpdatedHangmanAction.java index 660c5e9e8..24b777355 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/GetUpdatedHangmanAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/GetUpdatedHangmanAction.java @@ -21,16 +21,15 @@ package org.apache.struts2.showcase.hangman; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.SessionAware; import java.util.Map; -import org.apache.struts2.action.SessionAware; - public class GetUpdatedHangmanAction extends ActionSupport implements SessionAware { private static final long serialVersionUID = 5506025785406043027L; - private Map<String, Object> session; + private Map session; private Hangman hangman; @@ -46,6 +45,10 @@ public class GetUpdatedHangmanAction extends ActionSupport implements SessionAwa return SUCCESS; } + public void setSession(Map session) { + this.session = session; + } + public Hangman getHangman() { return hangman; } @@ -53,9 +56,4 @@ public class GetUpdatedHangmanAction extends ActionSupport implements SessionAwa public void setHangman(Hangman hangman) { this.hangman = hangman; } - - @Override - public void withSession(Map<String, Object> session) { - this.session = session; - } } diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/GuessCharacterAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/GuessCharacterAction.java index 67ea15099..40ab60fe4 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/GuessCharacterAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/GuessCharacterAction.java @@ -21,16 +21,15 @@ package org.apache.struts2.showcase.hangman; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.SessionAware; import java.util.Map; -import org.apache.struts2.action.SessionAware; - public class GuessCharacterAction extends ActionSupport implements SessionAware { private static final long serialVersionUID = 9050915577007590674L; - private Map<String, Object> session; + private Map session; private Character character; private Hangman hangman; @@ -45,6 +44,10 @@ public class GuessCharacterAction extends ActionSupport implements SessionAware return hangman; } + public void setSession(Map session) { + this.session = session; + } + public void setCharacter(Character character) { this.character = character; } @@ -52,9 +55,4 @@ public class GuessCharacterAction extends ActionSupport implements SessionAware public Character getCharacter() { return this.character; } - - @Override - public void withSession(Map<String, Object> session) { - this.session = session; - } } diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/StartHangmanAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/StartHangmanAction.java index bc4138a23..a38b10954 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/StartHangmanAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/hangman/StartHangmanAction.java @@ -21,11 +21,10 @@ package org.apache.struts2.showcase.hangman; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.SessionAware; import java.util.Map; -import org.apache.struts2.action.SessionAware; - import static org.apache.struts2.showcase.hangman.HangmanConstants.HANGMAN_SESSION_KEY; public class StartHangmanAction extends ActionSupport implements SessionAware { @@ -34,7 +33,7 @@ public class StartHangmanAction extends ActionSupport implements SessionAware { private HangmanService service; private Hangman hangman; - private Map<String, Object> session; + private Map session; public StartHangmanAction(HangmanService service) { @@ -54,8 +53,8 @@ public class StartHangmanAction extends ActionSupport implements SessionAware { } - @Override - public void withSession(Map<String, Object> session) { - this.session = session; - } + // === SessionAware === + public void setSession(Map session) { + this.session = session; + } } diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java index 6472adf62..059b300af 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java @@ -21,11 +21,9 @@ package org.apache.struts2.showcase.xslt; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.ServletRequestAware; import javax.servlet.http.HttpServletRequest; - -import org.apache.struts2.action.ServletRequestAware; - import java.util.Map; import java.util.Properties; @@ -55,6 +53,10 @@ public class JVMAction implements ServletRequestAware { return servletRequest; } + public void setServletRequest(HttpServletRequest servletRequest) { + this.servletRequest = servletRequest; + } + public Map<String, String> getEnvironment() { return environment; } @@ -98,9 +100,4 @@ public class JVMAction implements ServletRequestAware { this.systemProperties = systemProperties; } } - - @Override - public void withServletRequest(HttpServletRequest request) { - this.servletRequest = request; - } } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/HttpParameters.java b/core/src/main/java/org/apache/struts2/dispatcher/HttpParameters.java index 54f465eea..4b16edce0 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/HttpParameters.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/HttpParameters.java @@ -18,12 +18,15 @@ */ package org.apache.struts2.dispatcher; +import org.apache.struts2.interceptor.ParameterAware; + import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; @@ -89,6 +92,10 @@ public class HttpParameters implements Map<String, Parameter> { return this; } + public void applyParameters(ParameterAware parameterAware) { + parameterAware.setParameters(toMap()); + } + @Override public int size() { return parameters.size(); diff --git a/core/src/main/java/org/apache/struts2/interceptor/ApplicationAware.java b/core/src/main/java/org/apache/struts2/interceptor/ApplicationAware.java new file mode 100644 index 000000000..c240fa8cb --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/ApplicationAware.java @@ -0,0 +1,45 @@ +/* + * 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.interceptor; + +import java.util.Map; + +/** + * <p> + * Actions that want to be aware of the application Map object should implement this interface. + * This will give them access to a Map where they can put objects that should be available + * to other parts of the application. + * </p> + * <p> + * Typical uses are configuration objects and caches. + * </p> + * @deprecated please use {@link org.apache.struts2.action.ApplicationAware} instead + */ +@Deprecated +public interface ApplicationAware { + + /** + * Sets the map of application properties in the implementing class. + * + * @param application a Map of application properties. + * @deprecated please use {@link org.apache.struts2.action.ApplicationAware#withApplication(Map)} instead + */ + @Deprecated + public void setApplication(Map<String,Object> application); +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java b/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java new file mode 100644 index 000000000..c27a05694 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java @@ -0,0 +1,49 @@ +/* + * 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.interceptor; + +import org.apache.struts2.dispatcher.HttpParameters; + +/** + * <p> + * This interface gives actions an alternative way of receiving input parameters. The parameters will + * contain all input parameters as implementation of {@link org.apache.struts2.dispatcher.Parameter}. + * Actions that need this should simply implement it. + * </p> + * + * <p> + * One common use for this is to have the action propagate parameters to internally instantiated data + * objects. + * </p> + * + * @deprecated please use {@link org.apache.struts2.action.ParametersAware} instead + */ +@Deprecated +public interface HttpParametersAware { + + /** + * Sets the HTTP parameters in the implementing class. + * + * @param parameters an instance of {@link HttpParameters}. + * + * @deprecated please use {@link org.apache.struts2.action.ParametersAware#withParameters(HttpParameters)} instead + */ + @Deprecated + void setParameters(HttpParameters parameters); +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java b/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java new file mode 100644 index 000000000..9689e36b9 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java @@ -0,0 +1,50 @@ +/* + * 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.interceptor; + +import java.util.Map; + +/** + * <p> + * This interface gives actions an alternative way of receiving input parameters. The map will + * contain all input parameters as name/value entries. Actions that need this should simply implement it. + * </p> + * + * <p> + * One common use for this is to have the action propagate parameters to internally instantiated data + * objects. + * </p> + * + * <p> + * Note that all parameter values for a given name will be returned, so the type of the objects in + * the map is <tt>java.lang.String[]</tt>. + * </p> + * + * @deprecated please use {@link org.apache.struts2.action.ParametersAware} instead + */ +@Deprecated +public interface ParameterAware { + + /** + * Sets the map of input parameters in the implementing class. + * + * @param parameters a Map of parameters (name/value Strings). + */ + public void setParameters(Map<String, String[]> parameters); +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/PrincipalAware.java b/core/src/main/java/org/apache/struts2/interceptor/PrincipalAware.java new file mode 100644 index 000000000..ac1e3296b --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/PrincipalAware.java @@ -0,0 +1,38 @@ +/* + * 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.interceptor; + +/** + * Actions that want access to the Principal information from HttpServletRequest object + * should implement this interface. + * + * <p>This interface is only relevant if the Action is used in a servlet environment. + * By using this interface you will not become tied to servlet environment.</p> + * + * @deprecated please use {@link org.apache.struts2.action.PrincipalAware} instead + */ +@Deprecated +public interface PrincipalAware { + + /** + * @deprecated please use {@link org.apache.struts2.action.PrincipalAware#withPrincipalProxy(PrincipalProxy)} instead + */ + @Deprecated + void setPrincipalProxy(PrincipalProxy principalProxy); +} diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLogoutAction.java b/core/src/main/java/org/apache/struts2/interceptor/RequestAware.java similarity index 50% copy from apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLogoutAction.java copy to core/src/main/java/org/apache/struts2/interceptor/RequestAware.java index 452ce0d08..e8e73eee5 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/ChatLogoutAction.java +++ b/core/src/main/java/org/apache/struts2/interceptor/RequestAware.java @@ -1,6 +1,4 @@ /* - * $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 @@ -18,41 +16,31 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.struts2.showcase.chat; +package org.apache.struts2.interceptor; -import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.dispatcher.HttpParameters; import java.util.Map; -import org.apache.struts2.action.SessionAware; - -public class ChatLogoutAction extends ActionSupport implements SessionAware { - - private static final long serialVersionUID = 1L; - - private ChatService chatService; - - private Map<String, Object> session; - - - public ChatLogoutAction(ChatService chatService) { - this.chatService = chatService; - } - - public String execute() throws Exception { - - User user = (User) session.get(ChatAuthenticationInterceptor.USER_SESSION_KEY); - if (user != null) { - chatService.logout(user.getName()); - session.remove(ChatAuthenticationInterceptor.USER_SESSION_KEY); - } - - return SUCCESS; - } - - - @Override - public void withSession(Map<String, Object> session) { - this.session = session; - } +/** + * <p> + * Actions that want access to the current servlet request attributes should implement this interface. + * </p> + * + * <p> + * This interface is only relevant if the Action is used in a servlet environment. + * </p> + * @deprecated please use {@link org.apache.struts2.action.ParametersAware} instead + */ +@Deprecated +public interface RequestAware { + + /** + * Sets the Map of request attributes in the implementing class. + * + * @param request a Map of HTTP request attribute name/value pairs. + * @deprecated please use {@link org.apache.struts2.action.ParametersAware#withParameters(HttpParameters)} instead + */ + @Deprecated + public void setRequest(Map<String,Object> request); } diff --git a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java index f54c5e35f..634240085 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java @@ -18,19 +18,16 @@ */ package org.apache.struts2.interceptor; +import java.util.Map; + import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.StrutsStatics; -import org.apache.struts2.action.ApplicationAware; import org.apache.struts2.action.ParametersAware; -import org.apache.struts2.action.PrincipalAware; -import org.apache.struts2.action.ServletContextAware; -import org.apache.struts2.action.ServletRequestAware; -import org.apache.struts2.action.ServletResponseAware; -import org.apache.struts2.action.SessionAware; import org.apache.struts2.interceptor.servlet.ServletPrincipalProxy; +import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; @@ -49,13 +46,22 @@ import com.opensymphony.xwork2.interceptor.AbstractInterceptor; * * <ul> * <li>{@link ServletContextAware}</li> + * <li>{@link org.apache.struts2.action.ServletContextAware}</li> * <li>{@link ServletRequestAware}</li> + * <li>{@link org.apache.struts2.action.ServletRequestAware}</li> * <li>{@link ServletResponseAware}</li> - * <li>{@link ParametersAware}</li> - * <li>{@link ServletRequestAware}</li> + * <li>{@link org.apache.struts2.action.ServletResponseAware}</li> + * <li>{@link ParameterAware} - deprecated since 2.5.4, please use {@link HttpParametersAware}</li> + * <li>{@link HttpParametersAware}</li> + * <li>{@link org.apache.struts2.action.ParametersAware}</li> + * <li>{@link RequestAware}</li> + * <li>{@link org.apache.struts2.action.ServletRequestAware}</li> * <li>{@link SessionAware}</li> + * <li>{@link org.apache.struts2.action.SessionAware}</li> * <li>{@link ApplicationAware}</li> + * <li>{@link org.apache.struts2.action.ApplicationAware}</li> * <li>{@link PrincipalAware}</li> + * <li>{@link org.apache.struts2.action.PrincipalAware}</li> * </ul> * * <!-- END SNIPPET: description --> @@ -95,11 +101,17 @@ import com.opensymphony.xwork2.interceptor.AbstractInterceptor; * @see ServletContextAware * @see org.apache.struts2.action.ServletContextAware * @see ServletRequestAware + * @see org.apache.struts2.action.ServletRequestAware * @see ServletResponseAware - * @see ParametersAware + * @see org.apache.struts2.action.ServletResponseAware + * @see ParameterAware + * @see org.apache.struts2.action.ParametersAware * @see SessionAware + * @see org.apache.struts2.action.SessionAware * @see ApplicationAware + * @see org.apache.struts2.action.ApplicationAware * @see PrincipalAware + * @see org.apache.struts2.action.PrincipalAware */ public class ServletConfigInterceptor extends AbstractInterceptor implements StrutsStatics { @@ -118,12 +130,30 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str if (action instanceof ServletRequestAware) { HttpServletRequest request = context.getServletRequest(); - ((ServletRequestAware) action).withServletRequest(request); + ((ServletRequestAware) action).setServletRequest(request); + } + + if (action instanceof org.apache.struts2.action.ServletRequestAware) { + HttpServletRequest request = context.getServletRequest(); + ((org.apache.struts2.action.ServletRequestAware) action).withServletRequest(request); } if (action instanceof ServletResponseAware) { HttpServletResponse response = context.getServletResponse(); - ((ServletResponseAware) action).withServletResponse(response); + ((ServletResponseAware) action).setServletResponse(response); + } + + if (action instanceof org.apache.struts2.action.ServletResponseAware) { + HttpServletResponse response = context.getServletResponse(); + ((org.apache.struts2.action.ServletResponseAware) action).withServletResponse(response); + } + + if (action instanceof ParameterAware) { + context.getParameters().applyParameters((ParameterAware) action); + } + + if (action instanceof HttpParametersAware) { + ((HttpParametersAware) action).setParameters(context.getParameters()); } if (action instanceof ParametersAware) { @@ -131,24 +161,49 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str } if (action instanceof ApplicationAware) { - ((ApplicationAware) action).withApplication(context.getApplication()); + ((ApplicationAware) action).setApplication(context.getApplication()); + } + + if (action instanceof org.apache.struts2.action.ApplicationAware) { + ((org.apache.struts2.action.ApplicationAware) action).withApplication(context.getApplication()); } if (action instanceof SessionAware) { - ((SessionAware) action).withSession(context.getSession()); + ((SessionAware) action).setSession(context.getSession()); + } + + if (action instanceof org.apache.struts2.action.SessionAware) { + ((org.apache.struts2.action.SessionAware) action).withSession(context.getSession()); + } + + if (action instanceof RequestAware) { + ((RequestAware) action).setRequest((Map) context.get("request")); } if (action instanceof PrincipalAware) { HttpServletRequest request = context.getServletRequest(); if(request != null) { // We are in servlet environment, so principal information resides in HttpServletRequest - ((PrincipalAware) action).withPrincipalProxy(new ServletPrincipalProxy(request)); + ((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request)); + } + } + + if (action instanceof org.apache.struts2.action.PrincipalAware) { + HttpServletRequest request = context.getServletRequest(); + if(request != null) { + // We are in servlet environment, so principal information resides in HttpServletRequest + ((org.apache.struts2.action.PrincipalAware) action).withPrincipalProxy(new ServletPrincipalProxy(request)); } } if (action instanceof ServletContextAware) { ServletContext servletContext = context.getServletContext(); - ((ServletContextAware) action).withServletContext(servletContext); + ((ServletContextAware) action).setServletContext(servletContext); + } + + if (action instanceof org.apache.struts2.action.ServletContextAware) { + ServletContext servletContext = context.getServletContext(); + ((org.apache.struts2.action.ServletContextAware) action).withServletContext(servletContext); } return invocation.invoke(); diff --git a/core/src/main/java/org/apache/struts2/interceptor/ServletRequestAware.java b/core/src/main/java/org/apache/struts2/interceptor/ServletRequestAware.java new file mode 100644 index 000000000..7c9da3032 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/ServletRequestAware.java @@ -0,0 +1,49 @@ +/* + * 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.interceptor; + +import javax.servlet.http.HttpServletRequest; + +/** + * <p> + * All Actions that want to have access to the servlet request object must implement this interface. + * </p> + * + * <p> + * This interface is only relevant if the Action is used in a servlet environment. + * </p> + * + * <p> + * Note that using this interface makes the Action tied to a servlet environment, so it should be + * avoided if possible since things like unit testing will become more difficult. + * </p> + * @deprecated please use {@link org.apache.struts2.action.ServletRequestAware} instead + */ +@Deprecated +public interface ServletRequestAware { + + /** + * Sets the HTTP request object in implementing classes. + * + * @param request the HTTP request. + * @deprecated please use {@link org.apache.struts2.action.ServletRequestAware#withServletRequest(HttpServletRequest)} + */ + @Deprecated + public void setServletRequest(HttpServletRequest request); +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/ServletResponseAware.java b/core/src/main/java/org/apache/struts2/interceptor/ServletResponseAware.java new file mode 100644 index 000000000..9043b4434 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/ServletResponseAware.java @@ -0,0 +1,47 @@ +/* + * 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.interceptor; + +import javax.servlet.http.HttpServletResponse; + +/** + * <p> + * All Actions that want to have access to the servlet response object must implement this interface. + * </p> + * <p> + * This interface is only relevant if the Action is used in a servlet environment. + * </p> + * <p> + * Note that using this interface makes the Action tied to a servlet environment, so it should be + * avoided if possible since things like unit testing will become more difficult. + * </p> + * @deprecated please use {@link org.apache.struts2.action.ServletResponseAware} instead + */ +@Deprecated +public interface ServletResponseAware { + + /** + * Sets the HTTP response object in implementing classes. + * + * @param response the HTTP response. + * @deprecated please use {@link org.apache.struts2.action.ServletResponseAware#withServletResponse(HttpServletResponse)} instead + */ + @Deprecated + public void setServletResponse(HttpServletResponse response); +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/SessionAware.java b/core/src/main/java/org/apache/struts2/interceptor/SessionAware.java new file mode 100644 index 000000000..0380c9702 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/SessionAware.java @@ -0,0 +1,47 @@ +/* + * 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.interceptor; + +import java.util.Map; + +/** + * <p> + * Actions that want access to the user's HTTP session attributes should implement this interface. + * </p> + * <p> + * This will give them access to a Map where they can put objects that can be made available + * to subsequent requests. + * </p> + * <p> + * Typical uses may be cached user data such as name, or a shopping cart. + * </p> + * @deprecated use {@link org.apache.struts2.action.SessionAware} + */ +@Deprecated +public interface SessionAware { + + /** + * Sets the Map of session attributes in the implementing class. + * + * @param session a Map of HTTP session attribute name/value pairs. + * @deprecated please use {@link org.apache.struts2.action.SessionAware#withSession(Map)} + */ + @Deprecated + public void setSession(Map<String,Object> session); +} diff --git a/core/src/main/java/org/apache/struts2/util/ServletContextAware.java b/core/src/main/java/org/apache/struts2/util/ServletContextAware.java new file mode 100644 index 000000000..13a717cc7 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/util/ServletContextAware.java @@ -0,0 +1,36 @@ +/* + * 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.util; + +import javax.servlet.ServletContext; + +/** + * For components that have a dependence on the Servlet context. + * + * @deprecated please use {@link org.apache.struts2.action.ServletContextAware} instead + */ +@Deprecated +public interface ServletContextAware { + + /** + * @deprecated please use {@link org.apache.struts2.action.ServletContextAware#withServletContext(ServletContext)} instead + */ + @Deprecated + public void setServletContext(ServletContext context); +} diff --git a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java index dd57019ac..3cefbf18c 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java @@ -23,18 +23,17 @@ import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.mock.MockActionInvocation; import org.apache.struts2.StrutsInternalTestCase; import org.apache.struts2.StrutsStatics; -import org.apache.struts2.action.ApplicationAware; import org.apache.struts2.action.ParametersAware; -import org.apache.struts2.action.PrincipalAware; -import org.apache.struts2.action.ServletRequestAware; -import org.apache.struts2.action.ServletResponseAware; -import org.apache.struts2.action.SessionAware; import org.apache.struts2.dispatcher.HttpParameters; import org.apache.struts2.interceptor.servlet.ServletPrincipalProxy; +import org.apache.struts2.util.ServletContextAware; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletContext; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; @@ -52,7 +51,23 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { private ServletConfigInterceptor interceptor; public void testServletRequestAware() throws Exception { - ServletRequestAware mock = createMock(ServletRequestAware.class); + ServletRequestAware mock = (ServletRequestAware) createMock(ServletRequestAware.class); + + MockHttpServletRequest req = new MockHttpServletRequest(); + + MockActionInvocation mai = createActionInvocation(mock); + mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req); + + mock.setServletRequest((HttpServletRequest) req); + expectLastCall(); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + + public void testActionServletRequestAware() throws Exception { + org.apache.struts2.action.ServletRequestAware mock = createMock(org.apache.struts2.action.ServletRequestAware.class); MockHttpServletRequest req = new MockHttpServletRequest(); @@ -68,7 +83,23 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { } public void testServletResponseAware() throws Exception { - ServletResponseAware mock = createMock(ServletResponseAware.class); + ServletResponseAware mock = (ServletResponseAware) createMock(ServletResponseAware.class); + + MockHttpServletResponse res = new MockHttpServletResponse(); + + MockActionInvocation mai = createActionInvocation(mock); + mai.getInvocationContext().put(StrutsStatics.HTTP_RESPONSE, res); + + mock.setServletResponse((HttpServletResponse) res); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + + public void testActionServletResponseAware() throws Exception { + org.apache.struts2.action.ServletResponseAware mock = createMock(org.apache.struts2.action.ServletResponseAware.class); MockHttpServletResponse res = new MockHttpServletResponse(); @@ -83,7 +114,39 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { verify(mock); } - public void testParametersAware() throws Exception { + public void testParameterAware() throws Exception { + ParameterAware mock = createMock(ParameterAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + + HttpParameters param = HttpParameters.create().build(); + mai.getInvocationContext().setParameters(param); + + param.applyParameters(mock); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + + public void testHttpParametersAware() throws Exception { + HttpParametersAware mock = createMock(HttpParametersAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + + HttpParameters param = HttpParameters.create().build(); + mai.getInvocationContext().setParameters(param); + + mock.setParameters(param); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + + public void testActionParametersAware() throws Exception { ParametersAware mock = createMock(ParametersAware.class); MockActionInvocation mai = createActionInvocation(mock); @@ -100,7 +163,23 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { } public void testSessionAware() throws Exception { - SessionAware mock = createMock(SessionAware.class); + SessionAware mock = (SessionAware) createMock(SessionAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + + Map<String, Object> session = new HashMap<String, Object>(); + mai.getInvocationContext().setSession(session); + + mock.setSession(session); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + + public void testActionSessionAware() throws Exception { + org.apache.struts2.action.SessionAware mock = createMock(org.apache.struts2.action.SessionAware.class); MockActionInvocation mai = createActionInvocation(mock); @@ -120,6 +199,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { MockActionInvocation mai = createActionInvocation(mock); + Map<String, Object> app = new HashMap<String, Object>(); + mai.getInvocationContext().withApplication(app); + + mock.setApplication(app); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + + public void testActionApplicationAware() throws Exception { + org.apache.struts2.action.ApplicationAware mock = createMock(org.apache.struts2.action.ApplicationAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + Map<String, Object> app = new HashMap<>(); mai.getInvocationContext().withApplication(app); @@ -135,7 +230,27 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { MockHttpServletRequest req = new MockHttpServletRequest(); req.setUserPrincipal(null); req.setRemoteUser("Santa"); - PrincipalAware mock = createMock(PrincipalAware.class); + PrincipalAware mock = (PrincipalAware) createMock(PrincipalAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req); + + MockServletContext ctx = new MockServletContext(); + mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx); + + mock.setPrincipalProxy(anyObject(ServletPrincipalProxy.class)); // less strick match is needed for this unit test to be conducted using mocks + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + + public void testActionPrincipalAware() throws Exception { + MockHttpServletRequest req = new MockHttpServletRequest(); + req.setUserPrincipal(null); + req.setRemoteUser("Santa"); + org.apache.struts2.action.PrincipalAware mock = createMock(org.apache.struts2.action.PrincipalAware.class); MockActionInvocation mai = createActionInvocation(mock); mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req); @@ -151,13 +266,35 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { verify(mock); } + public void testPrincipalProxy() throws Exception { + // uni test that does not use mock, but an Action so we also get code coverage for the PrincipalProxy class + MockHttpServletRequest req = new MockHttpServletRequest(); + req.setUserPrincipal(null); + req.setRemoteUser("Santa"); + + MyPrincipalAction action = new MyPrincipalAction(); + MockActionInvocation mai = createActionInvocation(action); + mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req); + + assertNull(action.getProxy()); + interceptor.intercept(mai); + assertNotNull(action.getProxy()); + + PrincipalProxy proxy = action.getProxy(); + assertNull(proxy.getUserPrincipal()); + assertTrue(!proxy.isRequestSecure()); + assertTrue(!proxy.isUserInRole("no.role")); + assertEquals("Santa", proxy.getRemoteUser()); + + } + public void testActionPrincipalProxy() throws Exception { // unit test that does not use mock, but an Action so we also get code coverage for the PrincipalProxy class MockHttpServletRequest req = new MockHttpServletRequest(); req.setUserPrincipal(null); req.setRemoteUser("Santa"); - MyPrincipalAction action = new MyPrincipalAction(); + MyNewPrincipalAction action = new MyNewPrincipalAction(); MockActionInvocation mai = createActionInvocation(action); mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req); @@ -173,6 +310,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { } + public void testServletContextAware() throws Exception { + ServletContextAware mock = (ServletContextAware) createMock(ServletContextAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + + MockServletContext ctx = new MockServletContext(); + mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx); + + mock.setServletContext((ServletContext) ctx); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + public void testActionServletContextAware() throws Exception { org.apache.struts2.action.ServletContextAware mock = createMock(org.apache.struts2.action.ServletContextAware.class); @@ -218,6 +371,23 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { return SUCCESS; } + public void setPrincipalProxy(PrincipalProxy proxy) { + this.proxy = proxy; + } + + public PrincipalProxy getProxy() { + return proxy; + } + } + + private class MyNewPrincipalAction implements Action, org.apache.struts2.action.PrincipalAware { + + private PrincipalProxy proxy; + + public String execute() throws Exception { + return SUCCESS; + } + public void withPrincipalProxy(PrincipalProxy proxy) { this.proxy = proxy; } diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/DirectRenderFromEventAction.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/DirectRenderFromEventAction.java index 628404fb0..355630cc1 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/DirectRenderFromEventAction.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/DirectRenderFromEventAction.java @@ -19,8 +19,7 @@ package org.apache.struts2.portlet.dispatcher; import com.opensymphony.xwork2.Action; - -import org.apache.struts2.action.SessionAware; +import org.apache.struts2.interceptor.SessionAware; import org.apache.struts2.portlet.PortletConstants; import java.io.Serializable; @@ -68,8 +67,7 @@ public class DirectRenderFromEventAction implements SessionAware, Action, Serial return SUCCESS; } - @Override - public void withSession(Map<String, Object> session) { - location = (String)session.get(PortletConstants.RENDER_DIRECT_LOCATION); - } + public void setSession(Map session) { + location = (String)session.get(PortletConstants.RENDER_DIRECT_LOCATION); + } } 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 ca41bb7f7..420042e1d 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 @@ -24,12 +24,8 @@ import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.struts2.StrutsStatics; -import org.apache.struts2.action.PrincipalAware; +import org.apache.struts2.interceptor.PrincipalAware; import org.apache.struts2.portlet.PortletConstants; -import org.apache.struts2.portlet.action.PortletContextAware; -import org.apache.struts2.portlet.action.PortletPreferencesAware; -import org.apache.struts2.portlet.action.PortletRequestAware; -import org.apache.struts2.portlet.action.PortletResponseAware; import javax.portlet.PortletContext; import javax.portlet.PortletRequest; @@ -54,22 +50,42 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Stru if (action instanceof PortletRequestAware) { PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); - ((PortletRequestAware) action).withPortletRequest(request); + ((PortletRequestAware) action).setPortletRequest(request); + } + + if (action instanceof org.apache.struts2.portlet.action.PortletRequestAware) { + PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); + ((org.apache.struts2.portlet.action.PortletRequestAware) action).withPortletRequest(request); } if (action instanceof PortletResponseAware) { PortletResponse response = (PortletResponse) context.get(PortletConstants.RESPONSE); - ((PortletResponseAware) action).withPortletResponse(response); + ((PortletResponseAware) action).setPortletResponse(response); + } + + if (action instanceof org.apache.struts2.portlet.action.PortletResponseAware) { + PortletResponse response = (PortletResponse) context.get(PortletConstants.RESPONSE); + ((org.apache.struts2.portlet.action.PortletResponseAware) action).withPortletResponse(response); } if (action instanceof PrincipalAware) { PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); - ((PrincipalAware) action).withPrincipalProxy(new PortletPrincipalProxy(request)); + ((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy(request)); + } + + if (action instanceof org.apache.struts2.action.PrincipalAware) { + PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); + ((org.apache.struts2.action.PrincipalAware) action).withPrincipalProxy(new PortletPrincipalProxy(request)); } if (action instanceof PortletContextAware) { PortletContext portletContext = (PortletContext) context.get(StrutsStatics.STRUTS_PORTLET_CONTEXT); - ((PortletContextAware) action).withPortletContext(portletContext); + ((PortletContextAware) action).setPortletContext(portletContext); + } + + if (action instanceof org.apache.struts2.portlet.action.PortletContextAware) { + PortletContext portletContext = (PortletContext) context.get(StrutsStatics.STRUTS_PORTLET_CONTEXT); + ((org.apache.struts2.portlet.action.PortletContextAware) action).withPortletContext(portletContext); } if (action instanceof PortletPreferencesAware) { @@ -78,9 +94,21 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Stru // Check if running in a servlet environment if (request == null) { LOG.warn("This portlet preferences implementation should only be used during development"); - ((PortletPreferencesAware) action).withPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession())); + ((PortletPreferencesAware) action).setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession())); + } else { + ((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 { - ((PortletPreferencesAware) action).withPortletPreferences(request.getPreferences()); + ((org.apache.struts2.portlet.action.PortletPreferencesAware) action).withPortletPreferences(request.getPreferences()); } } diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java new file mode 100644 index 000000000..5ea01856a --- /dev/null +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java @@ -0,0 +1,34 @@ +/* + * 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; + +/** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletContextAware} instead + */ +@Deprecated +public interface PortletContextAware { + + /** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletContextAware#withPortletContext(PortletContext)} instead + */ + void setPortletContext(PortletContext portletContext); + +} 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 new file mode 100644 index 000000000..de39b25eb --- /dev/null +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletPreferencesAware.java @@ -0,0 +1,42 @@ +/* + * 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.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 + */ + @Deprecated + void setPortletPreferences(PortletPreferences prefs); +} diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java new file mode 100644 index 000000000..8903d9f45 --- /dev/null +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java @@ -0,0 +1,35 @@ +/* + * 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; + +/** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletRequestAware} instead + */ +@Deprecated +public interface PortletRequestAware { + + /** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletRequestAware#withPortletRequest(PortletRequest)} instead + */ + @Deprecated + void setPortletRequest(PortletRequest request); + +} diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java new file mode 100644 index 000000000..dc4f71b93 --- /dev/null +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java @@ -0,0 +1,35 @@ +/* + * 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; + +/** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletResponseAware} instead + */ +@Deprecated +public interface PortletResponseAware { + + /** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletResponseAware#withPortletResponse(PortletResponse)} instead + */ + @Deprecated + void setPortletResponse(PortletResponse response); + +} diff --git a/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java b/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java index 4e798bcc2..8c400a1ea 100644 --- a/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java +++ b/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java @@ -22,8 +22,6 @@ import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import junit.framework.TestCase; import org.apache.struts2.portlet.PortletConstants; -import org.apache.struts2.portlet.action.PortletRequestAware; -import org.apache.struts2.portlet.action.PortletResponseAware; import org.easymock.EasyMock; import javax.portlet.PortletRequest; @@ -44,12 +42,33 @@ public class PortletAwareInterceptorTest extends TestCase { super.tearDown(); } - public void testPortletRequestAware() throws Exception { + public void testPortletRequestIsSet() throws Exception { PortletRequest request = EasyMock.createMock(PortletRequest.class); Map<String, Object> ctx = new HashMap<>(); - ActionContext actionContext = ActionContext.of(ctx).bind(); ctx.put(PortletConstants.REQUEST, request); + ActionContext actionContext = ActionContext.of(ctx).bind(); + PortletRequestAware action = EasyMock.createMock(PortletRequestAware.class); + action.setPortletRequest(request); + + ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); + EasyMock.expect(invocation.getInvocationContext()).andReturn(actionContext); + EasyMock.expect(invocation.getAction()).andReturn(action); + + EasyMock.replay(action); + EasyMock.replay(invocation); + + interceptor.intercept(invocation); + + EasyMock.verify(action); + } + + public void testActionPortletRequestAware() throws Exception { + PortletRequest request = EasyMock.createMock(PortletRequest.class); + Map<String, Object> ctx = new HashMap<>(); + ActionContext actionContext = ActionContext.of(ctx).bind(); + ctx.put(PortletConstants.REQUEST, request); + org.apache.struts2.portlet.action.PortletRequestAware action = EasyMock.createMock(org.apache.struts2.portlet.action.PortletRequestAware.class); action.withPortletRequest(request); ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); @@ -64,12 +83,12 @@ public class PortletAwareInterceptorTest extends TestCase { EasyMock.verify(action); } - public void testPortletResponseAware() throws Exception { + public void testActionPortletResponseAware() throws Exception { PortletResponse response = EasyMock.createMock(PortletResponse.class); Map<String, Object> ctx = new HashMap<>(); ctx.put(PortletConstants.RESPONSE, response); ActionContext actionContext = ActionContext.of(ctx).bind(); - PortletResponseAware action = EasyMock.createMock(PortletResponseAware.class); + org.apache.struts2.portlet.action.PortletResponseAware action = EasyMock.createMock(org.apache.struts2.portlet.action.PortletResponseAware.class); action.withPortletResponse(response); ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); diff --git a/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletStateInterceptorTest.java b/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletStateInterceptorTest.java index d60b866e4..f324cf225 100644 --- a/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletStateInterceptorTest.java +++ b/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletStateInterceptorTest.java @@ -58,7 +58,7 @@ public class PortletStateInterceptorTest extends StrutsTestCasePortletTests { Map<String, Object> session = new HashMap<>(); ActionContext ctx = ActionContext.of(ctxMap).bind(); - ctx.withSession(session); + ctx.setSession(session); EasyMock.expect(invocation.getInvocationContext()).andStubReturn(ctx); actionResponse.setRenderParameter(EVENT_ACTION, "true"); @@ -97,7 +97,7 @@ public class PortletStateInterceptorTest extends StrutsTestCasePortletTests { ctxMap.put(REQUEST, renderRequest); ActionContext ctx = ActionContext.of(ctxMap).bind(); - ctx.withSession(session); + ctx.setSession(session); EasyMock.expect(invocation.getInvocationContext()).andStubReturn(ctx); EasyMock.expect(invocation.getStack()).andStubReturn(currentStack); @@ -138,7 +138,7 @@ public class PortletStateInterceptorTest extends StrutsTestCasePortletTests { ctxMap.put(REQUEST, renderRequest); ActionContext ctx = ActionContext.of(ctxMap).bind(); - ctx.withSession(session); + ctx.setSession(session); EasyMock.expect(invocation.getInvocationContext()).andStubReturn(ctx); EasyMock.expect(invocation.getStack()).andStubReturn(currentStack);