Example usage for javax.servlet.http HttpServletRequest getContextPath

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

Introduction

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

Prototype

public String getContextPath();

Source Link

Document

Returns the portion of the request URI that indicates the context of the request.

Usage

From source file:de.iteratec.iteraplan.businesslogic.common.URLBuilder.java

/**
 * Retrieves the URL of the application. It should be something like:
 * https://localhost:8443/iteraplan//w  w  w.  j  a v  a  2 s .  co  m
 * 
 * @param req
 *          The current servlet request providing the necessary information.
 * @return The constructed application URL. Without trailing slash.
 */
public static String getApplicationURL(HttpServletRequest req) {
    StringBuilder appUrlBuffer = new StringBuilder(BUFFER_SIZE);

    // Iteraplan properties are checked for server address override
    if (StringUtils.isNotEmpty(APPLICATION_ADDRESS_FROM_PROPERTIES)) {
        appUrlBuffer.append(APPLICATION_ADDRESS_FROM_PROPERTIES);
    } else {
        appUrlBuffer.append(getServerURL(req));
        appUrlBuffer.append(req.getContextPath());
    }

    return appUrlBuffer.toString();
}

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

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

    appendRequestChain(request);

    appendResponseChain(response);

    append("Path elements:");
    append("    RequestUri  = " + request.getRequestURI());
    append("    ContextPath = " + request.getContextPath());
    append("    ServletPath = " + request.getServletPath());
    append("    PathInfo    = " + request.getPathInfo());
    append("    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("    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("");
}

From source file:ie.wombat.rt.fireeagle.CookieConsumer.java

/**
 * Get a fresh access token from the service provider.
 * @throws IOException //from   w  w w . j  av  a2 s.c o  m
 * @throws URISyntaxException 
 * 
 * @throws RedirectException
 *             to obtain authorization
 */
private static void getAccessToken(HttpServletRequest request, CookieMap cookies, OAuthAccessor accessor)
        throws OAuthException, IOException, URISyntaxException {
    CLIENT.getRequestToken(accessor);
    String consumerName = (String) accessor.consumer.getProperty("name");
    cookies.put(consumerName + ".requestToken", accessor.requestToken);
    cookies.put(consumerName + ".tokenSecret", accessor.tokenSecret);
    String authorizationURL = accessor.consumer.serviceProvider.userAuthorizationURL;
    if (authorizationURL.startsWith("/")) {
        authorizationURL = (new URL(new URL(request.getRequestURL().toString()),
                request.getContextPath() + authorizationURL)).toString();
    }
    URL callbackURL = new URL(new URL(request.getRequestURL().toString()),
            request.getContextPath() + Callback.PATH);
    throw new RedirectException(OAuth.addParameters(authorizationURL //
            , "oauth_token", accessor.requestToken //
            , "oauth_callback", OAuth.addParameters(callbackURL.toString() //
                    , "consumer", consumerName //
                    , "returnTo", getRequestPath(request) //
            )));
}

From source file:com.tc.utils.XSPUtils.java

public static String getURL() {

    HttpServletRequest req = XSPUtils.getRequest();
    String scheme = req.getScheme(); // http
    String serverName = req.getServerName(); // hostname.com
    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
    StringBuffer url = new StringBuffer();
    url.append(scheme).append("://").append(serverName);

    if ((serverPort != 80) && (serverPort != 443)) {
        url.append(":").append(serverPort);
    }/*from   w ww .  ja va 2 s  .  c  o  m*/

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

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

From source file:com.sun.faban.harness.webclient.Deployer.java

private static void writeHeader(HttpServletRequest request, Writer w) throws IOException {
    w.write("<!DOCTYPE html\n");
    w.write("    PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n");
    w.write("    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-" + "transitional.dtd\">\n");
    w.write("<html>\n");
    w.write("    <head>\n");
    w.write("        <title>" + Config.HARNESS_NAME + " Benchmark/Service Deployment</title>\n");
    w.write("<link rel=\"icon\" type=\"image/gif\" href=\"" + request.getContextPath() + "/img/faban.gif\">");
    w.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/style.css\" />");

    w.write("    </head>\n");
    w.write("    <body>\n");
    w.write("        <table BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\" WIDTH=\"100%\" >\n");
    w.write("            <tr class=\"gradient\">\n");
    w.write("                <td align=\"left\" width=\"25%\" style=\"color:white; font-size:10px\">\n");
    w.write("                &nbsp;&nbsp;<img src=\"img/faban_large.png\" height=\"50\" width=\"58\"/><br></td>\n");
    w.write("                <td align=\"center\" width=\"50%\" style=\"color:white; font-size:10px\">\n");
    w.write("                <b>Benchmark/Service Deployment</b></td>\n");
    w.write("                <td align=\"right\" valign=\"bottom\" width=25% style=\"color:white\">");
    w.write(Config.HARNESS_NAME + "&nbsp;&nbsp;" + Config.HARNESS_VERSION + "&nbsp;</td>\n");
    w.write("            </tr>\n");
    w.write("        </table>");
    w.write("        <br><center><b>");
}

