Example usage for javax.servlet.http HttpServletRequest getServletPath

List of usage examples for javax.servlet.http HttpServletRequest getServletPath

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getServletPath.

Prototype

public String getServletPath();

Source Link

Document

Returns the part of this request's URL that calls the servlet.

Usage

From source file:com.runwaysdk.controller.ServletDispatcher.java

/**
 * This method strips the context path from the request URI and returns it. Use this method to handle URI's in a context path agnostic manner.
 * /*w  w  w .  j  a v a  2s. c om*/
 * @param request
 * @return
 */
private static final String getServletPath(HttpServletRequest request) {
    String servletPath = request.getServletPath();

    if (!"".equals(servletPath)) {
        return servletPath;
    }

    String requestUri = request.getRequestURI();
    int startIndex = request.getContextPath().equals("") ? 0 : request.getContextPath().length();
    int endIndex = request.getPathInfo() == null ? requestUri.length()
            : requestUri.indexOf(request.getPathInfo());

    return requestUri.substring(startIndex, endIndex);
}

From source file:de.kurashigegollub.dev.gcatest.Utils.java

/**
 * /*ww  w.j ava2 s  . c o  m*/
 * Will return a String with the request URL. 
 * @param req The current HttpServletRequest.
 * @param includeServlet Will include the servlet name in the return value.
 * @param includePathInfo Will include the path and query parts in the return value (only added, if includeServlet is true as well).
 * @return 
 */
// http://hostname.com:80/appname/servlet/MyServlet/a/b;c=123?d=789
public static String reconstructURL(HttpServletRequest req, boolean includeServlet, boolean includePathInfo) {
    String scheme = req.getScheme(); // http
    String serverName = req.getServerName(); // hostname.com
    int serverPort = req.getServerPort(); // 80
    String contextPath = req.getContextPath(); // /appname
    String servletPath = req.getServletPath(); // /servlet/MyServlet
    String pathInfo = req.getPathInfo(); // /a/b;c=123
    String queryString = req.getQueryString(); // d=789

    // Reconstruct original requesting URL
    String url = scheme + "://" + serverName + ":" + serverPort + contextPath;

    if (includeServlet) {
        url += servletPath;
        if (includePathInfo) {
            if (pathInfo != null) {
                url += pathInfo;
            }
            if (queryString != null) {
                url += "?" + queryString;
            }
        }
    }
    return url;
}

From source file:net.maritimecloud.identityregistry.utils.ValidateUtil.java

public static void hasErrors(BindingResult bindingResult, HttpServletRequest request)
        throws McBasicRestException {
    if (bindingResult.hasErrors()) {
        String combinedErrMsg = "";
        for (ObjectError err : bindingResult.getAllErrors()) {
            if (combinedErrMsg.length() != 0) {
                combinedErrMsg += ", ";
            }//  w  ww . j a  v  a2 s  . co  m
            combinedErrMsg += err.getDefaultMessage();
        }
        throw new McBasicRestException(HttpStatus.BAD_REQUEST, combinedErrMsg, request.getServletPath());
    }
}

From source file:eu.webtoolkit.jwt.servlet.WebRequest.java

public static String computeScriptName(HttpServletRequest request, Configuration configuration) {
    String scriptName = request.getServletPath();

    if (request.getContextPath() != null)
        scriptName = request.getContextPath() + scriptName;

    if (!scriptName.startsWith("/"))
        scriptName = "/" + scriptName;

    // Jetty will auto-redirect in this case to .../
    // I am not sure if this is according to the servlet spec ?
    if (request.getServletPath().length() == 0 && !scriptName.endsWith("/"))
        scriptName += "/";

    if (configuration.internalDeploymentSize() != 0) {
        String pathInfo = getPathInfo(request, scriptName);
        // Move part of the internal path to the script name
        if (pathInfo != null) {
            for (int i = 0; i < configuration.internalDeploymentSize(); ++i) {
                if (pathInfo.length() > 1) {
                    int slashPos = pathInfo.indexOf('/', 1);
                    if (slashPos == -1)
                        slashPos = pathInfo.length();
                    if (slashPos != -1) {
                        scriptName += pathInfo.substring(0, slashPos);
                        pathInfo = pathInfo.substring(slashPos);
                    }//from ww  w  .jav  a2 s  . c om
                }
            }
        }
        if (!scriptName.endsWith("/"))
            scriptName += "/";
    }

    return scriptName;
}

From source file:eu.webtoolkit.jwt.servlet.WebRequest.java

