001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.services.impl;
016
017import java.io.IOException;
018
019import javax.servlet.ServletException;
020import javax.servlet.http.HttpServletRequest;
021import javax.servlet.http.HttpServletResponse;
022
023import org.apache.tapestry.services.RequestGlobals;
024import org.apache.tapestry.services.ServletRequestServicer;
025import org.apache.tapestry.services.WebRequestServicer;
026import org.apache.tapestry.web.ServletWebRequest;
027import org.apache.tapestry.web.ServletWebResponse;
028import org.apache.tapestry.web.WebRequest;
029import org.apache.tapestry.web.WebResponse;
030
031/**
032 * Bridges from the <code>tapestry.request.ServletRequestServicerPipeline</code> to the
033 * <code>tapestry.request.WebRequestServicerPipeline</code>. Also, stores the web request and
034 * web response into {@link org.apache.tapestry.services.RequestGlobals}. Intercepts runtime
035 * exceptions and throws them wrapped as {@link javax.servlet.ServletException}.
036 * 
037 * @author Howard M. Lewis Ship
038 * @since 4.0
039 */
040public class WebRequestServicerPipelineBridge implements ServletRequestServicer
041{
042    private RequestGlobals _requestGlobals;
043
044    private WebRequestServicer _webRequestServicer;
045
046    public void service(HttpServletRequest request, HttpServletResponse response)
047            throws IOException, ServletException
048    {
049        _requestGlobals.store(request, response);
050
051        WebRequest webRequest = new ServletWebRequest(request, response);
052        WebResponse webResponse = new ServletWebResponse(response);
053
054        try
055        {
056            _webRequestServicer.service(webRequest, webResponse);
057        }
058        catch (RuntimeException ex)
059        {
060            throw new ServletException(ex);
061        }
062    }
063
064    public void setRequestGlobals(RequestGlobals requestGlobals)
065    {
066        _requestGlobals = requestGlobals;
067    }
068
069    public void setWebRequestServicer(WebRequestServicer webRequestServicer)
070    {
071        _webRequestServicer = webRequestServicer;
072    }
073}