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.eharmony.services.swagger.SwaggerResourceServer.java

/**
 * Provides initial entry point by redirecting Swagger js with desired target url as parameter.
 *///from   w w  w  .j  av  a 2 s . c  om
@GET
public Response getBase(@Context HttpServletRequest request) throws IOException, URISyntaxException {
    String cp = contextPath;
    if (StringUtils.isBlank(contextPath)) {
        cp = request.getContextPath();
    }

    if (!cp.endsWith("/")) {
        cp += "/";
    }

    String loc = "swagger-ui/index.html?url=" + cp + "swagger.json";

    String queryString = request.getQueryString();
    if (StringUtils.isNotBlank(queryString)) {
        loc += "&" + queryString;
    }

    return Response.seeOther(new URI(loc)).build();
}

From source file:gov.nih.nci.calims2.taglib.form.FormTag.java

/**
 * {@inheritDoc}/*from  w w w . j a  v a2  s .co  m*/
 */
protected Tag createTag() {
    Tag formTag = new CompositeTag(HtmlElement.FORM);
    formTag.addAttribute("dojoType", DojoType.DIJIT_FORM_FORM.getTypeName());
    addAttributes(formTag);
    addBody(formTag);
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    String fullAction = (action != null && action.startsWith("/")) ? request.getContextPath() + action : action;
    formTag.addAttribute("action", fullAction);
    formTag.addAttribute("enctype", enctype);
    formTag.addAttribute("method", method);
    return formTag;
}

From source file:com.pontecultural.flashcards.DeckController.java

@RequestMapping(method = RequestMethod.GET)
public String getUploadForm(HttpServletRequest req, Locale locale, Model model) {
    logger.info("List of decks");

    // compute base URL to allow client to call us back.
    String baseURL = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort()
            + req.getContextPath();

    model.addAttribute("baseURL", baseURL);
    model.addAttribute("decks", jdbcFlashcardsDao.fetchAllDecks());
    return "deckList";
}

From source file:com.easyshop.common.web.filter.SetCommonDataFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    ////ww w . jav a 2 s  . c o  m
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    if (request.getAttribute(Constants.CONTEXT_PATH) == null) {
        request.setAttribute(Constants.CONTEXT_PATH, httpServletRequest.getContextPath());
    }
    if (request.getAttribute(Constants.WEB_BASE) == null) {
        request.setAttribute(Constants.WEB_BASE, webBase);
    }

    //?
    String currentPageStr = request.getParameter(currentPageParamKey);
    if (StringUtils.isNotBlank(currentPageStr)) {
        try {
            Integer currentPage = Integer.valueOf(currentPageStr);
            PageContext.setCurrentPage(currentPage);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
            LOGGER.warn(
                    "?'currentPage'?,???,?{}",
                    currentPageStr);
        }
    }

    String pageSizeStr = request.getParameter(pageSizeParamKey);
    if (StringUtils.isNotBlank(pageSizeStr)) {
        try {
            Integer pageSize = Integer.valueOf(pageSizeStr);
            PageContext.setPageSize(pageSize);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
            LOGGER.warn(
                    "?'pageSize'?,???,?{}",
                    pageSizeStr);
        }
    }

    try {
        chain.doFilter(request, response);
    } finally {
        PageContext.clear();
    }
}

From source file:com.softwareaggov.messaging.simplesoapjms.client.web.SendSoapCall.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();

    out.write("<h1>" + req.getContextPath() + " - Sending SOAP Call</h1>");
    try {/*from   w ww  .j av a 2  s  .c o  m*/
        String callType = req.getParameter("calltype");
        SoapClientLocal soapJmsService = null;
        if (null != callType) {
            if ("oneway".equals(callType.toLowerCase()))
                soapJmsService = soapHttpClientOneWayService;
            else if ("twoway".equals(callType.toLowerCase()))
                soapJmsService = soapHttpClientTwoWayService;
            else
                throw new IllegalArgumentException("call type [" + callType + "] not supported.");
        } else {
            throw new IllegalArgumentException("call type [null] not supported.");
        }

        if (null == soapJmsService)
            throw new IllegalArgumentException(
                    "soapJmsService is null...should not be...check code or configs");

        String response = soapJmsService.callWS();

        String responseToDisplay = "";
        if (null != response) {
            if (response.toLowerCase().startsWith("<?xml")) {
                responseToDisplay = StringEscapeUtils.escapeXml10(response);
            } else {
                responseToDisplay = response;
            }
        } else {
            responseToDisplay = "null";
        }

        out.write("<p><b>messages sent successfully</b></p>");
        out.write(String.format("<div><p>Response:</p><p>%s</p></div>", responseToDisplay));

        out.close();
    } catch (Throwable exc) {
        log.error("Error Occurred", exc);
        throw new ServletException(exc);
    }
}