From source file:edu.ucmerced.cas.authentication.principal.CasShibWebApplicationServiceImpl.java

public static CasShibWebApplicationServiceImpl createServiceFrom(final HttpServletRequest request,
        final HttpClient httpClient) {
    final String targetService = request.getParameter(CONST_PARAM_TARGET_SERVICE);
    final String method = request.getParameter(CONST_PARAM_METHOD);
    final String serviceToUse = StringUtils.hasText(targetService) ? targetService
            : request.getParameter(CONST_PARAM_SERVICE);

    if (!StringUtils.hasText(serviceToUse)) {
        return null;
    }//from  w  w w.j a v  a 2  s.  c om

    final String id = cleanupUrl(serviceToUse);
    final String artifactId = request.getParameter(CONST_PARAM_TICKET);

    // Extract the service passcode from url.
    // URLs should be in the following format:
    // /<contextPath>/shib/<appNameOrPasscode>/?
    String appNameOrPasscode = null;
    if ((request.getContextPath() != null
            ? request.getRequestURI().startsWith(request.getContextPath() + "/shib")
            : request.getRequestURI().startsWith("/shib"))) {
        String[] components = request.getRequestURI()
                .substring((request.getContextPath() != null ? request.getContextPath().length() : 0))
                .split("/");
        // 0 is the empty string before the first slash
        // 1 should be the shibX string
        // 2 should be the app name or passcode
        // 3... should be everything after the app name or passcode
        if (components.length > 3) {
            appNameOrPasscode = components[2];
            log.debug("application name or passcode = " + appNameOrPasscode);
        }
    } else {
        log.debug("no application name or passcode detected in url");
    }

    return new CasShibWebApplicationServiceImpl(id, serviceToUse, artifactId,
            "POST".equals(method) ? ResponseType.POST : ResponseType.REDIRECT, httpClient, appNameOrPasscode);
}

From source file:org.itracker.web.util.LoginUtilities.java

public static boolean checkAutoLogin(HttpServletRequest request, boolean allowSaveLogin) {
    boolean foundLogin = false;

    if (request != null) {
        int authType = getRequestAuthType(request);

        // Check for auto login in request
        if (authType == AuthenticationConstants.AUTH_TYPE_REQUEST) {
            String redirectURL = request.getRequestURI().substring(request.getContextPath().length())
                    + (request.getQueryString() != null ? "?" + request.getQueryString() : "");
            request.setAttribute(Constants.AUTH_TYPE_KEY, AuthenticationConstants.AUTH_TYPE_REQUEST);
            request.setAttribute(Constants.AUTH_REDIRECT_KEY, redirectURL);
            request.setAttribute("processLogin", "true");
            foundLogin = true;// ww  w .  j  a  va  2s  .  com

        }

        // Add in check for client certs

        // Check for auto login with cookies, this will only happen if users
        // are allowed to save
        // their logins to cookies
        if (allowSaveLogin && !foundLogin) {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (Constants.COOKIE_NAME.equals(cookie.getName())) {
                        int seperator = cookie.getValue().indexOf('~');
                        final String login;
                        if (seperator > 0) {
                            login = cookie.getValue().substring(0, seperator);
                            if (logger.isDebugEnabled()) {
                                logger.debug("Attempting autologin for user " + login + ".");
                            }

                            String redirectURL = request.getRequestURI()
                                    .substring(request.getContextPath().length())
                                    + (request.getQueryString() != null ? "?" + request.getQueryString() : "");
                            request.setAttribute(Constants.AUTH_LOGIN_KEY,
                                    cookie.getValue().substring(0, seperator));
                            request.setAttribute(Constants.AUTH_TYPE_KEY,
                                    AuthenticationConstants.AUTH_TYPE_PASSWORD_ENC);

                            request.setAttribute(Constants.AUTH_VALUE_KEY,
                                    cookie.getValue().substring(seperator + 1));
                            request.setAttribute(Constants.AUTH_REDIRECT_KEY, redirectURL);
                            request.setAttribute("processLogin", "true");
                            foundLogin = true;
                        }
                    }
                }
            }
        }

    }

    return foundLogin;
}

