提交 dcf92d72 编写于 作者: K kohsuke

supported XPath selection in the remote API.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@3601 71c3de6d-444a-0410-be80-ed276b4c234a
上级 5168f715
......@@ -2,14 +2,22 @@ package hudson.model;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.Flavor;
import org.kohsuke.stapler.export.SchemaGenerator;
import org.kohsuke.stapler.export.ModelBuilder;
import org.kohsuke.stapler.export.Model;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringWriter;
import java.io.StringReader;
/**
* Used to expose remote access API for ".../api/"
......@@ -34,8 +42,43 @@ public class Api extends AbstractModelObject {
/**
* Exposes the bean as XML.
*/
public void doXml(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.serveExposedBean(req,bean, Flavor.XML);
public void doXml(StaplerRequest req, StaplerResponse rsp, @QueryParameter("xpath") String xpath) throws IOException, ServletException {
if(xpath==null) {
// serve the whole thing
rsp.serveExposedBean(req,bean,Flavor.XML);
return;
}
StringWriter sw = new StringWriter();
// first write to String
Model p = MODEL_BUILDER.get(bean.getClass());
p.writeTo(bean,Flavor.XML.createDataWriter(bean,sw));
// apply XPath
org.dom4j.Node result;
try {
Document dom = new SAXReader().read(new StringReader(sw.toString()));
result = dom.selectSingleNode(xpath);
} catch (DocumentException e) {
throw new IOException(e);
}
if(result==null) {
// XPath didn't match
rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
rsp.getWriter().print("XPath "+xpath+" didn't match");
return;
}
if(result instanceof CharacterData) {
rsp.setContentType("text/plain");
rsp.getWriter().print(result.getText());
return;
}
// otherwise XML
rsp.setContentType("application/xml;charset=UTF-8");
new XMLWriter(rsp.getWriter()).write(result);
}
/**
......@@ -54,4 +97,6 @@ public class Api extends AbstractModelObject {
public void doJson(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.serveExposedBean(req,bean, Flavor.JSON);
}
private static final ModelBuilder MODEL_BUILDER = new ModelBuilder();
}
......@@ -14,6 +14,10 @@
<dd>
Access data exposed in <a href="..">HTML</a> as XML for machine consumption.
<a href="schema">Schema</a> is also available.
You can also specify optional XPath to control the fragment you'd like to obtain.
For example, <tt>../api/xml?xpath=/*/*[0]</tt>. If the XPath only matches a text node,
the result will be sent with <tt>text/plain</tt> MIME type to simplify
further processing.
</dd>
<dt><a href="json">JSON API</a></dt>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册