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:com.consol.citrus.admin.web.ProjectSetupInterceptor.java

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    if (projectService.getActiveProject() == null
            && !request.getRequestURI().startsWith(request.getContextPath() + redirect)
            && !request.getRequestURI().startsWith(redirect)
            && !isExcluded(request.getRequestURI(), request.getContextPath())) {
        log.debug("Intercept " + request.getRequestURI() + " as project home is not set properly");
        log.debug("Redirecting to " + request.getContextPath() + redirect);
        response.sendRedirect(request.getContextPath() + redirect);
        return false;
    }//from   w  ww .  j  a va 2  s.c om

    return true;
}

From source file:org.opencron.server.service.SecurityHandlerInterceptor.java

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {

    HttpSession session = request.getSession();
    String requestURI = request.getContextPath() + request.getServletPath();

    //???,?/*from w  ww  .j a va2 s  .co  m*/
    if (requestURI.contains("/css/") || requestURI.contains("/fonts/") || requestURI.contains("/img/")
            || requestURI.contains("/js/") || requestURI.contains("/WEB-INF")) {
        return super.preHandle(request, response, handler);
    }

    //
    if (requestURI.contains("/login") || requestURI.contains("/upload")) {
        return super.preHandle(request, response, handler);
    }

    String referer = request.getHeader("referer");
    if (referer != null && !referer.startsWith(WebUtils.getWebUrlPath(request))) {
        response.sendRedirect("/");
        logger.info("[opencron]Bad request,redirect to login page");
        OpencronTools.invalidSession();
        return false;
    }

    User user = OpencronTools.getUser();
    if (user == null) {
        logger.info(request.getRequestURL().toString());
        //?
        response.sendRedirect("/");
        OpencronTools.invalidSession();
        logger.info("[opencron]session is null,redirect to login page");
        return false;
    }

    //????
    if (!OpencronTools.isPermission(session) && (requestURI.contains("/config/")
            || requestURI.contains("/user/view") || requestURI.contains("/user/add")
            || requestURI.contains("/agent/add") || requestURI.contains("/agent/edit"))) {
        logger.info("[opencron]illegal or limited access");
        return false;
    }

    if (handler instanceof HandlerMethod) {
        if (!verifyCSRF(request)) {
            response.sendRedirect("/");
            logger.info("[opencron]Bad request,redirect to login page");
            OpencronTools.invalidSession();
            return false;
        }
    }

    return super.preHandle(request, response, handler);
}

From source file:psiprobe.controllers.apps.RemoveApplicationAttributeController.java

@Override
protected ModelAndView handleContext(String contextName, Context context, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    String attrName = ServletRequestUtils.getStringParameter(request, "attr");
    context.getServletContext().removeAttribute(attrName);

    return new ModelAndView(
            new RedirectView(request.getContextPath() + getViewName() + "?" + request.getQueryString()));
}

From source file:com.googlesource.gerrit.plugins.github.oauth.OAuthGitFilter.java

private String getRequestPathWithQueryString(HttpServletRequest httpRequest) {
    String requestPathWithQueryString = httpRequest.getContextPath() + httpRequest.getServletPath()
            + Strings.nullToEmpty(httpRequest.getPathInfo()) + "?" + httpRequest.getQueryString();
    return requestPathWithQueryString;
}

From source file:net.big_oh.common.web.listener.request.ObservedHttpServletRequest.java

public ObservedHttpServletRequest(HttpServletRequest request) {
    super(request);

    httpMethod = request.getMethod();//from  w w  w . j  a  v a2s . c o  m

    resourceRequested = request.getRequestURL().toString();

    queryString = request.getQueryString();

    contextName = request.getContextPath();

    containerUserName = request.getRemoteUser();

    HttpSession session = request.getSession(false);
    jSessionId = (session == null) ? null : session.getId();
}

From source file:com.leapfrog.springhibernate.filter.AuthInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
    HttpSession session = request.getSession();
    Boolean checking = (Boolean) session.getAttribute("LoggedIn");
    if (checking == null || checking == false) {
        response.sendRedirect(request.getContextPath());
        return false;
    } else {/*from   ww  w . ja v  a2 s.co  m*/
        return true;
    }

}

