jhorsman
1/15/2018 - 8:47 AM

Use the DXA 1.7 ContentProvider and Localization without a web request.

Use the DXA 1.7 ContentProvider and Localization without a web request.

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();
        }
    }
}