From source file:com.sun.faban.harness.webclient.Deployer.java

private static void writeHtml(HttpServletRequest request, Writer w, List<String> errHeaders,
        List<String> errDetails) throws IOException {
    w.write("<!DOCTYPE html\n");
    w.write("    PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n");
    w.write("    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-" + "transitional.dtd\">\n");
    w.write("<html>\n");
    w.write("    <head>\n");
    w.write("        <title>" + Config.HARNESS_NAME + " Benchmark/Service Deployment</title>\n");
    w.write("<link rel=\"icon\" type=\"image/gif\" href=\"" + request.getContextPath() + "/img/faban.gif\">");
    w.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/style.css\" />");

    w.write("    </head>\n");
    w.write("    <body>\n");
    w.write("        <table BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\" WIDTH=\"100%\" >\n");
    w.write("            <tr class=\"gradient\">\n");
    w.write("                <td align=\"left\" width=\"25%\" style=\"color:white; font-size:10px\">\n");
    w.write("                &nbsp;&nbsp;<img src=\"img/faban_large.png\" height=\"50\" width=\"58\"/><br></td>\n");
    w.write("                <td align=\"center\" width=\"50%\" style=\"color:white; font-size:10px\">\n");
    w.write("                <b>Benchmark/Service Deployment</b></td>\n");
    w.write("                <td align=\"right\" valign=\"bottom\" width=25% style=\"color:white\">");
    w.write(Config.HARNESS_NAME + "&nbsp;&nbsp;" + Config.HARNESS_VERSION + "&nbsp;</td>\n");
    w.write("            </tr>\n");
    w.write("        </table>");
    w.write("        <br><center><b>");
    for (String errHdr : errHeaders) {
        w.write("            ");
        w.write(errHdr);//from  ww  w.j  av  a2  s . co  m
        w.write("<br>\n");
    }
    w.write("        </b></center>");
    for (String errDetail : errDetails) {
        w.write("         <table border=\"0\" cellpadding=\"4\" cellspacing=\"3\" "
                + "style=\"padding: 2px; border: 2px solid #cccccc; text-align: left; width: 100%;\">\n");
        w.write("         <tbody><tr class=\"even\"><td><pre>");
        w.write(errDetail);
        w.write("</pre></td></tr></tbody></table>\n");
    }
    w.write("    </body>\n");
    w.write("</html>\n");
}

From source file:net.cloudfree.apps.shop.internal.app.JsonListingServlet.java

private static StringBuilder getBaseUrl(final HttpServletRequest req) {
    final StringBuilder builder = new StringBuilder(50);
    builder.append(req.getScheme());//from   w w w  .  j a  v  a  2s . c  o  m
    builder.append("://");
    builder.append(req.getServerName());
    if ((req.getScheme().equals("http") && (req.getServerPort() != 80))
            || (req.getScheme().equals("https") && (req.getServerPort() != 443))) {
        builder.append(":");
        builder.append(req.getServerPort());
    }
    builder.append(req.getContextPath());
    builder.append(req.getServletPath());
    builder.append("/");
    return builder;
}

From source file:com.googlecode.fascinator.portal.services.impl.CachingDynamicPageServiceImpl.java

public static String getURL(HttpServletRequest req, JsonSimpleConfig config) {

    // Sometimes when proxying https behind apache or another web server, if
    // this is managed by the web server the scheme may be reported
    // incorrectly to Fascinator
    String scheme = config.getString(req.getScheme(), "urlSchemeName");
    String serverName = req.getServerName(); // hostname.com
    int serverPort = req.getServerPort(); // 80
    String contextPath = req.getContextPath(); // /redbox

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

    if ((serverPort != 80) && (serverPort != 443)) {
        url.append(":").append(serverPort);
    }//from   w  w w. j  av a2  s.co  m

    url.append(contextPath).append("/");

    return url.toString();
}