Search in sources :

Example 11 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class InternalValue method create.

/**
     * Create a new internal value from the given JCR value.
     * If the data store is enabled, large binary values are stored in the data store.
     *
     * @param value the JCR value
     * @param resolver
     * @param store the data store
     * @return the created internal value
     * @throws RepositoryException
     * @throws ValueFormatException
     */
public static InternalValue create(Value value, NamePathResolver resolver, DataStore store) throws ValueFormatException, RepositoryException {
    switch(value.getType()) {
        case PropertyType.BINARY:
            BLOBFileValue blob = null;
            if (value instanceof BinaryValueImpl) {
                BinaryValueImpl bin = (BinaryValueImpl) value;
                DataIdentifier identifier = bin.getDataIdentifier();
                if (identifier != null) {
                    if (bin.usesDataStore(store)) {
                        // access the record to ensure it is not garbage collected
                        store.getRecord(identifier);
                        blob = BLOBInDataStore.getInstance(store, identifier);
                    } else {
                        if (store.getRecordIfStored(identifier) != null) {
                            // it exists - so we don't need to stream it again
                            // but we need to create a new object because the original
                            // one might be in a different data store (repository)
                            blob = BLOBInDataStore.getInstance(store, identifier);
                        }
                    }
                }
            }
            if (blob == null) {
                Binary b = value.getBinary();
                boolean dispose = false;
                try {
                    if (b instanceof BLOBFileValue) {
                        // use as is
                        blob = (BLOBFileValue) b;
                    } else {
                        // create a copy from the stream
                        dispose = true;
                        blob = getBLOBFileValue(store, b.getStream(), true);
                    }
                } finally {
                    if (dispose) {
                        b.dispose();
                    }
                }
            }
            return new InternalValue(blob);
        case PropertyType.BOOLEAN:
            return create(value.getBoolean());
        case PropertyType.DATE:
            return create(value.getDate());
        case PropertyType.DOUBLE:
            return create(value.getDouble());
        case PropertyType.DECIMAL:
            return create(value.getDecimal());
        case PropertyType.LONG:
            return create(value.getLong());
        case PropertyType.REFERENCE:
            return create(new NodeId(value.getString()));
        case PropertyType.WEAKREFERENCE:
            return create(new NodeId(value.getString()), true);
        case PropertyType.URI:
            try {
                return create(new URI(value.getString()));
            } catch (URISyntaxException e) {
                throw new ValueFormatException(e.getMessage());
            }
        case PropertyType.NAME:
            try {
                if (value instanceof QValueValue) {
                    QValue qv = ((QValueValue) value).getQValue();
                    if (qv instanceof InternalValue) {
                        return (InternalValue) qv;
                    } else {
                        return create(qv.getName());
                    }
                } else {
                    return create(resolver.getQName(value.getString()));
                }
            } catch (NameException e) {
                throw new ValueFormatException(e.getMessage());
            }
        case PropertyType.PATH:
            try {
                if (value instanceof QValueValue) {
                    QValue qv = ((QValueValue) value).getQValue();
                    if (qv instanceof InternalValue) {
                        return (InternalValue) qv;
                    } else {
                        return create(qv.getPath());
                    }
                } else {
                    return create(resolver.getQPath(value.getString(), false));
                }
            } catch (MalformedPathException mpe) {
                throw new ValueFormatException(mpe.getMessage());
            }
        case PropertyType.STRING:
            return create(value.getString());
        default:
            throw new IllegalArgumentException("illegal value");
    }
}
Also used : QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) AbstractQValue(org.apache.jackrabbit.spi.commons.value.AbstractQValue) QValue(org.apache.jackrabbit.spi.QValue) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeId(org.apache.jackrabbit.core.id.NodeId) ValueFormatException(javax.jcr.ValueFormatException) Binary(javax.jcr.Binary)

Example 12 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class NodeImpl method createProperty.

/**
     * Create a new single valued property
     *
     * @param qName
     * @param type
     * @param value
     * @return
     * @throws ConstraintViolationException if no applicable property definition
     * could be found.
     * @throws RepositoryException if another error occurs.
     */
private Property createProperty(Name qName, Value value, int type) throws ConstraintViolationException, RepositoryException {
    QPropertyDefinition def = getApplicablePropertyDefinition(qName, type, false);
    int targetType = def.getRequiredType();
    if (targetType == PropertyType.UNDEFINED) {
        targetType = type;
    }
    QValue qvs;
    if (targetType == PropertyType.UNDEFINED) {
        qvs = ValueFormat.getQValue(value, session.getNamePathResolver(), session.getQValueFactory());
        targetType = qvs.getType();
    } else {
        Value targetValue = ValueHelper.convert(value, targetType, session.getValueFactory());
        qvs = ValueFormat.getQValue(targetValue, session.getNamePathResolver(), session.getQValueFactory());
    }
    return createProperty(qName, targetType, def, new QValue[] { qvs });
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value)

Example 13 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class PropertyImpl method setValue.

/**
     * @see Property#setValue(Node)
     */
