You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2012/02/09 17:53:37 UTC
svn commit: r1242398 - in
/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings:
./ src/main/java/org/apache/chemistry/opencmis/server/filter/
src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/
src/main/we...
Author: fmui
Date: Thu Feb 9 16:53:37 2012
New Revision: 1242398
URL: http://svn.apache.org/viewvc?rev=1242398&view=rev
Log:
CMIS-500: factored X-Forwarded code out into a filter and added unit tests
Added:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java (with props)
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java (with props)
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java (with props)
Removed:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/ProxyHttpServletRequestWrapper.java
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/pom.xml
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml
Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/pom.xml
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/pom.xml?rev=1242398&r1=1242397&r2=1242398&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/pom.xml (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/pom.xml Thu Feb 9 16:53:37 2012
@@ -67,9 +67,7 @@
</build>
<dependencies>
-
-
- <dependency>
+ <dependency>
<groupId>${project.groupId}</groupId>
<artifactId>chemistry-opencmis-commons-api</artifactId>
<version>${project.version}</version>
@@ -112,6 +110,12 @@
<version>2.1.7</version>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.9.0</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<repositories>
Added: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java?rev=1242398&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java Thu Feb 9 16:53:37 2012
@@ -0,0 +1,69 @@
+/*
+ * 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.chemistry.opencmis.server.filter;
+
+import java.io.IOException;
+import java.util.regex.Pattern;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * A filter that corrects server name, server port and scheme if OpenCMIS is
+ * running behind a proxy or load balancer.
+ */
+public class ProxyFilter implements Filter {
+
+ public static final String PARAM_TRUSTED_PROXIES = "trustedProxies";
+
+ private Pattern trustedProxies;
+
+ public void init(FilterConfig filterConfig) throws ServletException {
+ trustedProxies = null;
+ String trustedProxiesString = filterConfig.getInitParameter(PARAM_TRUSTED_PROXIES);
+ if (trustedProxiesString != null) {
+ try {
+ trustedProxies = Pattern.compile(trustedProxiesString);
+ } catch (Exception e) {
+ throw new ServletException("Could not compile trustedProxies parameter: " + e, e);
+ }
+ }
+ }
+
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
+ ServletException {
+
+ // check for trusted proxy
+ if (trustedProxies != null && (request instanceof HttpServletRequest)
+ && trustedProxies.matcher(request.getRemoteAddr()).matches()) {
+ request = new ProxyHttpServletRequestWrapper((HttpServletRequest) request);
+ }
+
+ // call next
+ chain.doFilter(request, response);
+ }
+
+ public void destroy() {
+ }
+}
Propchange: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java?rev=1242398&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java Thu Feb 9 16:53:37 2012
@@ -0,0 +1,86 @@
+/*
+ * 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.chemistry.opencmis.server.filter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+
+public class ProxyHttpServletRequestWrapper extends HttpServletRequestWrapper {
+
+ public static final String FORWARDED_HOST_HEADER = "X-Forwarded-Host";
+ public static final String FORWARDED_PROTO_HEADER = "X-Forwarded-Proto";
+ public static final String HTTPS_SCHEME = "https";
+ public static final String HTTP_SCHEME = "http";
+
+ private String scheme;
+ private String serverName;
+ private int serverPort;
+
+ public ProxyHttpServletRequestWrapper(HttpServletRequest request) {
+ super(request);
+
+ scheme = request.getHeader(FORWARDED_PROTO_HEADER);
+
+ if (!HTTP_SCHEME.equalsIgnoreCase(scheme) && !HTTPS_SCHEME.equalsIgnoreCase(scheme)) {
+ scheme = request.getScheme();
+ }
+
+ serverName = request.getServerName();
+ serverPort = request.getServerPort();
+
+ String host = request.getHeader(FORWARDED_HOST_HEADER);
+ if ((host != null) && (host.length() > 0)) {
+ int index = host.indexOf(':');
+ if (index < 0) {
+ serverName = host;
+ serverPort = getDefaultPort(scheme);
+ } else {
+ serverName = host.substring(0, index);
+ try {
+ serverPort = Integer.parseInt(host.substring(index + 1));
+ } catch (NumberFormatException e) {
+ serverPort = getDefaultPort(scheme);
+ }
+ }
+ }
+ }
+
+ private int getDefaultPort(String scheme) {
+ if (HTTPS_SCHEME.equalsIgnoreCase(scheme)) {
+ return 443;
+ }
+
+ return 80;
+ }
+
+ @Override
+ public String getScheme() {
+ return scheme;
+ }
+
+ @Override
+ public String getServerName() {
+ return serverName;
+ }
+
+ @Override
+ public int getServerPort() {
+ return serverPort;
+ }
+}
Propchange: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java?rev=1242398&r1=1242397&r2=1242398&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java Thu Feb 9 16:53:37 2012
@@ -45,7 +45,6 @@ import static org.apache.chemistry.openc
import java.io.IOException;
import java.io.PrintWriter;
-import java.util.regex.Pattern;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@@ -94,7 +93,6 @@ public class CmisAtomPubServlet extends
private Dispatcher dispatcher;
private CallContextHandler callContextHandler;
- private Pattern trustedProxies;
@Override
public void init(ServletConfig config) throws ServletException {
@@ -111,17 +109,6 @@ public class CmisAtomPubServlet extends
}
}
- // initialize trusted proxy pattern
- trustedProxies = null;
- String trustedProxiesString = config.getInitParameter(PARAM_TRUSTED_PROXIES);
- if (trustedProxiesString != null) {
- try {
- trustedProxies = Pattern.compile(trustedProxiesString);
- } catch (Exception e) {
- throw new ServletException("Could not compile trustedProxies parameter: " + e, e);
- }
- }
-
// initialize the dispatcher
dispatcher = new Dispatcher();
@@ -168,12 +155,6 @@ public class CmisAtomPubServlet extends
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
-
- // check for trusted proxy
- if (trustedProxies != null && trustedProxies.matcher(request.getRemoteAddr()).matches()) {
- request = new ProxyHttpServletRequestWrapper(request);
- }
-
// set default headers
response.addHeader("Cache-Control", "private, max-age=0");
response.addHeader("Server", ServerVersion.OPENCMIS_SERVER);
Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml?rev=1242398&r1=1242397&r2=1242398&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml Thu Feb 9 16:53:37 2012
@@ -69,6 +69,26 @@
</filter-mapping>
-->
+ <!--
+ Uncomment the following filter if the OpenCMIS server runs behind a proxy server or a load balancer.
+ The value of the 'trustedProxies' parameter is a regular expression. It must match the IP address of the proxy or load balancer.
+ -->
+ <!--
+ <filter>
+ <filter-name>ProxyFilter</filter-name>
+ <filter-class>org.apache.chemistry.opencmis.server.filter.ProxyFilter</filter-class>
+ <init-param>
+ <param-name>trustedProxies</param-name>
+ <param-value>10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|169\.254\.\d{1,3}\.\d{1,3}|127\.\d{1,3}\.\d{1,3}\.\d{1,3}</param-value>
+ </init-param>
+ </filter>
+
+ <filter-mapping>
+ <filter-name>ProxyFilter</filter-name>
+ <servlet-name>cmisatom</servlet-name>
+ </filter-mapping>
+ -->
+
<servlet>
<servlet-name>cmisws</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
@@ -82,16 +102,6 @@
<param-name>callContextHandler</param-name>
<param-value>org.apache.chemistry.opencmis.server.shared.BasicAuthCallContextHandler</param-value>
</init-param>
- <!--
- Uncomment the following parameter if the OpenCMIS server runs behind a proxy server or a load balancer.
- The value of the parameter is a regular expression. It must match the IP address of the proxy or load balancer.
- -->
- <!--
- <init-param>
- <param-name>trustedProxies</param-name>
- <param-value>10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|169\.254\.\d{1,3}\.\d{1,3}|127\.\d{1,3}\.\d{1,3}\.\d{1,3}</param-value>
- </init-param>
- -->
<load-on-startup>2</load-on-startup>
</servlet>
Added: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java?rev=1242398&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java Thu Feb 9 16:53:37 2012
@@ -0,0 +1,202 @@
+/*
+ * 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.chemistry.opencmis.server.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.chemistry.opencmis.server.filter.ProxyHttpServletRequestWrapper;
+import org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class ProxyRequestTest {
+ private static final int FORWARDED_SERVER_PORT = 2443;
+ private static final String FORWARDED_SERVER_NAME = "www.frontend.org";
+ private static final String FORWARDED_HOST = FORWARDED_SERVER_NAME + ":" + FORWARDED_SERVER_PORT;
+ private static final String FORWARDED_HTTPS_PROTO = ProxyHttpServletRequestWrapper.HTTPS_SCHEME;
+ private static final String FORWARDED_HTTP_PROTO = ProxyHttpServletRequestWrapper.HTTP_SCHEME;
+ private static final String CONTEXT_PATH = "/context";
+ private static final String SERVLET_PATH = "cmisatom";
+ private static final String REPOSITORY_ID = "22d2880a- bae5-4cfc-a5a9-3b2618e6e11c";
+ private static final String EXPECTED_PATH = CONTEXT_PATH + "/" + SERVLET_PATH + "/" + REPOSITORY_ID;
+ private static final int BACKEND_SERVER_PORT = 8080;
+ private static final String BACKEND_SERVER_NAME = "www.backend.be";
+ private static final String BACKEND_SERVER_PROTO = ProxyHttpServletRequestWrapper.HTTP_SCHEME;
+
+ @Mock
+ private HttpServletRequest request;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ when(this.request.getScheme()).thenReturn(BACKEND_SERVER_PROTO);
+ when(this.request.getServerName()).thenReturn(BACKEND_SERVER_NAME);
+ when(this.request.getServerPort()).thenReturn(BACKEND_SERVER_PORT);
+ when(this.request.getContextPath()).thenReturn(CONTEXT_PATH);
+ when(this.request.getServletPath()).thenReturn(SERVLET_PATH);
+ }
+
+ @Test
+ public void testGetProxiedProtoBaseAddress() throws URISyntaxException {
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
+ FORWARDED_HTTPS_PROTO);
+
+ ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+
+ assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
+
+ URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
+
+ assertEquals(FORWARDED_HTTPS_PROTO, baseUri.getScheme());
+ assertEquals(BACKEND_SERVER_NAME, baseUri.getHost());
+ assertEquals(BACKEND_SERVER_PORT, baseUri.getPort());
+ assertEquals(EXPECTED_PATH, baseUri.getPath());
+ }
+
+ @Test
+ public void testGetProxiedHostBaseAddressAndHttpsProto() throws URISyntaxException {
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
+ FORWARDED_HTTPS_PROTO);
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
+ FORWARDED_SERVER_NAME);
+
+ ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+
+ assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
+ assertEquals(FORWARDED_SERVER_NAME, proxyRequest.getServerName());
+
+ URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
+
+ assertEquals(FORWARDED_HTTPS_PROTO, baseUri.getScheme());
+ assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
+ assertEquals(-1, baseUri.getPort());
+ assertEquals(EXPECTED_PATH, baseUri.getPath());
+ }
+
+ @Test
+ public void testGetProxiedHostBaseAddress() throws URISyntaxException {
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
+ FORWARDED_SERVER_NAME);
+
+ ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+
+ assertEquals(FORWARDED_SERVER_NAME, proxyRequest.getServerName());
+
+ URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
+
+ assertEquals(BACKEND_SERVER_PROTO, baseUri.getScheme());
+ assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
+ assertEquals(-1, baseUri.getPort());
+ assertEquals(EXPECTED_PATH, baseUri.getPath());
+ }
+
+ @Test
+ public void testGetProxiedHostAndPortBaseAddress() throws URISyntaxException {
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(FORWARDED_HOST);
+
+ ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+
+ assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
+
+ URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
+
+ assertEquals(BACKEND_SERVER_PROTO, baseUri.getScheme());
+ assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
+ assertEquals(FORWARDED_SERVER_PORT, baseUri.getPort());
+ assertEquals(EXPECTED_PATH, baseUri.getPath());
+ }
+
+ @Test
+ public void testGetProxiedHostPortAndProtoBaseAddress() throws URISyntaxException {
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
+ FORWARDED_HTTPS_PROTO);
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(FORWARDED_HOST);
+
+ ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+
+ assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
+ assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
+
+ URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
+
+ assertEquals(FORWARDED_HTTPS_PROTO, baseUri.getScheme());
+ assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
+ assertEquals(FORWARDED_SERVER_PORT, baseUri.getPort());
+ assertEquals(EXPECTED_PATH, baseUri.getPath());
+ }
+
+ @Test
+ public void testGetProxiedHostBadHttpPortAndProtoBaseAddress() throws URISyntaxException {
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
+ FORWARDED_HTTP_PROTO);
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
+ FORWARDED_SERVER_NAME + ":noportnumber");
+
+ ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+
+ assertEquals(FORWARDED_HTTP_PROTO, proxyRequest.getScheme());
+ assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
+
+ URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
+
+ assertEquals(FORWARDED_HTTP_PROTO, baseUri.getScheme());
+ assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
+ assertEquals(-1, baseUri.getPort());
+ assertEquals(EXPECTED_PATH, baseUri.getPath());
+ }
+
+ @Test
+ public void testGetProxiedHostBadHttpsPortAndProtoBaseAddress() throws URISyntaxException {
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
+ FORWARDED_HTTPS_PROTO);
+ when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
+ FORWARDED_SERVER_NAME + ":noportnumber");
+
+ ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+
+ assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
+ assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
+
+ URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
+
+ assertEquals(FORWARDED_HTTPS_PROTO, baseUri.getScheme());
+ assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
+ assertEquals(-1, baseUri.getPort());
+ assertEquals(EXPECTED_PATH, baseUri.getPath());
+ }
+
+ @Test
+ public void testCompileBaseUrl() throws URISyntaxException {
+ URI baseUri = new URI(AtomPubUtils.compileBaseUrl(request, REPOSITORY_ID).toString());
+
+ assertEquals(BACKEND_SERVER_PROTO, baseUri.getScheme());
+ assertEquals(BACKEND_SERVER_NAME, baseUri.getHost());
+ assertEquals(BACKEND_SERVER_PORT, baseUri.getPort());
+ assertEquals(EXPECTED_PATH, baseUri.getPath());
+ }
+}
Propchange: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java
------------------------------------------------------------------------------
svn:eol-style = native