Search in sources :

Example 1 with CmsSite

use of org.opencms.site.CmsSite in project opencms-core by alkacon.

the class CmsQuickLaunchLocationCache method getPageEditorLocation.

/**
 * Returns the page editor location for the given site root.<p>
 *
 * @param cms the current CMS context
 * @param siteRoot the site root
 *
 * @return the location
 */
public String getPageEditorLocation(CmsObject cms, String siteRoot) {
    CmsResource res = m_pageEditorResources.get(siteRoot);
    if (res == null) {
        return null;
    }
    try {
        String sitePath = cms.getSitePath(res);
        cms.readResource(sitePath, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED);
        return sitePath;
    } catch (CmsVfsResourceNotFoundException e) {
        try {
            CmsResource newRes = cms.readResource(res.getStructureId(), CmsResourceFilter.ONLY_VISIBLE_NO_DELETED);
            CmsSite site = OpenCms.getSiteManager().getSiteForRootPath(newRes.getRootPath());
            if (site == null) {
                return null;
            }
            if (normalizePath(site.getSiteRoot()).equals(normalizePath(siteRoot))) {
                return cms.getSitePath(newRes);
            } else {
                return null;
            }
        } catch (CmsVfsResourceNotFoundException e2) {
            return null;
        } catch (CmsException e2) {
            LOG.error(e.getLocalizedMessage(), e2);
            return null;
        }
    } catch (CmsException e) {
        LOG.error(e.getLocalizedMessage(), e);
        return null;
    }
}
Also used : CmsVfsResourceNotFoundException(org.opencms.file.CmsVfsResourceNotFoundException) CmsResource(org.opencms.file.CmsResource) CmsException(org.opencms.main.CmsException) CmsSite(org.opencms.site.CmsSite)

Example 2 with CmsSite

use of org.opencms.site.CmsSite in project opencms-core by alkacon.

the class CmsDefaultLinkSubstitutionHandler method getLink.

/**
 * @see org.opencms.staticexport.I_CmsLinkSubstitutionHandler#getLink(org.opencms.file.CmsObject, java.lang.String, java.lang.String, java.lang.String, boolean)
 */