public void setValue(Node value) throws ValueFormatException, VersionException, LockException, RepositoryException {
    checkIsWritable(false);
    int reqType = getRequiredType(PropertyType.REFERENCE);
    if (value == null) {
        setInternalValues(null, reqType);
    } else {
        checkValidReference(value, reqType, session.getNameResolver());
        QValue qValue = session.getQValueFactory().create(value.getUUID(), PropertyType.REFERENCE);
        setInternalValues(new QValue[] { qValue }, reqType);
    }
}
Also used : QValue(org.apache.jackrabbit.spi.QValue)

Example 14 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class PropertyImpl method setValue.

/**
     *
     * @param value
     * @param requiredType
     * @throws RepositoryException
     */
private void setValue(Value value, int requiredType) throws RepositoryException {
    if (requiredType == PropertyType.UNDEFINED) {
        // should never get here since calling methods assert valid type
        throw new IllegalArgumentException("Property type of a value cannot be undefined (" + safeGetJCRPath() + ").");
    }
    if (value == null) {
        setInternalValues(null, requiredType);
        return;
    }
    QValue qValue;
    if (requiredType != value.getType()) {
        // type conversion required
        Value v = ValueHelper.convert(value, requiredType, session.getValueFactory());
        qValue = ValueFormat.getQValue(v, session.getNamePathResolver(), session.getQValueFactory());
    } else {
        // no type conversion required
        qValue = ValueFormat.getQValue(value, session.getNamePathResolver(), session.getQValueFactory());
    }
    setInternalValues(new QValue[] { qValue }, requiredType);
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) SetPropertyValue(org.apache.jackrabbit.jcr2spi.operation.SetPropertyValue) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value)

Example 15 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class RepositoryServiceImpl method getPropertyInfo.

@Override
public PropertyInfo getPropertyInfo(SessionInfo sessionInfo, PropertyId propertyId) throws RepositoryException {
    HttpGet request = null;
    try {
        String uri = getItemUri(propertyId, sessionInfo);
        request = new HttpGet(uri);
        HttpResponse response = executeRequest(sessionInfo, request);
        int status = response.getStatusLine().getStatusCode();
        if (status != DavServletResponse.SC_OK) {
            throw ExceptionConverter.generate(new DavException(status, response.getStatusLine().getReasonPhrase()));
        }
        Path path = uriResolver.getQPath(uri, sessionInfo);
        HttpEntity entity = response.getEntity();
        ContentType ct = ContentType.get(entity);
        boolean isMultiValued;
        QValue[] values;
        int type;
        NamePathResolver resolver = getNamePathResolver(sessionInfo);
        if (ct != null && ct.getMimeType().startsWith("jcr-value")) {
            type = JcrValueType.typeFromContentType(ct.getMimeType());
            QValue v;
            if (type == PropertyType.BINARY) {
                v = getQValueFactory().create(entity.getContent());
            } else {
                Reader reader = new InputStreamReader(entity.getContent(), ct.getCharset());
                StringBuffer sb = new StringBuffer();
                int c;
                while ((c = reader.read()) > -1) {
                    sb.append((char) c);
                }
                Value jcrValue = valueFactory.createValue(sb.toString(), type);
                if (jcrValue instanceof QValueValue) {
                    v = ((QValueValue) jcrValue).getQValue();
                } else {
                    v = ValueFormat.getQValue(jcrValue, resolver, getQValueFactory());
                }
            }
            values = new QValue[] { v };
            isMultiValued = false;
        } else if (ct != null && ct.getMimeType().equals("text/xml")) {
            // jcr:values property spooled
            values = getValues(entity.getContent(), resolver, propertyId);
            type = (values.length > 0) ? values[0].getType() : loadType(uri, getClient(sessionInfo), propertyId, sessionInfo, resolver);
            isMultiValued = true;
        } else {
            throw new ItemNotFoundException("Unable to retrieve the property with id " + saveGetIdString(propertyId, resolver));
        }
        return new PropertyInfoImpl(propertyId, path, type, isMultiValued, values);
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } catch (NameException e) {
        throw new RepositoryException(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) NamePathResolver(org.apache.jackrabbit.spi.commons.conversion.NamePathResolver) QValue(org.apache.jackrabbit.spi.QValue) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) InputStreamReader(java.io.InputStreamReader) DavException(org.apache.jackrabbit.webdav.DavException) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) IllegalNameException(org.apache.jackrabbit.spi.commons.conversion.IllegalNameException) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Aggregations

QValue (org.apache.jackrabbit.spi.QValue)125 Name (org.apache.jackrabbit.spi.Name)40 RepositoryException (javax.jcr.RepositoryException)23 Value (javax.jcr.Value)21 NodeId (org.apache.jackrabbit.spi.NodeId)21 Batch (org.apache.jackrabbit.spi.Batch)19 PropertyInfo (org.apache.jackrabbit.spi.PropertyInfo)18 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)12 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 ValueFormatException (javax.jcr.ValueFormatException)8 Path (org.apache.jackrabbit.spi.Path)8 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)8 HashMap (java.util.HashMap)7 ValueConstraint (org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)7 InputStream (java.io.InputStream)5 Node (javax.jcr.Node)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 PropertyId (org.apache.jackrabbit.spi.PropertyId)5