Search in sources :

Example 1 with AtomicFileWriter

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();
    }
}
Also used : AtomicFileWriter(hudson.util.AtomicFileWriter) IOException2(hudson.util.IOException2) StreamException(com.thoughtworks.xstream.io.StreamException)

Example 2 with AtomicFileWriter

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);
}
Also used : Transformer(javax.xml.transform.Transformer) XmlFile(hudson.XmlFile) StreamResult(javax.xml.transform.stream.StreamResult) AtomicFileWriter(hudson.util.AtomicFileWriter) StreamSource(javax.xml.transform.stream.StreamSource) TransformerException(javax.xml.transform.TransformerException) IOException2(hudson.util.IOException2) WebMethod(org.kohsuke.stapler.WebMethod)

Aggregations

AtomicFileWriter (hudson.util.AtomicFileWriter)2 IOException2 (hudson.util.IOException2)2 StreamException (com.thoughtworks.xstream.io.StreamException)1 XmlFile (hudson.XmlFile)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 WebMethod (org.kohsuke.stapler.WebMethod)1