001    /**
002     * Copyright (c) 2000-2013 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.wai;
016    
017    import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
018    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
019    import com.liferay.portal.kernel.portlet.LiferayWindowState;
020    import com.liferay.portal.kernel.portlet.Router;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.model.PortletConstants;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import java.util.Map;
028    
029    import javax.portlet.PortletMode;
030    import javax.portlet.WindowState;
031    
032    /**
033     * @author Jorge Ferrer
034     */
035    public class WAIFriendlyURLMapper implements FriendlyURLMapper {
036    
037            @Override
038            public String buildPath(LiferayPortletURL liferayPortletURL) {
039                    String portletId = liferayPortletURL.getPortletId();
040    
041                    String prefix = portletId;
042    
043                    int pos = portletId.indexOf(PortletConstants.WAR_SEPARATOR);
044    
045                    if (pos != -1) {
046                            prefix = portletId.substring(0, pos);
047                    }
048    
049                    String appUrl = GetterUtil.getString(
050                            liferayPortletURL.getParameter("appURL"));
051    
052                    liferayPortletURL.addParameterIncludedInPath("p_p_id");
053    
054                    return StringPool.SLASH + _MAPPING + StringPool.SLASH + prefix +
055                            StringPool.SLASH + appUrl;
056            }
057    
058            @Override
059            public String getMapping() {
060                    return _MAPPING;
061            }
062    
063            @Override
064            public String getPortletId() {
065                    return _portletId;
066            }
067    
068            @Override
069            public Router getRouter() {
070                    return router;
071            }
072    
073            @Override
074            public boolean isCheckMappingWithPrefix() {
075                    return _CHECK_MAPPING_WITH_PREFIX;
076            }
077    
078            @Override
079            public boolean isPortletInstanceable() {
080                    return false;
081            }
082    
083            @Override
084            public void populateParams(
085                    String friendlyURLPath, Map<String, String[]> parameterMap,
086                    Map<String, Object> requestContext) {
087    
088                    int x = friendlyURLPath.indexOf(_MAPPING);
089                    int y = friendlyURLPath.indexOf(
090                            CharPool.SLASH, x + _MAPPING.length() + 1);
091    
092                    if (x == -1) {
093                            return;
094                    }
095    
096                    String prefix = friendlyURLPath.substring(x + _MAPPING.length() + 1, y);
097    
098                    String portletId = prefix + PortletConstants.WAR_SEPARATOR + prefix;
099    
100                    parameterMap.put("p_p_id", new String[] {portletId});
101                    parameterMap.put("p_p_lifecycle", new String[] {"0"});
102    
103                    if (hasBinaryExtension(friendlyURLPath)) {
104                            parameterMap.put(
105                                    "p_p_state",
106                                    new String[] {LiferayWindowState.EXCLUSIVE.toString()});
107                    }
108                    else {
109                            parameterMap.put(
110                                    "p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
111                    }
112    
113                    parameterMap.put(
114                            "p_p_mode", new String[] {PortletMode.VIEW.toString()});
115    
116                    String namespace = PortalUtil.getPortletNamespace(portletId);
117    
118                    String path = friendlyURLPath.substring(y);
119    
120                    parameterMap.put(namespace + "appURL", new String[] {path});
121            }
122    
123            @Override
124            public void setMapping(String mapping) {
125            }
126    
127            @Override
128            public void setPortletId(String portletId) {
129                    _portletId = portletId;
130            }
131    
132            @Override
133            public void setPortletInstanceable(boolean portletInstanceable) {
134            }
135    
136            @Override
137            public void setRouter(Router router) {
138                    this.router = router;
139            }
140    
141            protected boolean hasBinaryExtension(String friendlyURLPath) {
142                    for (int i = 0; i < _BINARY_EXTENSIONS.length; i++) {
143                            String binaryExtension = _BINARY_EXTENSIONS[i];
144    
145                            if (friendlyURLPath.endsWith(binaryExtension)) {
146                                    return true;
147                            }
148                    }
149    
150                    return false;
151            }
152    
153            protected Router router;
154    
155            private static final String[] _BINARY_EXTENSIONS = new String[] {
156                    ".css", ".doc", ".gif", ".jpeg", ".jpg", ".js", ".odp", ".png", ".ppt",
157                    ".tgz", ".xls", ".zip",
158            };
159    
160            private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
161    
162            private static final String _MAPPING = "waiapp";
163    
164            private String _portletId;
165    
166    }