提交 eabb943b 编写于 作者: K kohsuke

handle config.xml now in Java, by using @WebMethod.

This also enables POST-ing config.xml


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@7806 71c3de6d-444a-0410-be80-ed276b4c234a
上级 74a19786
......@@ -3,6 +3,7 @@ package hudson.model;
import hudson.ExtensionPoint;
import hudson.StructuredForm;
import hudson.Util;
import hudson.XmlFile;
import hudson.model.Descriptor.FormException;
import hudson.model.listeners.ItemListener;
import hudson.search.QuickSilver;
......@@ -11,6 +12,7 @@ import hudson.search.SearchIndexBuilder;
import hudson.search.SearchItem;
import hudson.search.SearchItems;
import hudson.tasks.LogRotator;
import hudson.util.AtomicFileWriter;
import hudson.util.ChartUtil;
import hudson.util.ColorPalette;
import hudson.util.CopyOnWriteList;
......@@ -38,10 +40,17 @@ import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.RectangleInsets;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.export.Exported;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.awt.*;
import java.io.File;
import java.io.IOException;
......@@ -733,6 +742,47 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
}
}
/**
* Accepts <tt>config.xml</tt> submission, as well as serve it.
*/
@WebMethod(name="config.xml")
public void doConfigDotXml(StaplerRequest req, StaplerResponse rsp) throws IOException {
checkPermission(CONFIGURE);
if(req.getMethod().equals("GET")) {
// read
rsp.setContentType("application/xml;charset=UTF-8");
getConfigFile().writeRawTo(rsp.getWriter());
return;
}
if(req.getMethod().equals("POST")) {
// submission
XmlFile configXmlFile = getConfigFile();
AtomicFileWriter out = new AtomicFileWriter(configXmlFile.getFile());
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(out.getTemporaryFile()).unmarshal(this);
onLoad(getParent(),getName());
// if everything went well, commit this new version
out.commit();
return;
}
// huh?
rsp.sendError(SC_BAD_REQUEST);
}
/**
* Derived class can override this to perform additional config submission work.
*/
......
<!-- Sends config.xml verbatim. Particular attention needs to be paid to avoid sending whitespace -->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt"
><j:new var="h" className="hudson.Functions"
/><!-- if this page needs to be secured, make sure the user is admin
-->${h.adminCheck(request,response,true)
}<!-- write config.xml
--><st:contentType value="application/xml;charset=UTF-8"
/>${it.configFile.asString()}
</j:jelly>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册