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    
015    package org.apache.tapestry.portlet;
016    
017    import org.apache.tapestry.IRequestCycle;
018    import org.apache.tapestry.engine.IEngineService;
019    import org.apache.tapestry.engine.ILink;
020    import org.apache.tapestry.util.ContentType;
021    import org.apache.tapestry.web.WebRequest;
022    import org.apache.tapestry.web.WebResponse;
023    import org.apache.tapestry.web.WebSession;
024    
025    import javax.portlet.PortletURL;
026    import java.io.IOException;
027    import java.io.PrintWriter;
028    
029    /**
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     * @see org.apache.tapestry.portlet.PortletConstants#PORTLET_EXCEPTION_MARKUP_ATTRIBUTE
033     */
034    public class ExceptionService implements IEngineService
035    {
036    
037        private WebRequest _request;
038    
039        private WebResponse _response;
040    
041        private PortletRequestGlobals _globals;
042    
043        public ILink getLink(boolean post, Object parameter)
044        {
045            throw new UnsupportedOperationException(PortletMessages
046                    .unsupportedMethod("getLink"));
047        }
048    
049        public void service(IRequestCycle cycle)
050            throws IOException
051        {
052            WebSession session = _request.getSession(true);
053            String markup = (String) session
054                    .getAttribute(PortletConstants.PORTLET_EXCEPTION_MARKUP_ATTRIBUTE);
055    
056            PrintWriter writer = _response.getPrintWriter(new ContentType(
057                    "text/html"));
058    
059            PortletURL url = _globals.getRenderResponse().createActionURL();
060    
061            writer.println("<span class=\"portlet-msg-error\">An exception has occured.</span>");
062            writer.println("<br/>");
063            writer.println("<a href=\"" + url.toString()
064                    + "\">Click here to continue</a>");
065            writer.print("<br/><hr/>");
066            writer.println();
067    
068            writer.print(markup);
069        }
070    
071        public String getName()
072        {
073            return PortletConstants.EXCEPTION_SERVICE;
074        }
075    
076        public void setRequest(WebRequest request)
077        {
078            _request = request;
079        }
080    
081        public void setResponse(WebResponse response)
082        {
083            _response = response;
084        }
085    
086        public void setGlobals(PortletRequestGlobals globals)
087        {
088            _globals = globals;
089        }
090    }