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