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.util.bridges.alloy;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
020    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
021    import com.liferay.portal.kernel.portlet.Router;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Portlet;
025    
026    import java.io.IOException;
027    
028    import java.util.HashMap;
029    import java.util.Map;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.GenericPortlet;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.PortletException;
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletRequestDispatcher;
038    import javax.portlet.PortletResponse;
039    import javax.portlet.RenderRequest;
040    import javax.portlet.RenderResponse;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class AlloyPortlet extends GenericPortlet {
046    
047            public void init(PortletConfig portletConfig) throws PortletException {
048                    LiferayPortletConfig liferayPortletConfig =
049                            (LiferayPortletConfig)portletConfig;
050    
051                    Portlet portlet = liferayPortletConfig.getPortlet();
052    
053                    FriendlyURLMapper friendlyURLMapper =
054                            portlet.getFriendlyURLMapperInstance();
055    
056                    Router router = friendlyURLMapper.getRouter();
057    
058                    _defaultRouteParameters = new HashMap<String, String>();
059    
060                    router.urlToParameters("GET/", _defaultRouteParameters);
061    
062                    super.init(portletConfig);
063            }
064    
065            public void processAction(
066                            ActionRequest actionRequest, ActionResponse actionResponse)
067                    throws IOException, PortletException {
068    
069                    String path = getPath(actionRequest);
070    
071                    include(path, actionRequest, actionResponse);
072            }
073    
074            public void render(
075                            RenderRequest renderRequest, RenderResponse renderResponse)
076                    throws IOException, PortletException {
077    
078                    String path = getPath(renderRequest);
079    
080                    include(path, renderRequest, renderResponse);
081            }
082    
083            protected Map<String, String> getDefaultRouteParameters() {
084                    return _defaultRouteParameters;
085                    /*Map<String, String> defaultRouteParameters =
086                            new HashMap<String, String[]>();
087    
088                    defaultRouteParameters.put("controller", new String[] {"assets"});
089                    defaultRouteParameters.put("action", new String[] {"index"});
090    
091                    return defaultRouteParameters;*/
092            }
093    
094            protected String getPath(PortletRequest portletRequest) {
095                    String controllerPath = ParamUtil.getString(
096                            portletRequest, "controller");
097    
098                    if (Validator.isNull(controllerPath)) {
099                            Map<String, String> defaultRouteParameters =
100                                    getDefaultRouteParameters();
101    
102                            controllerPath = defaultRouteParameters.get("controller");
103                    }
104    
105                    return "/WEB-INF/jsp/controllers/" + controllerPath + "_controller.jsp";
106            }
107    
108            protected void include(
109                            String path, PortletRequest portletRequest,
110                            PortletResponse portletResponse)
111                    throws IOException, PortletException {
112    
113                    PortletRequestDispatcher portletRequestDispatcher =
114                            getPortletContext().getRequestDispatcher(path);
115    
116                    if (portletRequestDispatcher == null) {
117                            _log.error(path + " is not a valid include");
118                    }
119                    else {
120                            portletRequestDispatcher.include(portletRequest, portletResponse);
121                    }
122            }
123    
124            private static Log _log = LogFactoryUtil.getLog(AlloyPortlet.class);
125    
126            private Map<String, String> _defaultRouteParameters =
127                    new HashMap<String, String>();
128    
129    }