private static String getPathInfo(HttpServletRequest request, String scriptName) {
    String pathInfo = request.getPathInfo();

    // Jetty will report "/" as an internal path. Which totally makes no sense but is according
    // to the spec
    if (request.getServletPath().length() == 0)
        if (pathInfo != null && pathInfo.equals("/"))
            pathInfo = "";

    if (pathInfo == null || pathInfo.length() == 0) {
        // Work around (jetty) bug where path info is not interpreted correctly for a URL
        // like /bla/hello;jsessionid=q0f2lqgeivq9uipxwk12gj6s/wt-resources/webkit-transitions.css

        String uri = request.getRequestURI();
        if (uri.startsWith(scriptName + ";")) {
            int pathInfoStart = uri.indexOf('/', scriptName.length() + 1);
            if (pathInfoStart != -1)
                pathInfo = uri.substring(pathInfoStart);
        }/*from  www  . j ava 2 s .  c om*/
    }

    if (pathInfo == null)
        pathInfo = "";
    return pathInfo;
}

From source file:net.siegmar.japtproxy.JaptProxy.java

public static String getURL(HttpServletRequest req, boolean serverOnly, Configuration configuration) {

    String scheme = req.getScheme(); // http
    String serverName = req.getServerName(); // hostname.com

    // do we have a remap for this serverName??
    String remap = configuration.getRemap(serverName);
    if (remap != null) {
        serverName = remap;//from  w  ww  .j ava 2 s  .  c o m
    }

    int serverPort = req.getServerPort(); // 80
    String contextPath = req.getContextPath(); // /mywebapp
    String servletPath = req.getServletPath(); // /servlet/MyServlet
    String pathInfo = req.getPathInfo(); // /a/b;c=123
    String queryString = req.getQueryString(); // d=789

    // Reconstruct original requesting URL
    StringBuilder url = new StringBuilder();
    url.append(scheme).append("://").append(serverName);

    if (serverPort != 80 && serverPort != 443) {
        url.append(":").append(serverPort);
    }

    if (serverOnly)
        return url.toString();

    url.append(contextPath).append(servletPath);

    if (pathInfo != null) {
        url.append(pathInfo);
    }
    if (queryString != null) {
        url.append("?").append(queryString);
    }
    return url.toString();
}

From source file:fr.paris.lutece.util.http.SecurityUtil.java

/**
 * Write request variables into the dump stringbuffer
 * @param sb The dump stringbuffer//  www.  j  a  v  a 2  s  .c  o m
 * @param request The HTTP request
 */
private static void dumpVariables(StringBuffer sb, HttpServletRequest request) {
    dumpVariable(sb, "AUTH_TYPE", request.getAuthType());
    dumpVariable(sb, "REQUEST_METHOD", request.getMethod());
    dumpVariable(sb, "PATH_INFO", request.getPathInfo());
    dumpVariable(sb, "PATH_TRANSLATED", request.getPathTranslated());
    dumpVariable(sb, "QUERY_STRING", request.getQueryString());
    dumpVariable(sb, "REQUEST_URI", request.getRequestURI());
    dumpVariable(sb, "SCRIPT_NAME", request.getServletPath());
    dumpVariable(sb, "LOCAL_ADDR", request.getLocalAddr());
    dumpVariable(sb, "SERVER_PROTOCOL", request.getProtocol());
    dumpVariable(sb, "REMOTE_ADDR", request.getRemoteAddr());
    dumpVariable(sb, "REMOTE_HOST", request.getRemoteHost());
    dumpVariable(sb, "HTTPS", request.getScheme());
    dumpVariable(sb, "SERVER_NAME", request.getServerName());
    dumpVariable(sb, "SERVER_PORT", String.valueOf(request.getServerPort()));
}

From source file:lucee.runtime.net.rpc.server.RPCServer.java

/**
 * getRequestPath a returns request path for web service padded with
 * request.getPathInfo for web services served from /services directory.
 * This is a required to support serving .jws web services from /services
 * URL. See AXIS-843 for more information.
 *
 * @param request HttpServletRequest/*from  www  . java  2  s.  c  o m*/
 * @return String
 */
private static String getRequestPath(HttpServletRequest request) {
    return request.getServletPath() + ((request.getPathInfo() != null) ? request.getPathInfo() : "");
}

From source file:org.impalaframework.extension.mvc.util.RequestModelHelper.java

/**
 * //w  ww.ja v  a  2  s  .co  m
 * @param logger
 * @param request
 */
