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