public String getLink(CmsObject cms, String link, String siteRoot, String targetDetailPage, boolean forceSecure) {
    if (CmsStringUtil.isEmpty(link)) {
        // not a valid link parameter, return an empty String
        return "";
    }
    if (CmsStaticResourceHandler.isStaticResourceUri(link)) {
        return CmsWorkplace.getStaticResourceUri(link);
    }
    // make sure we have an absolute link
    String absoluteLink = CmsLinkManager.getAbsoluteUri(link, cms.getRequestContext().getUri());
    String overrideSiteRoot = null;
    String vfsName;
    CmsUriSplitter splitter = new CmsUriSplitter(absoluteLink, true);
    String parameters = null;
    if (splitter.getQuery() != null) {
        parameters = "?" + splitter.getQuery();
    }
    String anchor = null;
    if (splitter.getAnchor() != null) {
        anchor = "#" + splitter.getAnchor();
    }
    vfsName = splitter.getPrefix();
    String resultLink = null;
    String uriBaseName = null;
    boolean useRelativeLinks = false;
    // determine the target site of the link
    CmsSite currentSite = OpenCms.getSiteManager().getCurrentSite(cms);
    CmsSite targetSite = null;
    if (CmsStringUtil.isNotEmpty(siteRoot)) {
        targetSite = OpenCms.getSiteManager().getSiteForSiteRoot(siteRoot);
    }
    if (targetSite == null) {
        targetSite = currentSite;
    }
    String targetSiteRoot = targetSite.getSiteRoot();
    String originalVfsName = vfsName;
    String detailPage = null;
    CmsResource detailContent = null;
    try {
        String rootVfsName;
        if (!vfsName.startsWith(targetSiteRoot) && !vfsName.startsWith(CmsResource.VFS_FOLDER_SYSTEM + "/") && !OpenCms.getSiteManager().startsWithShared(vfsName)) {
            rootVfsName = CmsStringUtil.joinPaths(targetSiteRoot, vfsName);
        } else {
            rootVfsName = vfsName;
        }
        if (!rootVfsName.startsWith(CmsWorkplace.VFS_PATH_WORKPLACE)) {
            // never use the ADE manager for workplace links, to be sure the workplace stays usable in case of configuration errors
            I_CmsDetailPageHandler finder = OpenCms.getADEManager().getDetailPageHandler();
            detailPage = finder.getDetailPage(cms, rootVfsName, cms.getRequestContext().getUri(), targetDetailPage);
        }
        if (detailPage != null) {
            CmsSite detailPageSite = OpenCms.getSiteManager().getSiteForRootPath(detailPage);
            if (detailPageSite != null) {
                targetSite = detailPageSite;
                overrideSiteRoot = targetSiteRoot = targetSite.getSiteRoot();
                detailPage = detailPage.substring(targetSiteRoot.length());
                if (!detailPage.startsWith("/")) {
                    detailPage = "/" + detailPage;
                }
            }
            String originalSiteRoot = cms.getRequestContext().getSiteRoot();
            try {
                cms.getRequestContext().setSiteRoot("");
                CmsResource element = cms.readResource(rootVfsName, CmsResourceFilter.IGNORE_EXPIRATION);
                detailContent = element;
                Locale locale = cms.getRequestContext().getLocale();
                List<Locale> defaultLocales = OpenCms.getLocaleManager().getDefaultLocales();
                vfsName = CmsStringUtil.joinPaths(detailPage, cms.getDetailName(element, locale, defaultLocales), "/");
            } catch (CmsVfsException e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn(e.getLocalizedMessage(), e);
                }
            } finally {
                cms.getRequestContext().setSiteRoot(originalSiteRoot);
            }
        }
    } catch (CmsVfsResourceNotFoundException e) {
        LOG.info(e.getLocalizedMessage(), e);
    } catch (CmsException e) {
        LOG.error(e.getLocalizedMessage(), e);
    }
    // if the link points to another site, there needs to be a server prefix
    String serverPrefix;
    if ((targetSite != currentSite) || cms.getRequestContext().isForceAbsoluteLinks()) {
        serverPrefix = targetSite.getUrl();
    } else {
        serverPrefix = "";
    }
    // in the online project, check static export and secure settings
    if (cms.getRequestContext().getCurrentProject().isOnlineProject()) {
        // first check if this link needs static export
        CmsStaticExportManager exportManager = OpenCms.getStaticExportManager();
        String oriUri = cms.getRequestContext().getUri();
        // check if we need relative links in the exported pages
        if (exportManager.relativeLinksInExport(cms.getRequestContext().getSiteRoot() + oriUri)) {
            // try to get base URI from cache
            String cacheKey = exportManager.getCacheKey(targetSiteRoot, oriUri);
            uriBaseName = exportManager.getCachedOnlineLink(cacheKey);
            if (uriBaseName == null) {
                // base not cached, check if we must export it
                if (exportManager.isExportLink(cms, oriUri)) {
                    // base URI must also be exported
                    uriBaseName = exportManager.getRfsName(cms, oriUri);
                } else {
                    // base URI dosn't need to be exported
                    CmsPair<String, String> uriParamPair = addVfsPrefix(cms, oriUri, targetSite, parameters);
                    uriBaseName = uriParamPair.getFirst();
                    parameters = uriParamPair.getSecond();
                }
                // cache export base URI
                exportManager.cacheOnlineLink(cacheKey, uriBaseName);
            }
            // use relative links only on pages that get exported
            useRelativeLinks = uriBaseName.startsWith(exportManager.getRfsPrefix(cms.getRequestContext().getSiteRoot() + oriUri));
        }
        String detailPagePart = detailPage == null ? "" : detailPage + ":";
        // check if we have the absolute VFS name for the link target cached
        // (We really need the target site root in the cache key, because different resources with the same site paths
        // but in different sites may have different export settings. It seems we don't really need the site root
        // from the request context as part of the key, but we'll leave it in to make sure we don't break anything.)
        String cacheKey = generateCacheKey(cms, targetSiteRoot, detailPagePart, absoluteLink);
        resultLink = exportManager.getCachedOnlineLink(cacheKey);
        if (resultLink == null) {
            String storedSiteRoot = cms.getRequestContext().getSiteRoot();
            try {
                cms.getRequestContext().setSiteRoot(targetSite.getSiteRoot());
                // didn't find the link in the cache
                if (exportManager.isExportLink(cms, vfsName)) {
                    parameters = prepareExportParameters(cms, vfsName, parameters);
                    // export required, get export name for target link
                    resultLink = exportManager.getRfsName(cms, vfsName, parameters, targetDetailPage);
                    // now set the parameters to null, we do not need them anymore
                    parameters = null;
                } else {
                    // no export required for the target link
                    CmsPair<String, String> uriParamPair = addVfsPrefix(cms, vfsName, targetSite, parameters);
                    resultLink = uriParamPair.getFirst();
                    parameters = uriParamPair.getSecond();
                    // add cut off parameters if required
                    if (parameters != null) {
                        resultLink = resultLink.concat(parameters);
                    }
                }
            } finally {
                cms.getRequestContext().setSiteRoot(storedSiteRoot);
            }
            // cache the result
            exportManager.cacheOnlineLink(cacheKey, resultLink);
        }
        // check if either the current site or the target site does have a secure server configured
        if (targetSite.hasSecureServer() || currentSite.hasSecureServer()) {
            if (!vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) {
                // don't make a secure connection to the "/system" folder (why ?)
                int linkType = -1;
                try {
                    // read the linked resource
                    linkType = cms.readResource(originalVfsName).getTypeId();
                } catch (CmsException e) {
                    // the resource could not be read
                    if (LOG.isInfoEnabled()) {
                        String message = Messages.get().getBundle().key(Messages.LOG_RESOURCE_ACESS_ERROR_3, vfsName, cms.getRequestContext().getCurrentUser().getName(), cms.getRequestContext().getSiteRoot());
                        if (LOG.isDebugEnabled()) {
                            LOG.debug(message, e);
                        } else {
                            LOG.info(message);
                        }
                    }
                }
                // images are always referenced without a server prefix
                int imageId;
                try {
                    imageId = OpenCms.getResourceManager().getResourceType(CmsResourceTypeImage.getStaticTypeName()).getTypeId();
                } catch (CmsLoaderException e1) {
                    // should really never happen
                    LOG.warn(e1.getLocalizedMessage(), e1);
                    imageId = CmsResourceTypeImage.getStaticTypeId();
                }
                boolean hasIsImageLinkAttr = Boolean.parseBoolean("" + cms.getRequestContext().getAttribute(ATTR_IS_IMAGE_LINK));
                if ((linkType != imageId) && !hasIsImageLinkAttr) {
                    // check the secure property of the link
                    boolean secureRequest = cms.getRequestContext().isSecureRequest() || exportManager.isSecureLink(cms, oriUri);
                    boolean secureLink;
                    if (detailContent == null) {
                        secureLink = isSecureLink(cms, vfsName, targetSite, secureRequest);
                    } else {
                        secureLink = isDetailPageLinkSecure(cms, detailPage, detailContent, targetSite, secureRequest);
                    }
                    // the server name has to be prepended
                    if (secureLink && (forceSecure || !secureRequest)) {
                        serverPrefix = targetSite.getSecureUrl();
                    } else if (!secureLink && secureRequest) {
                        serverPrefix = targetSite.getUrl();
                    }
                }
            }
        }
        // and if the link does not point to another server
        if (useRelativeLinks && CmsStringUtil.isEmpty(serverPrefix)) {
            // in case the current page is a detailpage, append another path level
            if (cms.getRequestContext().getDetailContentId() != null) {
                uriBaseName = CmsStringUtil.joinPaths(CmsResource.getFolderPath(uriBaseName), cms.getRequestContext().getDetailContentId().toString() + "/index.html");
            }
            resultLink = CmsLinkManager.getRelativeUri(uriBaseName, resultLink);
        }
    } else {
        // offline project, no export or secure handling required
        if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) {
            // in unit test this code would fail otherwise
            CmsPair<String, String> uriParamPair = addVfsPrefix(cms, vfsName, targetSite, parameters);
            resultLink = uriParamPair.getFirst();
            parameters = uriParamPair.getSecond();
        }
        // add cut off parameters and return the result
        if ((parameters != null) && (resultLink != null)) {
            resultLink = resultLink.concat(parameters);
        }
    }
    if ((anchor != null) && (resultLink != null)) {
        resultLink = resultLink.concat(anchor);
    }
    if (overrideSiteRoot != null) {
        cms.getRequestContext().setAttribute(OVERRIDE_SITEROOT_PREFIX + resultLink, overrideSiteRoot);
    }
    return serverPrefix.concat(resultLink);
}
Also used : Locale(java.util.Locale) CmsLoaderException(org.opencms.loader.CmsLoaderException) I_CmsDetailPageHandler(org.opencms.ade.detailpage.I_CmsDetailPageHandler) CmsSite(org.opencms.site.CmsSite) CmsVfsException(org.opencms.file.CmsVfsException) CmsVfsResourceNotFoundException(org.opencms.file.CmsVfsResourceNotFoundException) CmsResource(org.opencms.file.CmsResource) CmsUriSplitter(org.opencms.util.CmsUriSplitter) CmsException(org.opencms.main.CmsException)

