001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class AlloyPortlet extends GenericPortlet {
048    
049            @Override
050            public void init(PortletConfig portletConfig) throws PortletException {
051                    super.init(portletConfig);
052    
053                    LiferayPortletConfig liferayPortletConfig =
054                            (LiferayPortletConfig)portletConfig;
055    
056                    Portlet portlet = liferayPortletConfig.getPortlet();
057    
058                    FriendlyURLMapper friendlyURLMapper =
059                            portlet.getFriendlyURLMapperInstance();
060    
061                    Router router = friendlyURLMapper.getRouter();
062    
063                    router.urlToParameters("GET", _defaultRouteParameters);
064            }
065    
066            @Override
067            public void processAction(
068                            ActionRequest actionRequest, ActionResponse actionResponse)
069                    throws IOException, PortletException {
070    
071                    String path = getPath(actionRequest);
072    
073                    include(path, actionRequest, actionResponse);
074            }
075    
076            @Override
077            public void render(
078                            RenderRequest renderRequest, RenderResponse renderResponse)
079                    throws IOException, PortletException {
080    
081                    String path = getPath(renderRequest);
082    
083                    include(path, renderRequest, renderResponse);
084            }
085    
086            protected Map<String, String> getDefaultRouteParameters() {
087                    /*Map<String, String> defaultRouteParameters =
088                            new HashMap<String, String[]>();
089    
090                    defaultRouteParameters.put("controller", new String[] {"assets"});
091                    defaultRouteParameters.put("action", new String[] {"index"});
092    
093                    return defaultRouteParameters;*/
094    
095                    return _defaultRouteParameters;
096            }
097    
098            protected String getPath(PortletRequest portletRequest) {
099                    LiferayPortletConfig liferayPortletConfig =
100                            (LiferayPortletConfig)portletRequest.getAttribute(
101                                    JavaConstants.JAVAX_PORTLET_CONFIG);
102    
103                    Portlet portlet = liferayPortletConfig.getPortlet();
104    
105                    String controllerPath = ParamUtil.getString(
106                            portletRequest, "controller");
107    
108                    if (Validator.isNull(controllerPath)) {
109                            Map<String, String> defaultRouteParameters =
110                                    getDefaultRouteParameters();
111    
112                            controllerPath = defaultRouteParameters.get("controller");
113                    }
114    
115                    StringBundler sb = new StringBundler(5);
116    
117                    sb.append("/WEB-INF/jsp/");
118                    sb.append(portlet.getFriendlyURLMapping());
119                    sb.append("/controllers/");
120                    sb.append(controllerPath);
121                    sb.append("_controller.jsp");
122    
123                    return sb.toString();
124            }
125    
126            protected void include(
127                            String path, PortletRequest portletRequest,
128                            PortletResponse portletResponse)
129                    throws IOException, PortletException {
130    
131                    PortletRequestDispatcher portletRequestDispatcher =
132                            getPortletContext().getRequestDispatcher(path);
133    
134                    if (portletRequestDispatcher == null) {
135                            _log.error(path + " is not a valid include");
136                    }
137                    else {
138                            portletRequestDispatcher.include(portletRequest, portletResponse);
139                    }
140            }
141    
142            private static Log _log = LogFactoryUtil.getLog(AlloyPortlet.class);
143    
144            private Map<String, String> _defaultRouteParameters =
145                    new HashMap<String, String>();
146    
147    }