Example usage for com.liferay.portal.kernel.util StringUtil indexOfAny

List of usage examples for com.liferay.portal.kernel.util StringUtil indexOfAny

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util StringUtil indexOfAny.

Prototype

public static int indexOfAny(String s, String[] texts, int fromIndex, int toIndex) 

Source Link

Document

Returns the index within the string of the first occurrence of any string from the array, up to and including the specified end index within the string, starting the search at the specified start index within the string.

Usage

From source file:com.liferay.exportimport.content.processor.base.BaseTextExportImportContentProcessor.java

License:Open Source License

protected Map<String, String[]> getDLReferenceParameters(long groupId, String content, int beginPos,
        int endPos) {

    boolean legacyURL = true;
    char[] stopChars = DL_REFERENCE_LEGACY_STOP_CHARS;

    if (content.startsWith("/documents/", beginPos)) {
        legacyURL = false;/* w  w w  .  j a  v a  2  s . co  m*/
        stopChars = DL_REFERENCE_STOP_CHARS;
    }

    endPos = StringUtil.indexOfAny(content, stopChars, beginPos, endPos);

    if (endPos == -1) {
        return null;
    }

    Map<String, String[]> map = new HashMap<>();

    String dlReference = content.substring(beginPos, endPos);

    while (dlReference.contains(StringPool.AMPERSAND_ENCODED)) {
        dlReference = dlReference.replace(StringPool.AMPERSAND_ENCODED, StringPool.AMPERSAND);
    }

    if (!legacyURL) {
        String[] pathArray = dlReference.split(StringPool.SLASH);

        if (pathArray.length < 3) {
            return map;
        }

        map.put("groupId", new String[] { pathArray[2] });

        if (pathArray.length == 4) {
            map.put("uuid", new String[] { pathArray[3] });
        } else if (pathArray.length == 5) {
            map.put("folderId", new String[] { pathArray[3] });
            map.put("title", new String[] { HttpUtil.decodeURL(pathArray[4]) });
        } else if (pathArray.length > 5) {
            map.put("uuid", new String[] { pathArray[5] });
        }
    } else {
        dlReference = dlReference.substring(dlReference.indexOf(CharPool.QUESTION) + 1);

        map = HttpUtil.parameterMapFromString(dlReference);

        String[] imageIds = null;

        if (map.containsKey("img_id")) {
            imageIds = map.get("img_id");
        } else if (map.containsKey("i_id")) {
            imageIds = map.get("i_id");
        }

        imageIds = ArrayUtil.filter(imageIds, new PredicateFilter<String>() {

            @Override
            public boolean filter(String imageId) {
                if (Validator.isNotNull(imageId)) {
                    return true;
                }

                return false;
            }

        });

        if (ArrayUtil.isNotEmpty(imageIds)) {
            map.put("image_id", imageIds);
        }
    }

    map.put("endPos", new String[] { String.valueOf(endPos) });

    String groupIdString = MapUtil.getString(map, "groupId");

    if (groupIdString.equals("@group_id@")) {
        groupIdString = String.valueOf(groupId);

        map.put("groupId", new String[] { groupIdString });
    }

    return map;
}

From source file:com.liferay.exportimport.content.processor.base.BaseTextExportImportContentProcessor.java

License:Open Source License

