Search in sources :

Example 1 with QValueValue

use of org.apache.jackrabbit.spi.commons.value.QValueValue 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 2 with QValueValue

use of org.apache.jackrabbit.spi.commons.value.QValueValue 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) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) IllegalNameException(org.apache.jackrabbit.spi.commons.conversion.IllegalNameException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 3 with QValueValue

use of org.apache.jackrabbit.spi.commons.value.QValueValue in project jackrabbit by apache.

the class RepositoryServiceImpl method getValues.

private QValue[] getValues(InputStream response, NamePathResolver resolver, ItemId id) throws RepositoryException {
    try {
        Document doc = DomUtil.parseDocument(response);
        Element prop = DomUtil.getChildElement(doc, JcrRemotingConstants.JCR_VALUES_LN, ItemResourceConstants.NAMESPACE);
        if (prop == null) {
            // not representation of a jcr-property
            throw new ItemNotFoundException("No property found at " + saveGetIdString(id, resolver));
        } else {
            DavProperty<?> p = DefaultDavProperty.createFromXml(prop);
            Value[] jcrVs = ValueUtil.valuesFromXml(p.getValue(), PropertyType.STRING, valueFactory);
            QValue[] qvs = new QValue[jcrVs.length];
            int type = (jcrVs.length > 0) ? jcrVs[0].getType() : PropertyType.STRING;
            for (int i = 0; i < jcrVs.length; i++) {
                if (jcrVs[i] instanceof QValueValue) {
                    qvs[i] = ((QValueValue) jcrVs[i]).getQValue();
                } else if (type == PropertyType.BINARY) {
                    qvs[i] = qValueFactory.create(jcrVs[i].getStream());
                } else {
                    qvs[i] = ValueFormat.getQValue(jcrVs[i], resolver, qValueFactory);
                }
            }
            return qvs;
        }
    } catch (SAXException e) {
        log.warn("Internal error: {}", e.getMessage());
        throw new RepositoryException(e);
    } catch (IOException e) {
        log.warn("Internal error: {}", e.getMessage());
        throw new RepositoryException(e);
    } catch (ParserConfigurationException e) {
        log.warn("Internal error: {}", e.getMessage());
        throw new RepositoryException(e);
    }
}
Also used : QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) QValue(org.apache.jackrabbit.spi.QValue) Element(org.w3c.dom.Element) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 4 with QValueValue

use of org.apache.jackrabbit.spi.commons.value.QValueValue in project jackrabbit by apache.

the class AccessControlEntryImpl method createJcrValue.

/**
 * Creates a jcr Value from the given qvalue using the specified
 * factory.
 * @return         the jcr value representing the qvalue.
 */
private Value createJcrValue(QValue qValue) throws RepositoryException {
    // build ValueFactory
    ValueFactoryQImpl valueFactory = new ValueFactoryQImpl(qvf, resolver);
    // build jcr value
    QValueValue jcrValue = new QValueValue(qValue, resolver);
    return ValueHelper.copy(jcrValue, valueFactory);
}
Also used : QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) ValueFactoryQImpl(org.apache.jackrabbit.spi.commons.value.ValueFactoryQImpl)

Example 5 with QValueValue

use of org.apache.jackrabbit.spi.commons.value.QValueValue in project jackrabbit-oak by apache.

the class RepositoryTest method addAlienBinaryProperty.

@Test
public void addAlienBinaryProperty() throws RepositoryException, IOException {
    Session session = getAdminSession();
    QValue qValue = QValueFactoryImpl.getInstance().create("binaryValue".getBytes());
    Value value = new QValueValue(qValue, new DefaultNamePathResolver(session));
    getNode(TEST_PATH).setProperty("binary", value);
    session.save();
    Value valueAgain = getNode(TEST_PATH).getProperty("binary").getValue();
    assertEqualStream(value.getBinary().getStream(), valueAgain.getBinary().getStream());
}
Also used : QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) QValue(org.apache.jackrabbit.spi.QValue) QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) DefaultNamePathResolver(org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver) Session(javax.jcr.Session) Test(org.junit.Test)

Aggregations

QValueValue (org.apache.jackrabbit.spi.commons.value.QValueValue)5 QValue (org.apache.jackrabbit.spi.QValue)4 Value (javax.jcr.Value)3 IOException (java.io.IOException)2 ItemNotFoundException (javax.jcr.ItemNotFoundException)2 RepositoryException (javax.jcr.RepositoryException)2 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Binary (javax.jcr.Binary)1 Session (javax.jcr.Session)1 ValueFormatException (javax.jcr.ValueFormatException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 ContentType (org.apache.http.entity.ContentType)1 DataIdentifier (org.apache.jackrabbit.core.data.DataIdentifier)1