use of org.alfresco.service.cmr.repository.ContentData in project alfresco-remote-api by Alfresco.
the class PropFindMethod method generateNamedPropertiesResponse.
/**
* Generates the XML response for a PROPFIND request that asks for a
* specific set of properties
*
* @param xml XMLWriter
* @param nodeInfo FileInfo
* @param isDir boolean
*/
private void generateNamedPropertiesResponse(XMLWriter xml, FileInfo nodeInfo, boolean isDir) throws Exception {
// Get the properties for the node
Map<QName, Serializable> props = nodeInfo.getProperties();
Map<QName, String> deadProperties = null;
// Output the start of the properties element
Attributes nullAttr = getDAVHelper().getNullAttributes();
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_PROPSTAT, WebDAV.XML_NS_PROPSTAT, nullAttr);
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_PROP, WebDAV.XML_NS_PROP, nullAttr);
ArrayList<WebDAVProperty> propertiesNotFound = new ArrayList<WebDAVProperty>();
TypeConverter typeConv = DefaultTypeConverter.INSTANCE;
// Loop through the requested property list
for (WebDAVProperty property : m_properties) {
// Get the requested property details
String propName = property.getName();
String propNamespaceUri = property.getNamespaceUri();
// Check if the property is a standard WebDAV property
Object davValue = null;
if (WebDAV.DEFAULT_NAMESPACE_URI.equals(propNamespaceUri)) {
// Check if the client is requesting lock information
if (// && metaData.isLocked())
propName.equals(WebDAV.XML_LOCK_DISCOVERY)) {
generateLockDiscoveryResponse(xml, nodeInfo, isDir);
} else if (propName.equals(WebDAV.XML_SUPPORTED_LOCK)) {
// Output the supported lock types
writeLockTypes(xml);
} else if (propName.equals(WebDAV.XML_RESOURCE_TYPE)) {
// If the node is a folder then return as a collection type
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_RESOURCE_TYPE, WebDAV.XML_NS_RESOURCE_TYPE, nullAttr);
if (isDir) {
xml.write(DocumentHelper.createElement(WebDAV.XML_NS_COLLECTION));
}
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_RESOURCE_TYPE, WebDAV.XML_NS_RESOURCE_TYPE);
} else if (propName.equals(WebDAV.XML_DISPLAYNAME)) {
// Get the node name
if (getRootNodeRef().equals(nodeInfo.getNodeRef())) {
// Output an empty name for the root node
xml.write(DocumentHelper.createElement(WebDAV.XML_NS_SOURCE));
} else {
// Get the node name
davValue = WebDAV.getDAVPropertyValue(props, WebDAV.XML_DISPLAYNAME);
// Output the node name
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_DISPLAYNAME, WebDAV.XML_NS_DISPLAYNAME, nullAttr);
if (davValue != null) {
String name = typeConv.convert(String.class, davValue);
if (name == null || name.length() == 0) {
logger.error("WebDAV name is null, value=" + davValue.getClass().getName() + ", node=" + nodeInfo.getNodeRef());
}
xml.write(name);
}
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_DISPLAYNAME, WebDAV.XML_NS_DISPLAYNAME);
}
} else if (propName.equals(WebDAV.XML_SOURCE)) {
// NOTE: source is always a no content element in our
// implementation
xml.write(DocumentHelper.createElement(WebDAV.XML_NS_SOURCE));
} else if (propName.equals(WebDAV.XML_GET_LAST_MODIFIED)) {
// Get the modifed date/time
davValue = WebDAV.getDAVPropertyValue(props, WebDAV.XML_GET_LAST_MODIFIED);
// Output the last modified date of the node
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_GET_LAST_MODIFIED, WebDAV.XML_NS_GET_LAST_MODIFIED, nullAttr);
if (davValue != null)
xml.write(WebDAV.formatModifiedDate(typeConv.convert(Date.class, davValue)));
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_GET_LAST_MODIFIED, WebDAV.XML_NS_GET_LAST_MODIFIED);
} else if (propName.equals(WebDAV.XML_GET_CONTENT_LANGUAGE) && !isDir) {
// Get the content language
// TODO:
// Output the content language
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_GET_CONTENT_LANGUAGE, WebDAV.XML_NS_GET_CONTENT_LANGUAGE, nullAttr);
// TODO:
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_GET_CONTENT_LANGUAGE, WebDAV.XML_NS_GET_CONTENT_LANGUAGE);
} else if (propName.equals(WebDAV.XML_GET_CONTENT_TYPE) && !isDir) {
// Get the content type
davValue = WebDAV.getDAVPropertyValue(props, WebDAV.XML_GET_CONTENT_TYPE);
// Output the content type
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_GET_CONTENT_TYPE, WebDAV.XML_NS_GET_CONTENT_TYPE, nullAttr);
if (davValue != null)
xml.write(typeConv.convert(String.class, davValue));
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_GET_CONTENT_TYPE, WebDAV.XML_NS_GET_CONTENT_TYPE);
} else if (propName.equals(WebDAV.XML_GET_ETAG) && !isDir) {
// Output the etag
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_GET_ETAG, WebDAV.XML_NS_GET_ETAG, nullAttr);
xml.write(getDAVHelper().makeETag(nodeInfo));
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_GET_ETAG, WebDAV.XML_NS_GET_ETAG);
} else if (propName.equals(WebDAV.XML_GET_CONTENT_LENGTH)) {
// Get the content length, if it's not a folder
long len = 0;
if (!isDir) {
ContentData contentData = (ContentData) props.get(ContentModel.PROP_CONTENT);
if (contentData != null)
len = contentData.getSize();
}
// Output the content length
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_GET_CONTENT_LENGTH, WebDAV.XML_NS_GET_CONTENT_LENGTH, nullAttr);
xml.write("" + len);
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_GET_CONTENT_LENGTH, WebDAV.XML_NS_GET_CONTENT_LENGTH);
} else if (propName.equals(WebDAV.XML_CREATION_DATE)) {
// Get the creation date
davValue = WebDAV.getDAVPropertyValue(props, WebDAV.XML_CREATION_DATE);
// Output the creation date
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_CREATION_DATE, WebDAV.XML_NS_CREATION_DATE, nullAttr);
if (davValue != null)
xml.write(WebDAV.formatCreationDate(typeConv.convert(Date.class, davValue)));
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_CREATION_DATE, WebDAV.XML_NS_CREATION_DATE);
} else if (propName.equals(WebDAV.XML_ALF_AUTHTICKET)) {
// Get the users authentication ticket
SessionUser davUser = (SessionUser) m_request.getSession().getAttribute(AuthenticationFilter.AUTHENTICATION_USER);
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_ALF_AUTHTICKET, WebDAV.XML_NS_ALF_AUTHTICKET, nullAttr);
if (davUser != null)
xml.write(davUser.getTicket());
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_ALF_AUTHTICKET, WebDAV.XML_NS_ALF_AUTHTICKET);
} else {
// Could not map the requested property to an Alfresco property
if (property.getName().equals(WebDAV.XML_HREF) == false)
propertiesNotFound.add(property);
}
} else {
// Look in the custom properties
// String qualifiedName = propNamespaceUri + WebDAV.NAMESPACE_SEPARATOR + propName;
String value = (String) nodeInfo.getProperties().get(property.createQName());
if (value == null) {
if (deadProperties == null) {
deadProperties = loadDeadProperties(nodeInfo.getNodeRef());
}
value = deadProperties.get(property.createQName());
}
if (value == null) {
propertiesNotFound.add(property);
} else {
if (property.hasNamespaceName()) {
xml.startElement(property.getNamespaceName(), property.getName(), property.getNamespaceName() + WebDAV.NAMESPACE_SEPARATOR + property.getName(), nullAttr);
xml.write(value);
xml.endElement(property.getNamespaceName(), property.getName(), property.getNamespaceName() + WebDAV.NAMESPACE_SEPARATOR + property.getName());
} else {
xml.startElement("", property.getName(), property.getName(), nullAttr);
xml.write(value);
xml.endElement("", property.getName(), property.getName());
}
}
}
}
// Close off the successful part of the response
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_PROP, WebDAV.XML_NS_PROP);
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_STATUS, WebDAV.XML_NS_STATUS, nullAttr);
xml.write(WebDAV.HTTP1_1 + " " + HttpServletResponse.SC_OK + " " + WebDAV.SC_OK_DESC);
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_STATUS, WebDAV.XML_NS_STATUS);
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_PROPSTAT, WebDAV.XML_NS_PROPSTAT);
if (propertiesNotFound.size() > 0) {
// Start the second status section
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_PROPSTAT, WebDAV.XML_NS_PROPSTAT, nullAttr);
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_PROP, WebDAV.XML_NS_PROP, nullAttr);
for (WebDAVProperty property : propertiesNotFound) {
// Output the property not found status block
String propName = property.getName();
String propNamespaceName = property.getNamespaceName();
String propQName = propName;
if (propNamespaceName != null && propNamespaceName.length() > 0)
propQName = propNamespaceName + ":" + propName;
xml.write(DocumentHelper.createElement(propQName));
}
// Close the unsuccessful part of the response
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_PROP, WebDAV.XML_NS_PROP);
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_STATUS, WebDAV.XML_NS_STATUS, nullAttr);
xml.write(WebDAV.HTTP1_1 + " " + HttpServletResponse.SC_NOT_FOUND + " " + WebDAV.SC_NOT_FOUND_DESC);
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_STATUS, WebDAV.XML_NS_STATUS);
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_PROPSTAT, WebDAV.XML_NS_PROPSTAT);
}
}
use of org.alfresco.service.cmr.repository.ContentData in project alfresco-remote-api by Alfresco.
the class WorkflowRestImpl method createItemForNodeRef.
protected Item createItemForNodeRef(NodeRef nodeRef) {
Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
Item item = new Item();
String name = (String) properties.get(ContentModel.PROP_NAME);
String title = (String) properties.get(ContentModel.PROP_TITLE);
String description = (String) properties.get(ContentModel.PROP_DESCRIPTION);
Date createdAt = (Date) properties.get(ContentModel.PROP_CREATED);
String createdBy = (String) properties.get(ContentModel.PROP_CREATOR);
Date modifiedAt = (Date) properties.get(ContentModel.PROP_MODIFIED);
String modifiedBy = (String) properties.get(ContentModel.PROP_MODIFIER);
ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
item.setId(nodeRef.getId());
item.setName(name);
item.setTitle(title);
item.setDescription(description);
item.setCreatedAt(createdAt);
item.setCreatedBy(createdBy);
item.setModifiedAt(modifiedAt);
item.setModifiedBy(modifiedBy);
if (contentData != null) {
item.setMimeType(contentData.getMimetype());
item.setSize(contentData.getSize());
}
return item;
}
use of org.alfresco.service.cmr.repository.ContentData in project records-management by Alfresco.
the class ApiNodesModelFactory method mapRecordInfo.
/**
* Utility method that maps record specific fields
*
* @param record the record to set the fields to
* @param info info of the record
* @param includeParam the requested include parameters
*/
private void mapRecordInfo(Record record, FileInfo info, List<String> includeParam) {
if (includeParam == null || includeParam.isEmpty()) {
return;
}
if (includeParam.contains(Record.PARAM_IS_COMPLETED)) {
record.setIsCompleted(nodeService.hasAspect(info.getNodeRef(), RecordsManagementModel.ASPECT_DECLARED_RECORD));
}
if (includeParam.contains(Record.PARAM_CONTENT)) {
Serializable val = info.getProperties().get(ContentModel.PROP_CONTENT);
if ((val != null) && (val instanceof ContentData)) {
ContentData cd = (ContentData) val;
String mimeType = cd.getMimetype();
String mimeTypeName = serviceRegistry.getMimetypeService().getDisplaysByMimetype().get(mimeType);
ContentInfo contentInfo = new ContentInfo(mimeType, mimeTypeName, cd.getSize(), cd.getEncoding());
record.setContent(contentInfo);
}
}
}
use of org.alfresco.service.cmr.repository.ContentData in project records-management by Alfresco.
the class CreateRecordTest method testCreateRecordViaCoreServices.
/**
* Given I have ViewRecord and CreateRecord capabilities
* And I have filling on a record folder
* When I create content via ScriptNode (simulated)
* Then the record is successfully created
*
* @see https://issues.alfresco.com/jira/browse/RM-1956
*/
public void testCreateRecordViaCoreServices() throws Exception {
doBehaviourDrivenTest(new BehaviourDrivenTest() {
/**
* test data
*/
String roleName = GUID.generate();
String user = GUID.generate();
NodeRef recordFolder;
NodeRef record;
public void given() {
// create a role with view and create capabilities
Set<Capability> capabilities = new HashSet<Capability>(2);
capabilities.add(capabilityService.getCapability("ViewRecords"));
capabilities.add(capabilityService.getCapability("CreateRecords"));
filePlanRoleService.createRole(filePlan, roleName, roleName, capabilities);
// create user and assign to role
createPerson(user, true);
filePlanRoleService.assignRoleToAuthority(filePlan, roleName, user);
// create file plan structure
NodeRef rc = filePlanService.createRecordCategory(filePlan, GUID.generate());
recordFolder = recordFolderService.createRecordFolder(rc, GUID.generate());
}
public void when() {
// give read and file permissions to user
filePlanPermissionService.setPermission(recordFolder, user, RMPermissionModel.FILING);
record = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {
public NodeRef doWork() throws Exception {
NodeRef record = fileFolderService.create(recordFolder, "testRecord.txt", ContentModel.TYPE_CONTENT).getNodeRef();
ContentData content = (ContentData) nodeService.getProperty(record, PROP_CONTENT);
nodeService.setProperty(record, PROP_CONTENT, ContentData.setMimetype(content, MimetypeMap.MIMETYPE_TEXT_PLAIN));
return record;
}
}, user);
}
public void then() {
// check the details of the record
assertTrue(recordService.isRecord(record));
AuthenticationUtil.runAs(new RunAsWork<Void>() {
public Void doWork() throws Exception {
// we are expecting an expception here
try {
ContentData content = (ContentData) nodeService.getProperty(record, PROP_CONTENT);
nodeService.setProperty(record, PROP_CONTENT, ContentData.setMimetype(content, MimetypeMap.MIMETYPE_TEXT_PLAIN));
fail("Expecting access denied exception");
} catch (AccessDeniedException exception) {
// expceted
}
return null;
}
}, user);
}
});
}
Aggregations