protected String replaceExportLayoutReferences(PortletDataContext portletDataContext, StagedModel stagedModel,
        String content) throws Exception {

    Group group = GroupLocalServiceUtil.getGroup(portletDataContext.getScopeGroupId());

    StringBuilder sb = new StringBuilder(content);

    String[] patterns = { "href=", "[[" };

    int beginPos = -1;
    int endPos = content.length();
    int offset = 0;

    while (true) {
        if (beginPos > -1) {
            endPos = beginPos - 1;//w w  w.j a  v a  2  s .  c om
        }

        beginPos = StringUtil.lastIndexOfAny(content, patterns, endPos);

        if (beginPos == -1) {
            break;
        }

        if (content.startsWith("href=", beginPos)) {
            offset = 5;

            char c = content.charAt(beginPos + offset);

            if ((c == CharPool.APOSTROPHE) || (c == CharPool.QUOTE)) {
                offset++;
            }
        } else if (content.charAt(beginPos) == CharPool.OPEN_BRACKET) {
            offset = 2;
        }

        endPos = StringUtil.indexOfAny(content, LAYOUT_REFERENCE_STOP_CHARS, beginPos + offset, endPos);

        if (endPos == -1) {
            continue;
        }

        String url = content.substring(beginPos + offset, endPos);

        if (url.endsWith(StringPool.SLASH)) {
            url = url.substring(0, url.length() - 1);
        }

        StringBundler urlSB = new StringBundler(6);

        try {
            url = replaceExportHostname(portletDataContext.getScopeGroupId(), url, urlSB);

            if (!url.startsWith(StringPool.SLASH)) {
                continue;
            }

            String pathContext = PortalUtil.getPathContext();

            if (pathContext.length() > 1) {
                if (!url.startsWith(pathContext)) {
                    continue;
                }

                urlSB.append(DATA_HANDLER_PATH_CONTEXT);

                url = url.substring(pathContext.length());
            }

            if (!url.startsWith(StringPool.SLASH)) {
                continue;
            }

            int pos = url.indexOf(StringPool.SLASH, 1);

            String localePath = StringPool.BLANK;

            Locale locale = null;

            if (pos != -1) {
                localePath = url.substring(0, pos);

                locale = LocaleUtil.fromLanguageId(localePath.substring(1), true, false);
            }

            if (locale != null) {
                String urlWithoutLocale = url.substring(localePath.length());

                if (urlWithoutLocale.startsWith(PRIVATE_GROUP_SERVLET_MAPPING)
                        || urlWithoutLocale.startsWith(PRIVATE_USER_SERVLET_MAPPING)
                        || urlWithoutLocale.startsWith(PUBLIC_GROUP_SERVLET_MAPPING)) {

                    urlSB.append(localePath);

                    url = urlWithoutLocale;
                }
            }

            boolean privateLayout = false;

            if (url.startsWith(PRIVATE_GROUP_SERVLET_MAPPING)) {
                urlSB.append(DATA_HANDLER_PRIVATE_GROUP_SERVLET_MAPPING);

                url = url.substring(PRIVATE_GROUP_SERVLET_MAPPING.length() - 1);

                privateLayout = true;
            } else if (url.startsWith(PRIVATE_USER_SERVLET_MAPPING)) {
                urlSB.append(DATA_HANDLER_PRIVATE_USER_SERVLET_MAPPING);

                url = url.substring(PRIVATE_USER_SERVLET_MAPPING.length() - 1);

                privateLayout = true;
            } else if (url.startsWith(PUBLIC_GROUP_SERVLET_MAPPING)) {
                urlSB.append(DATA_HANDLER_PUBLIC_SERVLET_MAPPING);

                url = url.substring(PUBLIC_GROUP_SERVLET_MAPPING.length() - 1);
            } else {
                String urlSBString = urlSB.toString();

                LayoutSet layoutSet = null;

                if (urlSBString.contains(DATA_HANDLER_PUBLIC_LAYOUT_SET_SECURE_URL)
                        || urlSBString.contains(DATA_HANDLER_PUBLIC_LAYOUT_SET_URL)) {

                    layoutSet = group.getPublicLayoutSet();
                } else if (urlSBString.contains(DATA_HANDLER_PRIVATE_LAYOUT_SET_SECURE_URL)
                        || urlSBString.contains(DATA_HANDLER_PRIVATE_LAYOUT_SET_URL)) {

                    layoutSet = group.getPrivateLayoutSet();
                }

                if (layoutSet == null) {
                    continue;
                }

                privateLayout = layoutSet.isPrivateLayout();

                LayoutFriendlyURL layoutFriendlyUrl = LayoutFriendlyURLLocalServiceUtil
                        .fetchFirstLayoutFriendlyURL(group.getGroupId(), privateLayout, url);

                if (layoutFriendlyUrl == null) {
                    continue;
                }

                if (privateLayout) {
                    if (group.isUser()) {
                        urlSB.append(DATA_HANDLER_PRIVATE_USER_SERVLET_MAPPING);
                    } else {
                        urlSB.append(DATA_HANDLER_PRIVATE_GROUP_SERVLET_MAPPING);
                    }
                } else {
                    urlSB.append(DATA_HANDLER_PUBLIC_SERVLET_MAPPING);
                }

                urlSB.append(DATA_HANDLER_GROUP_FRIENDLY_URL);

                continue;
            }

            long groupId = group.getGroupId();

            Layout layout = LayoutLocalServiceUtil.fetchLayoutByFriendlyURL(groupId, privateLayout, url);

            if (layout != null) {
                Element entityElement = portletDataContext.getExportDataElement(stagedModel);

                portletDataContext.addReferenceElement(stagedModel, entityElement, layout,
                        PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);

                continue;
            }

            pos = url.indexOf(StringPool.SLASH, 1);

            String groupFriendlyURL = url;

            if (pos != -1) {
                groupFriendlyURL = url.substring(0, pos);
            }

            Group urlGroup = GroupLocalServiceUtil.fetchFriendlyURLGroup(group.getCompanyId(),
                    groupFriendlyURL);

            if (urlGroup == null) {
                throw new NoSuchLayoutException();
            }

            urlSB.append(DATA_HANDLER_GROUP_FRIENDLY_URL);

            // Append the UUID. This information will be used during the
            // import process when looking up the proper group for the link.

            urlSB.append(StringPool.AT);

            if (urlGroup.isStagedRemotely()) {
                String remoteGroupUuid = urlGroup.getTypeSettingsProperty("remoteGroupUUID");

                if (Validator.isNotNull(remoteGroupUuid)) {
                    urlSB.append(remoteGroupUuid);
                }
            } else if (urlGroup.isStaged()) {
                Group liveGroup = urlGroup.getLiveGroup();

                urlSB.append(liveGroup.getUuid());
            } else if (group.getGroupId() == urlGroup.getGroupId()) {
                urlSB.append(urlGroup.getFriendlyURL());
            } else {
                urlSB.append(urlGroup.getUuid());
            }

            urlSB.append(StringPool.AT);

            String siteAdminURL = GroupConstants.CONTROL_PANEL_FRIENDLY_URL
                    + PropsValues.CONTROL_PANEL_LAYOUT_FRIENDLY_URL;

            if (url.endsWith(siteAdminURL)) {
                urlSB.append(DATA_HANDLER_SITE_ADMIN_URL);

                url = StringPool.BLANK;

                continue;
            }

            if (pos == -1) {
                url = StringPool.BLANK;

                continue;
            }

            url = url.substring(pos);

            layout = LayoutLocalServiceUtil.getFriendlyURLLayout(urlGroup.getGroupId(), privateLayout, url);

            Element entityElement = portletDataContext.getExportDataElement(stagedModel);

            portletDataContext.addReferenceElement(stagedModel, entityElement, layout,
                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
        } catch (Exception e) {
            if (e instanceof NoSuchLayoutException && !isValidateLayoutReferences()) {

                continue;
            }

            if (_log.isDebugEnabled()) {
                _log.debug(e, e);
            } else if (_log.isWarnEnabled()) {
                StringBundler exceptionSB = new StringBundler(6);

                exceptionSB.append("Unable to process layout URL ");
                exceptionSB.append(url);
                exceptionSB.append(" for staged model ");
                exceptionSB.append(stagedModel.getModelClassName());
                exceptionSB.append(" with primary key ");
                exceptionSB.append(stagedModel.getPrimaryKeyObj());

                _log.warn(exceptionSB.toString());
            }
        } finally {
            if (urlSB.length() > 0) {
                urlSB.append(url);

                url = urlSB.toString();
            }

            sb.replace(beginPos + offset, endPos, url);
        }
    }

    return sb.toString();
}

From source file:com.liferay.exportimport.content.processor.base.BaseTextExportImportContentProcessor.java

License:Open Source License

protected void validateLayoutReferences(long groupId, String content) throws PortalException {

    if (!isValidateLayoutReferences()) {
        return;/*from   w ww  .  ja  v  a2s.  co  m*/
    }

    Group group = GroupLocalServiceUtil.getGroup(groupId);

    String[] patterns = { "href=", "[[" };

    int beginPos = -1;
    int endPos = content.length();
    int offset = 0;

    while (true) {
        if (beginPos > -1) {
            endPos = beginPos - 1;
        }

        beginPos = StringUtil.lastIndexOfAny(content, patterns, endPos);

        if (beginPos == -1) {
            break;
        }

        if (content.startsWith("href=", beginPos)) {
            offset = 5;

            char c = content.charAt(beginPos + offset);

            if ((c == CharPool.APOSTROPHE) || (c == CharPool.QUOTE)) {
                offset++;
            }
        } else if (content.charAt(beginPos) == CharPool.OPEN_BRACKET) {
            offset = 2;
        }

        endPos = StringUtil.indexOfAny(content, LAYOUT_REFERENCE_STOP_CHARS, beginPos + offset, endPos);

        if (endPos == -1) {
            continue;
        }

        String url = content.substring(beginPos + offset, endPos);

        endPos = url.indexOf(Portal.FRIENDLY_URL_SEPARATOR);

        if (endPos != -1) {
            url = url.substring(0, endPos);
        }

        if (url.endsWith(StringPool.SLASH)) {
            url = url.substring(0, url.length() - 1);
        }

        StringBundler urlSB = new StringBundler(1);

        url = replaceExportHostname(groupId, url, urlSB);

        if (!url.startsWith(StringPool.SLASH)) {
            continue;
        }

        String pathContext = PortalUtil.getPathContext();

        if (pathContext.length() > 1) {
            if (!url.startsWith(pathContext)) {
                continue;
            }

            url = url.substring(pathContext.length());
        }

        if (!url.startsWith(StringPool.SLASH)) {
            continue;
        }

        int pos = url.indexOf(StringPool.SLASH, 1);

        String localePath = StringPool.BLANK;

        Locale locale = null;

        if (pos != -1) {
            localePath = url.substring(0, pos);

            locale = LocaleUtil.fromLanguageId(localePath.substring(1), true, false);
        }

        if (locale != null) {
            String urlWithoutLocale = url.substring(localePath.length());

            if (urlWithoutLocale.startsWith(PRIVATE_GROUP_SERVLET_MAPPING)
                    || urlWithoutLocale.startsWith(PRIVATE_USER_SERVLET_MAPPING)
                    || urlWithoutLocale.startsWith(PUBLIC_GROUP_SERVLET_MAPPING)) {

                url = urlWithoutLocale;
            }
        }

        boolean privateLayout = false;

        if (url.startsWith(PRIVATE_GROUP_SERVLET_MAPPING)) {
            url = url.substring(PRIVATE_GROUP_SERVLET_MAPPING.length() - 1);

            privateLayout = true;
        } else if (url.startsWith(PRIVATE_USER_SERVLET_MAPPING)) {
            url = url.substring(PRIVATE_USER_SERVLET_MAPPING.length() - 1);

            privateLayout = true;
        } else if (url.startsWith(PUBLIC_GROUP_SERVLET_MAPPING)) {
            url = url.substring(PUBLIC_GROUP_SERVLET_MAPPING.length() - 1);
        } else {
            String urlSBString = urlSB.toString();

            LayoutSet layoutSet = null;

            if (urlSBString.contains(DATA_HANDLER_PUBLIC_LAYOUT_SET_SECURE_URL)
                    || urlSBString.contains(DATA_HANDLER_PUBLIC_LAYOUT_SET_URL)) {

                layoutSet = group.getPublicLayoutSet();
            } else if (urlSBString.contains(DATA_HANDLER_PRIVATE_LAYOUT_SET_SECURE_URL)
                    || urlSBString.contains(DATA_HANDLER_PRIVATE_LAYOUT_SET_URL)) {

                layoutSet = group.getPrivateLayoutSet();
            }

            if (layoutSet == null) {
                continue;
            }

            privateLayout = layoutSet.isPrivateLayout();
        }

        Layout layout = LayoutLocalServiceUtil.fetchLayoutByFriendlyURL(groupId, privateLayout, url);

        if (layout != null) {
            continue;
        }

        String siteAdminURL = GroupConstants.CONTROL_PANEL_FRIENDLY_URL
                + PropsValues.CONTROL_PANEL_LAYOUT_FRIENDLY_URL;

        if (url.endsWith(VirtualLayoutConstants.CANONICAL_URL_SEPARATOR + siteAdminURL)) {

            url = url.substring(url.indexOf(siteAdminURL));
        }

        pos = url.indexOf(StringPool.SLASH, 1);

        String groupFriendlyURL = url;

        if (pos != -1) {
            groupFriendlyURL = url.substring(0, pos);
        }

        Group urlGroup = GroupLocalServiceUtil.fetchFriendlyURLGroup(group.getCompanyId(), groupFriendlyURL);

        if (urlGroup == null) {
            throw new NoSuchLayoutException();
        }

        if (pos == -1) {
            continue;
        }

        url = url.substring(pos);

        layout = LayoutLocalServiceUtil.getFriendlyURLLayout(urlGroup.getGroupId(), privateLayout, url);
    }
}

From source file:com.liferay.exportimport.internal.content.processor.DLReferencesExportImportContentProcessor.java

License:Open Source License

protected Map<String, String[]> getDLReferenceParameters(long groupId, String content, int beginPos,
        int endPos) {

    boolean legacyURL = true;
    char[] stopChars = _DL_REFERENCE_LEGACY_STOP_CHARS;

    if (content.startsWith("/documents/", beginPos)) {
        legacyURL = false;// w w w .  j a va  2 s .  co  m
        stopChars = _DL_REFERENCE_STOP_CHARS;
    }

    endPos = StringUtil.indexOfAny(content, stopChars, beginPos, endPos);

    if (endPos == -1) {
        return null;
    }

    Map<String, String[]> map = new HashMap<>();

    String dlReference = content.substring(beginPos, endPos);

    while (dlReference.contains(StringPool.AMPERSAND_ENCODED)) {
        dlReference = dlReference.replace(StringPool.AMPERSAND_ENCODED, StringPool.AMPERSAND);
    }

    if (!legacyURL) {
        String[] pathArray = dlReference.split(StringPool.SLASH);

        if (pathArray.length < 3) {
            return map;
        }

        if ("portlet_file_entry".equals(pathArray[2])) {
            map.put("groupId", new String[] { pathArray[3] });
            map.put("title", new String[] { _http.decodeURL(pathArray[4]) });
            map.put("uuid", new String[] { pathArray[5] });
        } else {
            map.put("groupId", new String[] { pathArray[2] });

            if (pathArray.length == 4) {
                map.put("uuid", new String[] { pathArray[3] });
            } else if (pathArray.length == 5) {
                map.put("folderId", new String[] { pathArray[3] });
                map.put("title", new String[] { _http.decodeURL(pathArray[4]) });
            } else if (pathArray.length > 5) {
                map.put("uuid", new String[] { pathArray[5] });
            }
        }
    } else {
        dlReference = dlReference.substring(dlReference.indexOf(CharPool.QUESTION) + 1);

        map = _http.parameterMapFromString(dlReference);

        String[] imageIds = null;

        if (map.containsKey("img_id")) {
            imageIds = map.get("img_id");
        } else if (map.containsKey("i_id")) {
            imageIds = map.get("i_id");
        }

        imageIds = ArrayUtil.filter(imageIds, Validator::isNotNull);

        if (ArrayUtil.isNotEmpty(imageIds)) {
            map.put("image_id", imageIds);
        }
    }

    map.put("endPos", new String[] { String.valueOf(endPos) });

    String groupIdString = MapUtil.getString(map, "groupId");

    if (groupIdString.equals("@group_id@")) {
        groupIdString = String.valueOf(groupId);

        map.put("groupId", new String[] { groupIdString });
    }

    return map;
}

From source file:com.liferay.exportimport.internal.content.processor.LayoutReferencesExportImportContentProcessor.java

License:Open Source License

protected String replaceExportLayoutReferences(PortletDataContext portletDataContext, StagedModel stagedModel,
        String content) throws Exception {

    Group group = _groupLocalService.getGroup(portletDataContext.getScopeGroupId());

    StringBuilder sb = new StringBuilder(content);

    String[] patterns = { "href=", "[[" };

    int beginPos = -1;
    int endPos = content.length();
    int offset = 0;

    while (true) {
        if (beginPos > -1) {
            endPos = beginPos - 1;/*from  w  ww.j av  a2s  . c om*/
        }

        beginPos = StringUtil.lastIndexOfAny(content, patterns, endPos);

        if (beginPos == -1) {
            break;
        }

        if (content.startsWith("href=", beginPos)) {
            offset = 5;

            char c = content.charAt(beginPos + offset);

            if ((c == CharPool.APOSTROPHE) || (c == CharPool.QUOTE)) {
                offset++;
            }
        } else if (content.charAt(beginPos) == CharPool.OPEN_BRACKET) {
            offset = 2;
        }

        endPos = StringUtil.indexOfAny(content, _LAYOUT_REFERENCE_STOP_CHARS, beginPos + offset, endPos);

        if (endPos == -1) {
            continue;
        }

        String url = content.substring(beginPos + offset, endPos);

        if (url.endsWith(StringPool.SLASH)) {
            url = url.substring(0, url.length() - 1);
        }

        StringBundler urlSB = new StringBundler(6);

        try {
            url = replaceExportHostname(portletDataContext.getScopeGroupId(), url, urlSB);

            if (!url.startsWith(StringPool.SLASH)) {
                continue;
            }

            String pathContext = _portal.getPathContext();

            if (pathContext.length() > 1) {
                if (!url.startsWith(pathContext)) {
                    continue;
                }

                urlSB.append(_DATA_HANDLER_PATH_CONTEXT);

                url = url.substring(pathContext.length());
            }

            if (!url.startsWith(StringPool.SLASH)) {
                continue;
            }

            int pos = url.indexOf(StringPool.SLASH, 1);

            String localePath = StringPool.BLANK;

            Locale locale = null;

            if (pos != -1) {
                localePath = url.substring(0, pos);

                locale = LocaleUtil.fromLanguageId(localePath.substring(1), true, false);
            }

            if (locale != null) {
                String urlWithoutLocale = url.substring(localePath.length());

                if (urlWithoutLocale.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING)
                        || urlWithoutLocale.startsWith(_PRIVATE_USER_SERVLET_MAPPING)
                        || urlWithoutLocale.startsWith(_PUBLIC_GROUP_SERVLET_MAPPING)) {

                    urlSB.append(localePath);

                    url = urlWithoutLocale;
                }
            }

            boolean privateLayout = false;

            if (url.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING)) {
                urlSB.append(_DATA_HANDLER_PRIVATE_GROUP_SERVLET_MAPPING);

                url = url.substring(_PRIVATE_GROUP_SERVLET_MAPPING.length() - 1);

                privateLayout = true;
            } else if (url.startsWith(_PRIVATE_USER_SERVLET_MAPPING)) {
                urlSB.append(_DATA_HANDLER_PRIVATE_USER_SERVLET_MAPPING);

                url = url.substring(_PRIVATE_USER_SERVLET_MAPPING.length() - 1);

                privateLayout = true;
            } else if (url.startsWith(_PUBLIC_GROUP_SERVLET_MAPPING)) {
                urlSB.append(_DATA_HANDLER_PUBLIC_SERVLET_MAPPING);

                url = url.substring(_PUBLIC_GROUP_SERVLET_MAPPING.length() - 1);
            } else {
                String urlSBString = urlSB.toString();

                LayoutSet layoutSet = null;

                if (urlSBString.contains(_DATA_HANDLER_PUBLIC_LAYOUT_SET_SECURE_URL)
                        || urlSBString.contains(_DATA_HANDLER_PUBLIC_LAYOUT_SET_URL)) {

                    layoutSet = group.getPublicLayoutSet();
                } else if (urlSBString.contains(_DATA_HANDLER_PRIVATE_LAYOUT_SET_SECURE_URL)
                        || urlSBString.contains(_DATA_HANDLER_PRIVATE_LAYOUT_SET_URL)) {

                    layoutSet = group.getPrivateLayoutSet();
                }

                if (layoutSet == null) {
                    continue;
                }

                privateLayout = layoutSet.isPrivateLayout();

                LayoutFriendlyURL layoutFriendlyUrl = _layoutFriendlyURLLocalService
                        .fetchFirstLayoutFriendlyURL(group.getGroupId(), privateLayout, url);

                if (layoutFriendlyUrl == null) {
                    continue;
                }

                if (privateLayout) {
                    if (group.isUser()) {
                        urlSB.append(_DATA_HANDLER_PRIVATE_USER_SERVLET_MAPPING);
                    } else {
                        urlSB.append(_DATA_HANDLER_PRIVATE_GROUP_SERVLET_MAPPING);
                    }
                } else {
                    urlSB.append(_DATA_HANDLER_PUBLIC_SERVLET_MAPPING);
                }

                urlSB.append(_DATA_HANDLER_GROUP_FRIENDLY_URL);

                continue;
            }

            long groupId = group.getGroupId();

            Layout layout = _layoutLocalService.fetchLayoutByFriendlyURL(groupId, privateLayout, url);

            if (layout != null) {
                Element entityElement = portletDataContext.getExportDataElement(stagedModel);

                portletDataContext.addReferenceElement(stagedModel, entityElement, layout,
                        PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);

                continue;
            }

            pos = url.indexOf(StringPool.SLASH, 1);

            String groupFriendlyURL = url;

            if (pos != -1) {
                groupFriendlyURL = url.substring(0, pos);
            }

            Group urlGroup = _groupLocalService.fetchFriendlyURLGroup(group.getCompanyId(), groupFriendlyURL);

            if (urlGroup == null) {
                throw new NoSuchLayoutException();
            }

            urlSB.append(_DATA_HANDLER_GROUP_FRIENDLY_URL);

            // Append the UUID. This information will be used during the
            // import process when looking up the proper group for the link.

            urlSB.append(StringPool.AT);

            if (urlGroup.isStagedRemotely()) {
                String remoteGroupUuid = urlGroup.getTypeSettingsProperty("remoteGroupUUID");

                if (Validator.isNotNull(remoteGroupUuid)) {
                    urlSB.append(remoteGroupUuid);
                }
            } else if (urlGroup.isStaged()) {
                Group liveGroup = urlGroup.getLiveGroup();

                urlSB.append(liveGroup.getUuid());
            } else if (group.getGroupId() == urlGroup.getGroupId()) {
                urlSB.append(urlGroup.getFriendlyURL());
            } else {
                urlSB.append(urlGroup.getUuid());
            }

            urlSB.append(StringPool.AT);

            String siteAdminURL = GroupConstants.CONTROL_PANEL_FRIENDLY_URL
                    + PropsValues.CONTROL_PANEL_LAYOUT_FRIENDLY_URL;

            if (url.endsWith(siteAdminURL)) {
                urlSB.append(_DATA_HANDLER_SITE_ADMIN_URL);

                url = StringPool.BLANK;

                continue;
            }

            if (pos == -1) {
                url = StringPool.BLANK;

                continue;
            }

            url = url.substring(pos);

            layout = _layoutLocalService.getFriendlyURLLayout(urlGroup.getGroupId(), privateLayout, url);

            Element entityElement = portletDataContext.getExportDataElement(stagedModel);

            portletDataContext.addReferenceElement(stagedModel, entityElement, layout,
                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
        } catch (Exception e) {
            if (e instanceof NoSuchLayoutException
                    && !_exportImportServiceConfiguration.validateLayoutReferences()) {

                continue;
            }

            if (_log.isDebugEnabled()) {
                _log.debug(e, e);
            } else if (_log.isWarnEnabled()) {
                StringBundler exceptionSB = new StringBundler(6);

                exceptionSB.append("Unable to process layout URL ");
                exceptionSB.append(url);
                exceptionSB.append(" for staged model ");
                exceptionSB.append(stagedModel.getModelClassName());
                exceptionSB.append(" with primary key ");
                exceptionSB.append(stagedModel.getPrimaryKeyObj());

                _log.warn(exceptionSB.toString());
            }
        } finally {
            if (urlSB.length() > 0) {
                urlSB.append(url);

                url = urlSB.toString();
            }

            sb.replace(beginPos + offset, endPos, url);
        }
    }

    return sb.toString();
}

