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