Search in sources :

Example 1 with TypeConverter

use of org.alfresco.service.cmr.repository.datatype.TypeConverter in project alfresco-remote-api by Alfresco.

the class GetMethod method checkPreConditions.

/**
 * Checks the If header conditions
 *
 * @param nodeInfo the node to check
 * @throws WebDAVServerException if a pre-condition is not met
 */
private void checkPreConditions(FileInfo nodeInfo) throws WebDAVServerException {
    // Make an etag for the node
    String strETag = getDAVHelper().makeQuotedETag(nodeInfo);
    TypeConverter typeConv = DefaultTypeConverter.INSTANCE;
    if (ifMatchTags != null) {
        if (ifMatchTags.contains(WebDAV.ASTERISK) == false && ifMatchTags.contains(strETag) == false) {
            throw new WebDAVServerException(HttpServletResponse.SC_PRECONDITION_FAILED);
        }
    }
    if (ifNoneMatchTags != null) {
        if (ifNoneMatchTags.contains(WebDAV.ASTERISK) || ifNoneMatchTags.contains(strETag)) {
            throw new WebDAVServerException(HttpServletResponse.SC_NOT_MODIFIED);
        }
    }
    if (m_ifModifiedSince != null && ifNoneMatchTags == null) {
        Date lastModifiedDate = nodeInfo.getModifiedDate();
        long fileLastModified = lastModifiedDate != null ? typeConv.longValue(lastModifiedDate) : 0L;
        long modifiedSince = m_ifModifiedSince.getTime();
        if (fileLastModified != 0L && fileLastModified <= modifiedSince) {
            throw new WebDAVServerException(HttpServletResponse.SC_NOT_MODIFIED);
        }
    }
    if (m_ifUnModifiedSince != null) {
        Date lastModifiedDate = nodeInfo.getModifiedDate();
        long fileLastModified = lastModifiedDate != null ? typeConv.longValue(lastModifiedDate) : 0L;
        long unModifiedSince = m_ifUnModifiedSince.getTime();
        if (fileLastModified >= unModifiedSince) {
            throw new WebDAVServerException(HttpServletResponse.SC_PRECONDITION_FAILED);
        }
    }
}
Also used : DefaultTypeConverter(org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter) TypeConverter(org.alfresco.service.cmr.repository.datatype.TypeConverter) Date(java.util.Date)

Example 2 with TypeConverter

use of org.alfresco.service.cmr.repository.datatype.TypeConverter in project alfresco-remote-api by Alfresco.

the class PropFindMethod method generateAllPropertiesResponse.

/**
 * Generates the XML response for a PROPFIND request that asks for all known
 * properties
 *
 * @param xml XMLWriter
 * @param nodeInfo FileInfo
 * @param isDir boolean
 */
protected void generateAllPropertiesResponse(XMLWriter xml, FileInfo nodeInfo, boolean isDir) throws Exception {
    // Get the properties for the node
    Map<QName, Serializable> props = nodeInfo.getProperties();
    // 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);
    // Generate a lock status report, if locked
    generateLockDiscoveryResponse(xml, nodeInfo, isDir);
    // Output the supported lock types
    writeLockTypes(xml);
    // 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);
    // Get the node name
    Object davValue = WebDAV.getDAVPropertyValue(props, WebDAV.XML_DISPLAYNAME);
    TypeConverter typeConv = DefaultTypeConverter.INSTANCE;
    // 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);
    // Output the source
    // 
    // NOTE: source is always a no content element in our implementation
    xml.write(DocumentHelper.createElement(WebDAV.XML_NS_SOURCE));
    // 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);
    // 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);
    if (isDir == false) {
        // 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);
        // 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);
        // 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);
    }
    // Get the content length, if it's not a folder
    long len = 0;
    if (isDir == false) {
        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);
    // Print out all the custom properties
    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);
    // Close off 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);
}
Also used : DefaultTypeConverter(org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter) TypeConverter(org.alfresco.service.cmr.repository.datatype.TypeConverter) Serializable(java.io.Serializable) SessionUser(org.alfresco.repo.SessionUser) ContentData(org.alfresco.service.cmr.repository.ContentData) QName(org.alfresco.service.namespace.QName) Attributes(org.xml.sax.Attributes) Date(java.util.Date)

Example 3 with TypeConverter

use of org.alfresco.service.cmr.repository.datatype.TypeConverter 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);
    }
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) Attributes(org.xml.sax.Attributes) ArrayList(java.util.ArrayList) Date(java.util.Date) DefaultTypeConverter(org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter) TypeConverter(org.alfresco.service.cmr.repository.datatype.TypeConverter) SessionUser(org.alfresco.repo.SessionUser) ContentData(org.alfresco.service.cmr.repository.ContentData)

Aggregations

Date (java.util.Date)3 DefaultTypeConverter (org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter)3 TypeConverter (org.alfresco.service.cmr.repository.datatype.TypeConverter)3 Serializable (java.io.Serializable)2 SessionUser (org.alfresco.repo.SessionUser)2 ContentData (org.alfresco.service.cmr.repository.ContentData)2 QName (org.alfresco.service.namespace.QName)2 Attributes (org.xml.sax.Attributes)2 ArrayList (java.util.ArrayList)1