Java Code Examples for javax.servlet.ServletContext#getNamedDispatcher()

The following examples show how to use javax.servlet.ServletContext#getNamedDispatcher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestDispatcherServiceImpl.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
public PortletRequestDispatcher getNamedDispatcher(ServletContext servletContext, PortletApplicationDefinition app,
                                                   String name)
{
    if (LOG.isDebugEnabled())
    {
        LOG.debug("Named PortletRequestDispatcher requested for name: "+name+" at context: "+app.getContextPath());
    }
    
    RequestDispatcher dispatcher = servletContext.getNamedDispatcher(name);
    if (dispatcher != null)
    {
        return new PortletRequestDispatcherImpl(dispatcher, name, true);
    }
    if (LOG.isInfoEnabled())
    {
        LOG.info("No matching request dispatcher found for name: "+ name);
    }
    return null;
}
 
Example 2
Source File: GenericWebServiceServlet.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void redirect(HttpServletRequest request, HttpServletResponse response, String pathInfo) throws ServletException {

		String theServletPath = dispatcherServletPath == null ? "/" : dispatcherServletPath;

		ServletContext sc = super.getServletContext();
		RequestDispatcher rd = dispatcherServletName != null ? sc.getNamedDispatcher(dispatcherServletName) : sc.getRequestDispatcher(theServletPath + pathInfo);
		if (rd == null) {
			throw new ServletException("No RequestDispatcher can be created for path " + pathInfo);
		}
		try {
			HttpServletRequestFilter servletRequest = new HttpServletRequestFilter(request, pathInfo, theServletPath);
			rd.forward(servletRequest, response);
		} catch (Throwable ex) {
			throw new ServletException("RequestDispatcher for path " + pathInfo + " has failed");
		}
	}
 
Example 3
Source File: ServletForwardingController.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	ServletContext servletContext = getServletContext();
	Assert.state(servletContext != null, "No ServletContext");
	RequestDispatcher rd = servletContext.getNamedDispatcher(this.servletName);
	if (rd == null) {
		throw new ServletException("No servlet with name '" + this.servletName + "' defined in web.xml");
	}

	// If already included, include again, else forward.
	if (useInclude(request, response)) {
		rd.include(request, response);
		if (logger.isTraceEnabled()) {
			logger.trace("Included servlet [" + this.servletName +
					"] in ServletForwardingController '" + this.beanName + "'");
		}
	}
	else {
		rd.forward(request, response);
		if (logger.isTraceEnabled()) {
			logger.trace("Forwarded to servlet [" + this.servletName +
					"] in ServletForwardingController '" + this.beanName + "'");
		}
	}

	return null;
}
 
Example 4
Source File: ServletForwardingController.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	ServletContext servletContext = getServletContext();
	Assert.state(servletContext != null, "No ServletContext");
	RequestDispatcher rd = servletContext.getNamedDispatcher(this.servletName);
	if (rd == null) {
		throw new ServletException("No servlet with name '" + this.servletName + "' defined in web.xml");
	}

	// If already included, include again, else forward.
	if (useInclude(request, response)) {
		rd.include(request, response);
		if (logger.isTraceEnabled()) {
			logger.trace("Included servlet [" + this.servletName +
					"] in ServletForwardingController '" + this.beanName + "'");
		}
	}
	else {
		rd.forward(request, response);
		if (logger.isTraceEnabled()) {
			logger.trace("Forwarded to servlet [" + this.servletName +
					"] in ServletForwardingController '" + this.beanName + "'");
		}
	}

	return null;
}
 
Example 5
Source File: SimpleUrlHandler.java    From doodle with Apache License 2.0 5 votes vote down vote up
public SimpleUrlHandler(ServletContext servletContext) {
    defaultServlet = servletContext.getNamedDispatcher(TOMCAT_DEFAULT_SERVLET);

    if (null == defaultServlet) {
        throw new RuntimeException("没有默认的Servlet");
    }

    log.info("The default servlet for serving static resource is [{}]", TOMCAT_DEFAULT_SERVLET);
}
 
Example 6
Source File: MCRServletTarget.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handleSubmission(ServletContext context, MCRServletJob job, MCREditorSession session,
    String servletNameOrPath)
    throws Exception {
    session.getSubmission().setSubmittedValues(job.getRequest().getParameterMap());
    Document result = session.getEditedXML();

    if (session.getValidator().isValid()) {
        result = MCRChangeTracker.removeChangeTracking(result);
        result = session.getXMLCleaner().clean(result);
        result = session.getPostProcessor().process(result);

        RequestDispatcher dispatcher = context.getNamedDispatcher(servletNameOrPath);
        if (dispatcher == null) {
            dispatcher = context.getRequestDispatcher(servletNameOrPath);
        }

        job.getRequest().setAttribute("MCRXEditorSubmission", result);

        session.setBreakpoint("After handling target servlet " + servletNameOrPath);

        dispatcher.forward(job.getRequest(), job.getResponse());
    } else {
        session.setBreakpoint("After validation failed, target servlet " + servletNameOrPath);
        job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(session.getRedirectURL(null)));
    }
}
 
Example 7
Source File: RequestDispatcherProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected RequestDispatcher getRequestDispatcher(ServletContext sc, Class<?> clazz, String path) {

        RequestDispatcher rd = dispatcherName != null ? sc.getNamedDispatcher(dispatcherName)
                                                      : sc.getRequestDispatcher(path);
        if (rd == null) {
            String message =
                new org.apache.cxf.common.i18n.Message("RESOURCE_PATH_NOT_FOUND",
                                                       BUNDLE, path).toString();
            LOG.severe(message);
            throw ExceptionUtils.toInternalServerErrorException(null, null);
        }
        return rd;
    }
 
Example 8
Source File: AbstractHTTPServlet.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void redirect(HttpServletRequest request, HttpServletResponse response, String pathInfo)
    throws ServletException {
    boolean customServletPath = dispatcherServletPath != null;
    String theServletPath = customServletPath ? dispatcherServletPath : "/";

    ServletContext sc = super.getServletContext();
    RequestDispatcher rd = dispatcherServletName != null
        ? sc.getNamedDispatcher(dispatcherServletName)
        : sc.getRequestDispatcher((theServletPath + pathInfo).replace("//", "/"));
    if (rd == null) {
        String errorMessage = "No RequestDispatcher can be created for path " + pathInfo;
        if (dispatcherServletName != null) {
            errorMessage += ", dispatcher name: " + dispatcherServletName;
        }
        throw new ServletException(errorMessage);
    }
    try {
        for (Map.Entry<String, String> entry : redirectAttributes.entrySet()) {
            request.setAttribute(entry.getKey(), entry.getValue());
        }
        HttpServletRequest servletRequest =
            new HttpServletRequestRedirectFilter(request, pathInfo, theServletPath, customServletPath);
        if (PropertyUtils.isTrue(getServletConfig().getInitParameter(REDIRECT_WITH_INCLUDE_PARAMETER))) {
            rd.include(servletRequest, response);
        } else {
            rd.forward(servletRequest, response);
        }
    } catch (Throwable ex) {
        throw new ServletException("RequestDispatcher for path " + pathInfo + " has failed", ex);
    }
}
 
Example 9
Source File: JspHandler.java    From doodle with Apache License 2.0 4 votes vote down vote up
public JspHandler(ServletContext servletContext) {
    jspServlet = servletContext.getNamedDispatcher(JSP_SERVLET);
    if (null == jspServlet) {
        throw new RuntimeException("没有jsp Servlet");
    }
}
 
Example 10
Source File: ActiveToolComponent.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * @inheritDoc
 */
public void register(Tool tool, ServletContext context)
{
	ActiveTool at = null;

	// make it an active tool
	if (tool instanceof MyActiveTool)
	{
		at = (MyActiveTool) tool;
	}
	else if (tool instanceof ActiveTool)
	{
		at = (ActiveTool) tool;
	}
	else
	{
		at = new MyActiveTool(tool);
	}

	// TODO: elevate setServletContext to ActiveTool interface to avoid instance testing
	if (at instanceof MyActiveTool) {
		((MyActiveTool) at).setServletContext(context);
	}

	// KNL-409 - JSR-168 Portlets do not dispatch the same as normal
	// Sakai tools - so the warning below is not necessary for JSR-168
	// tools

               String portletContext = null;
               Properties toolProps = at.getFinalConfig();
               if (toolProps != null) {
               	portletContext = toolProps
                               .getProperty(TOOL_PORTLET_CONTEXT_PATH);
	}

	if (portletContext == null )
	{
		// try getting the RequestDispatcher, just to test - but DON'T SAVE IT!
		// Tomcat's RequestDispatcher is NOT thread safe and must be gotten from the context
		// every time its needed!
		RequestDispatcher dispatcher = context.getNamedDispatcher(at.getId());
		if (dispatcher == null)
		{
			log.warn("missing dispatcher for tool: " + at.getId());
		}
	}

	m_tools.put(at.getId(), at);
}
 
Example 11
Source File: ActiveToolComponent.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * @inheritDoc
 */
public void register(Tool tool, ServletContext context)
{
	ActiveTool at = null;

	// make it an active tool
	if (tool instanceof MyActiveTool)
	{
		at = (MyActiveTool) tool;
	}
	else if (tool instanceof ActiveTool)
	{
		at = (ActiveTool) tool;
	}
	else
	{
		at = new MyActiveTool(tool);
	}

	// TODO: elevate setServletContext to ActiveTool interface to avoid instance testing
	if (at instanceof MyActiveTool) {
		((MyActiveTool) at).setServletContext(context);
	}

	// KNL-409 - JSR-168 Portlets do not dispatch the same as normal
	// Sakai tools - so the warning below is not necessary for JSR-168
	// tools

               String portletContext = null;
               Properties toolProps = at.getFinalConfig();
               if (toolProps != null) {
               	portletContext = toolProps
                               .getProperty(TOOL_PORTLET_CONTEXT_PATH);
	}

	if (portletContext == null )
	{
		// try getting the RequestDispatcher, just to test - but DON'T SAVE IT!
		// Tomcat's RequestDispatcher is NOT thread safe and must be gotten from the context
		// every time its needed!
		RequestDispatcher dispatcher = context.getNamedDispatcher(at.getId());
		if (dispatcher == null)
		{
			log.warn("missing dispatcher for tool: " + at.getId());
		}
	}

	m_tools.put(at.getId(), at);
}