001/*
002 * Copyright (c) 2004-2010, Kohsuke Kawaguchi
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
006 * that the following conditions are met:
007 *
008 *     * Redistributions of source code must retain the above copyright notice, this list of
009 *       conditions and the following disclaimer.
010 *     * Redistributions in binary form must reproduce the above copyright notice, this list of
011 *       conditions and the following disclaimer in the documentation and/or other materials
012 *       provided with the distribution.
013 *
014 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
015 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
016 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
017 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
018 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
019 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
020 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
021 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022 */
023
024package org.kohsuke.stapler.jelly;
025
026import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
027import org.apache.commons.jelly.JellyTagException;
028import org.apache.commons.jelly.Script;
029import org.kohsuke.stapler.MetaClass;
030import org.kohsuke.stapler.StaplerRequest;
031import org.kohsuke.stapler.StaplerResponse;
032import org.kohsuke.stapler.WebApp;
033
034import javax.servlet.RequestDispatcher;
035import javax.servlet.ServletException;
036import javax.servlet.ServletRequest;
037import javax.servlet.ServletResponse;
038import java.io.IOException;
039
040/**
041 * @author Kohsuke Kawaguchi
042 */
043public final class JellyRequestDispatcher implements RequestDispatcher {
044    private final Object it;
045    private final Script script;
046    private final JellyFacet facet;
047
048    public JellyRequestDispatcher(Object it, Script script) {
049        this.it = it;
050        this.script = script;
051        facet = WebApp.getCurrent().getFacet(JellyFacet.class);
052    }
053
054    public void forward(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
055        try {
056            facet.scriptInvoker.invokeScript(
057                (StaplerRequest)servletRequest,
058                (StaplerResponse)servletResponse,
059                script, it);
060        } catch (JellyTagException e) {
061            throw new ServletException(e);
062        }
063    }
064
065    @SuppressFBWarnings(value = "REQUESTDISPATCHER_FILE_DISCLOSURE", justification = "Forwarding the request to be handled correctly.")
066    public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
067        forward(servletRequest,servletResponse);
068    }
069}