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