use of org.craftercms.core.service.Content in project engine by craftercms.
the class StaticAssetsRequestHandler method handleRequest.
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
checkAndPrepare(request, response, true);
SiteContext siteContext = SiteContext.getCurrent();
String path = getPath(request, siteContext);
if (siteContext == null) {
throw new IllegalStateException("No current site context found");
}
if (logger.isDebugEnabled()) {
logger.debug("Trying to get content for static asset at [context=" + siteContext + ", path='" + path + "']");
}
Content content = getContent(siteContext, path);
if (content == null) {
if (logger.isDebugEnabled()) {
logger.debug("No static asset found at [context=" + siteContext + ", path='" + path + "'] - returning 404");
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
MediaType mediaType = getMediaType(path);
if (mediaType != null) {
if (logger.isDebugEnabled()) {
logger.debug("Determined media type '" + mediaType + "' for static asset at [context=" + siteContext + ", path='" + path + "']");
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("No media type found for static asset at [context=" + siteContext + ", path='" + path + "'] - not sending a content-type header");
}
}
if ((new ServletWebRequest(request, response)).checkNotModified(content.getLastModified())) {
if (logger.isDebugEnabled()) {
logger.debug("Static asset not modified - returning 304");
}
return;
}
setHeaders(response, content, mediaType);
if (disableCaching) {
if (logger.isDebugEnabled()) {
logger.debug("Caching disabled on client");
}
HttpUtils.disableCaching(response);
}
if (METHOD_HEAD.equals(request.getMethod())) {
logger.trace("HEAD request - skipping content");
return;
}
writeContent(response, content);
}
use of org.craftercms.core.service.Content in project engine by craftercms.
the class ContentStoreServiceMockUtils method setUpGetContentFromClassPath.
public static ContentStoreService setUpGetContentFromClassPath(ContentStoreService mock) {
Answer<Content> getContentAnswer = new Answer<Content>() {
@Override
public Content answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
String url;
if (args.length == 2) {
url = (String) args[1];
} else {
url = (String) args[2];
}
Content content = getContentFromClassPath(url);
if (content == null) {
throw new PathNotFoundException();
}
return content;
}
};
Answer<Boolean> existsAnswer = new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
String url = (String) args[1];
return new ClassPathResource(url).exists();
}
};
when(mock.getContent(any(Context.class), anyString())).then(getContentAnswer);
when(mock.getContent(any(Context.class), any(CachingOptions.class), anyString())).then(getContentAnswer);
when(mock.exists(any(Context.class), anyString())).then(existsAnswer);
when(mock.findChildren(any(Context.class), anyString())).then(new Answer<List<Item>>() {
@Override
public List<Item> answer(InvocationOnMock invocation) throws Throwable {
String folderUrl = (String) invocation.getArguments()[1];
Resource folderRes = new ClassPathResource(folderUrl);
File folder = folderRes.getFile();
String[] childNames = folder.list();
List<Item> children = new ArrayList<>(childNames.length);
for (String childName : childNames) {
Item child = new Item();
child.setUrl(folderUrl + "/" + childName);
children.add(child);
}
return children;
}
});
return mock;
}
use of org.craftercms.core.service.Content in project engine by craftercms.
the class TargetedContentStoreAdapterTest method testFindContent.
@Test
public void testFindContent() throws Exception {
Context context = new TargetedContentStoreAdapter.ContextWrapper(storeAdapter, mock(Context.class));
CachingOptions cachingOptions = CachingOptions.DEFAULT_CACHING_OPTIONS;
Content content = storeAdapter.findContent(context, cachingOptions, "/site/website/en/index.xml");
assertNotNull(content);
content = storeAdapter.findContent(context, cachingOptions, "/site/website/ja_JP_JP/index.xml");
assertNotNull(content);
content = storeAdapter.findContent(context, cachingOptions, "/site/website/index.xml");
assertNotNull(content);
content = storeAdapter.findContent(context, cachingOptions, "/static-assets/css/main.css");
assertNotNull(content);
}
use of org.craftercms.core.service.Content in project engine by craftercms.
the class CrafterFreeMarkerTemplateLoader method findTemplateSource.
@Override
public Object findTemplateSource(String name) throws IOException {
SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
String path = getTemplatePath(siteContext, name);
if (logger.isDebugEnabled()) {
logger.debug("Looking for FreeMarker template at [context=" + siteContext + ", path='" + path + "']");
}
Content content = contentStoreService.findContent(siteContext.getContext(), path);
if (content == null) {
if (logger.isDebugEnabled()) {
logger.debug("Unable to find FreeMarker template at [context=" + siteContext + ", path='" + path + "']");
}
}
return content;
} else {
return null;
}
}
Aggregations