001    /**
002     * Copyright (c) 2000-present 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.portlet.bridges.mvc;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.util.PortalUtil;
019    import com.liferay.portlet.PortletConfigFactoryUtil;
020    
021    import java.io.IOException;
022    
023    import javax.portlet.PortletConfig;
024    import javax.portlet.PortletContext;
025    import javax.portlet.PortletException;
026    import javax.portlet.PortletRequestDispatcher;
027    import javax.portlet.ResourceRequest;
028    import javax.portlet.ResourceResponse;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public abstract class BaseMVCResourceCommand implements MVCResourceCommand {
034    
035            @Override
036            public boolean serveResource(
037                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
038                    throws PortletException {
039    
040                    try {
041                            doServeResource(resourceRequest, resourceResponse);
042    
043                            return SessionErrors.isEmpty(resourceRequest);
044                    }
045                    catch (PortletException pe) {
046                            throw pe;
047                    }
048                    catch (Exception e) {
049                            throw new PortletException(e);
050                    }
051            }
052    
053            protected abstract void doServeResource(
054                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
055                    throws Exception;
056    
057            protected PortletConfig getPortletConfig(ResourceRequest resourceRequest) {
058                    String portletId = PortalUtil.getPortletId(resourceRequest);
059    
060                    return PortletConfigFactoryUtil.get(portletId);
061            }
062    
063            protected PortletRequestDispatcher getPortletRequestDispatcher(
064                    ResourceRequest resourceRequest, String path) {
065    
066                    PortletConfig portletConfig = getPortletConfig(resourceRequest);
067    
068                    PortletContext portletContext = portletConfig.getPortletContext();
069    
070                    return portletContext.getRequestDispatcher(path);
071            }
072    
073            protected void include(
074                            ResourceRequest resourceRequest, ResourceResponse resourceResponse,
075                            String jspPath)
076                    throws IOException, PortletException {
077    
078                    PortletConfig portletConfig = getPortletConfig(resourceRequest);
079    
080                    PortletContext portletContext = portletConfig.getPortletContext();
081    
082                    PortletRequestDispatcher portletRequestDispatcher =
083                            portletContext.getRequestDispatcher(jspPath);
084    
085                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
086            }
087    
088    }