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.github.javarch.jsf.context.FacesContextUtils.java

/**
 * Retorna o caminho da aplicao, incluindo protocolo http.
 * /*from  ww w.ja  va  2s . c o  m*/
 * @return Caminho da aplicao
 */
public String getApplicationPath() {
    HttpServletRequest request = getRequest();
    return request.getScheme() + "://" + request.getServerName()
            + (request.getServerPort() != 80 ? ":" + request.getServerPort() : "") + request.getContextPath();
}

From source file:net.sf.j2ep.ProxyFilter.java

/**
 * Will build a URI but including the Query String. That means that it really
 * isn't a URI, but quite near.//from   w w w. j a  v  a2  s  . c o m
 * 
 * @param httpRequest Request to get the URI and query string from
 * @return The URI for this request including the query string
 */
private String getURI(HttpServletRequest httpRequest) {
    String contextPath = httpRequest.getContextPath();
    String uri = httpRequest.getRequestURI().substring(contextPath.length());
    if (httpRequest.getQueryString() != null) {
        uri += "?" + httpRequest.getQueryString();
    }
    return uri;
}

From source file:br.com.gerenciapessoal.security.JsfRedirectStrategy.java

/**
 * Redirects the response to the supplied URL.
 * <p>/*  ww w . j  a  v  a 2 s  .  co m*/
 * If <tt>contextRelative</tt> is set, the redirect value will be the value
 * after the request context path. Note that this will result in the loss of
 * protocol information (HTTP or HTTPS), so will cause problems if a
 * redirect is being performed to change to HTTPS, for example.
 *
 * @param request
 * @param response
 * @param url
 * @throws java.io.IOException
 */
@Override
public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url)
        throws IOException {
    String redirectUrl = calculateRedirectUrl(request.getContextPath(), url);
    redirectUrl = response.encodeRedirectURL(redirectUrl);

    if (logger.isDebugEnabled()) {
        logger.debug("Redirecting to '" + redirectUrl + "'");
    }

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

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

        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 {
        response.sendRedirect(redirectUrl);
    }

}

From source file:com.exxonmobile.ace.hybris.storefront.security.DefaultAccessDeniedHandler.java

@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
        AccessDeniedException accessDeniedException) throws IOException, ServletException {
    // redirecting to the home page when a 403 http error occurs.

    response.sendRedirect(request.getContextPath());
}

From source file:alfio.controller.api.user.RestEventApiController.java

@RequestMapping("events")
public List<EventListItem> listEvents(HttpServletRequest request) {
    return eventManager.getPublishedEvents().stream()
            .map(e -> new EventListItem(e, request.getContextPath(), descriptionsLoader.eventDescriptions()))
            .collect(Collectors.toList());
}

From source file:org.archive.wayback.webapp.RequestMapper.java

private String getContextID(HttpServletRequest request) {
    String requestPath = request.getRequestURI();
    String contextPath = request.getContextPath();
    if (requestPath.startsWith(contextPath)) {
        requestPath = requestPath.substring(contextPath.length());
    }//from   www .  ja  v  a2  s  .co m
    String collection = "";
    if (requestPath.startsWith("/")) {
        int secondSlash = requestPath.indexOf("/", 1);
        if (secondSlash != -1) {
            collection = PORT_SEPARATOR + requestPath.substring(1, requestPath.indexOf("/", 1));
        } else {
            collection = PORT_SEPARATOR + requestPath.substring(1);
        }
    }
    return String.valueOf(request.getLocalPort()) + collection;
}

From source file:net.sourceforge.fenixedu.presentationTier.TagLib.units.UnitsTree.java

private void putImage(Unit parentUnit, Unit parentUnitParent, StringBuilder buffer,
        HttpServletRequest request) {
    buffer.append("<img ").append("src='").append(request.getContextPath())
            .append((getExpanded() != null && Boolean.valueOf(getExpanded()) ? "/images/toggle_minus10.gif"
                    : "/images/toggle_plus10.gif"))
            .append("' id=\"").append(parentUnit.getExternalId())
            .append((parentUnitParent != null) ? parentUnitParent.getExternalId() : "").append("\" ")
            .append("indexed='true' onClick=\"").append("check(document.getElementById('").append("aa")
            .append(parentUnit.getExternalId())
            .append((parentUnitParent != null) ? parentUnitParent.getExternalId() : "")
            .append("'),document.getElementById('").append(parentUnit.getExternalId())
            .append((parentUnitParent != null) ? parentUnitParent.getExternalId() : "")
            .append("'));return false;").append("\"> ");
}

From source file:com.github.terma.gigaspacewebconsole.server.ImportServlet.java

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {
    try {//from  ww w.ja  va 2s . co m
        safeDoPost(request);

        response.sendRedirect(request.getContextPath());
    } catch (IOException exception) {
        throw exception;
    } catch (Exception exception) {
        throw new IOException(exception);
    }
}

From source file:Controllers.SeatSelectionController.java