Example 3 with CmsSite

use of org.opencms.site.CmsSite in project opencms-core by alkacon.

the class CmsLinkManager method appendServerPrefix.

/**
 * Returns the link for the given resource in the current project, with full server prefix.<p>
 *
 * The input link must already have been processed according to the link substitution rules.
 * This method does just append the server prefix in case this is requires.<p>
 *
 * @param cms the current OpenCms user context
 * @param link the resource to generate the online link for
 * @param pathWithOptionalParameters the resource name
 * @param workplaceLink if this is set, use the workplace server prefix even if we are in the Online project
 *
 * @return the link for the given resource in the current project, with full server prefix
 */
private String appendServerPrefix(CmsObject cms, String link, String pathWithOptionalParameters, boolean workplaceLink) {
    int paramPos = pathWithOptionalParameters.indexOf("?");
    String resourceName = paramPos > -1 ? pathWithOptionalParameters.substring(0, paramPos) : pathWithOptionalParameters;
    if (isAbsoluteUri(link) && !hasScheme(link)) {
        // URI is absolute and contains no schema
        // this indicates source and target link are in the same site
        String serverPrefix;
        if (cms.getRequestContext().getCurrentProject().isOnlineProject() && !workplaceLink) {
            String overrideSiteRoot = (String) (cms.getRequestContext().getAttribute(CmsDefaultLinkSubstitutionHandler.OVERRIDE_SITEROOT_PREFIX + link));
            // on online project, get the real site name from the site manager
            CmsSite currentSite = OpenCms.getSiteManager().getSite(overrideSiteRoot != null ? overrideSiteRoot : resourceName, cms.getRequestContext().getSiteRoot());
            serverPrefix = currentSite.getServerPrefix(cms, resourceName);
        } else {
            // in offline mode, source must be the workplace
            // so append the workplace server so links can still be clicked
            serverPrefix = OpenCms.getSiteManager().getWorkplaceServer(cms);
        }
        link = serverPrefix + link;
    }
    return link;
}
Also used : CmsSite(org.opencms.site.CmsSite)