From source file:com.liferay.exportimport.internal.content.processor.LayoutReferencesExportImportContentProcessor.java

License:Open Source License

protected void validateLayoutReferences(long groupId, String content) throws PortalException {

    if (!_exportImportServiceConfiguration.validateLayoutReferences()) {
        return;/*w w w .  j a va  2s .  c om*/
    }

    Group group = _groupLocalService.getGroup(groupId);

    String[] patterns = { "href=", "[[" };

    int beginPos = -1;
    int endPos = content.length();
    int offset = 0;

    while (true) {
        if (beginPos > -1) {
            endPos = beginPos - 1;
        }

        beginPos = StringUtil.lastIndexOfAny(content, patterns, endPos);

        if (beginPos == -1) {
            break;
        }

        if (content.startsWith("href=", beginPos)) {
            offset = 5;

            char c = content.charAt(beginPos + offset);

            if ((c == CharPool.APOSTROPHE) || (c == CharPool.QUOTE)) {
                offset++;
            }
        } else if (content.charAt(beginPos) == CharPool.OPEN_BRACKET) {
            offset = 2;
        }

        endPos = StringUtil.indexOfAny(content, _LAYOUT_REFERENCE_STOP_CHARS, beginPos + offset, endPos);

        if (endPos == -1) {
            continue;
        }

        String url = content.substring(beginPos + offset, endPos);

        endPos = url.indexOf(Portal.FRIENDLY_URL_SEPARATOR);

        if (endPos != -1) {
            url = url.substring(0, endPos);
        }

        if (url.endsWith(StringPool.SLASH)) {
            url = url.substring(0, url.length() - 1);
        }

        StringBundler urlSB = new StringBundler(1);

        url = replaceExportHostname(groupId, url, urlSB);

        if (!url.startsWith(StringPool.SLASH)) {
            continue;
        }

        String pathContext = _portal.getPathContext();

        if (pathContext.length() > 1) {
            if (!url.startsWith(pathContext)) {
                continue;
            }

            url = url.substring(pathContext.length());
        }

        if (!url.startsWith(StringPool.SLASH)) {
            continue;
        }

        int pos = url.indexOf(StringPool.SLASH, 1);

        String localePath = StringPool.BLANK;

        Locale locale = null;

        if (pos != -1) {
            localePath = url.substring(0, pos);

            locale = LocaleUtil.fromLanguageId(localePath.substring(1), true, false);
        }

        if (locale != null) {
            String urlWithoutLocale = url.substring(localePath.length());

            if (urlWithoutLocale.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING)
                    || urlWithoutLocale.startsWith(_PRIVATE_USER_SERVLET_MAPPING)
                    || urlWithoutLocale.startsWith(_PUBLIC_GROUP_SERVLET_MAPPING)) {

                url = urlWithoutLocale;
            }
        }

        boolean privateLayout = false;

        if (url.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING)) {
            url = url.substring(_PRIVATE_GROUP_SERVLET_MAPPING.length() - 1);

            privateLayout = true;
        } else if (url.startsWith(_PRIVATE_USER_SERVLET_MAPPING)) {
            url = url.substring(_PRIVATE_USER_SERVLET_MAPPING.length() - 1);

            privateLayout = true;
        } else if (url.startsWith(_PUBLIC_GROUP_SERVLET_MAPPING)) {
            url = url.substring(_PUBLIC_GROUP_SERVLET_MAPPING.length() - 1);
        } else {
            String urlSBString = urlSB.toString();

            LayoutSet layoutSet = null;

            if (urlSBString.contains(_DATA_HANDLER_PUBLIC_LAYOUT_SET_SECURE_URL)
                    || urlSBString.contains(_DATA_HANDLER_PUBLIC_LAYOUT_SET_URL)) {

                layoutSet = group.getPublicLayoutSet();
            } else if (urlSBString.contains(_DATA_HANDLER_PRIVATE_LAYOUT_SET_SECURE_URL)
                    || urlSBString.contains(_DATA_HANDLER_PRIVATE_LAYOUT_SET_URL)) {

                layoutSet = group.getPrivateLayoutSet();
            }

            if (layoutSet == null) {
                continue;
            }

            privateLayout = layoutSet.isPrivateLayout();
        }

        Layout layout = _layoutLocalService.fetchLayoutByFriendlyURL(groupId, privateLayout, url);

        if (layout != null) {
            continue;
        }

        String siteAdminURL = GroupConstants.CONTROL_PANEL_FRIENDLY_URL
                + PropsValues.CONTROL_PANEL_LAYOUT_FRIENDLY_URL;

        if (url.endsWith(VirtualLayoutConstants.CANONICAL_URL_SEPARATOR + siteAdminURL)) {

            url = url.substring(url.indexOf(siteAdminURL));
        }

        pos = url.indexOf(StringPool.SLASH, 1);

        String groupFriendlyURL = url;

        if (pos != -1) {
            groupFriendlyURL = url.substring(0, pos);
        }

        Group urlGroup = _groupLocalService.fetchFriendlyURLGroup(group.getCompanyId(), groupFriendlyURL);

        if (urlGroup == null) {
            throw new NoSuchLayoutException(
                    "Unable validate referenced page because it cannot be " + "found with url: " + url);
        }

        if (pos == -1) {
            continue;
        }

        url = url.substring(pos);

        try {
            layout = _layoutLocalService.getFriendlyURLLayout(urlGroup.getGroupId(), privateLayout, url);
        } catch (NoSuchLayoutException nsle) {
            throw new NoSuchLayoutException("Unable to validate referenced page because the page "
                    + "group cannot be found: " + groupId, nsle);
        }
    }
}