Search in sources :

Example 1 with ValuesProperty

use of org.apache.jackrabbit.webdav.jcr.property.ValuesProperty in project jackrabbit by apache.

the class DefaultItemCollection method addMember.

/**
 * If the specified resource represents a collection, a new node is {@link Node#addNode(String)
 * added} to the item represented by this resource. If an input stream is specified
 * together with a collection resource {@link Session#importXML(String, java.io.InputStream, int)}
 * is called instead and this resource path is used as <code>parentAbsPath</code> argument.
 * <p>
 * However, if the specified resource is not of resource type collection a
 * new {@link Property} is set or an existing one is changed by modifying its
 * value.<br>
 * NOTE: with the current implementation it is not possible to create or
 * modify multivalue JCR properties.<br>
 * NOTE: if the JCR property represented by the specified resource has an
 * {@link PropertyType#UNDEFINED undefined} resource type, its value will be
 * changed/set to type {@link PropertyType#BINARY binary}.
 *
 * @param resource
 * @param inputContext
 * @throws org.apache.jackrabbit.webdav.DavException
 * @see org.apache.jackrabbit.webdav.DavResource#addMember(org.apache.jackrabbit.webdav.DavResource, InputContext)
 * @see Node#addNode(String)
 * @see Node#setProperty(String, java.io.InputStream)
 */
