Search in sources :

Example 1 with Content

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);
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Content(org.craftercms.core.service.Content) MediaType(org.springframework.http.MediaType) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 2 with 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;
}
Also used : Context(org.craftercms.core.service.Context) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Mockito.anyString(org.mockito.Mockito.anyString) ClassPathResource(org.springframework.core.io.ClassPathResource) Answer(org.mockito.stubbing.Answer) Item(org.craftercms.core.service.Item) CachingOptions(org.craftercms.core.service.CachingOptions) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CachedContent(org.craftercms.core.service.impl.CachedContent) Content(org.craftercms.core.service.Content) ArrayList(java.util.ArrayList) List(java.util.List) PathNotFoundException(org.craftercms.core.exception.PathNotFoundException) File(java.io.File)

Example 3 with Content

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);
}
Also used : Context(org.craftercms.core.service.Context) CachingOptions(org.craftercms.core.service.CachingOptions) Content(org.craftercms.core.service.Content) Test(org.junit.Test)

Example 4 with 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;
    }
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Content(org.craftercms.core.service.Content)

Aggregations

Content (org.craftercms.core.service.Content)4 CachingOptions (org.craftercms.core.service.CachingOptions)2 Context (org.craftercms.core.service.Context)2 SiteContext (org.craftercms.engine.service.context.SiteContext)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 PathNotFoundException (org.craftercms.core.exception.PathNotFoundException)1 Item (org.craftercms.core.service.Item)1 CachedContent (org.craftercms.core.service.impl.CachedContent)1 Test (org.junit.Test)1 Mockito.anyString (org.mockito.Mockito.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1 MediaType (org.springframework.http.MediaType)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1