use of org.alfresco.service.cmr.repository.ContentReader in project SearchServices by Alfresco.
the class SolrContentStoreTest method exampleUsage.
/**
* A demonstration of how the store might be used.
*/
@Test
public void exampleUsage() {
SolrContentStore store = new SolrContentStore(solrHome);
String tenant = "alfresco.com";
long dbId = 12345;
String otherData = "sdfklsfdl";
ContentContext ctxWrite = SolrContentUrlBuilder.start().add(SolrContentUrlBuilder.KEY_DB_ID, String.valueOf(dbId)).add(SolrContentUrlBuilder.KEY_TENANT, tenant).add("otherData", otherData).getContentContext();
ContentWriter writer = store.getWriter(ctxWrite);
writer.putContent("a document in plain text");
// The URL can be reliably rebuilt in any order
String urlRead = SolrContentUrlBuilder.start().add("otherData", otherData).add(SolrContentUrlBuilder.KEY_TENANT, tenant).add(SolrContentUrlBuilder.KEY_DB_ID, String.valueOf(dbId)).get();
ContentReader reader = store.getReader(urlRead);
String documentText = reader.getContentString();
Assert.assertEquals("a document in plain text", documentText);
}
use of org.alfresco.service.cmr.repository.ContentReader in project SearchServices by Alfresco.
the class SolrContentStore method retrieveDocFromSolrContentStore.
/**
* Retrieve document from SolrContentStore.
* @param tenant identifier
* @param dbId identifier
* @return {@link SolrInputDocument} searched document
* @throws IOException if error
*/
public SolrInputDocument retrieveDocFromSolrContentStore(String tenant, long dbId) throws IOException {
String contentUrl = SolrContentUrlBuilder.start().add(SolrContentUrlBuilder.KEY_TENANT, tenant).add(SolrContentUrlBuilder.KEY_DB_ID, String.valueOf(dbId)).get();
ContentReader reader = this.getReader(contentUrl);
SolrInputDocument cachedDoc = null;
if (reader.exists()) {
// try-with-resources statement closes all these InputStreams
try (InputStream contentInputStream = reader.getContentInputStream();
// Uncompresses the document
GZIPInputStream gzip = new GZIPInputStream(contentInputStream)) {
cachedDoc = (SolrInputDocument) new JavaBinCodec(resolver).unmarshal(gzip);
} catch (Exception e) {
// Don't fail for this
log.warn("Failed to get doc from store using URL: " + contentUrl, e);
return null;
}
}
return cachedDoc;
}
use of org.alfresco.service.cmr.repository.ContentReader in project alfresco-remote-api by Alfresco.
the class PeopleImpl method processPersonProperties.
protected void processPersonProperties(String userName, final Map<QName, Serializable> nodeProps) {
if (!contentUsageService.getEnabled()) {
// quota used will always be 0 in this case so remove from the person properties
nodeProps.remove(ContentModel.PROP_SIZE_QUOTA);
nodeProps.remove(ContentModel.PROP_SIZE_CURRENT);
}
// The person description is located in a separate content file located at cm:persondescription
// "Inline" this data, by removing the cm:persondescription property and adding a temporary property
// (Person.PROP_PERSON_DESCRIPTION) containing the actual content as a string.
final ContentData personDescription = (ContentData) nodeProps.get(ContentModel.PROP_PERSONDESC);
if (personDescription != null) {
nodeProps.remove(ContentModel.PROP_PERSONDESC);
AuthenticationUtil.runAsSystem(new RunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
ContentReader reader = contentService.getRawReader(personDescription.getContentUrl());
if (reader != null && reader.exists()) {
String description = reader.getContentString();
nodeProps.put(Person.PROP_PERSON_DESCRIPTION, description);
}
return null;
}
});
}
}
use of org.alfresco.service.cmr.repository.ContentReader in project alfresco-remote-api by Alfresco.
the class CommentsImpl method toComment.
private Comment toComment(NodeRef nodeRef, NodeRef commentNodeRef, List<String> include) {
Map<QName, Serializable> nodeProps = nodeService.getProperties(commentNodeRef);
ContentReader reader = contentService.getReader(commentNodeRef, ContentModel.PROP_CONTENT);
if (reader != null) {
String content = reader.getContentString();
nodeProps.put(Comment.PROP_COMMENT_CONTENT, content);
nodeProps.remove(ContentModel.PROP_CONTENT);
}
Map<String, Boolean> map = commentService.getCommentPermissions(nodeRef, commentNodeRef);
boolean canEdit = map.get(CommentService.CAN_EDIT);
boolean canDelete = map.get(CommentService.CAN_DELETE);
Person createdBy = people.getPerson((String) nodeProps.get(ContentModel.PROP_CREATOR), include);
nodeProps.put(Comment.PROP_COMMENT_CREATED_BY, createdBy);
Person modifiedBy = people.getPerson((String) nodeProps.get(ContentModel.PROP_MODIFIER), include);
nodeProps.put(Comment.PROP_COMMENT_MODIFIED_BY, modifiedBy);
Comment comment = new Comment(commentNodeRef.getId(), nodeProps, canEdit, canDelete);
return comment;
}
use of org.alfresco.service.cmr.repository.ContentReader in project alfresco-remote-api by Alfresco.
the class ContentStreamer method streamContent.
/**
* Streams the content on a given node's content property to the response of the web script.
*
* @param req Request
* @param res Response
* @param nodeRef The node reference
* @param propertyQName The content property name
* @param attach Indicates whether the content should be streamed as an attachment or not
* @param attachFileName Optional file name to use when attach is <code>true</code>
* @throws IOException
*/
public void streamContent(WebScriptRequest req, WebScriptResponse res, NodeRef nodeRef, QName propertyQName, boolean attach, String attachFileName, Map<String, Object> model) throws IOException {
if (logger.isDebugEnabled())
logger.debug("Retrieving content from node ref " + nodeRef.toString() + " (property: " + propertyQName.toString() + ") (attach: " + attach + ")");
// TODO
// This was commented out to accomadate records management permissions. We need to review how we cope with this
// hard coded permission checked.
// check that the user has at least READ_CONTENT access - else redirect to the login page
// if (permissionService.hasPermission(nodeRef, PermissionService.READ_CONTENT) == AccessStatus.DENIED)
// {
// throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "Permission denied");
// }
// check If-Modified-Since header and set Last-Modified header as appropriate
Date modified = (Date) nodeService.getProperty(nodeRef, ContentModel.PROP_MODIFIED);
if (modified != null) {
long modifiedSince = -1;
String modifiedSinceStr = req.getHeader("If-Modified-Since");
if (modifiedSinceStr != null) {
try {
modifiedSince = dateFormat.parse(modifiedSinceStr).getTime();
} catch (Throwable e) {
if (logger.isInfoEnabled())
logger.info("Browser sent badly-formatted If-Modified-Since header: " + modifiedSinceStr);
}
if (modifiedSince > 0L) {
// round the date to the ignore millisecond value which is not supplied by header
long modDate = (modified.getTime() / 1000L) * 1000L;
if (modDate <= modifiedSince) {
res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
}
}
}
// get the content reader
ContentReader reader = contentService.getReader(nodeRef, propertyQName);
if (reader == null || !reader.exists()) {
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to locate content for node ref " + nodeRef + " (property: " + propertyQName.toString() + ")");
}
// Stream the content
streamContentImpl(req, res, reader, nodeRef, propertyQName, attach, modified, modified == null ? null : Long.toString(modified.getTime()), attachFileName, model);
}
Aggregations