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