Last active
January 15, 2018 08:49
-
-
Save jhorsman/e1f18e4c381a1f459c54f74a299c0e00 to your computer and use it in GitHub Desktop.
Use the DXA 1.7 ContentProvider and Localization without a web request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment