001    /**
002     * Copyright (c) 2000-present 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.portal.kernel.portlet;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.model.Group;
019    import com.liferay.portal.kernel.model.LayoutFriendlyURLComposite;
020    import com.liferay.portal.kernel.model.LayoutQueryStringComposite;
021    import com.liferay.portal.kernel.model.VirtualLayoutConstants;
022    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
023    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
024    import com.liferay.portal.kernel.util.CharPool;
025    import com.liferay.portal.kernel.util.HttpUtil;
026    import com.liferay.portal.kernel.util.PortalUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    
029    import java.util.Map;
030    
031    /**
032     * @author Eduardo Garcia
033     */
034    @OSGiBeanProperties(service = FriendlyURLResolver.class)
035    public class VirtualLayoutFriendlyURLResolver implements FriendlyURLResolver {
036    
037            @Override
038            public String getActualURL(
039                            long companyId, long groupId, boolean privateLayout,
040                            String mainPath, String friendlyURL, Map<String, String[]> params,
041                            Map<String, Object> requestContext)
042                    throws PortalException {
043    
044                    // Group friendly URL
045    
046                    String groupFriendlyURL = null;
047    
048                    int pos = friendlyURL.indexOf(CharPool.SLASH, 3);
049    
050                    if (pos != -1) {
051                            groupFriendlyURL = friendlyURL.substring(2, pos);
052                    }
053    
054                    if (Validator.isNull(groupFriendlyURL)) {
055                            return mainPath;
056                    }
057    
058                    Group group = GroupLocalServiceUtil.fetchFriendlyURLGroup(
059                            companyId, groupFriendlyURL);
060    
061                    if (group == null) {
062                            return mainPath;
063                    }
064    
065                    // Layout friendly URL
066    
067                    String layoutFriendlyURL = null;
068    
069                    if ((pos != -1) && ((pos + 1) != friendlyURL.length())) {
070                            layoutFriendlyURL = friendlyURL.substring(pos);
071                    }
072    
073                    if (Validator.isNull(layoutFriendlyURL)) {
074                            return mainPath;
075                    }
076    
077                    String actualURL = PortalUtil.getActualURL(
078                            group.getGroupId(), privateLayout, mainPath, layoutFriendlyURL,
079                            params, requestContext);
080    
081                    return HttpUtil.addParameter(
082                            HttpUtil.removeParameter(actualURL, "p_v_l_s_g_id"), "p_v_l_s_g_id",
083                            groupId);
084            }
085    
086            @Override
087            public LayoutFriendlyURLComposite getLayoutFriendlyURLComposite(
088                            long companyId, long groupId, boolean privateLayout,
089                            String friendlyURL, Map<String, String[]> params,
090                            Map<String, Object> requestContext)
091                    throws PortalException {
092    
093                    // Group friendly URL
094    
095                    String groupFriendlyURL = null;
096    
097                    int pos = friendlyURL.indexOf(CharPool.SLASH, 3);
098    
099                    if (pos != -1) {
100                            groupFriendlyURL = friendlyURL.substring(2, pos);
101                    }
102    
103                    Group group = GroupLocalServiceUtil.fetchFriendlyURLGroup(
104                            companyId, groupFriendlyURL);
105    
106                    // Layout friendly URL
107    
108                    String layoutFriendlyURL = null;
109    
110                    if ((pos != -1) && ((pos + 1) != friendlyURL.length())) {
111                            layoutFriendlyURL = friendlyURL.substring(pos);
112                    }
113    
114                    LayoutQueryStringComposite layoutQueryStringComposite =
115                            PortalUtil.getActualLayoutQueryStringComposite(
116                                    group.getGroupId(), privateLayout, layoutFriendlyURL, params,
117                                    requestContext);
118    
119                    return new LayoutFriendlyURLComposite(
120                            layoutQueryStringComposite.getLayout(), layoutFriendlyURL);
121            }
122    
123            @Override
124            public String getURLSeparator() {
125                    return VirtualLayoutConstants.CANONICAL_URL_SEPARATOR;
126            }
127    
128    }