com.liferay.util.bridges.wai.WAIFriendlyURLMapper.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.util.bridges.wai.WAIFriendlyURLMapper.java

Source

/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.util.bridges.wai;

import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
import com.liferay.portal.kernel.portlet.LiferayPortletURL;
import com.liferay.portal.kernel.portlet.LiferayWindowState;
import com.liferay.portal.kernel.portlet.Router;
import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.model.PortletConstants;
import com.liferay.portal.util.PortalUtil;

import java.util.Map;

import javax.portlet.PortletMode;
import javax.portlet.WindowState;

/**
 * @author Jorge Ferrer
 */
public class WAIFriendlyURLMapper implements FriendlyURLMapper {

    public String buildPath(LiferayPortletURL liferayPortletURL) {
        String portletId = liferayPortletURL.getPortletId();

        String prefix = portletId;

        int pos = portletId.indexOf(PortletConstants.WAR_SEPARATOR);

        if (pos != -1) {
            prefix = portletId.substring(0, pos);
        }

        String appUrl = GetterUtil.getString(liferayPortletURL.getParameter("appURL"));

        liferayPortletURL.addParameterIncludedInPath("p_p_id");

        return StringPool.SLASH + _MAPPING + StringPool.SLASH + prefix + StringPool.SLASH + appUrl;
    }

    public String getMapping() {
        return _MAPPING;
    }

    public String getPortletId() {
        return _portletId;
    }

    public Router getRouter() {
        return router;
    }

    public boolean isCheckMappingWithPrefix() {
        return _CHECK_MAPPING_WITH_PREFIX;
    }

    public boolean isPortletInstanceable() {
        return false;
    }

    public void populateParams(String friendlyURLPath, Map<String, String[]> parameterMap,
            Map<String, Object> requestContext) {

        int x = friendlyURLPath.indexOf(_MAPPING);
        int y = friendlyURLPath.indexOf(CharPool.SLASH, x + _MAPPING.length() + 1);

        if (x == -1) {
            return;
        }

        String prefix = friendlyURLPath.substring(x + _MAPPING.length() + 1, y);

        String portletId = prefix + PortletConstants.WAR_SEPARATOR + prefix;

        parameterMap.put("p_p_id", new String[] { portletId });
        parameterMap.put("p_p_lifecycle", new String[] { "0" });

        if (hasBinaryExtension(friendlyURLPath)) {
            parameterMap.put("p_p_state", new String[] { LiferayWindowState.EXCLUSIVE.toString() });
        } else {
            parameterMap.put("p_p_state", new String[] { WindowState.MAXIMIZED.toString() });
        }

        parameterMap.put("p_p_mode", new String[] { PortletMode.VIEW.toString() });

        String namespace = PortalUtil.getPortletNamespace(portletId);

        String path = friendlyURLPath.substring(y);

        parameterMap.put(namespace + "appURL", new String[] { path });
    }

    public void setMapping(String mapping) {
    }

    public void setPortletId(String portletId) {
        _portletId = portletId;
    }

    public void setPortletInstanceable(boolean portletInstanceable) {
    }

    public void setRouter(Router router) {
        this.router = router;
    }

    protected boolean hasBinaryExtension(String friendlyURLPath) {
        for (int i = 0; i < _BINARY_EXTENSIONS.length; i++) {
            String binaryExtension = _BINARY_EXTENSIONS[i];

            if (friendlyURLPath.endsWith(binaryExtension)) {
                return true;
            }
        }

        return false;
    }

    private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;

    private static final String _MAPPING = "waiapp";

    private static final String[] _BINARY_EXTENSIONS = new String[] { ".css", ".doc", ".gif", ".jpeg", ".jpg",
            ".js", ".odp", ".png", ".ppt", ".tgz", ".xls", ".zip", };

    protected Router router;

    private String _portletId;

}