use of hudson.util.AtomicFileWriter in project hudson-2.x by hudson.
the class XmlFile method write.
public void write(Object o) throws IOException {
mkdirs();
AtomicFileWriter w = new AtomicFileWriter(file);
try {
w.write("<?xml version='1.0' encoding='UTF-8'?>\n");
xs.toXML(o, w);
w.commit();
} catch (StreamException e) {
throw new IOException2(e);
} finally {
w.abort();
}
}
use of hudson.util.AtomicFileWriter in project hudson-2.x by hudson.
the class AbstractItem method doConfigDotXml.
/**
* Accepts <tt>config.xml</tt> submission, as well as serve it.
*/
@WebMethod(name = "config.xml")
public void doConfigDotXml(StaplerRequest req, StaplerResponse rsp) throws IOException {
if (req.getMethod().equals("GET")) {
// read
checkPermission(EXTENDED_READ);
rsp.setContentType("application/xml");
getConfigFile().writeRawTo(rsp.getOutputStream());
return;
}
if (req.getMethod().equals("POST")) {
// submission
checkPermission(CONFIGURE);
XmlFile configXmlFile = getConfigFile();
AtomicFileWriter out = new AtomicFileWriter(configXmlFile.getFile());
try {
try {
// this allows us to use UTF-8 for storing data,
// plus it checks any well-formedness issue in the submitted
// data
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new StreamSource(req.getReader()), new StreamResult(out));
out.close();
} catch (TransformerException e) {
throw new IOException2("Failed to persist configuration.xml", e);
}
// try to reflect the changes by reloading
new XmlFile(Items.XSTREAM, out.getTemporaryFile()).unmarshal(this);
onLoad(getParent(), getRootDir().getName());
// if everything went well, commit this new version
out.commit();
} finally {
// don't leave anything behind
out.abort();
}
return;
}
// huh?
rsp.sendError(SC_BAD_REQUEST);
}
Aggregations