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