From source file:com.softwareaggov.messaging.simplesoapjms.client.web.SendSoapJmsCall.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();

    out.write("<h1>" + req.getContextPath() + " - Sending SOAP Call</h1>");
    try {//  w w  w  .  j  av a  2  s  .com
        String callType = req.getParameter("calltype");
        SoapClientLocal soapJmsService = null;
        if (null != callType) {
            if ("oneway".equals(callType.toLowerCase()))
                soapJmsService = soapJmsClientOneWayService;
            else if ("twoway".equals(callType.toLowerCase()))
                soapJmsService = soapJmsClientTwoWayService;
            else
                throw new IllegalArgumentException("call type [" + callType + "] not supported.");
        } else {
            throw new IllegalArgumentException("call type [null] not supported.");
        }

        if (null == soapJmsService)
            throw new IllegalArgumentException(
                    "soapJmsService is null...should not be...check code or configs");

        String response = soapJmsService.callWS();

        String responseToDisplay = "";
        if (null != response) {
            if (response.toLowerCase().startsWith("<?xml")) {
                responseToDisplay = StringEscapeUtils.escapeXml10(response);
            } else {
                responseToDisplay = response;
            }
        } else {
            responseToDisplay = "null";
        }

        out.write("<p><b>messages sent successfully</b></p>");
        out.write(String.format("<div><p>Response:</p><p>%s</p></div>", responseToDisplay));

        out.close();
    } catch (Throwable exc) {
        log.error("Error Occurred", exc);
        throw new ServletException(exc);
    }
}

From source file:org.jdal.auth.AuthFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws ServletException, IOException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    if (!isLoginPage(req) && req.getSession().getAttribute(SESSION_USER_KEY) == null) {
        // not authorized
        res.sendRedirect(req.getContextPath() + "/" + loginPage);
    } else {/*from   w  ww . ja v a2s . c o  m*/
        // authorized
        chain.doFilter(request, response);
    }

}

From source file:org.xine.marketplace.frontend.views.security.JsfAccessDeniedHandler.java

@Override
public void handle(final HttpServletRequest request, final HttpServletResponse response,
        final AccessDeniedException accessDeniedException) throws IOException, ServletException {
    String redirectUrl = calculateRedirectUrl(request.getContextPath(), this.loginPath);
    redirectUrl = response.encodeRedirectURL(redirectUrl);

    // we should redirect using ajax response if the case warrants
    final boolean ajaxRedirect = request.getHeader("faces-request") != null
            && request.getHeader("faces-request").toLowerCase().indexOf("ajax") > -1;

    if (ajaxRedirect) {
        // javax.faces.context.FacesContext ctxt =
        // javax.faces.context.FacesContext.getCurrentInstance();
        // ctxt.getExternalContext().redirect(redirectUrl);

        final String ajaxRedirectXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<partial-response><redirect url=\"" + redirectUrl + "\"></redirect></partial-response>";
        response.setContentType("text/xml");
        response.getWriter().write(ajaxRedirectXml);
    } else {//from   w w  w.  j av a  2 s .c  o m
        response.sendRedirect(redirectUrl);
    }
}

From source file:com.acmeair.web.RESTCookieSessionFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    try {/* ww  w. j  a v  a 2s .  c om*/
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        String path = request.getContextPath() + request.getServletPath() + request.getPathInfo();
        // The following code is to ensure that OG is always set on the thread
        try {
            TransactionService txService = getTxService();
            if (txService != null)
                txService.prepareForTransaction();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // could do .startsWith for now, but plan to move LOGOUT to its own REST interface eventually
        if (path.endsWith(LOGIN_PATH) || path.endsWith(LOGOUT_PATH)) {
            // if logging in, let the request flow
            chain.doFilter(req, resp);
            return;
        }

        Cookie cookies[] = request.getCookies();
        Cookie sessionCookie = null;
        if (cookies != null) {
            for (Cookie c : cookies) {
                if (c.getName().equals(LoginREST.SESSIONID_COOKIE_NAME)) {
                    sessionCookie = c;
                }
                if (sessionCookie != null)
                    break;
            }
            String sessionId = "";
            if (sessionCookie != null) // We need both cookie to work
                sessionId = sessionCookie.getValue().trim();
            else {
                log.info("falling through with a sessionCookie break, but it was null");
            }
            // did this check as the logout currently sets the cookie value to "" instead of aging it out
            // see comment in LogingREST.java
            if (sessionId.equals("")) {
                log.info("sending SC_FORBIDDEN due to empty session cookie");
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
            // Need the URLDecoder so that I can get @ not %40
            ValidateTokenCommand validateCommand = new ValidateTokenCommand(sessionId);
            CustomerSession cs = validateCommand.execute();
            if (cs != null) {
                request.setAttribute(LOGIN_USER, cs.getCustomerid());
                chain.doFilter(req, resp);
                return;
            } else {
                log.info("sending SC_FORBIDDEN due  to validateCommand returning null");
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
        }

        // if we got here, we didn't detect the session cookie, so we need to return 403
        log.info("sending SC_FORBIDDEN due finding no sessionCookie");
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
}

From source file:com.nec.harvest.security.handler.HarvestAuthenticationSuccessHandler.java

protected void sendRedirectToLogout(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType(HttpServletContentType.PLAN_TEXT);
    response.sendRedirect(request.getContextPath() + "/logout");
    response.flushBuffer();/*from ww  w. ja v  a2  s  . com*/
}