001    /**
002     * Copyright (c) 2000-2012 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.JavaConstants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Portlet;
027    
028    import java.io.IOException;
029    
030    import java.util.HashMap;
031    import java.util.Map;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.GenericPortlet;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.PortletContext;
038    import javax.portlet.PortletException;
039    import javax.portlet.PortletRequest;
040    import javax.portlet.PortletRequestDispatcher;
041    import javax.portlet.PortletResponse;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
044    import javax.portlet.ResourceRequest;
045    import javax.portlet.ResourceResponse;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     */
050    public class AlloyPortlet extends GenericPortlet {
051    
052            @Override
053            public void init(PortletConfig portletConfig) throws PortletException {
054                    super.init(portletConfig);
055    
056                    LiferayPortletConfig liferayPortletConfig =
057                            (LiferayPortletConfig)portletConfig;
058    
059                    Portlet portlet = liferayPortletConfig.getPortlet();
060    
061                    FriendlyURLMapper friendlyURLMapper =
062                            portlet.getFriendlyURLMapperInstance();
063    
064                    Router router = friendlyURLMapper.getRouter();
065    
066                    router.urlToParameters("GET", _defaultRouteParameters);
067            }
068    
069            @Override
070            public void processAction(
071                            ActionRequest actionRequest, ActionResponse actionResponse)
072                    throws IOException, PortletException {
073    
074                    String path = getPath(actionRequest);
075    
076                    include(path, actionRequest, actionResponse);
077            }
078    
079            @Override
080            public void render(
081                            RenderRequest renderRequest, RenderResponse renderResponse)
082                    throws IOException, PortletException {
083    
084                    String path = getPath(renderRequest);
085    
086                    include(path, renderRequest, renderResponse);
087            }
088    
089            @Override
090            public void serveResource(
091                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
092                    throws IOException, PortletException {
093    
094                    String path = getPath(resourceRequest);
095    
096                    include(path, resourceRequest, resourceResponse);
097            }
098    
099            protected Map<String, String> getDefaultRouteParameters() {
100                    /*Map<String, String> defaultRouteParameters =
101                            new HashMap<String, String[]>();
102    
103                    defaultRouteParameters.put("controller", new String[] {"assets"});
104                    defaultRouteParameters.put("action", new String[] {"index"});
105    
106                    return defaultRouteParameters;*/
107    
108                    return _defaultRouteParameters;
109            }
110    
111            protected String getPath(PortletRequest portletRequest) {
112                    LiferayPortletConfig liferayPortletConfig =
113                            (LiferayPortletConfig)portletRequest.getAttribute(
114                                    JavaConstants.JAVAX_PORTLET_CONFIG);
115    
116                    Portlet portlet = liferayPortletConfig.getPortlet();
117    
118                    String controllerPath = ParamUtil.getString(
119                            portletRequest, "controller");
120    
121                    if (Validator.isNull(controllerPath)) {
122                            Map<String, String> defaultRouteParameters =
123                                    getDefaultRouteParameters();
124    
125                            controllerPath = defaultRouteParameters.get("controller");
126                    }
127    
128                    StringBundler sb = new StringBundler(5);
129    
130                    sb.append("/WEB-INF/jsp/");
131                    sb.append(portlet.getFriendlyURLMapping());
132                    sb.append("/controllers/");
133                    sb.append(controllerPath);
134                    sb.append("_controller.jsp");
135    
136                    return sb.toString();
137            }
138    
139            protected void include(
140                            String path, PortletRequest portletRequest,
141                            PortletResponse portletResponse)
142                    throws IOException, PortletException {
143    
144                    PortletContext portletContext = getPortletContext();
145    
146                    PortletRequestDispatcher portletRequestDispatcher =
147                            portletContext.getRequestDispatcher(path);
148    
149                    if (portletRequestDispatcher == null) {
150                            _log.error(path + " is not a valid include");
151                    }
152                    else {
153                            portletRequestDispatcher.include(portletRequest, portletResponse);
154                    }
155            }
156    
157            private static Log _log = LogFactoryUtil.getLog(AlloyPortlet.class);
158    
159            private Map<String, String> _defaultRouteParameters =
160                    new HashMap<String, String>();
161    
162    }