Example 4 with CmsSite

use of org.opencms.site.CmsSite in project opencms-core by alkacon.

the class CmsLinkManager method getPermalink.

/**
 * Returns the perma link for the given resource and optional detail content.<p<
 *
 * @param cms the CMS context to use
 * @param resourceName the page to generate the perma link for
 * @param detailContentId the structure id of the detail content (may be null)
 *
 * @return the perma link
 */
public String getPermalink(CmsObject cms, String resourceName, CmsUUID detailContentId) {
    String permalink = "";
    try {
        permalink = substituteLink(cms, CmsPermalinkResourceHandler.PERMALINK_HANDLER);
        String id = cms.readResource(resourceName, CmsResourceFilter.ALL).getStructureId().toString();
        permalink += id;
        if (detailContentId != null) {
            permalink += ":" + detailContentId;
        }
        String ext = CmsFileUtil.getExtension(resourceName);
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(ext)) {
            permalink += ext;
        }
        CmsSite currentSite = OpenCms.getSiteManager().getCurrentSite(cms);
        String serverPrefix = null;
        if (currentSite == OpenCms.getSiteManager().getDefaultSite()) {
            Optional<CmsSite> siteForDefaultUri = OpenCms.getSiteManager().getSiteForDefaultUri();
            if (siteForDefaultUri.isPresent()) {
                serverPrefix = siteForDefaultUri.get().getServerPrefix(cms, resourceName);
            } else {
                serverPrefix = OpenCms.getSiteManager().getWorkplaceServer();
            }
        } else {
            serverPrefix = currentSite.getServerPrefix(cms, resourceName);
        }
        if (!permalink.startsWith(serverPrefix)) {
            permalink = serverPrefix + permalink;
        }
    } catch (CmsException e) {
        // if something wrong
        permalink = e.getLocalizedMessage();
        if (LOG.isErrorEnabled()) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    }
    return permalink;
}
Also used : CmsException(org.opencms.main.CmsException) CmsSite(org.opencms.site.CmsSite)

Example 5 with CmsSite

use of org.opencms.site.CmsSite in project opencms-core by alkacon.

the class CmsStaticExportManager method isExportLink.

