From 8df069a1fd451a085af5c23f98a32f0743ae7d92 Mon Sep 17 00:00:00 2001 From: lresende Date: Fri, 26 Feb 2010 00:57:54 +0000 Subject: 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 --- .../binding-atom-runtime/META-INF/MANIFEST.MF | 1 + .../trunk/modules/binding-atom-runtime/pom.xml | 6 ++ .../atom/provider/AtomBindingListenerServlet.java | 29 ++------- sca-java-2.x/trunk/modules/common-http/pom.xml | 16 +++++ .../apache/tuscany/sca/common/http/HTTPUtil.java | 69 ++++++++++++++++++++++ 5 files changed, 97 insertions(+), 24 deletions(-) create mode 100644 sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPUtil.java (limited to 'sca-java-2.x') diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/modules/binding-atom-runtime/META-INF/MANIFEST.MF index 9c4e4b127a..4ae5784c2a 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/META-INF/MANIFEST.MF +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/META-INF/MANIFEST.MF @@ -28,6 +28,7 @@ Import-Package: javax.servlet, org.apache.tuscany.sca.assembly;version="2.0.0", org.apache.tuscany.sca.binding.atom;version="2.0.0", org.apache.tuscany.sca.binding.http;version="2.0.0", + org.apache.tuscany.sca.common.http;version="2.0.0", org.apache.tuscany.sca.core;version="2.0.0", org.apache.tuscany.sca.data.collection;version="2.0.0", org.apache.tuscany.sca.databinding;version="2.0.0", diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/pom.xml b/sca-java-2.x/trunk/modules/binding-atom-runtime/pom.xml index 4afdce9945..b3ef011edd 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/pom.xml +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/pom.xml @@ -72,6 +72,12 @@ 2.0-SNAPSHOT + + org.apache.tuscany.sca + tuscany-common-http + 2.0-SNAPSHOT + + org.apache.tuscany.sca tuscany-host-http diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java index 523b6237e2..eead0b9183 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java @@ -52,6 +52,7 @@ import org.apache.abdera.parser.Parser; import org.apache.abdera.writer.WriterFactory; import org.apache.commons.codec.binary.Base64; import org.apache.tuscany.sca.binding.http.HTTPCacheContext; +import org.apache.tuscany.sca.common.http.HTTPUtil; import org.apache.tuscany.sca.data.collection.Entry; import org.apache.tuscany.sca.databinding.Mediator; import org.apache.tuscany.sca.interfacedef.DataType; @@ -171,7 +172,7 @@ class AtomBindingListenerServlet extends HttpServlet { //int servletPathLength = request.getContextPath().length() + request.getServletPath().length(); //String path = URLDecoder.decode(request.getRequestURI().substring(servletPathLength), "UTF-8"); - String path = URLDecoder.decode(getRequestPath(request), "UTF-8"); + String path = URLDecoder.decode(HTTPUtil.getRequestPath(request), "UTF-8"); logger.fine("get " + request.getRequestURI()); @@ -553,7 +554,7 @@ class AtomBindingListenerServlet extends HttpServlet { } // Get the request path - String path = URLDecoder.decode(getRequestPath(request), "UTF-8"); + String path = URLDecoder.decode(HTTPUtil.getRequestPath(request), "UTF-8"); if (path == null || path.length() == 0 || path.equals("/")) { org.apache.abdera.model.Entry createdFeedEntry = null; @@ -678,7 +679,7 @@ class AtomBindingListenerServlet extends HttpServlet { } // Get the request path - String path = URLDecoder.decode(getRequestPath(request), "UTF-8"); + String path = URLDecoder.decode(HTTPUtil.getRequestPath(request), "UTF-8"); if (path != null && path.startsWith("/")) { String id = path.substring(1); @@ -765,7 +766,7 @@ class AtomBindingListenerServlet extends HttpServlet { } // Get the request path - String path = URLDecoder.decode(getRequestPath(request), "UTF-8"); + String path = URLDecoder.decode(HTTPUtil.getRequestPath(request), "UTF-8"); String id; if (path != null && path.startsWith("/")) { @@ -787,26 +788,6 @@ class AtomBindingListenerServlet extends HttpServlet { } } } - - - /** - * - * @param request - * @return - */ - private static String getRequestPath(HttpServletRequest request) { - // Get the request path - String contextPath = request.getContextPath(); - String servletPath = request.getServletPath(); - String requestURI = request.getRequestURI(); - - int contextPathLength = request.getContextPath().length(); - int servletPathLenght = servletPath.contains(contextPath) ? servletPath.length() - contextPath.length() : servletPath.length(); - - String requestPath = requestURI.substring(contextPathLength + servletPathLenght); - - return requestPath; - } /** * Process the authorization header 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 @@ tuscany-common-http Apache Tuscany SCA Common HTTP + + + javax.servlet + servlet-api + 2.5 + provided + + + + junit + junit + 4.8.1 + test + + + 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; + } +} -- cgit v1.2.3