@Override
public void addMember(DavResource resource, InputContext inputContext) throws DavException {
    /* RFC 2815 states that all 'parents' must exist in order all addition of members */
    if (!exists()) {
        throw new DavException(DavServletResponse.SC_CONFLICT);
    }
    File tmpFile = null;
    try {
        Node n = (Node) item;
        InputStream in = (inputContext != null) ? inputContext.getInputStream() : null;
        String itemPath = getLocator().getRepositoryPath();
        String memberName = getItemName(resource.getLocator().getRepositoryPath());
        if (resource.isCollection()) {
            if (in == null) {
                // MKCOL without a request body, try if a default-primary-type is defined.
                n.addNode(memberName);
            } else {
                // MKCOL, which is not allowed for existing resources
                int uuidBehavior = ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW;
                String str = inputContext.getProperty(IMPORT_UUID_BEHAVIOR);
                if (str != null) {
                    try {
                        uuidBehavior = Integer.parseInt(str);
                    } catch (NumberFormatException e) {
                        throw new DavException(DavServletResponse.SC_BAD_REQUEST);
                    }
                }
                if (getTransactionId() == null) {
                    // if not part of a transaction directly import on workspace
                    // since changes would be explicitly saved in the
                    // complete-call.
                    getRepositorySession().getWorkspace().importXML(itemPath, in, uuidBehavior);
                } else {
                    // changes will not be persisted unless the tx is completed.
                    getRepositorySession().importXML(itemPath, in, uuidBehavior);
                }
            }
        } else {
            if (in == null) {
                // PUT: not possible without request body
                throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Cannot create a new non-collection resource without request body.");
            }
            // PUT : create new or overwrite existing property.
            String ct = inputContext.getContentType();
            int type = JcrValueType.typeFromContentType(ct);
            if (type != PropertyType.UNDEFINED) {
                // no need to create value/values property. instead
                // prop-value can be retrieved directly:
                int pos = ct.indexOf(';');
                String charSet = (pos > -1) ? ct.substring(pos) : "UTF-8";
                if (type == PropertyType.BINARY) {
                    n.setProperty(memberName, inputContext.getInputStream());
                } else {
                    BufferedReader r = new BufferedReader(new InputStreamReader(inputContext.getInputStream(), charSet));
                    String line;
                    StringBuffer value = new StringBuffer();
                    while ((line = r.readLine()) != null) {
                        value.append(line);
                    }
                    n.setProperty(memberName, value.toString(), type);
                }
            } else {
                // try to parse the request body into a 'values' property.
                tmpFile = File.createTempFile(TMP_PREFIX + Text.escape(memberName), null, null);
                FileOutputStream out = new FileOutputStream(tmpFile);
                IOUtil.spool(in, out);
                out.close();
                // try to parse the request body into a 'values' property.
                ValuesProperty vp = buildValuesProperty(new FileInputStream(tmpFile));
                if (vp != null) {
                    if (JCR_VALUE.equals(vp.getName())) {
                        n.setProperty(memberName, vp.getJcrValue());
                    } else {
                        n.setProperty(memberName, vp.getJcrValues());
                    }
                } else {
                    // request body cannot be parsed into a 'values' property.
                    // fallback: try to import as single value from stream.
                    n.setProperty(memberName, new FileInputStream(tmpFile));
                }
            }
        }
        if (resource.exists() && resource instanceof AbstractItemResource) {
            // PUT may modify value of existing jcr property. thus, this
            // node is not modified by the 'addMember' call.
            ((AbstractItemResource) resource).complete();
        } else {
            complete();
        }
    } catch (ItemExistsException e) {
        // according to RFC 2518: MKCOL only possible on non-existing/deleted resource
        throw new JcrDavException(e, DavServletResponse.SC_METHOD_NOT_ALLOWED);
    } catch (RepositoryException e) {
        throw new JcrDavException(e);
    } catch (IOException e) {
        throw new DavException(DavServletResponse.SC_UNPROCESSABLE_ENTITY, e.getMessage());
    } finally {
        if (tmpFile != null) {
            tmpFile.delete();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DavException(org.apache.jackrabbit.webdav.DavException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) ValuesProperty(org.apache.jackrabbit.webdav.jcr.property.ValuesProperty) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ItemExistsException(javax.jcr.ItemExistsException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 2 with ValuesProperty

use of org.apache.jackrabbit.webdav.jcr.property.ValuesProperty in project jackrabbit by apache.

the class DefaultItemResource method internalSetProperty.

/**
 * Internal method that performs the setting or adding of properties
 *
 * @param property
 * @throws DavException
 * @see #setProperty(DavProperty)
 * @see #alterProperties(List)
 */
private void internalSetProperty(DavProperty<?> property) throws DavException {
    if (!exists()) {
        throw new DavException(DavServletResponse.SC_NOT_FOUND);
    }
    try {
        Property prop = (Property) item;
        int defaultType = prop.getType();
        ValueFactory vfact = getRepositorySession().getValueFactory();
        ValuesProperty vp = new ValuesProperty(property, defaultType, vfact);
        if (property.getName().equals(JCR_VALUE)) {
            prop.setValue(vp.getJcrValue(vp.getValueType(), vfact));
        } else if (property.getName().equals(JCR_VALUES)) {
            prop.setValue(vp.getJcrValues());
        } else {
            throw new DavException(DavServletResponse.SC_CONFLICT);
        }
    } catch (RepositoryException e) {
        throw new JcrDavException(e);
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) ValuesProperty(org.apache.jackrabbit.webdav.jcr.property.ValuesProperty) RepositoryException(javax.jcr.RepositoryException) ValueFactory(javax.jcr.ValueFactory) LengthsProperty(org.apache.jackrabbit.webdav.jcr.property.LengthsProperty) Property(javax.jcr.Property) DefaultDavProperty(org.apache.jackrabbit.webdav.property.DefaultDavProperty) DavProperty(org.apache.jackrabbit.webdav.property.DavProperty) ValuesProperty(org.apache.jackrabbit.webdav.jcr.property.ValuesProperty)

Example 3 with ValuesProperty

use of org.apache.jackrabbit.webdav.jcr.property.ValuesProperty in project jackrabbit by apache.

the class DefaultItemResource method initProperties.

/**
 * Add resource specific properties.
 */
@Override
protected void initProperties() {
    super.initProperties();
    if (exists()) {
        try {
            Property prop = (Property) item;
            int type = prop.getType();
            // set the content type
            String contentType;
            if (isMultiple()) {
                contentType = IOUtil.buildContentType("text/xml", "utf-8");
            } else {
                contentType = IOUtil.buildContentType(JcrValueType.contentTypeFromType(type), "utf-8");
            }
            properties.add(new DefaultDavProperty<String>(DavPropertyName.GETCONTENTTYPE, contentType));
            // add jcr-specific resource properties
            properties.add(new DefaultDavProperty<String>(JCR_TYPE, PropertyType.nameFromValue(type)));
            if (isMultiple()) {
                properties.add(new ValuesProperty(prop.getValues()));
            } else {
                properties.add(new ValuesProperty(prop.getValue()));
            }
        } catch (RepositoryException e) {
            log.error("Failed to retrieve resource properties: " + e.getMessage());
        }
    }
}
Also used : ValuesProperty(org.apache.jackrabbit.webdav.jcr.property.ValuesProperty) RepositoryException(javax.jcr.RepositoryException) LengthsProperty(org.apache.jackrabbit.webdav.jcr.property.LengthsProperty) Property(javax.jcr.Property) DefaultDavProperty(org.apache.jackrabbit.webdav.property.DefaultDavProperty) DavProperty(org.apache.jackrabbit.webdav.property.DavProperty) ValuesProperty(org.apache.jackrabbit.webdav.jcr.property.ValuesProperty)

Example 4 with ValuesProperty

use of org.apache.jackrabbit.webdav.jcr.property.ValuesProperty in project jackrabbit by apache.

the class DefaultItemCollection method buildValuesProperty.

/**
 * Tries to parse the given input stream as xml document and build a
 * {@link ValuesProperty} out of it.
 *
 * @param in
 * @return values property or 'null' if the given stream cannot be parsed
 * into an XML document or if build the property fails.
 */
private ValuesProperty buildValuesProperty(InputStream in) {
    String errorMsg = "Cannot parse stream into a 'ValuesProperty'.";
    try {
        Document reqBody = DomUtil.parseDocument(in);
        DavProperty<?> defaultProp = DefaultDavProperty.createFromXml(reqBody.getDocumentElement());
        ValuesProperty vp = new ValuesProperty(defaultProp, PropertyType.STRING, getRepositorySession().getValueFactory());
        return vp;
    } catch (IOException e) {
        log.debug(errorMsg, e);
    } catch (ParserConfigurationException e) {
        log.debug(errorMsg, e);
    } catch (SAXException e) {
        log.debug(errorMsg, e);
    } catch (DavException e) {
        log.debug(errorMsg, e);
    } catch (RepositoryException e) {
        log.debug(errorMsg, e);
    }
    // cannot parse request body into a 'values' property
    return null;
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) ValuesProperty(org.apache.jackrabbit.webdav.jcr.property.ValuesProperty) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Aggregations

RepositoryException (javax.jcr.RepositoryException)4 ValuesProperty (org.apache.jackrabbit.webdav.jcr.property.ValuesProperty)4 DavException (org.apache.jackrabbit.webdav.DavException)3 IOException (java.io.IOException)2 Property (javax.jcr.Property)2 LengthsProperty (org.apache.jackrabbit.webdav.jcr.property.LengthsProperty)2 DavProperty (org.apache.jackrabbit.webdav.property.DavProperty)2 DefaultDavProperty (org.apache.jackrabbit.webdav.property.DefaultDavProperty)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ItemExistsException (javax.jcr.ItemExistsException)1 Node (javax.jcr.Node)1 ValueFactory (javax.jcr.ValueFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Document (org.w3c.dom.Document)1 SAXException (org.xml.sax.SAXException)1