@RequestMapping(value = "/seatselection", method = RequestMethod.POST)
protected ModelAndView getSeatSelectionPage(@RequestParam("adult") int numAdults,
        @RequestParam("senior") int numSeniors, @RequestParam("child") int numChildren,
        HttpServletRequest request) {

    String contextPath = request.getContextPath();
    System.out.println("Path: " + contextPath);
    request.setAttribute("contextPath", contextPath);

    HttpSession session = request.getSession();
    int totalNumSeats = numAdults + numSeniors + numChildren;
    session.setAttribute("numAdults", numAdults);
    request.setAttribute("numAdults", numAdults);
    session.setAttribute("numSeniors", numSeniors);
    request.setAttribute("numSeniors", numSeniors);
    session.setAttribute("numChildren", numChildren);
    request.setAttribute("numChildren", numChildren);
    session.setAttribute("totalNumSeats", totalNumSeats);
    request.setAttribute("totalNumSeats", totalNumSeats);
    Showing showing = (Showing) session.getAttribute("showing");

    ModelAndView modelandview;//from  w w  w. ja v  a  2 s  .  c o  m
    String seatingLayout = showing.getTheatreRoom().getLayout();
    if (!seatingLayout.equals("")) {
        seatingLayout = seatingLayout.replaceAll(" ", "");
        String[] rowsArray = seatingLayout.split("R");
        int numColumns = rowsArray[0].replaceAll(",", "").length();
        int numRows = rowsArray.length;
        Seat[][] seatingMatrix = new Seat[numRows][numColumns];
        int seatValue = 0;

        char seatRow = 'A';
        int seatNum = 1;
        System.out.println("NumRows: " + numRows);
        System.out.println("NumColumns: " + numColumns);
        for (int rowIndex = 0; rowIndex < rowsArray.length; rowIndex++) {
            String rowLayout = rowsArray[rowIndex];
            String[] seatValuesArray = rowLayout.split(",");
            for (int seatIndex = 0; seatIndex < seatValuesArray.length; seatIndex++) {
                if (!seatValuesArray[seatIndex].equals("")) {
                    seatValue = Integer.parseInt(seatValuesArray[seatIndex]);
                }
                Seat seat = null;
                if (seatValue == 1) {
                    seat = new Seat();
                    seat.setRow(String.valueOf(seatRow));
                    seat.setSeatNumber(Integer.toString(seatNum));
                    seat.setAvailable(true);
                    seatNum++;
                }
                seatingMatrix[rowIndex][seatIndex] = seat;
            }
            seatRow = (char) (seatRow + 1);
            seatNum = 1;
        }

        // make purchased seats unavailable
        List<Ticket> purchasedTickets = ticketService.getTicketByShowing(showing);
        for (Ticket ticket : purchasedTickets) {
            char purchasedSeatRow = ticket.getSeat().getRow().charAt(0);
            int rowIndex = ((int) purchasedSeatRow) - ((int) 'A');
            // now that we have the right row seat for this seat search all of the columns for it
            for (int i = 0; i < numColumns; i++) {
                Seat seatInMatrix = seatingMatrix[rowIndex][i];
                if (seatInMatrix != null) {
                    if (seatInMatrix.getRow().equals(ticket.getSeat().getRow())
                            && seatInMatrix.getSeatNumber().equals(ticket.getSeat().getSeatNumber())) {
                        seatInMatrix.setAvailable(false);
                    }
                }
            }
        }

        // make all locked live seats unavailable
        LiveTickets ts = ticketService.getLiveTickets();
        List<Ticket> tickets = ts.getTicketsByShowing(showing);
        for (Ticket ticket : tickets) {
            Seat seat = ticket.getSeat();
            char purchasedSeatRow = seat.getRow().charAt(0);
            int rowIndex = ((int) purchasedSeatRow) - ((int) 'A');
            for (int i = 0; i < numColumns; i++) {
                Seat seatInMatrix = seatingMatrix[rowIndex][i];
                if (seatInMatrix != null) {
                    if (seatInMatrix.getRow().equals(ticket.getSeat().getRow())
                            && seatInMatrix.getSeatNumber().equals(ticket.getSeat().getSeatNumber())) {
                        seatInMatrix.setAvailable(false);
                    }
                }
            }
        }

        request.setAttribute("seatingMatrix", seatingMatrix);
        modelandview = new ModelAndView("seatselection");
    } else {
        modelandview = new ModelAndView("paymentpage");
    }
    return modelandview;
}

From source file:UrlRewrite.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    String contextPath = request.getContextPath();
    String encodedUrl = response.encodeURL(contextPath + "/default.jsp");

    out.println("<html>");
    out.println("<head>");
    out.println("<title>URL Rewriter</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>This page will use URL rewriting if necessary</h2>");
    out.println("Go to the default.jsp page <a href=\"" + encodedUrl + "\">here</a>.");
    out.println("</body>");
    out.println("</html>");

}