Search in sources :

Example 31 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class AbstractFileBasedContentStoreAdapter method doFindItem.

@Override
protected Item doFindItem(Context context, CachingOptions cachingOptions, String path, boolean withDescriptor) throws InvalidContextException, PathNotFoundException, XmlFileParseException, StoreException {
    path = normalizePath(path);
    File file = findFile(context, path);
    if (file == null) {
        return null;
    }
    Item item = new Item();
    item.setName(file.getName());
    item.setUrl(path);
    item.setFolder(file.isDirectory());
    if (withDescriptor) {
        File descriptorFile;
        // a DOM.
        if (file.isFile() && item.getName().endsWith(descriptorFileExtension)) {
            item.setDescriptorUrl(path);
            descriptorFile = file;
        // If it's not a file (a dir) or is not a descriptor (a static asset, like an image), locate the file's
        // descriptor by appending a metadata file extension to the file name. If the file exists, load it as
        // a DOM.
        } else {
            String descriptorPath = FilenameUtils.removeExtension(path) + metadataFileExtension;
            item.setDescriptorUrl(descriptorPath);
            descriptorFile = findFile(context, descriptorPath);
            if (descriptorFile != null && !descriptorFile.isFile()) {
                throw new StoreException("Descriptor file at " + descriptorFile + " is not really a file");
            }
        }
        if (descriptorFile != null) {
            try {
                InputStream fileInputStream = new BufferedInputStream(descriptorFile.getInputStream());
                Reader fileReader = new InputStreamReader(fileInputStream, charset);
                try {
                    item.setDescriptorDom(createXmlReader().read(fileReader));
                } finally {
                    IOUtils.closeQuietly(fileReader);
                }
            } catch (IOException e) {
                throw new StoreException("Unable to open input stream for descriptor file at " + descriptorFile, e);
            } catch (DocumentException e) {
                throw new XmlFileParseException("Error while parsing xml document at " + descriptorFile, e);
            }
        }
    }
    return item;
}
Also used : Item(org.craftercms.core.service.Item) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) DocumentException(org.dom4j.DocumentException) SAXReader(org.dom4j.io.SAXReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) StoreException(org.craftercms.core.exception.StoreException) XmlFileParseException(org.craftercms.core.exception.XmlFileParseException)

Example 32 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class AbstractFileBasedContentStoreAdapter method doFindItems.

@Override
protected List<Item> doFindItems(Context context, CachingOptions cachingOptions, String path, boolean withDescriptor) throws InvalidContextException, PathNotFoundException, XmlFileParseException, StoreException {
    path = normalizePath(path);
    File dir = findFile(context, path);
    if (dir == null) {
        return null;
    }
    if (!dir.isDirectory()) {
        throw new StoreException("The path " + dir + " doesn't correspond to a dir");
    }
    List<File> children = getChildren(context, dir);
    CachingAwareList<Item> items = new CachingAwareList<>(children.size());
    if (CollectionUtils.isNotEmpty(children)) {
        for (File child : children) {
            // items.
            if (!child.isFile() || !child.getName().endsWith(metadataFileExtension)) {
                String fileRelPath = path + (!path.equals("/") ? "/" : "") + child.getName();
                Item item = findItem(context, cachingOptions, fileRelPath, withDescriptor);
                if (item != null) {
                    items.add(item);
                    items.addDependencyKey(item.getKey());
                }
            }
        }
    }
    return items;
}
Also used : Item(org.craftercms.core.service.Item) StoreException(org.craftercms.core.exception.StoreException) CachingAwareList(org.craftercms.core.util.cache.impl.CachingAwareList)

Example 33 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class TextMetaDataCollectionExtractingProcessorTest method setUpTestItem.

private void setUpTestItem() {
    Node css1Node = mock(Node.class);
    when(css1Node.getText()).thenReturn(CSS1);
    Node css2Node = mock(Node.class);
    when(css2Node.getText()).thenReturn(CSS2);
    Node js1Node = mock(Node.class);
    when(js1Node.getText()).thenReturn(JS1);
    Node js2Node = mock(Node.class);
    when(js2Node.getText()).thenReturn(JS2);
    Document descriptorDom = mock(Document.class);
    when(descriptorDom.selectNodes(testMetaDataNodesXPathQueries[0])).thenReturn(Arrays.asList(css1Node, css2Node));
    when(descriptorDom.selectNodes(testMetaDataNodesXPathQueries[1])).thenReturn(Arrays.asList(js1Node, js2Node));
    item = new Item();
    item.setDescriptorDom(descriptorDom);
}
Also used : Item(org.craftercms.core.service.Item) Node(org.dom4j.Node) Document(org.dom4j.Document)

Example 34 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class ItemProcessorResolverChainTest method setUpTestItems.

private void setUpTestItems() {
    item1 = new Item();
    item1.setUrl(ITEM1_URL);
    item2 = new Item();
    item2.setUrl(ITEM2_URL);
    item3 = new Item();
    item3.setUrl(ITEM3_URL);
}
Also used : Item(org.craftercms.core.service.Item)

Example 35 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class ContentStoreRestController method getChildren.

@RequestMapping(value = URL_CHILDREN, method = RequestMethod.GET)
public Map<String, Object> getChildren(WebRequest request, HttpServletResponse response, @RequestParam(REQUEST_PARAM_CONTEXT_ID) String contextId, @RequestParam(REQUEST_PARAM_URL) String url) throws InvalidContextException, StoreException, PathNotFoundException, ItemProcessingException, XmlMergeException, XmlFileParseException {
    Context context = storeService.getContext(contextId);
    if (context == null) {
        throw new InvalidContextException("No context found for ID " + contextId);
    }
    CachingAwareList<Item> children = (CachingAwareList<Item>) storeService.getChildren(context, url);
    if (children.getCachingTime() != null && checkNotModified(children.getCachingTime(), request, response)) {
        return null;
    } else {
        return createMessageModel(MODEL_ATTR_CHILDREN, new ArrayList<Item>(children));
    }
}
Also used : Context(org.craftercms.core.service.Context) Item(org.craftercms.core.service.Item) CachingAwareList(org.craftercms.core.util.cache.impl.CachingAwareList) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Item (org.craftercms.core.service.Item)55 Test (org.junit.Test)21 Context (org.craftercms.core.service.Context)19 CachingOptions (org.craftercms.core.service.CachingOptions)8 Document (org.dom4j.Document)8 ArrayList (java.util.ArrayList)7 ContentStoreAdapter (org.craftercms.core.store.ContentStoreAdapter)7 CachingAwareList (org.craftercms.core.util.cache.impl.CachingAwareList)5 InheritLevelsMergeStrategyTest (org.craftercms.core.xml.mergers.impl.strategies.InheritLevelsMergeStrategyTest)4 DecoratedStoreAdapterContext (org.craftercms.engine.util.store.decorators.DecoratedStoreAdapterContext)4 PathNotFoundException (org.craftercms.core.exception.PathNotFoundException)3 TextMetaDataExtractingProcessor (org.craftercms.core.processors.impl.TextMetaDataExtractingProcessor)3 Content (org.craftercms.core.service.Content)3 ContentStoreService (org.craftercms.core.service.ContentStoreService)3 Tree (org.craftercms.core.service.Tree)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 StoreException (org.craftercms.core.exception.StoreException)2 ItemProcessor (org.craftercms.core.processors.ItemProcessor)2 ItemProcessorPipeline (org.craftercms.core.processors.impl.ItemProcessorPipeline)2 TextMetaDataCollectionExtractingProcessor (org.craftercms.core.processors.impl.TextMetaDataCollectionExtractingProcessor)2