1

一些问题上下文

目前,我正在 HippoCMS 支持的网站中开发一些重定向逻辑,为旧网站上的 URL 创建 301 重定向到新网站上的相应页面。

对于旧网站,我只能访问基于“URL 标题”的索引。无法进一步访问。新网站中的 Hippo 文档使用与旧网站完全相同的标题创建。所以理论上应该可以匹配。

开发方法的原因是有超过 7000 个页面需要重定向。手动执行此操作(使用 UrlRewriter 插件)意味着 40 多个小时的大脑麻木工作。

目前的发展

我创建了一个 Groovy 更新脚本,它应该

  1. 遍历所有节点
  2. 对于内容节点,将文档标题与旧索引匹配
    • 存在匹配项时,检索旧 URL
  3. 问题出在此处:检索新 URL
    • 该文档在站点地图中有一个可解析的路径
  4. 在urlrewriter 插件存储库部分中存储为新节点

编码

import org.hippoecm.hst.configuration.hosting.Mount;
import org.hippoecm.hst.container.RequestContextProvider;
import org.hippoecm.hst.core.linking.HstLink;
import org.hippoecm.hst.core.linking.HstLinkCreator;
import org.hippoecm.hst.core.request.HstRequestContext;

class UpdaterTemplate extends BaseNodeUpdateVisitor {

    boolean doUpdate(Node node) {
        def HstRequestContext = RequestContextProvider.get();
        def mount = HstRequestContext.getMount();
        def linkCreator= temp.getHstLinkCreator();
        def link = linkCreator.create(node.path, mount);
        def url = link.toUrlForm(HstRequestContext);

        // Output the url
        log.debug(url);
    }
}

该代码基于关于重写链接的河马示例页面

问题

我似乎无法检索新网站的 URL。所有其他步骤都工作正常,我在新站点上有文档 JCR 节点。我只是无法生成 URL。

我得到的第一个错误表明 CMS 无法解析某些 HST 类。

ERROR Cannot run updater: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
updater: 8: unable to resolve class org.hippoecm.hst.container.RequestContextProvider
 @ line 8, column 1.
   import org.hippoecm.hst.container.RequestContextProvider
   ^

1 error

这很容易通过更新我的 pom.xml 来解决。仅适用于开发期间。之后会重置。

然后出现了真正的问题,HstRequestContext 为空。

ERROR Updating /content/documents/XYZXYZ failed - java.lang.NullPointerException: Cannot invoke method getMount() on null object

当然这是有道理的,因为我们没有真实的上下文。但是仍然......如果没有完整站点地图逻辑的副本,我应该如何生成 URL。

我是在一条完全虚假的道路上吗?还是我错过了什么?请帮帮我。

4

1 回答 1

1

HST 库/上下文在 CMS 部分中不可用(这两个是单独的应用程序,具有不同的上下文/生命周期)。我认为最简单的方法是创建一个 HST 组件而不是 groovy 脚本(您可以使用 getPersistableSession(request) 将数据写入存储库),或者创建一个 HST REST 服务,您可以调用该服务来解析链接。这样的服务已经存在,但您需要对其进行身份验证:http: //127.0.0.1 :8080/site/_cmsrest/documents/DOCUMENT_UUID/url/live

    private static final String CMSREST_CREDENTIALS_HEADER = "X-CMSREST-CREDENTIALS";
private static final String CMSREST_CMSHOST_HEADER = "X-CMSREST-CMSHOST";
private static final String CREDENTIAL_CIPHER_KEY = "ENC_DEC_KEY";



public String getUrl(final String url) throws IOException {
    final CredentialCipher credentialCipher = CredentialCipher.getInstance();
    final String encryptedCredentials = credentialCipher.getEncryptedString(CREDENTIAL_CIPHER_KEY, new SimpleCredentials(user, password.toCharArray()));
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
        final HttpGet httpget = new HttpGet(url);
        httpget.setHeader(CMSREST_CREDENTIALS_HEADER, encryptedCredentials);
        httpget.setHeader(CMSREST_CMSHOST_HEADER, encryptedCredentials);
        log.info("Executing request {}", httpget.getRequestLine());
        final ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
            public String handleResponse(final HttpResponse response) throws IOException {
                final int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    final HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    log.error("Invalid status: {}", status);
                    return null;
                }
            }

        };
        return httpclient.execute(httpget, responseHandler);

    }
}

注意:只需将 DOCUMENT_UUID 替换为您自己的 uuid。这必须是文档句柄的 uuid。

高温高压

于 2015-03-24T14:04:38.440 回答