Merge pull request #252 from lukaszlenart/with-no-setters

WW-4963 Implement new Aware interfaces
This commit is contained in:
Yasser Zamani
2018-10-22 12:42:48 +03:30
committed by GitHub
42 changed files with 1176 additions and 99 deletions
@@ -23,7 +23,7 @@ package org.apache.struts2.showcase.source;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ClassLoaderUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;
import org.apache.struts2.action.ServletContextAware;
import javax.servlet.ServletContext;
import java.io.BufferedReader;
@@ -227,7 +227,7 @@ public class ViewSourceAction extends ActionSupport implements ServletContextAwa
return snippet;
}
public void setServletContext(ServletContext arg0) {
public void withServletContext(ServletContext arg0) {
this.servletContext = arg0;
}
@@ -28,7 +28,7 @@ import com.opensymphony.xwork2.inject.Inject;
import org.apache.struts2.osgi.BundleAccessor;
import org.apache.struts2.osgi.host.OsgiHost;
import org.apache.struts2.osgi.StrutsOsgiListener;
import org.apache.struts2.util.ServletContextAware;
import org.apache.struts2.action.ServletContextAware;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
@@ -197,7 +197,7 @@ public class BundlesAction extends ActionSupport implements ServletContextAware
this.bundleAccessor = bundleAccessor;
}
public void setServletContext(ServletContext servletContext) {
public void withServletContext(ServletContext servletContext) {
osgiHost = (OsgiHost) servletContext.getAttribute(StrutsOsgiListener.OSGI_HOST);
}
}
@@ -22,7 +22,7 @@ package actions.osgi;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.osgi.interceptor.BundleContextAware;
import org.apache.struts2.osgi.action.BundleContextAware;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -33,7 +33,7 @@ import org.osgi.framework.BundleContext;
public class BundlesAction extends ActionSupport implements BundleContextAware {
private BundleContext bundleContext;
public void setBundleContext(BundleContext bundleContext) {
public void withBundleContext(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
@@ -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.action;
import java.util.Map;
/**
* 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.
*
* Typical uses are configuration objects and caches.
*/
public interface ApplicationAware {
/**
* Applies the map of application properties in the implementing class.
*
* @param application a Map of application properties.
*/
void withApplication(Map<String, Object> application);
}
@@ -0,0 +1,40 @@
/*
* 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.action;
import org.apache.struts2.interceptor.CookieInterceptor;
import java.util.Map;
/**
* Actions implementing the CookiesAware interface will receive
* a Map of filtered cookies via the setCookiesMap method.
*
* Please note that the {@link CookieInterceptor} needs to be
* activated to receive a cookies map.
*
* @since 2.6
*/
public interface CookiesAware {
/**
* Sets a map of filtered cookies.
* @param cookies the cookies
*/
void withCookies(Map<String, String> cookies);
}
@@ -0,0 +1,41 @@
/*
* 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.action;
import org.apache.struts2.dispatcher.HttpParameters;
/**
* 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.
*
* One common use for this is to have the action propagate parameters to internally instantiated data
* objects.
*
* @since 2.6
*/
public interface ParametersAware {
/**
* Sets the HTTP parameters in the implementing class.
*
* @param parameters an instance of {@link HttpParameters}.
*/
void withParameters(HttpParameters parameters);
}
@@ -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.action;
import org.apache.struts2.interceptor.PrincipalProxy;
/**
* Actions that want access to the Principal information from HttpServletRequest object
* should implement this interface.
*
* 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.
*/
public interface PrincipalAware {
void withPrincipalProxy(PrincipalProxy principalProxy);
}
@@ -0,0 +1,31 @@
/*
* 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.action;
import javax.servlet.ServletContext;
/**
* For components that have a dependence on the Servlet context.
*
* @since 2.6
*/
public interface ServletContextAware {
void withServletContext(ServletContext context);
}
@@ -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.action;
import javax.servlet.http.HttpServletRequest;
/**
* All Actions that want to have access to the servlet request object must implement this interface.
*
* This interface is only relevant if the Action is used in a servlet environment.
*
* 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.
*/
public interface ServletRequestAware {
/**
* Applies the HTTP request object in implementing classes.
*
* @param request the HTTP request.
*/
void withServletRequest(HttpServletRequest request);
}
@@ -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.action;
import javax.servlet.http.HttpServletResponse;
/**
* All Actions that want to have access to the servlet response object must implement this interface.
*
* This interface is only relevant if the Action is used in a servlet environment.
*
* 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.
*/
public interface ServletResponseAware {
/**
* Applies the HTTP response object in implementing classes.
*
* @param response the HTTP response.
*/
void withServletResponse(HttpServletResponse response);
}
@@ -0,0 +1,40 @@
/*
* 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.action;
import java.util.Map;
/**
* Actions that want access to the user's HTTP session attributes should implement this interface.
*
* This will give them access to a Map where they can put objects that can be made available
* to subsequent requests.
*
* Typical uses may be cached user data such as name, or a shopping cart.
*/
public interface SessionAware {
/**
* Applies the Map of session attributes in the implementing class.
*
* @param session a Map of HTTP session attribute name/value pairs.
*/
void withSession(Map<String, Object> session);
}
@@ -29,13 +29,17 @@ import java.util.Map;
* <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);
}
@@ -348,8 +348,8 @@ public class CookieInterceptor extends AbstractInterceptor {
}
/**
* Hook that set the <code>cookiesMap</code> into action that implements
* {@link CookiesAware}.
* Hook that set the <code>cookiesMap</code> into action that implements {@link CookiesAware}
* or {@link org.apache.struts2.action.CookiesAware}.
*
* @param action action object
* @param cookiesMap map of cookies
@@ -359,5 +359,9 @@ public class CookieInterceptor extends AbstractInterceptor {
LOG.debug("Action [{}] implements CookiesAware, injecting cookies map [{}]", action, cookiesMap);
((CookiesAware)action).setCookiesMap(cookiesMap);
}
if (action instanceof org.apache.struts2.action.CookiesAware) {
LOG.debug("Action [{}] implements CookiesAware, injecting cookies map [{}]", action, cookiesMap);
((org.apache.struts2.action.CookiesAware)action).withCookies(cookiesMap);
}
}
}
@@ -26,11 +26,15 @@ import java.util.Map;
*
* Please note that the {@link CookieInterceptor} needs to be
* activated to receive a cookies map.
*
* @deprecated please use {@link org.apache.struts2.action.CookiesAware} instead
*/
@Deprecated
public interface CookiesAware {
/**
* Sets a map of filtered cookies.
* @param cookies the cookies
* @deprecated please use {@link org.apache.struts2.action.CookiesAware#withCookies(Map)} instead
*/
void setCookiesMap(Map<String, String> cookies);
}
@@ -31,13 +31,19 @@ import org.apache.struts2.dispatcher.HttpParameters;
* 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);
}
@@ -36,7 +36,7 @@ import java.util.Map;
* the map is <tt>java.lang.String[]</tt>.
* </p>
*
* @deprecated please use {@link HttpParametersAware} instead
* @deprecated please use {@link org.apache.struts2.action.ParametersAware} instead
*/
@Deprecated
public interface ParameterAware {
@@ -25,7 +25,14 @@ package org.apache.struts2.interceptor;
* <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);
}
@@ -18,6 +18,8 @@
*/
package org.apache.struts2.interceptor;
import org.apache.struts2.dispatcher.HttpParameters;
import java.util.Map;
/**
@@ -28,13 +30,17 @@ import java.util.Map;
* <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);
}
@@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.action.ParametersAware;
import org.apache.struts2.interceptor.servlet.ServletPrincipalProxy;
import org.apache.struts2.util.ServletContextAware;
@@ -103,7 +104,7 @@ import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
* @see ServletRequestAware
* @see ServletResponseAware
* @see ParameterAware
* @see HttpParametersAware
* @see ParametersAware
* @see SessionAware
* @see ApplicationAware
* @see PrincipalAware
@@ -128,11 +129,21 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str
((ServletRequestAware) action).setServletRequest(request);
}
if (action instanceof org.apache.struts2.action.ServletRequestAware) {
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
((org.apache.struts2.action.ServletRequestAware) action).withServletRequest(request);
}
if (action instanceof ServletResponseAware) {
HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
((ServletResponseAware) action).setServletResponse(response);
}
if (action instanceof org.apache.struts2.action.ServletResponseAware) {
HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
((org.apache.struts2.action.ServletResponseAware) action).withServletResponse(response);
}
if (action instanceof ParameterAware) {
context.getParameters().applyParameters((ParameterAware) action);
}
@@ -141,14 +152,26 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str
((HttpParametersAware) action).setParameters(context.getParameters());
}
if (action instanceof ParametersAware) {
((ParametersAware) action).withParameters(context.getParameters());
}
if (action instanceof ApplicationAware) {
((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).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"));
}
@@ -160,10 +183,25 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str
((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request));
}
}
if (action instanceof org.apache.struts2.action.PrincipalAware) {
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
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 = (ServletContext) context.get(SERVLET_CONTEXT);
((ServletContextAware) action).setServletContext(servletContext);
}
if (action instanceof org.apache.struts2.action.ServletContextAware) {
ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
((org.apache.struts2.action.ServletContextAware) action).withServletContext(servletContext);
}
return invocation.invoke();
}
}
@@ -33,13 +33,17 @@ import javax.servlet.http.HttpServletRequest;
* 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);
}
@@ -31,13 +31,17 @@ import javax.servlet.http.HttpServletResponse;
* 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);
}
@@ -31,13 +31,17 @@ import java.util.Map;
* <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);
}
@@ -22,8 +22,15 @@ 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);
}
@@ -426,6 +426,49 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
assertFalse(excludedName.get(reqCookieName));
}
public void testActionCookieAwareWithStrutsInternalsAccess() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
String sessionCookieName = "session.userId";
String sessionCookieValue = "session.userId=1";
String appCookieName = "application.userId";
String appCookieValue = "application.userId=1";
String reqCookieName = "request.userId";
String reqCookieValue = "request.userId=1";
request.setCookies(
new Cookie(sessionCookieName, "1"),
new Cookie("1", sessionCookieValue),
new Cookie(appCookieName, "1"),
new Cookie("1", appCookieValue),
new Cookie(reqCookieName, "1"),
new Cookie("1", reqCookieValue)
);
ServletActionContext.setRequest(request);
final Map<String, Boolean> excludedName = new HashMap<>();
CookieInterceptor interceptor = new CookieInterceptor() {
@Override
protected boolean isAcceptableName(String name) {
boolean accepted = super.isAcceptableName(name);
excludedName.put(name, accepted);
return accepted;
}
};
interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
interceptor.setCookiesName("*");
MockActionInvocation invocation = new MockActionInvocation();
invocation.setAction(new MockActionWithActionCookieAware());
interceptor.intercept(invocation);
assertFalse(excludedName.get(sessionCookieName));
assertFalse(excludedName.get(appCookieName));
assertFalse(excludedName.get(reqCookieName));
}
public static class MockActionWithCookieAware extends ActionSupport implements CookiesAware {
private static final long serialVersionUID = -6202290616812813386L;
@@ -453,4 +496,29 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
public void setCookie3(String cookie3) { this.cookie3 = cookie3; }
}
public static class MockActionWithActionCookieAware extends ActionSupport implements org.apache.struts2.action.CookiesAware {
private Map cookies = Collections.EMPTY_MAP;
private String cookie1;
private String cookie2;
private String cookie3;
public void withCookies(Map<String, String> cookies) {
this.cookies = cookies;
}
public Map getCookiesMap() {
return this.cookies;
}
public String getCookie1() { return cookie1; }
public void setCookie1(String cookie1) { this.cookie1 = cookie1; }
public String getCookie2() { return cookie2; }
public void setCookie2(String cookie2) { this.cookie2 = cookie2; }
public String getCookie3() { return cookie3; }
public void setCookie3(String cookie3) { this.cookie3 = cookie3; }
}
}
@@ -18,31 +18,33 @@
*/
package org.apache.struts2.interceptor;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.Action;
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.interceptor.servlet.ServletPrincipalProxy;
import org.apache.struts2.action.ParametersAware;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.interceptor.servlet.ServletPrincipalProxy;
import org.apache.struts2.util.ServletContextAware;
import static org.easymock.EasyMock.*;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.mock.MockActionInvocation;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
/**
* Unit test for {@link ServletConfigInterceptor}.
*
*/
public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
@@ -64,6 +66,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
verify(mock);
}
public void testActionServletRequestAware() throws Exception {
org.apache.struts2.action.ServletRequestAware mock = createMock(org.apache.struts2.action.ServletRequestAware.class);
MockHttpServletRequest req = new MockHttpServletRequest();
MockActionInvocation mai = createActionInvocation(mock);
mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);
mock.withServletRequest(req);
expectLastCall();
replay(mock);
interceptor.intercept(mai);
verify(mock);
}
public void testServletResponseAware() throws Exception {
ServletResponseAware mock = (ServletResponseAware) createMock(ServletResponseAware.class);
@@ -80,6 +98,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
verify(mock);
}
public void testActionServletResponseAware() throws Exception {
org.apache.struts2.action.ServletResponseAware mock = createMock(org.apache.struts2.action.ServletResponseAware.class);
MockHttpServletResponse res = new MockHttpServletResponse();
MockActionInvocation mai = createActionInvocation(mock);
mai.getInvocationContext().put(StrutsStatics.HTTP_RESPONSE, res);
mock.withServletResponse(res);
expectLastCall().times(1);
replay(mock);
interceptor.intercept(mai);
verify(mock);
}
public void testParameterAware() throws Exception {
ParameterAware mock = createMock(ParameterAware.class);
@@ -112,6 +146,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
verify(mock);
}
public void testActionParametersAware() throws Exception {
ParametersAware mock = createMock(ParametersAware.class);
MockActionInvocation mai = createActionInvocation(mock);
HttpParameters params = HttpParameters.create().build();
mai.getInvocationContext().setParameters(params);
mock.withParameters(params);
expectLastCall().times(1);
replay(mock);
interceptor.intercept(mai);
verify(mock);
}
public void testSessionAware() throws Exception {
SessionAware mock = (SessionAware) createMock(SessionAware.class);
@@ -128,6 +178,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
verify(mock);
}
public void testActionSessionAware() throws Exception {
org.apache.struts2.action.SessionAware mock = createMock(org.apache.struts2.action.SessionAware.class);
MockActionInvocation mai = createActionInvocation(mock);
Map<String, Object> session = new HashMap<String, Object>();
mai.getInvocationContext().setSession(session);
mock.withSession(session);
expectLastCall().times(1);
replay(mock);
interceptor.intercept(mai);
verify(mock);
}
public void testApplicationAware() throws Exception {
ApplicationAware mock = (ApplicationAware) createMock(ApplicationAware.class);
@@ -144,6 +210,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
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().setApplication(app);
mock.withApplication(app);
expectLastCall().times(1);
replay(mock);
interceptor.intercept(mai);
verify(mock);
}
public void testPrincipalAware() throws Exception {
MockHttpServletRequest req = new MockHttpServletRequest();
req.setUserPrincipal(null);
@@ -152,7 +234,7 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
MockActionInvocation mai = createActionInvocation(mock);
mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);
MockServletContext ctx = new MockServletContext();
mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);
@@ -164,6 +246,26 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
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);
MockServletContext ctx = new MockServletContext();
mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);
mock.withPrincipalProxy(anyObject(ServletPrincipalProxy.class)); // less strict match is needed for this unit test to be conducted using mocks
expectLastCall().times(1);
replay(mock);
interceptor.intercept(mai);
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();
@@ -180,8 +282,30 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
PrincipalProxy proxy = action.getProxy();
assertNull(proxy.getUserPrincipal());
assertTrue(! proxy.isRequestSecure());
assertTrue(! proxy.isUserInRole("no.role"));
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");
MyNewPrincipalAction action = new MyNewPrincipalAction();
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());
assertFalse(proxy.isRequestSecure());
assertFalse(proxy.isUserInRole("no.role"));
assertEquals("Santa", proxy.getRemoteUser());
}
@@ -202,6 +326,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
verify(mock);
}
public void testActionServletContextAware() throws Exception {
org.apache.struts2.action.ServletContextAware mock = createMock(org.apache.struts2.action.ServletContextAware.class);
MockActionInvocation mai = createActionInvocation(mock);
MockServletContext ctx = new MockServletContext();
mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);
mock.withServletContext(ctx);
expectLastCall().times(1);
replay(mock);
interceptor.intercept(mai);
verify(mock);
}
private MockActionInvocation createActionInvocation(Object mock) {
MockActionInvocation mai = new MockActionInvocation();
mai.setResultCode("success");
@@ -240,4 +380,21 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
}
}
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;
}
public PrincipalProxy getProxy() {
return proxy;
}
}
}
@@ -0,0 +1,31 @@
/*
* 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.osgi.action;
import org.osgi.framework.BundleContext;
/**
* Actions implementing this interface will receive an instance of the BundleContext,
* the OsgiInterceptor must be applied to the action.
*/
public interface BundleContextAware {
void withBundleContext(BundleContext bundleContext);
}
@@ -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.osgi.action;
import java.util.List;
/**
* Classes implementing this interface, will be injected a list of services
* registered with the type of the parameterized type
*
* @param <T> The type of the service
* @since 2.6
*/
public interface ServiceAware<T> {
void withServices(List<T> services);
}
@@ -23,9 +23,16 @@ import org.osgi.framework.BundleContext;
/**
* Actions implementing this interface will receive an instance of the BundleContext,
* the OsgiInterceptor must be applied to the action.
*
* @deprecated please use {@link org.apache.struts2.osgi.action.BundleContextAware} instead
*/
@Deprecated
public interface BundleContextAware {
/**
* @deprecated please use {@link org.apache.struts2.osgi.action.BundleContextAware#withBundleContext(BundleContext)} instead
*/
@Deprecated
void setBundleContext(BundleContext bundleContext);
}
@@ -21,10 +21,11 @@ package org.apache.struts2.osgi.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
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.osgi.host.OsgiHost;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import javax.servlet.ServletContext;
@@ -46,35 +47,85 @@ public class OsgiInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
if (bundleContext != null) {
Object action = invocation.getAction();
injectBundleContext(action);
injectServicesUsingDeprecatedInterface(action);
injectServices(action);
} else if (LOG.isWarnEnabled()) {
LOG.warn("The OSGi interceptor was not able to find the BundleContext in the ServletContext");
}
//inject BundleContext
if (action instanceof BundleContextAware)
((BundleContextAware)action).setBundleContext(bundleContext);
return invocation.invoke();
}
//inject service implementations
if (action instanceof ServiceAware) {
Type[] types = action.getClass().getGenericInterfaces();
if (types != null) {
for (Type type : types) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
if (parameterizedType.getRawType() instanceof Class) {
Class clazz = (Class) parameterizedType.getRawType();
if (ServiceAware.class.equals(clazz)) {
Class serviceClass = (Class) parameterizedType.getActualTypeArguments()[0];
ServiceReference[] refs = bundleContext.getAllServiceReferences(serviceClass.getName(), null);
//get the services
if (refs != null) {
List services = new ArrayList(refs.length);
for (ServiceReference ref : refs) {
Object service = bundleContext.getService(ref);
//wow, that's a lot of nested ifs
if (service != null)
services.add(service);
}
private void injectBundleContext(Object action) {
if (action instanceof BundleContextAware)
((BundleContextAware) action).setBundleContext(bundleContext);
if (!services.isEmpty())
((ServiceAware)action).setServices(services);
if (action instanceof org.apache.struts2.osgi.action.BundleContextAware) {
((org.apache.struts2.osgi.action.BundleContextAware) action).withBundleContext(bundleContext);
}
}
@Deprecated
private void injectServicesUsingDeprecatedInterface(Object action) throws InvalidSyntaxException {
//inject service implementations
if (action instanceof ServiceAware) {
Type[] types = action.getClass().getGenericInterfaces();
if (types != null) {
for (Type type : types) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
if (parameterizedType.getRawType() instanceof Class) {
Class clazz = (Class) parameterizedType.getRawType();
if (ServiceAware.class.equals(clazz)) {
Class serviceClass = (Class) parameterizedType.getActualTypeArguments()[0];
ServiceReference[] refs = bundleContext.getAllServiceReferences(serviceClass.getName(), null);
//get the services
if (refs != null) {
List services = new ArrayList(refs.length);
for (ServiceReference ref : refs) {
Object service = bundleContext.getService(ref);
//wow, that's a lot of nested ifs
if (service != null)
services.add(service);
}
if (!services.isEmpty())
((ServiceAware) action).setServices(services);
}
}
}
}
}
}
}
}
private void injectServices(Object action) throws InvalidSyntaxException {
//inject service implementations
if (action instanceof org.apache.struts2.osgi.action.ServiceAware) {
Type[] types = action.getClass().getGenericInterfaces();
if (types != null) {
for (Type type : types) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
if (parameterizedType.getRawType() instanceof Class) {
Class clazz = (Class) parameterizedType.getRawType();
if (org.apache.struts2.osgi.action.ServiceAware.class.equals(clazz)) {
Class serviceClass = (Class) parameterizedType.getActualTypeArguments()[0];
ServiceReference[] refs = bundleContext.getAllServiceReferences(serviceClass.getName(), null);
//get the services
if (refs != null) {
List<Object> services = new ArrayList<>(refs.length);
for (ServiceReference ref : refs) {
Object service = bundleContext.getService(ref);
//wow, that's a lot of nested ifs
if (service != null)
services.add(service);
}
if (!services.isEmpty()) {
((org.apache.struts2.osgi.action.ServiceAware) action).withServices(services);
}
}
}
@@ -82,11 +133,7 @@ public class OsgiInterceptor extends AbstractInterceptor {
}
}
}
} else if (LOG.isWarnEnabled()){
LOG.warn("The OSGi interceptor was not able to find the BundleContext in the ServletContext");
}
return invocation.invoke();
}
@Inject
@@ -24,9 +24,15 @@ import java.util.List;
* Classes implementing this interface, will be injected a list of services
* registered with the type of the parameterized type
* @param <T> The type of the service
* @deprecated please use {@link org.apache.struts2.osgi.action.ServiceAware} instead
*/
@Deprecated
public interface ServiceAware<T> {
/**
* @deprecated please use {@link org.apache.struts2.osgi.action.ServiceAware#withServices(List)} instead
*/
@Deprecated
void setServices(List<T> services);
}
@@ -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.osgi.action;
import java.util.List;
public class ServiceAction implements ServiceAware<Object> {
private List<Object> services;
public List<Object> getServices() {
return services;
}
public void withServices(List<Object> services) {
this.services = services;
}
}
@@ -18,6 +18,7 @@
*/
package org.apache.struts2.osgi.interceptor;
import org.apache.struts2.osgi.action.ServiceAction;
import org.easymock.EasyMock;
import org.apache.struts2.osgi.host.OsgiHost;
import org.osgi.framework.BundleContext;
@@ -53,6 +54,28 @@ public class OsgiInterceptorTest extends TestCase {
EasyMock.verify(bundleContextAware);
}
public void testActionBundleContextAware() throws Exception {
ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class);
BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class);
ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class);
org.apache.struts2.osgi.action.BundleContextAware bundleContextAware = EasyMock.createStrictMock(org.apache.struts2.osgi.action.BundleContextAware.class);
EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(bundleContext);
EasyMock.expect(actionInvocation.getAction()).andReturn(bundleContextAware);
bundleContextAware.withBundleContext(bundleContext);
EasyMock.expect(actionInvocation.invoke()).andReturn("");
EasyMock.replay(bundleContextAware);
EasyMock.replay(servletContext);
EasyMock.replay(actionInvocation);
OsgiInterceptor osgiInterceptor = new OsgiInterceptor();
osgiInterceptor.setServletContext(servletContext);
osgiInterceptor.intercept(actionInvocation);
EasyMock.verify(bundleContextAware);
}
public void testBundleContextAwareNegative() throws Exception {
ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class);
ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class);
@@ -72,6 +95,25 @@ public class OsgiInterceptorTest extends TestCase {
EasyMock.verify(bundleContextAware);
}
public void testActionBundleContextAwareNegative() throws Exception {
ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class);
ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class);
org.apache.struts2.osgi.action.BundleContextAware bundleContextAware = EasyMock.createStrictMock(org.apache.struts2.osgi.action.BundleContextAware.class);
EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(null);
EasyMock.expect(actionInvocation.invoke()).andReturn("");
EasyMock.replay(bundleContextAware);
EasyMock.replay(servletContext);
EasyMock.replay(actionInvocation);
OsgiInterceptor osgiInterceptor = new OsgiInterceptor();
osgiInterceptor.setServletContext(servletContext);
osgiInterceptor.intercept(actionInvocation);
EasyMock.verify(bundleContextAware);
}
public void testServiceAware() throws Exception {
ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class);
BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class);
@@ -100,4 +142,33 @@ public class OsgiInterceptorTest extends TestCase {
assertNotNull(objects);
assertSame(someObject, objects.get(0));
}
public void testActionServiceAware() throws Exception {
ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class);
BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class);
ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class);
ServiceAction serviceAction = new ServiceAction();
//service refs
ServiceReference objectRef = EasyMock.createNiceMock(ServiceReference.class);
Object someObject = new Object();
EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(bundleContext);
EasyMock.expect(actionInvocation.getAction()).andReturn(serviceAction);
EasyMock.expect(actionInvocation.invoke()).andReturn("");
EasyMock.expect(bundleContext.getAllServiceReferences(Object.class.getName(), null)).andReturn(new ServiceReference[] {objectRef});
EasyMock.expect(bundleContext.getService(objectRef)).andReturn(someObject);
EasyMock.replay(bundleContext);
EasyMock.replay(servletContext);
EasyMock.replay(actionInvocation);
OsgiInterceptor osgiInterceptor = new OsgiInterceptor();
osgiInterceptor.setServletContext(servletContext);
osgiInterceptor.intercept(actionInvocation);
List<Object> objects = serviceAction.getServices();
assertNotNull(objects);
assertSame(someObject, objects.get(0));
}
}
@@ -0,0 +1,30 @@
/*
* 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.PortletContext;
/**
* @since 2.6
*/
public interface PortletContextAware {
void withPortletContext(PortletContext portletContext);
}
@@ -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);
}
@@ -0,0 +1,30 @@
/*
* 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.PortletRequest;
/**
* @since 2.6
*/
public interface PortletRequestAware {
void withPortletRequest(PortletRequest request);
}
@@ -0,0 +1,30 @@
/*
* 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.PortletResponse;
/**
* @since 2.6
*/
public interface PortletResponseAware {
void withPortletResponse(PortletResponse response);
}
@@ -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.
*
@@ -53,29 +53,65 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Stru
((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).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).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).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) {
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();
}
}
@@ -20,8 +20,15 @@ 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 {
void setPortletContext(PortletContext portletContext);
/**
* @deprecated please use {@link org.apache.struts2.portlet.action.PortletContextAware#withPortletContext(PortletContext)} instead
*/
void setPortletContext(PortletContext portletContext);
}
@@ -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);
}
@@ -20,8 +20,16 @@ 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 {
void setPortletRequest(PortletRequest request);
/**
* @deprecated please use {@link org.apache.struts2.portlet.action.PortletRequestAware#withPortletRequest(PortletRequest)} instead
*/
@Deprecated
void setPortletRequest(PortletRequest request);
}
@@ -20,8 +20,16 @@ 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);
}
@@ -25,38 +25,77 @@ import org.apache.struts2.portlet.PortletConstants;
import org.easymock.EasyMock;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import java.util.HashMap;
import java.util.Map;
public class PortletAwareInterceptorTest extends TestCase {
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(PortletConstants.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);
}
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(PortletConstants.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);
}
public void testActionPortletRequestAware() throws Exception {
PortletRequest request = EasyMock.createMock(PortletRequest.class);
Map<String, Object> ctx = new HashMap<>();
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);
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);
}
public void testActionPortletResponseAware() throws Exception {
PortletResponse response = EasyMock.createMock(PortletResponse.class);
Map<String, Object> ctx = new HashMap<>();
ctx.put(PortletConstants.RESPONSE, response);
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);
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);
}
}