diff options
author | Sauli Tähkäpää <sauli@vaadin.com> | 2014-03-21 22:04:16 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-04-10 11:28:48 +0000 |
commit | a452badd69632d9159081755b7519ffb743fcb03 (patch) | |
tree | 1f46761b53239df8646d88b4970878511fd62ac7 /server | |
parent | 55dfd2936a1680f444be8e7357ac4ceaa6870e23 (diff) | |
download | vaadin-framework-a452badd69632d9159081755b7519ffb743fcb03.tar.gz vaadin-framework-a452badd69632d9159081755b7519ffb743fcb03.zip |
Refactor VaadinPortletRequest extending. (#13551)
Change-Id: Ibe169bf0ec6d2f335e099ac2659079c8fad6ac0b
Diffstat (limited to 'server')
6 files changed, 472 insertions, 107 deletions
diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index 4c34b0aedb..b3ce238e72 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -29,6 +29,7 @@ import javax.portlet.ActionResponse; import javax.portlet.EventRequest; import javax.portlet.EventResponse; import javax.portlet.GenericPortlet; +import javax.portlet.PortalContext; import javax.portlet.PortletConfig; import javax.portlet.PortletContext; import javax.portlet.PortletException; @@ -51,61 +52,83 @@ import com.vaadin.util.CurrentInstance; * Portlet 2.0 base class. This replaces the servlet in servlet/portlet 1.0 * deployments and handles various portlet requests from the browser. * - * TODO Document me! - * - * @author peholmst + * @author Vaadin Ltd */ public class VaadinPortlet extends GenericPortlet implements Constants, Serializable { /** - * @deprecated As of 7.0. Will likely change or be removed in a future - * version + * Base class for portlet requests that need access to HTTP servlet + * requests. */ - @Deprecated - public static final String RESOURCE_URL_ID = "APP"; - - public static class VaadinHttpAndPortletRequest extends + public static abstract class VaadinHttpAndPortletRequest extends VaadinPortletRequest { + /** + * Constructs a new {@link VaadinHttpAndPortletRequest}. + * + * @since 7.2 + * @param request + * {@link PortletRequest} to be wrapped + * @param vaadinService + * {@link VaadinPortletService} associated with this request + */ public VaadinHttpAndPortletRequest(PortletRequest request, - HttpServletRequest originalRequest, VaadinPortletService vaadinService) { super(request, vaadinService); - this.originalRequest = originalRequest; } - private final HttpServletRequest originalRequest; + private HttpServletRequest originalRequest; + + /** + * Returns the original HTTP servlet request for this portlet request. + * + * @since 7.2 + * @param request + * {@link PortletRequest} used to + * @return the original HTTP servlet request + */ + protected abstract HttpServletRequest getServletRequest( + PortletRequest request); + + private HttpServletRequest getOriginalRequest() { + if (originalRequest == null) { + PortletRequest request = getRequest(); + originalRequest = getServletRequest(request); + } + + return originalRequest; + } @Override public String getParameter(String name) { String parameter = super.getParameter(name); if (parameter == null) { - parameter = originalRequest.getParameter(name); + parameter = getOriginalRequest().getParameter(name); } return parameter; } @Override public String getRemoteAddr() { - return originalRequest.getRemoteAddr(); + return getOriginalRequest().getRemoteAddr(); } @Override public String getRemoteHost() { - return originalRequest.getRemoteHost(); + return getOriginalRequest().getRemoteHost(); } @Override public int getRemotePort() { - return originalRequest.getRemotePort(); + return getOriginalRequest().getRemotePort(); } @Override public String getHeader(String name) { String header = super.getHeader(name); if (header == null) { - header = originalRequest.getHeader(name); + header = getOriginalRequest().getHeader(name); } return header; } @@ -114,7 +137,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants, public Enumeration<String> getHeaderNames() { Enumeration<String> headerNames = super.getHeaderNames(); if (headerNames == null) { - headerNames = originalRequest.getHeaderNames(); + headerNames = getOriginalRequest().getHeaderNames(); } return headerNames; } @@ -123,7 +146,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants, public Enumeration<String> getHeaders(String name) { Enumeration<String> headers = super.getHeaders(name); if (headers == null) { - headers = originalRequest.getHeaders(name); + headers = getOriginalRequest().getHeaders(name); } return headers; } @@ -132,64 +155,21 @@ public class VaadinPortlet extends GenericPortlet implements Constants, public Map<String, String[]> getParameterMap() { Map<String, String[]> parameterMap = super.getParameterMap(); if (parameterMap == null) { - parameterMap = originalRequest.getParameterMap(); + parameterMap = getOriginalRequest().getParameterMap(); } return parameterMap; } } - public static class VaadinGateinRequest extends VaadinHttpAndPortletRequest { - public VaadinGateinRequest(PortletRequest request, - VaadinPortletService vaadinService) { - super(request, getOriginalRequest(request), vaadinService); - } - - private static final HttpServletRequest getOriginalRequest( - PortletRequest request) { - try { - Method getRealReq = request.getClass().getMethod( - "getRealRequest"); - HttpServletRequestWrapper origRequest = (HttpServletRequestWrapper) getRealReq - .invoke(request); - return origRequest; - } catch (Exception e) { - throw new IllegalStateException("GateIn request not detected", - e); - } - } - } - - // Intentionally internal, will be refactored out in 7.2. - static class WebSpherePortalRequest extends VaadinHttpAndPortletRequest { - - public WebSpherePortalRequest(PortletRequest request, - VaadinPortletService vaadinService) { - super(request, getServletRequest(request), vaadinService); - } - - private static HttpServletRequest getServletRequest( - PortletRequest request) { - try { - Class<?> portletUtils = Class - .forName("com.ibm.ws.portletcontainer.portlet.PortletUtils"); - Method getHttpServletRequest = portletUtils.getMethod( - "getHttpServletRequest", PortletRequest.class); - - return (HttpServletRequest) getHttpServletRequest.invoke(null, - request); - } catch (Exception e) { - throw new IllegalStateException( - "WebSphere Portal request not detected."); - } - } - } - + /** + * Portlet request for Liferay. + */ public static class VaadinLiferayRequest extends VaadinHttpAndPortletRequest { public VaadinLiferayRequest(PortletRequest request, VaadinPortletService vaadinService) { - super(request, getOriginalRequest(request), vaadinService); + super(request, vaadinService); } @Override @@ -219,7 +199,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants, * @throws Exception * @return return value of the invoked method */ - private static Object invokeStaticLiferayMethod(String className, + private Object invokeStaticLiferayMethod(String className, String methodName, Object argument, String parameterClassName) throws Exception { Thread currentThread = Thread.currentThread(); @@ -251,8 +231,8 @@ public class VaadinPortlet extends GenericPortlet implements Constants, } } - private static HttpServletRequest getOriginalRequest( - PortletRequest request) { + @Override + protected HttpServletRequest getServletRequest(PortletRequest request) { try { // httpRequest = PortalUtil.getHttpServletRequest(request); HttpServletRequest httpRequest = (HttpServletRequest) invokeStaticLiferayMethod( @@ -272,10 +252,68 @@ public class VaadinPortlet extends GenericPortlet implements Constants, e); } } + } + /** + * Portlet request for GateIn. + */ + public static class VaadinGateInRequest extends VaadinHttpAndPortletRequest { + public VaadinGateInRequest(PortletRequest request, + VaadinPortletService vaadinService) { + super(request, vaadinService); + } + + @Override + protected HttpServletRequest getServletRequest(PortletRequest request) { + try { + Method getRealReq = request.getClass().getMethod( + "getRealRequest"); + HttpServletRequestWrapper origRequest = (HttpServletRequestWrapper) getRealReq + .invoke(request); + return origRequest; + } catch (Exception e) { + throw new IllegalStateException("GateIn request not detected", + e); + } + } + } + + /** + * Portlet request for WebSphere Portal. + */ + public static class VaadinWebSpherePortalRequest extends + VaadinHttpAndPortletRequest { + + public VaadinWebSpherePortalRequest(PortletRequest request, + VaadinPortletService vaadinService) { + super(request, vaadinService); + } + + @Override + protected HttpServletRequest getServletRequest(PortletRequest request) { + try { + Class<?> portletUtils = Class + .forName("com.ibm.ws.portletcontainer.portlet.PortletUtils"); + Method getHttpServletRequest = portletUtils.getMethod( + "getHttpServletRequest", PortletRequest.class); + + return (HttpServletRequest) getHttpServletRequest.invoke(null, + request); + } catch (Exception e) { + throw new IllegalStateException( + "WebSphere Portal request not detected."); + } + } } /** + * @deprecated As of 7.0. Will likely change or be removed in a future + * version + */ + @Deprecated + public static final String RESOURCE_URL_ID = "APP"; + + /** * This portlet parameter is used to add styles to the main element. E.g * "height:500px" generates a style="height:500px" to the main element. * @@ -443,50 +481,26 @@ public class VaadinPortlet extends GenericPortlet implements Constants, * * @param request * The original PortletRequest - * @return A wrapped version of the PorletRequest + * @return A wrapped version of the PortletRequest */ protected VaadinPortletRequest createVaadinRequest(PortletRequest request) { - if (isLiferay(request)) { - return new VaadinLiferayRequest(request, getService()); - } else if (isGateIn(request)) { - return new VaadinGateinRequest(request, getService()); - } else if (isWebSphere(request)) { - return new WebSpherePortalRequest(request, getService()); - } else { + PortalContext portalContext = request.getPortalContext(); + String portalInfo = portalContext.getPortalInfo().toLowerCase().trim(); + VaadinPortletService service = getService(); - return new VaadinPortletRequest(request, getService()); + if (portalInfo.contains("gatein")) { + return new VaadinGateInRequest(request, service); } - } - - /** - * Returns true if the portlet request is from Liferay. - * - * @param request - * @return True if Liferay, false otherwise - */ - private static boolean isLiferay(PortletRequest request) { - String portalInfo = request.getPortalContext().getPortalInfo() - .toLowerCase(); - return portalInfo.contains("liferay"); - } - /** - * Returns true if the portlet request if from GateIn - * - * @param request - * @return True if GateIn, false otherwise - */ - private static boolean isGateIn(PortletRequest request) { - String portalInfo = request.getPortalContext().getPortalInfo() - .toLowerCase(); - return portalInfo.contains("gatein"); - } + if (portalInfo.contains("liferay")) { + return new VaadinLiferayRequest(request, service); + } - private static boolean isWebSphere(PortletRequest request) { - String portalInfo = request.getPortalContext().getPortalInfo() - .toLowerCase(); + if (portalInfo.contains("websphere portal")) { + return new VaadinWebSpherePortalRequest(request, service); + } - return portalInfo.contains("websphere portal"); + return new VaadinPortletRequest(request, service); } private VaadinPortletResponse createVaadinResponse(PortletResponse response) { diff --git a/server/tests/src/com/vaadin/server/VaadinGateInRequestTests.java b/server/tests/src/com/vaadin/server/VaadinGateInRequestTests.java new file mode 100644 index 0000000000..017613661e --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinGateInRequestTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed 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 com.vaadin.server; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import com.vaadin.server.VaadinPortlet.VaadinGateInRequest; + +public class VaadinGateInRequestTests extends + VaadinHttpAndPortletRequestTests<VaadinGateInRequest> { + + @Override + protected VaadinGateInRequest createSut() { + + VaadinGateInRequest request = new VaadinGateInRequest(portletRequest, + vaadinPortletService); + + // Although partial mocking can be considered a code smell, + // here it's actually quite useful to mock reflection calls. + VaadinGateInRequest spy = spy(request); + doReturn(servletRequest).when(spy).getServletRequest(portletRequest); + + return spy; + } +} diff --git a/server/tests/src/com/vaadin/server/VaadinHttpAndPortletRequestTests.java b/server/tests/src/com/vaadin/server/VaadinHttpAndPortletRequestTests.java new file mode 100644 index 0000000000..01d501c4fb --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinHttpAndPortletRequestTests.java @@ -0,0 +1,140 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed 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 com.vaadin.server; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Enumeration; +import java.util.Map; + +import javax.portlet.PortletRequest; +import javax.servlet.http.HttpServletRequest; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import com.vaadin.server.VaadinPortlet.VaadinHttpAndPortletRequest; +import com.vaadin.server.VaadinPortletService; + +//Have to ignore this class - otherwise JUnit tries to instantiate it... +@Ignore +public abstract class VaadinHttpAndPortletRequestTests<T extends VaadinHttpAndPortletRequest> { + + protected VaadinHttpAndPortletRequest sut; + protected HttpServletRequest servletRequest; + protected PortletRequest portletRequest; + protected VaadinPortletService vaadinPortletService; + + protected abstract T createSut(); + + @Before + public void setup() { + portletRequest = mock(PortletRequest.class); + vaadinPortletService = mock(VaadinPortletService.class); + servletRequest = mock(HttpServletRequest.class); + + sut = createSut(); + } + + @Test + public void parameterIsFetchedFromServletRequest() { + when(servletRequest.getParameter("foo")).thenReturn("bar"); + + String parameter = sut.getParameter("foo"); + + assertThat(parameter, is("bar")); + } + + @Test + public void originalParameterIsOverridden() { + when(servletRequest.getParameter("foo")).thenReturn("braa"); + when(portletRequest.getParameter("foo")).thenReturn("bar"); + + String parameter = sut.getParameter("foo"); + + assertThat(parameter, is("bar")); + } + + @Test + public void remoteAddressIsFetchedFromServletRequest() { + when(servletRequest.getRemoteAddr()).thenReturn("foo"); + + String remoteAddr = sut.getRemoteAddr(); + + assertThat(remoteAddr, is("foo")); + } + + @Test + public void remoteHostIsFetchedFromServletRequest() { + when(servletRequest.getRemoteHost()).thenReturn("foo"); + + String remoteHost = sut.getRemoteHost(); + + assertThat(remoteHost, is("foo")); + } + + @Test + public void remotePortIsFetchedFromServletRequest() { + when(servletRequest.getRemotePort()).thenReturn(12345); + + int remotePort = sut.getRemotePort(); + + assertThat(remotePort, is(12345)); + } + + @Test + public void headerIsFetchedFromServletRequest() { + when(servletRequest.getHeader("foo")).thenReturn("bar"); + + String header = sut.getHeader("foo"); + + assertThat(header, is("bar")); + } + + @Test + public void headerNamesAreFetchedFromServletRequest() { + Enumeration expectedHeaderNames = mock(Enumeration.class); + when(servletRequest.getHeaderNames()).thenReturn(expectedHeaderNames); + + Enumeration<String> actualHeaderNames = sut.getHeaderNames(); + + assertThat(actualHeaderNames, is(expectedHeaderNames)); + } + + @Test + public void headersAreFetchedFromServletRequest() { + Enumeration expectedHeaders = mock(Enumeration.class); + when(servletRequest.getHeaders("foo")).thenReturn(expectedHeaders); + + Enumeration<String> actualHeaders = sut.getHeaders("foo"); + + assertThat(actualHeaders, is(expectedHeaders)); + } + + @Test + public void parameterMapIsFetchedFromServletRequest() { + Map expectedParameterMap = mock(Map.class); + when(servletRequest.getParameterMap()).thenReturn(expectedParameterMap); + + Map<String, String[]> actualParameterMap = sut.getParameterMap(); + + assertThat(actualParameterMap, is(expectedParameterMap)); + } +} diff --git a/server/tests/src/com/vaadin/server/VaadinLiferayRequestTests.java b/server/tests/src/com/vaadin/server/VaadinLiferayRequestTests.java new file mode 100644 index 0000000000..074941a556 --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinLiferayRequestTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed 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 com.vaadin.server; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import com.vaadin.server.VaadinPortlet.VaadinLiferayRequest; + +public class VaadinLiferayRequestTests extends + VaadinHttpAndPortletRequestTests<VaadinLiferayRequest> { + + @Override + protected VaadinLiferayRequest createSut() { + + VaadinLiferayRequest request = new VaadinLiferayRequest(portletRequest, + vaadinPortletService); + + // Although partial mocking can be considered a code smell, + // here it's actually quite useful to mock reflection calls. + VaadinLiferayRequest spy = spy(request); + doReturn(servletRequest).when(spy).getServletRequest(portletRequest); + + return spy; + } +}
\ No newline at end of file diff --git a/server/tests/src/com/vaadin/server/VaadinPortletTests.java b/server/tests/src/com/vaadin/server/VaadinPortletTests.java new file mode 100644 index 0000000000..93f8fd0778 --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinPortletTests.java @@ -0,0 +1,94 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed 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 com.vaadin.server; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import javax.portlet.PortalContext; +import javax.portlet.PortletRequest; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.VaadinPortlet.VaadinGateInRequest; +import com.vaadin.server.VaadinPortlet.VaadinLiferayRequest; +import com.vaadin.server.VaadinPortlet.VaadinWebSpherePortalRequest; + +public class VaadinPortletTests { + + private VaadinPortlet sut; + private PortletRequest portletRequest; + private PortalContext portalContext; + + @Before + public void setup() { + sut = new VaadinPortlet(); + + portletRequest = mock(PortletRequest.class); + portalContext = mock(PortalContext.class); + + when(portletRequest.getPortalContext()).thenReturn(portalContext); + } + + private void mockPortalInfo(String name) { + when(portalContext.getPortalInfo()).thenReturn(name); + } + + private VaadinPortletRequest createRequest() { + VaadinPortletRequest request = sut.createVaadinRequest(portletRequest); + return request; + } + + @Test + public void gateInRequestIsCreated() { + mockPortalInfo("gatein"); + + VaadinPortletRequest request = createRequest(); + + assertThat(request, instanceOf(VaadinGateInRequest.class)); + } + + @Test + public void liferayRequestIsCreated() { + mockPortalInfo("liferay"); + + VaadinPortletRequest request = createRequest(); + + assertThat(request, instanceOf(VaadinLiferayRequest.class)); + } + + @Test + public void webspherePortalRequestIsCreated() { + mockPortalInfo("websphere portal"); + + VaadinPortletRequest request = createRequest(); + + assertThat(request, instanceOf(VaadinWebSpherePortalRequest.class)); + } + + @Test + public void defaultPortletRequestIsCreated() { + mockPortalInfo("foobar"); + + VaadinPortletRequest request = createRequest(); + + assertThat(request, instanceOf(VaadinPortletRequest.class)); + } + +} diff --git a/server/tests/src/com/vaadin/server/VaadinWebSpherePortalRequestTests.java b/server/tests/src/com/vaadin/server/VaadinWebSpherePortalRequestTests.java new file mode 100644 index 0000000000..0da85c7111 --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinWebSpherePortalRequestTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed 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 com.vaadin.server; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import com.vaadin.server.VaadinPortlet.VaadinWebSpherePortalRequest; + +public class VaadinWebSpherePortalRequestTests extends + VaadinHttpAndPortletRequestTests<VaadinWebSpherePortalRequest> { + + @Override + protected VaadinWebSpherePortalRequest createSut() { + + VaadinWebSpherePortalRequest request = new VaadinWebSpherePortalRequest( + portletRequest, vaadinPortletService); + + // Although partial mocking can be considered a code smell, + // here it's actually quite useful to mock reflection calls. + VaadinWebSpherePortalRequest spy = spy(request); + doReturn(servletRequest).when(spy).getServletRequest(portletRequest); + + return spy; + } +}
\ No newline at end of file |