java 在没有Web请求的情况下使用DXA 1.7 ContentProvider和Localization。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 在没有Web请求的情况下使用DXA 1.7 ContentProvider和Localization。相关的知识,希望对你有一定的参考价值。

package org.example;

import com.sdl.webapp.common.api.WebRequestContext;
import com.sdl.webapp.common.api.content.ContentProvider;
import com.sdl.webapp.common.api.content.ContentProviderException;
import com.sdl.webapp.common.api.localization.Localization;
import com.sdl.webapp.common.api.localization.LocalizationResolver;
import com.sdl.webapp.common.api.model.PageModel;
import lombok.extern.slf4j.Slf4j;
import org.example.controller.SwitchController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.*;

import java.net.URI;

@EnableScheduling
@Component
@Slf4j
public class WebRequestExperiment {
    @Autowired
    LocalizationResolver localizationResolver;

    @Autowired
    ContentProvider contentProvider;

    @Autowired
    private WebRequestContext webRequestContext;

    @Scheduled(fixedRate = 30000 )
    public void runScheduler() {
        // by using a scheduled task we can do something after Spring fully initialized, without being dependent on a web request.
        // if needed this can be easily modified to run once at start of the web application.
        runTask();
    }

    protected void setRequest(String url) {
        // DD4T and DXA both assume that there is a HttpServletRequest. As a minimum we need to set the request url.

        MockHttpServletRequest request = new MockHttpServletRequest();
        URI uri = URI.create(url);
        request.setProtocol(uri.getScheme());
        request.setServerPort(uri.getPort());
        request.setServerName(uri.getHost());
        request.setRequestURI(uri.getPath());

        ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
        RequestContextHolder.setRequestAttributes(requestAttributes);
    }

    public void runTask() {
        try {
            log.info("Scheduled task RunOnce");

            // Make a fake ServletRequest. The url is used to resolve a publication
            String url = "http://localhost:8080";
            this.setRequest(url);

            // get a localization object, needed when adressing the ContentProvider
            Localization localization = webRequestContext.getLocalization();

            // grab configuration from the Localization
            String language = localization.getConfiguration("core.language");
            log.info("Localization config value: core.language: '" + language + "'");

            // grab resources (labels) from the Localization
            String readMoreLinkText = localization.getResource("core.readMoreLinkText");
            log.info("Localization resourec value: readMoreLinkText: '" + readMoreLinkText + "'");

            // get a page model
            PageModel pageModel = contentProvider.getPageModel("/articles/movies/up", localization);
            log.info("Scheduler found pageModel for '" + pageModel.getTitle() + "'");

        } catch (ContentProviderException e) {
            e.printStackTrace();
        }
    }
}

以上是关于java 在没有Web请求的情况下使用DXA 1.7 ContentProvider和Localization。的主要内容,如果未能解决你的问题,请参考以下文章

java 在DXA Java中使用模块配置

csharp 最小的DXA 1.6核心区域注册。如果没有这个,DXA webapp就无法使用框架控制器。那些控制器不在c中

csharp 最小的DXA 1.6核心区域注册。如果没有这个,DXA webapp就无法使用框架控制器。那些控制器不在c中

java DXA JAVA的自定义页面控制器。

在没有 Java EE 应用服务器的情况下使用 Web 服务在 C# 和 Java 之间进行互操作?

在没有原始数据的情况下重新发送 post/put 请求