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");
}
}
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();
}
}
}
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);
}
}
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);
}
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());
}
Aggregations