public static void maybeDebugRequest(Log logger, HttpServletRequest request) {

    if (logger.isDebugEnabled()) {

        logger.debug("#####################################################################################");
        logger.debug("---------------------------- Request details ---------------------------------------");
        logger.debug("Request context path: " + request.getContextPath());
        logger.debug("Request path info: " + request.getPathInfo());
        logger.debug("Request path translated: " + request.getPathTranslated());
        logger.debug("Request query string: " + request.getQueryString());
        logger.debug("Request servlet path: " + request.getServletPath());
        logger.debug("Request request URI: " + request.getRequestURI());
        logger.debug("Request request URL: " + request.getRequestURL());
        logger.debug("Request session ID: " + request.getRequestedSessionId());

        logger.debug("------------------------------------------------ ");
        logger.debug("Parameters ------------------------------------- ");
        final Enumeration<String> parameterNames = request.getParameterNames();

        Map<String, String> parameters = new TreeMap<String, String>();

        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String value = request.getParameter(name);
            final String lowerCase = name.toLowerCase();
            if (lowerCase.contains("password") || lowerCase.contains("cardnumber")) {
                value = "HIDDEN";
            }
            parameters.put(name, value);
        }

        //now output            
        final Set<String> parameterKeys = parameters.keySet();
        for (String key : parameterKeys) {
            logger.debug(key + ": " + parameters.get(key));
        }

        logger.debug("------------------------------------------------ ");

        Map<String, Object> attributes = new TreeMap<String, Object>();

        logger.debug("Attributes ------------------------------------- ");
        final Enumeration<String> attributeNames = request.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String name = attributeNames.nextElement();
            Object value = request.getAttribute(name);
            final String lowerCase = name.toLowerCase();
            if (lowerCase.contains("password") || lowerCase.contains("cardnumber")) {
                value = "HIDDEN";
            }
            attributes.put(name, value);
        }

        //now output
        final Set<String> keys = attributes.keySet();
        for (String name : keys) {
            Object value = attributes.get(name);
            logger.debug(name + ": " + (value != null ? value.toString() : value));
        }

        logger.debug("------------------------------------------------ ");
        logger.debug("#####################################################################################");
    } else {
        if (logger.isInfoEnabled()) {
            logger.info(
                    "#####################################################################################");
            logger.info("Request query string: " + request.getQueryString());
            logger.info("Request request URI: " + request.getRequestURI());
            logger.info(
                    "#####################################################################################");
        }
    }
}

From source file:info.magnolia.module.servletsanity.support.ServletAssert.java

public static void printRequestInfo(HttpServletRequest request, HttpServletResponse response, String location)
        throws IOException {
    append("");/*from w w  w  .j  ava2  s .c  o m*/
    append("");
    append("###################################");
    append("##");
    append("## " + location);
    append("##");
    append("##############");
    append("");

    appendRequestChain(request);

    appendResponseChain(response);

    append("Path elements:");
    append("&nbsp;&nbsp;&nbsp;&nbsp;RequestUri  = " + request.getRequestURI());
    append("&nbsp;&nbsp;&nbsp;&nbsp;ContextPath = " + request.getContextPath());
    append("&nbsp;&nbsp;&nbsp;&nbsp;ServletPath = " + request.getServletPath());
    append("&nbsp;&nbsp;&nbsp;&nbsp;PathInfo    = " + request.getPathInfo());
    append("&nbsp;&nbsp;&nbsp;&nbsp;QueryString = " + request.getQueryString());
    String x = request.getContextPath() + request.getServletPath()
            + StringUtils.defaultString(request.getPathInfo());
    if (!request.getRequestURI().equals(x)) {
        append("ERROR RequestURI is [" + request.getRequestURI() + "] according to spec it should be [" + x
                + "]");
    } else {
        append("&nbsp;&nbsp;&nbsp;&nbsp;Request path elements are in sync (requestURI = contextPath + servletPath + pathInfo) (SRV 3.4)");
    }
    append("");

    append("Forward attributes:");
    printAttribute(request, "javax.servlet.forward.request_uri");
    printAttribute(request, "javax.servlet.forward.context_path");
    printAttribute(request, "javax.servlet.forward.servlet_path");
    printAttribute(request, "javax.servlet.forward.path_info");
    printAttribute(request, "javax.servlet.forward.query_string");
    append("");

    append("Include attributes:");
    printAttribute(request, "javax.servlet.include.request_uri");
    printAttribute(request, "javax.servlet.include.context_path");
    printAttribute(request, "javax.servlet.include.servlet_path");
    printAttribute(request, "javax.servlet.include.path_info");
    printAttribute(request, "javax.servlet.include.query_string");
    append("");
}