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