/**
 * Checks if the static export is required for the given VFS resource.<p>
 *
 * Please note that the given OpenCms user context is NOT used to read the resource.
 * The check for export is always done with the permissions of the "Export" user.
 * The provided user context is just used to get the current site root.<p>
 *
 * Since the "Export" user always operates in the "Online" project, the resource
 * is also read from the "Online" project, not from the current project of the given
 * OpenCms context.<p>
 *
 * @param cms the current users OpenCms context
 * @param vfsName the VFS resource name to check
 *
 * @return <code>true</code> if static export is required for the given VFS resource
 */
public boolean isExportLink(CmsObject cms, String vfsName) {
    LOG.info("isExportLink? " + vfsName);
    if (!isStaticExportEnabled()) {
        return false;
    }
    String siteRoot = cms.getRequestContext().getSiteRoot();
    // vfsname may still be a root path for a site with a different site root
    CmsSite site = OpenCms.getSiteManager().getSiteForRootPath(vfsName);
    if (site != null) {
        siteRoot = site.getSiteRoot();
        vfsName = CmsStringUtil.joinPaths("/", vfsName.substring(siteRoot.length()));
    }
    String cacheKey = getCacheKey(siteRoot, vfsName);
    Boolean exportResource = getCacheExportLinks().get(cacheKey);
    if (exportResource != null) {
        return exportResource.booleanValue();
    }
    boolean result = false;
    try {
        // static export must always be checked with the export users permissions,
        // not the current users permissions
        CmsObject exportCms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
        exportCms.getRequestContext().setSiteRoot(siteRoot);
        // exportRes is usually the resource at path vfsName, but in case of detail page URIs it's the detail content
        CmsResource exportRes = CmsDetailPageUtil.lookupPage(exportCms, vfsName);
        // if we are handling request for robots.txt, don't export
        if (OpenCms.getResourceManager().matchResourceType(CmsXmlSeoConfiguration.SEO_FILE_TYPE, exportRes.getTypeId())) {
            if (vfsName.endsWith("robots.txt")) {
                return false;
            }
        }
        if (vfsName.toLowerCase().endsWith(".jsp") && !OpenCms.getResourceManager().matchResourceType(CmsResourceTypeJsp.getStaticTypeName(), exportRes.getTypeId())) {
            return false;
        }
        String exportValue = exportCms.readPropertyObject(exportCms.getSitePath(exportRes), CmsPropertyDefinition.PROPERTY_EXPORT, true).getValue();
        if (exportValue == null) {
            // no setting found for "export" property
            if (getExportPropertyDefault()) {
                // if the default is "true" we always export
                result = true;
            } else {
                // check if the resource is exportable by suffix
                result = isSuffixExportable(vfsName);
            }
        } else {
            // "export" value found, if it was "true" we export
            result = Boolean.valueOf(exportValue).booleanValue();
        }
    } catch (CmsException e) {
        // no export required (probably security issues, e.g. no access for export user)
        LOG.debug(e.getLocalizedMessage(), e);
    }
    getCacheExportLinks().put(cacheKey, Boolean.valueOf(result));
    return result;
}
Also used : CmsObject(org.opencms.file.CmsObject) CmsResource(org.opencms.file.CmsResource) CmsException(org.opencms.main.CmsException) CmsSite(org.opencms.site.CmsSite)

Aggregations

CmsSite (org.opencms.site.CmsSite)78 CmsException (org.opencms.main.CmsException)30 CmsObject (org.opencms.file.CmsObject)22 CmsResource (org.opencms.file.CmsResource)21 ArrayList (java.util.ArrayList)15 CmsVfsResourceNotFoundException (org.opencms.file.CmsVfsResourceNotFoundException)12 Locale (java.util.Locale)10 CmsUUID (org.opencms.util.CmsUUID)9 IOException (java.io.IOException)6 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 CmsProperty (org.opencms.file.CmsProperty)6 CmsIllegalArgumentException (org.opencms.main.CmsIllegalArgumentException)6 CmsSiteMatcher (org.opencms.site.CmsSiteMatcher)6 HashMap (java.util.HashMap)5 CmsLoaderException (org.opencms.loader.CmsLoaderException)5 CmsSiteManagerImpl (org.opencms.site.CmsSiteManagerImpl)5 Item (com.vaadin.v7.data.Item)3 BeanItemContainer (com.vaadin.v7.data.util.BeanItemContainer)3 IndexedContainer (com.vaadin.v7.data.util.IndexedContainer)3