001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.events;
016    
017    import com.liferay.portal.kernel.util.PortalClassInvoker;
018    
019    import javax.portlet.PortletRequest;
020    import javax.portlet.PortletResponse;
021    import javax.portlet.RenderRequest;
022    import javax.portlet.RenderResponse;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public abstract class Action {
031    
032            public abstract void run(
033                            HttpServletRequest request, HttpServletResponse response)
034                    throws ActionException;
035    
036            public void run(RenderRequest renderRequest, RenderResponse renderResponse)
037                    throws ActionException {
038    
039                    try {
040                            HttpServletRequest request = _getHttpServletRequest(renderRequest);
041                            HttpServletResponse response = _getHttpServletResponse(
042                                    renderResponse);
043    
044                            run(request, response);
045                    }
046                    catch (ActionException ae) {
047                            throw ae;
048                    }
049                    catch (Exception e) {
050                            throw new ActionException(e);
051                    }
052            }
053    
054            private HttpServletRequest _getHttpServletRequest(
055                            PortletRequest portletRequest)
056                    throws Exception {
057    
058                    Object returnObj = PortalClassInvoker.invoke(
059                            _CLASS, _METHOD_GETHTTPSERVLETREQUEST, portletRequest, false);
060    
061                    if (returnObj != null) {
062                            return (HttpServletRequest)returnObj;
063                    }
064                    else {
065                            return null;
066                    }
067            }
068    
069            private HttpServletResponse _getHttpServletResponse(
070                            PortletResponse portletResponse)
071                    throws Exception {
072    
073                    Object returnObj = PortalClassInvoker.invoke(
074                            _CLASS, _METHOD_GETHTTPSERVLETRESPONSE, portletResponse, false);
075    
076                    if (returnObj != null) {
077                            return (HttpServletResponse)returnObj;
078                    }
079                    else {
080                            return null;
081                    }
082            }
083    
084            private static final String _CLASS = "com.liferay.portal.util.PortalUtil";
085    
086            private static final String _METHOD_GETHTTPSERVLETREQUEST =
087                    "getHttpServletRequest";
088    
089            private static final String _METHOD_GETHTTPSERVLETRESPONSE =
090                    "getHttpServletResponse";
091    
092    }