From source file:com.twocharts.www.samples.apps.marketplace.OpenIdServlet.java

/**
 * Gets the <code>openid.return_to</code> URL to advertise to the IDP.  Dynamically constructs
 * the URL based on the current request.
 * @param request Current servlet request
 * @return Return to URL/*from   w ww  .  jav a 2  s .c  o m*/
 */
String returnTo(HttpServletRequest request) {
    return new StringBuffer(baseUrl(request)).append(request.getContextPath()).append(returnToPath).toString();
}

From source file:com.piusvelte.hydra.HydraRequest.java

private String[] getPathParts(HttpServletRequest request) {
    String path = request.getRequestURI().substring(request.getContextPath().length() + 4);
    String[] parts = new String[] { null, null };
    if (path.length() > 0) {
        if (path.substring(0, 1).equals("/"))
            path = path.substring(1);/*from  ww w.j  av a  2  s.  c o  m*/
        if (path.length() > 0) {
            String[] paths = path.split("/", -1);
            if (paths[DATABASE].length() > 0)
                parts[DATABASE] = paths[DATABASE];
            if ((paths.length > TARGET) && (paths[TARGET].length() > 0))
                parts[TARGET] = paths[TARGET];
        }
    }
    return parts;
}

From source file:com.sap.prd.mobile.ios.ota.webapp.BaseServlet.java

/**
 * Returns the Application Base URL based on the request.
 * @return the URL//from www  .ja v a2s  . c  om
 * @throws MalformedURLException
 */
public URL getApplicationBaseUrl(HttpServletRequest request) throws MalformedURLException {
    String requestUrl = request.getRequestURL().toString(); //e.g. "http://host:8765/ota-service/HTML/UmVmZXJlcj1odHRw..."
    String contextPath = request.getContextPath().toString(); //e.g. "/ota-service" or "" if root context

    String result;
    if (!isEmpty(contextPath)) {
        int idx = requestUrl.indexOf(contextPath);
        if (idx < 0)
            throw new IllegalStateException(format("Cannot find '%s' in '%s'", contextPath, requestUrl));
        result = requestUrl.substring(0, idx + contextPath.length()); //e.g. "http://host:8765/ota-service"
    } else { //root context
        int idx = requestUrl.indexOf("//");
        idx = requestUrl.indexOf("/", idx + "//".length());
        result = requestUrl.substring(0, idx); //e.g. "http://host:8765"
    }
    return new URL(result);
}

From source file:com.temenos.interaction.core.web.RequestContextFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    final HttpServletRequest servletRequest = (HttpServletRequest) request;

    String requestURI = servletRequest.getRequestURI();
    requestURI = StringUtils.removeStart(requestURI,
            servletRequest.getContextPath() + servletRequest.getServletPath());
    String baseURL = StringUtils.removeEnd(servletRequest.getRequestURL().toString(), requestURI);

    Map<String, List<String>> headersMap = new HashMap<>();
    Enumeration<String> headerNames = servletRequest.getHeaderNames();
    if (headerNames != null) {
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            List<String> valuesList = Collections.list(servletRequest.getHeaders(headerName));
            headersMap.put(headerName, valuesList);
        }/*from   w w  w  . j  av a 2s  . co m*/
    }

    RequestContext ctx;
    Principal userPrincipal = servletRequest.getUserPrincipal();
    if (userPrincipal != null) {
        ctx = new RequestContext(baseURL, servletRequest.getRequestURI(),
                servletRequest.getHeader(RequestContext.HATEOAS_OPTIONS_HEADER), userPrincipal, headersMap);
    } else {
        ctx = new RequestContext(baseURL, servletRequest.getRequestURI(),
                servletRequest.getHeader(RequestContext.HATEOAS_OPTIONS_HEADER), headersMap);
    }

    RequestContext.setRequestContext(ctx);

    try {
        chain.doFilter(request, response);
    } finally {
        RequestContext.clearRequestContext();
    }
}