// Parse the XML to check for correct tags etc.
reader.parse(new InputSource(new StringReader(xml)));
} catch (SAXException se) {
// Invalid XML
logger.error("xml-validation-failure", se);
throw new MessageStoreException(
localizer.format("xml-validation-failure"),
MessageStoreMessageEnumeration.NOT_ACCEPTABLE);
}
}
// Store the xml to disk
File file = null;
do {
// Generate or regenerate the ID. It is possible that in the
// intervening time another thread has used the id and written
// a file!
newID = generateValidID();
file = createEntryFile(newID);
} while (!file.createNewFile());
// get the xml as a byte array using specified encoding in prolog
// if available - we assume that the XML prolog provides the
// definitive answer w.r.t. the XML encoding
byte[] xmlBuffer = null;
if (encoding != null) {
xmlBuffer = xml.getBytes(encoding);
} else {
xmlBuffer = xml.getBytes();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(xmlBuffer, 0, xmlBuffer.length);
fos.flush();
} catch (IOException ioe) {
if (file != null) {
// Tidy up in case writing failed
file.delete();
}
logger.error("xml-write-error", ioe);
throw new MessageStoreException(
localizer.format("xml-write-error"),
MessageStoreMessageEnumeration.BAD_REQUEST);
} finally {
if (fos != null) {
try {
fos.close();
} catch(IOException ioe) {
// ignore
}
}
}
synchronized (messageStoreIDs) {
// Store the id/timestamp in the memory cache
messageStoreIDs.put(newID, generateTimeStamp(file));
}
} catch (IOException ioe) {
logger.error("xml-write-error", ioe);
throw new MessageStoreException(
localizer.format("xml-write-error"),
MessageStoreMessageEnumeration.BAD_REQUEST);
} catch (MessageStoreException mse) {
logger.error("id-generation-error", mse);
throw new MessageStoreException(
localizer.format("id-generation-error"),
MessageStoreMessageEnumeration.BAD_REQUEST);
}
return newID;