Example usage for javax.servlet ServletRequest getRequestDispatcher

List of usage examples for javax.servlet ServletRequest getRequestDispatcher

Introduction

In this page you can find the example usage for javax.servlet ServletRequest getRequestDispatcher.

Prototype

public RequestDispatcher getRequestDispatcher(String path);

Source Link

Document

Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.

Usage

From source file:io.github.microcks.web.filter.StaticResourcesFilter.java

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
    String contextPath = httpRequest.getContextPath();
    String requestURI = httpRequest.getRequestURI();
    requestURI = StringUtils.substringAfter(requestURI, contextPath);
    if (StringUtils.equals("/", requestURI)) {
        requestURI = "/index.html";
    }/*  w w  w .j  av  a  2s .c o m*/
    String newURI = "/dist" + requestURI;
    log.debug("Dispatching to URI: {}", newURI);
    servletRequest.getRequestDispatcher(newURI).forward(servletRequest, servletResponse);
}

From source file:org.openmrs.module.owa.filter.OwaFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    String requestURL = request.getRequestURL().toString();

    String owaBasePath = Context.getAdministrationService().getGlobalProperty(AppManager.KEY_APP_BASE_URL,
            "http://localhost:8080/openmrs/owa");

    if (Context.isAuthenticated()) {
        if (requestURL.startsWith(owaBasePath)) {
            String newURL = requestURL.replace(owaBasePath, "/ms/owa/fileServlet");
            req.getRequestDispatcher(newURL).forward(req, res);
        } else {/*from  ww  w . j  a v  a 2  s .  c o  m*/
            chain.doFilter(req, res);
        }
    } else {
        if (requestURL.startsWith(owaBasePath)) {
            String newURL = requestURL.replace(owaBasePath, "/ms/owa/redirectServlet");
            req.getRequestDispatcher(newURL).forward(req, res);
        } else {
            chain.doFilter(req, res);
        }
    }
}

From source file:org.esupportail.publisher.web.filter.StaticResourcesProductionFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String contextPath = ((HttpServletRequest) request).getContextPath();
    String requestURI = httpRequest.getRequestURI();
    requestURI = StringUtils.substringAfter(requestURI, contextPath);
    if (StringUtils.equals("/", requestURI)) {
        requestURI = "/index.html";
    }/*w  ww. jav  a2  s .  c om*/
    String newURI = "/dist" + requestURI;
    log.debug("RequestDispatcher - setting newURI to {}", newURI);
    request.getRequestDispatcher(newURI).forward(request, response);
}

From source file:com.mobileman.filter.OpenSessionFilter.java

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

    if (webApplicationContext == null) {
        HttpServletRequest rq1 = (HttpServletRequest) request;
        String requestURI = rq1.getRequestURI();
        if (!requestURI.endsWith(INIT_SERVLET_URI)) {
            request.getRequestDispatcher(INIT_SERVLET_URI).forward(request, response);
            return;
        }//www .  j  a  v a2 s  .  co m
    }

    SessionFactory sessionFactory = lookupSessionFactory();
    boolean participate = false;

    if (sessionFactory != null) {
        if (isSingleSession()) {
            // single session mode
            if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
                // Do not modify the Session: just set the participate flag.
                participate = true;
            } else {
                Session session = getSession(sessionFactory);
                TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
            }
        } else {
            // deferred close mode
            if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
                // Do not modify deferred close: just set the participate
                // flag.
                participate = true;
            } else {
                SessionFactoryUtils.initDeferredClose(sessionFactory);
            }
        }
    }

    try {
        filterChain.doFilter(request, response);
    }

    finally {
        if (sessionFactory != null) {
            if (!participate) {
                if (isSingleSession()) {
                    // single session mode
                    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
                            .unbindResource(sessionFactory);
                    closeSession(sessionHolder.getSession(), sessionFactory);
                } else {
                    // deferred close mode
                    SessionFactoryUtils.processDeferredClose(sessionFactory);
                }
            }
        }
    }
}

