return;
}
final TransactionManager transact = broker.getBrokerPool().getTransactionManager();
Txn transaction = null;
VirtualTempFile vtempFile = null;
try {
// fourth, process the request
InputStream is = request.getInputStream();
long len = request.getContentLength();
final String lenstr = request.getHeader("Content-Length");
if (lenstr != null) {
len = Long.parseLong(lenstr);
}
// put may send a lot of data, so save it
// to a temporary file first.
vtempFile = new VirtualTempFile();
vtempFile.setTempPrefix("existSRV");
vtempFile.setTempPostfix(".tmp");
vtempFile.write(is, len);
vtempFile.close();
final XmldbURI docUri = path.lastSegment();
final XmldbURI collUri = path.removeLastSegment();
if (docUri == null || collUri == null) {
//transact.abort(transaction);
throw new BadRequestException("Bad path: " + path);
}
// TODO : use getOrCreateCollection() right now ?
Collection collection = broker.getCollection(collUri);
if (collection == null) {
LOG.debug("creating collection " + collUri);
transaction = transact.beginTransaction();
collection = broker.getOrCreateCollection(transaction, collUri);
broker.saveCollection(transaction, collection);
}
MimeType mime;
String contentType = request.getContentType();
String charset = null;
if (contentType != null) {
final int semicolon = contentType.indexOf(';');
if (semicolon > 0) {
contentType = contentType.substring(0, semicolon).trim();
final int equals = contentType.indexOf('=', semicolon);
if (equals > 0) {
final String param = contentType.substring(semicolon + 1,
equals).trim();
if (param.compareToIgnoreCase("charset=") == 0) {
charset = param.substring(equals + 1).trim();
}
}
}
mime = MimeTable.getInstance().getContentType(contentType);
} else {
mime = MimeTable.getInstance().getContentTypeFor(docUri);
if (mime != null) {
contentType = mime.getName();
}
}
if (mime == null) {
mime = MimeType.BINARY_TYPE;
contentType = mime.getName();
}
if (transaction == null) {
transaction = transact.beginTransaction();
}
if (mime.isXMLType()) {
final InputSource vtfis = new VirtualTempFileInputSource(vtempFile, charset);
final IndexInfo info = collection.validateXMLResource(transaction, broker, docUri, vtfis);
info.getDocument().getMetadata().setMimeType(contentType);
collection.store(transaction, broker, info, vtfis, false);
response.setStatus(HttpServletResponse.SC_CREATED);
} else {
is = vtempFile.getByteStream();
try {
collection.addBinaryResource(transaction, broker, docUri, is,
contentType, vtempFile.length());
} finally {
is.close();
}
response.setStatus(HttpServletResponse.SC_CREATED);
}
transact.commit(transaction);
} catch (final SAXParseException e) {
transact.abort(transaction);
throw new BadRequestException("Parsing exception at "
+ e.getLineNumber() + "/" + e.getColumnNumber() + ": "
+ e.toString());
} catch (final TriggerException e) {
transact.abort(transaction);
throw new PermissionDeniedException(e.getMessage());
} catch (SAXException e) {
transact.abort(transaction);
Exception o = e.getException();
if (o == null) {
o = e;
}
throw new BadRequestException("Parsing exception: " + o.getMessage());
} catch (final EXistException e) {
transact.abort(transaction);
throw new BadRequestException("Internal error: " + e.getMessage());
} catch (final LockException e) {
transact.abort(transaction);
throw new PermissionDeniedException(e.getMessage());
} finally {
transact.close(transaction);
if (vtempFile != null) {
vtempFile.delete();
}
}
return;
}