summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/modules/common-http
diff options
context:
space:
mode:
authorlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2010-02-26 00:57:54 +0000
committerlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2010-02-26 00:57:54 +0000
commit8df069a1fd451a085af5c23f98a32f0743ae7d92 (patch)
tree442a1cbab7acc8e82696d4953be50f1551d7a199 /sca-java-2.x/trunk/modules/common-http
parent1c320ce9d5726f3b518807469a0bf027495b3f1c (diff)
Moving getRequestPath utility to common-http utility class
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@916511 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/common-http')
-rw-r--r--sca-java-2.x/trunk/modules/common-http/pom.xml16
-rw-r--r--sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPUtil.java69
2 files changed, 85 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/modules/common-http/pom.xml b/sca-java-2.x/trunk/modules/common-http/pom.xml
index 2a8cbdfa25..2fa94a708c 100644
--- a/sca-java-2.x/trunk/modules/common-http/pom.xml
+++ b/sca-java-2.x/trunk/modules/common-http/pom.xml
@@ -28,4 +28,20 @@
<artifactId>tuscany-common-http</artifactId>
<name>Apache Tuscany SCA Common HTTP</name>
+ <dependencies>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.5</version> <!-- to keep compatible with older servlet containers -->
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
</project>
diff --git a/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPUtil.java b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPUtil.java
new file mode 100644
index 0000000000..99aea93528
--- /dev/null
+++ b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPUtil.java
@@ -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.tuscany.sca.common.http;
+
+import javax.servlet.http.HttpServletRequest;
+
+public class HTTPUtil {
+
+ /**
+ * Calculate the relative request path taking in consideration if
+ * the application is running in a embedded webContatiner or from
+ * within a web application server host environment
+ *
+ * @param request The http request
+ * @return the relative path
+ */
+ public static String getRequestPath(HttpServletRequest request) {
+ // Get the request path
+ String contextPath = request.getContextPath();
+ String servletPath = request.getServletPath();
+ String requestURI = request.getRequestURI();
+
+ int contextPathLength = contextPath.length();
+ int servletPathLenght = servletPath.contains(contextPath) ? servletPath.length() - contextPath.length() : servletPath.length();
+
+ String requestPath = requestURI.substring(contextPathLength + servletPathLenght);
+
+ return requestPath;
+ }
+
+ /**
+ * Calculate the context root for an application taking in consideration if
+ * the application is running in a embedded webContatiner or from
+ * within a web application server host environment.
+ *
+ * In the case of webContainer the contextRoot will always be a empty string.
+ *
+ * @param request The http request
+ * @return the contextRoot
+ */
+ public static String getContextRoot(HttpServletRequest request) {
+ // Get the request path
+ String contextPath = request.getContextPath();
+ String requestURI = request.getRequestURI();
+
+ int contextPathLength = contextPath.length();
+
+ String contextRoot = requestURI.substring(0, contextPathLength);
+
+ return contextRoot;
+ }
+}