From source file:org.mfr.web.PrivateContentAccessManager.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    HttpServletRequest servletR = ((HttpServletRequest) request);
    RequestWrapper.httpRequest.set(servletR);
    try {/*from   w ww.jav a  2  s  .c o  m*/
        String requestUri = servletR.getRequestURI();
        Site site = (Site) request.getAttribute(SiteFilter.PORTFOLIOSITE);
        ;

        if (requestUri.length() == 1) {
            if (!SiteDao.isSitePasswordRequired(site) && !HttpHelper.isSiteGranted(site)) {
                request.getRequestDispatcher("/index.zul").forward(request, response);
                return;
            } else {
                if (SiteDao.isSitePasswordRequired(site) && !HttpHelper.isSiteGranted(site)) {
                    request.getRequestDispatcher("/privateaccess.zul").forward(request, response);
                } else {
                    request.getRequestDispatcher("/pindex.zul").forward(request, response);
                }
                return;
            }

        }
        int lastIndex = requestUri.lastIndexOf("/");

        requestUri = requestUri.substring(lastIndex);
        logger.debug("pageActUri [" + requestUri + "]");
        request.setAttribute(GlobalVariableResolver.PAGEACTURI, requestUri);
        User user = HttpHelper.getUser(((HttpServletRequest) request).getSession());
        String threadName = Thread.currentThread().getName();
        try {

            Thread.currentThread().setName(
                    requestUri + "-" + ((HttpServletRequest) request).getSession().getId() + "-" + count++);

            List<String> actualAllowedPages = null;
            String redirectUrl = null;
            boolean hasPermission = hasPermission(requestUri);
            if (!SiteDao.isSitePasswordRequired(site) && SiteDao.notIsPublicOrOwner(site)) {
                actualAllowedPages = allowedPage;
                redirectUrl = LoginComposer.INDEXPAGE;
            } else {
                if (GlobalVariableResolver.isSiteOwner()) {
                    redirectUrl = LoginComposer.PINDEXPAGE;
                } else if (SiteDao.isSitePasswordRequired(site) && !HttpHelper.isPasswordAccessGranted(site)) {
                    redirectUrl = "/privateaccess.zul";
                } else {
                    redirectUrl = LoginComposer.PINDEXPAGE;
                }
                actualAllowedPages = portfolioAllowedPage;
            }
            if (!hasPermission && (!actualAllowedPages.contains(requestUri) && user == null)
                    || (site != null && !redirectUrl.equals(requestUri)
                            && (!actualAllowedPages.contains(requestUri) || !HttpHelper.isSiteGranted(site)))) {
                request.getRequestDispatcher(redirectUrl).forward(request, response);
            } else {
                filterChain.doFilter(request, response);
            }
        } catch (ServletException e) {
            logger.error("doFilter", e);
            throw e;
        } finally {
            Thread.currentThread().setName(threadName);
        }
    } catch (Exception e) {
        logger.error("filter", e);
    } finally {
        RequestWrapper.httpRequest.set(null);
    }

}

From source file:com.persistent.cloudninja.controller.CloudNinjaAuthFilter.java

private void forwardToNextPage(ServletRequest request, ServletResponse response, FilterChain filterChain) {
    try {/*from   w ww  . j av a  2 s.  c  om*/
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        if (httpServletRequest.getRequestURI().contains("/logout")) {
            removeCookie(httpServletRequest, httpServletResponse, CloudNinjaConstants.AUTH_COOKIE_NAME);
            request.getRequestDispatcher("/logout.htm").forward(httpServletRequest, httpServletResponse);
        } else {
            filterChain.doFilter(request, response);
        }
    } catch (ServletException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.curl.orb.servlet.DefaultInstanceManagementFilter.java

@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    // accept just HTTP.
    if (!((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)))
        throw new ServletException("The request is not HTTP protocol.");

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    InstanceManagementRequest instanceManagementRequest = null;
    // serialize the request object and set to the attribute
    try {/*from ww w .j a  v  a  2  s. c om*/
        if (request.getContentLength() == -1) {
            // forward to requested page.
            request.getRequestDispatcher(httpRequest.getServletPath()).forward(request, response);
            return;
        }
        instanceManagementRequest = (InstanceManagementRequest) serializer
                .deserialize(httpRequest.getInputStream());
        // debug request object (instanceManagementRequest.toString())
        log.debug("Curl ORB Request object [" + instanceManagementRequest + "]");
        httpRequest.setAttribute(InstanceManagementServlet.REQUEST_OBJECT, instanceManagementRequest);
    } catch (SerializerException e) {
        // NOTE: 
        //  Cannot handle the exception when "stream response" mode. 
        //  Clients cannot receive the error.
        setResponse((HttpServletResponse) response,
                new InstanceManagementException("Could not deserialize the object from Curl.", e), null);
        return;
    }

    try {
        // do filters and servlets
        chain.doFilter(httpRequest, response);
        // deserialize the object and return the response
        setResponse((HttpServletResponse) response,
                httpRequest.getAttribute(InstanceManagementServlet.RESPONSE_OBJECT),
                (Map<String, Object>) httpRequest
                        .getAttribute(InstanceManagementServlet.RESPONSE_SUBORDINATE_OBJECT));
    } catch (Exception e) {
        setResponse((HttpServletResponse) response, e, null);
    }
}

From source file:org.wso2.carbon.appfactory.utilities.filters.RootContextRedirectFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (!(response instanceof HttpServletResponse)) {
        return;// w  w  w  .j  a  va 2 s  . co m
    }
    String appContext = ServiceReferenceHolder.getInstance().getAppFactoryConfiguration()
            .getFirstProperty(AppFactoryConstants.APPFACTORY_WEB_CONTEXT_ROOT);

    if (appContext != null && !appContext.isEmpty()) {
        log.info("ROOT / context is called. Redirecting to App Factory home page with context - " + appContext);
        ((HttpServletResponse) response).sendRedirect(appContext);
    } else {
        log.debug(
                "No appfactory root context is defined in appfactory.xml. Request will be redirected to carbon console");
        RequestDispatcher requestDispatcher = request.getRequestDispatcher(
                CarbonConfigurationContextFactory.getConfigurationContext().getContextRoot());
        requestDispatcher.forward(request, response);
    }
}

From source file:com.ocpsoft.pretty.PrettyFilter.java

public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {
    String requestURI = getRequestURI(request);
    if (getConfig().isURLMapped(requestURI)) {
        PrettyContext prettyContext = PrettyContext.newInstance((HttpServletRequest) request);
        FacesContext facesContext = initFacesContext(request, response);
        prettyContext.injectParameters();
        prettyContext.computeDynamicViewId();
        String viewId = prettyContext.getCurrentCalculatedViewId();
        messagesUtils.saveMessages(facesContext);
        facesContext.release();// w  w  w  .jav a 2  s  . co  m

        log.info("Forwarding mapped request [" + requestURI + "] to JSF viewId [" + viewId + "]");
        if (!response.isCommitted()) {
            request.getRequestDispatcher(viewId).forward(request, response);
        }
    } else {
        ensurePopulatedContext(request);
        log.debug("Request is not mapped using PrettyFaces. Continue.");
        chain.doFilter(request, response);
    }
}

From source file:de.highbyte_le.weberknecht.security.filters.LoginPageForwardFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (logger.isDebugEnabled())
        logger.debug("doFilter() - start");

    boolean forward = true;

    if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
        logger.error("servlet request is no HTTP servlet request");
    } else {//from  www  . ja  v  a 2  s .c o  m
        HttpSession session = ((HttpServletRequest) request).getSession();

        UserAuthentication userAuthentication = (UserAuthentication) session.getAttribute("user_auth");
        if (userAuthentication != null && userAuthentication.isAuthenticated())
            forward = false;
    }

    if (forward) {
        request.setAttribute("de.highbyte_le.weberknecht.login.status", "failed");

        logger.debug("doFilter() - forward to login page");
        RequestDispatcher rd = request.getRequestDispatcher(loginPage);
        rd.forward(request, response);
    } else {
        logger.debug("doFilter() - Continue with filter chain");
        chain.doFilter(request, response);
    }
}