mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-17 05:52:26 +00:00
reimplemented the remote XML/JSON API so that the Hudson code can expose data easily without writing a separate bean.
git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@3041 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
d90c1e0758
commit
5ca3f63fa9
19 files changed
+606
-181
No files matched your search
@@ -0,0 +1,35 @@
|
||||
package hudson.api;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Receives the event callback on the model data to be exposed.
|
||||
*
|
||||
* <p>
|
||||
* The call sequence is:
|
||||
*
|
||||
* <pre>
|
||||
* EVENTS := startObject PROPERTY* endObject
|
||||
* PROPERTY := name VALUE
|
||||
* VALUE := valuePrimitive
|
||||
* | value
|
||||
* | valueNull
|
||||
* | startArray VALUE* endArray
|
||||
* | EVENTS
|
||||
* </pre>
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public interface DataWriter {
|
||||
void name(String name) throws IOException;
|
||||
|
||||
void valuePrimitive(Object v) throws IOException;
|
||||
void value(String v) throws IOException;
|
||||
void valueNull() throws IOException;
|
||||
|
||||
void startArray() throws IOException;
|
||||
void endArray() throws IOException;
|
||||
|
||||
void startObject() throws IOException;
|
||||
void endObject() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package hudson.api;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Mark the field or the getter method whose value gets exposed
|
||||
* to the remote API.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
* @see ExposedBean
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Target({FIELD, METHOD})
|
||||
public @interface Exposed {
|
||||
/**
|
||||
* Controls how visible this property is.
|
||||
*
|
||||
* <p>
|
||||
* If the value is 1, which is the default, the property will be
|
||||
* visible only when the current model object is exposed as the
|
||||
* top-level object.
|
||||
* <p>
|
||||
* If the value is 2, in addition to above, the property will still
|
||||
* be visible if the current model object is exposed as the 2nd-level
|
||||
* object.
|
||||
* <p>
|
||||
* And the rest goes in the same way. If the value is N, the object
|
||||
* is exposed as the Nth level object.
|
||||
*
|
||||
* <p>
|
||||
* So bigger the number, more important the property is.
|
||||
*/
|
||||
int visibility() default 1;
|
||||
|
||||
/**
|
||||
* Name of the exposed property.
|
||||
* <p>
|
||||
* This token is used as the XML element name or the JSON property name.
|
||||
* The default is to use the Java property name.
|
||||
*/
|
||||
String name() default "";
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package hudson.api;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Indicates that the class has {@link Exposed} annotations
|
||||
* on its properties to indicate which properties are written
|
||||
* as values to the remote XML/JSON API.
|
||||
*
|
||||
* <p>
|
||||
* This annotation inherits, so it only needs to be placed on the base class.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
* @see Exposed
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface ExposedBean {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package hudson.api;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* {@link Property} based on {@link Field}.
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
class FieldProperty extends Property {
|
||||
private final Field field;
|
||||
|
||||
public FieldProperty(ParserBuilder owner, Field field, Exposed exposed) {
|
||||
super(owner,field.getName(),exposed);
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
protected Object getValue(Object object) throws IllegalAccessException {
|
||||
return field.get(object);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package hudson.api;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* {@link Property} based on {@link Method}.
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class MethodProperty extends Property {
|
||||
private final Method method;
|
||||
MethodProperty(ParserBuilder owner, Method m, Exposed exposed) {
|
||||
super(owner,buildName(m.getName()),exposed);
|
||||
this.method = m;
|
||||
}
|
||||
|
||||
private static String buildName(String name) {
|
||||
if(name.startsWith("get"))
|
||||
name = name.substring(3);
|
||||
else
|
||||
if(name.startsWith("is"))
|
||||
name = name.substring(2);
|
||||
|
||||
return Introspector.decapitalize(name);
|
||||
}
|
||||
|
||||
|
||||
protected Object getValue(Object object) throws IllegalAccessException, InvocationTargetException {
|
||||
return method.invoke(object);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package hudson.api;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Writes all the property of one {@link ExposedBean} to {@link DataWriter}.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class Parser<T> {
|
||||
private final Class<T> type;
|
||||
|
||||
/**
|
||||
* {@link Parser} for the super class.
|
||||
*/
|
||||
private final Parser<? super T> superParser;
|
||||
|
||||
private final Property[] properties;
|
||||
|
||||
/*package*/ Parser(ParserBuilder parent, Class<T> type) {
|
||||
this.type = type;
|
||||
if(type.getAnnotation(ExposedBean.class)==null)
|
||||
throw new IllegalArgumentException(type+" doesn't have @ExposedBean");
|
||||
|
||||
parent.parsers.put(type,this);
|
||||
|
||||
Class<? super T> sc = type.getSuperclass();
|
||||
if(sc!=null && sc.getAnnotation(ExposedBean.class)!=null)
|
||||
superParser = parent.get(sc);
|
||||
else
|
||||
superParser = null;
|
||||
|
||||
List<Property> properties = new ArrayList<Property>();
|
||||
|
||||
// Use reflection to find out what properties are exposed.
|
||||
for( Field f : type.getFields() ) {
|
||||
if(f.getDeclaringClass()!=type) continue;
|
||||
Exposed exposed = f.getAnnotation(Exposed.class);
|
||||
if(exposed !=null)
|
||||
properties.add(new FieldProperty(parent,f,exposed));
|
||||
}
|
||||
|
||||
for( Method m : type.getMethods() ) {
|
||||
if(m.getDeclaringClass()!=type) continue;
|
||||
Exposed exposed = m.getAnnotation(Exposed.class);
|
||||
if(exposed !=null)
|
||||
properties.add(new MethodProperty(parent,m,exposed));
|
||||
}
|
||||
|
||||
this.properties = properties.toArray(new Property[properties.size()]);
|
||||
Arrays.sort(this.properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the property values of the given object to the writer.
|
||||
*/
|
||||
public void writeTo(T object, DataWriter writer) throws IOException {
|
||||
writer.startObject();
|
||||
writeTo(object,1,writer);
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
void writeTo(T object, int depth, DataWriter writer) throws IOException {
|
||||
if(superParser!=null)
|
||||
superParser.writeTo(object,depth,writer);
|
||||
|
||||
for (Property p : properties)
|
||||
p.writeTo(object,depth,writer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package hudson.api;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Creates and maintains {@link Parser}s, that are used to write out
|
||||
* the value representation of {@link ExposedBean exposed beans}.
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class ParserBuilder {
|
||||
/**
|
||||
* Instanciated {@link Parser}s.
|
||||
* Registration happens in {@link Parser#Parser(ParserBuilder,Class)} so that cyclic references
|
||||
* are handled correctly.
|
||||
*/
|
||||
/*package*/ final Map<Class,Parser> parsers = new ConcurrentHashMap<Class,Parser>();
|
||||
|
||||
public <T> Parser<T> get(Class<T> type) {
|
||||
Parser p = parsers.get(type);
|
||||
if(p==null) {
|
||||
synchronized(this) {
|
||||
p = parsers.get(type);
|
||||
if(p==null)
|
||||
p = new Parser<T>(this,type);
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package hudson.api;
|
||||
|
||||
import hudson.util.IOException2;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Exposes one {@link Exposed exposed property} of {@link ExposedBean} to
|
||||
* {@link DataWriter}.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
abstract class Property implements Comparable<Property> {
|
||||
final String name;
|
||||
final ParserBuilder owner;
|
||||
final int visibility;
|
||||
|
||||
Property(ParserBuilder owner, String name, Exposed exposed) {
|
||||
this.owner = owner;
|
||||
this.name = exposed.name().length()>1 ? exposed.name() : name;
|
||||
this.visibility = exposed.visibility();
|
||||
}
|
||||
|
||||
public int compareTo(Property that) {
|
||||
return this.name.compareTo(that.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes one property of the given object to {@link DataWriter}.
|
||||
*/
|
||||
public void writeTo(Object object, int depth, DataWriter writer) throws IOException {
|
||||
if(visibility<depth) return; // not visible
|
||||
|
||||
try {
|
||||
writer.name(name);
|
||||
writeValue(getValue(object),depth,writer);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IOException2("Failed to write "+name,e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new IOException2("Failed to write "+name,e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes one value of the property to {@link DataWriter}.
|
||||
*/
|
||||
private void writeValue(Object value, int depth, DataWriter writer) throws IOException {
|
||||
if(value==null) {
|
||||
writer.valueNull();
|
||||
return;
|
||||
}
|
||||
|
||||
Class c = value.getClass();
|
||||
|
||||
if(STRING_TYPES.contains(c)) {
|
||||
writer.value(value.toString());
|
||||
return;
|
||||
}
|
||||
if(PRIMITIVE_TYPES.contains(c)) {
|
||||
writer.valuePrimitive(value);
|
||||
return;
|
||||
}
|
||||
if(c.getComponentType()!=null) { // array
|
||||
writer.startArray();
|
||||
for (Object item : (Object[]) value)
|
||||
writeValue(item,depth,writer);
|
||||
writer.endArray();
|
||||
return;
|
||||
}
|
||||
if(value instanceof Collection) {
|
||||
writer.startArray();
|
||||
for (Object item : (Collection) value)
|
||||
writeValue(item,depth,writer);
|
||||
writer.endArray();
|
||||
return;
|
||||
}
|
||||
if(value instanceof Map) {
|
||||
writer.startObject();
|
||||
for (Map.Entry e : ((Map<?,?>) value).entrySet()) {
|
||||
writer.name(e.getKey().toString());
|
||||
writeValue(e.getValue(),depth,writer);
|
||||
}
|
||||
writer.endObject();
|
||||
return;
|
||||
}
|
||||
if(value instanceof Calendar) {
|
||||
writer.valuePrimitive(((Calendar) value).getTimeInMillis());
|
||||
return;
|
||||
}
|
||||
if(value instanceof Enum) {
|
||||
writer.value(value.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
// otherwise handle it as a bean
|
||||
writer.startObject();
|
||||
owner.get(c).writeTo(value,depth+1,writer);
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of this property from the bean.
|
||||
*/
|
||||
protected abstract Object getValue(Object bean) throws IllegalAccessException, InvocationTargetException;
|
||||
|
||||
private static final Set<Class> STRING_TYPES = new HashSet<Class>(Arrays.asList(
|
||||
String.class,
|
||||
URL.class
|
||||
));
|
||||
|
||||
private static final Set<Class> PRIMITIVE_TYPES = new HashSet<Class>(Arrays.asList(
|
||||
Integer.class,
|
||||
Long.class,
|
||||
Boolean.class
|
||||
));
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Hudson remote XML/JSON API support.
|
||||
*
|
||||
* <h2>Design</h2>
|
||||
* <p>
|
||||
* Hudson provides the JSON/XML dump of its model objects
|
||||
* (mostly {@link ModelObject}s but can be others.) See
|
||||
* <a href="https://hudson.deva.java.net/remote-api.html"> for the user-level
|
||||
* documentation of this feature.
|
||||
*
|
||||
* <p>
|
||||
* This mechanism is implemented by marking model objects
|
||||
* by using {@link ExposedBean} and {@link Exposed} annotations.
|
||||
* Those annotation together designates what properties are exposed
|
||||
* to those remote APIs.
|
||||
*
|
||||
* <p>
|
||||
* So generally speaking the model classes only need to annotate themselves
|
||||
* by those annotations to get their data exposed to the remote API.
|
||||
*
|
||||
* @since 1.101
|
||||
*/
|
||||
package hudson.api;
|
||||
|
||||
import hudson.model.ModelObject;
|
||||
@@ -3,6 +3,7 @@ package hudson.model;
|
||||
import hudson.Launcher;
|
||||
import hudson.Proc.LocalProc;
|
||||
import hudson.Util;
|
||||
import hudson.api.Exposed;
|
||||
import hudson.tasks.Fingerprinter.FingerprintAction;
|
||||
import hudson.tasks.test.AbstractTestResultAction;
|
||||
import hudson.maven.MavenBuild;
|
||||
@@ -86,6 +87,7 @@ public abstract class AbstractBuild<P extends AbstractProject<P,R>,R extends Abs
|
||||
/**
|
||||
* Returns the name of the slave it was built on, or null if it was the master.
|
||||
*/
|
||||
@Exposed(name="builtOn")
|
||||
public String getBuiltOnStr() {
|
||||
return builtOn;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package hudson.model;
|
||||
|
||||
import hudson.XmlFile;
|
||||
import hudson.Util;
|
||||
import hudson.api.Exposed;
|
||||
import hudson.api.ExposedBean;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -9,6 +11,7 @@ import java.util.Collection;
|
||||
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
import org.kohsuke.stapler.StaplerResponse;
|
||||
import org.kohsuke.stapler.Stapler;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
@@ -19,6 +22,7 @@ import javax.servlet.ServletException;
|
||||
*/
|
||||
// Item doesn't necessarily have to be Actionable, but
|
||||
// Java doesn't let multiple inheritance.
|
||||
@ExposedBean
|
||||
public abstract class AbstractItem extends Actionable implements Item {
|
||||
/**
|
||||
* Project name.
|
||||
@@ -37,10 +41,12 @@ public abstract class AbstractItem extends Actionable implements Item {
|
||||
doSetName(name);
|
||||
}
|
||||
|
||||
@Exposed(visibility=2)
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Exposed
|
||||
public String getDisplayName() {
|
||||
return getName();
|
||||
}
|
||||
@@ -57,6 +63,7 @@ public abstract class AbstractItem extends Actionable implements Item {
|
||||
/**
|
||||
* Gets the project description HTML.
|
||||
*/
|
||||
@Exposed
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
@@ -114,6 +121,14 @@ public abstract class AbstractItem extends Actionable implements Item {
|
||||
return getParent().getUrlChildPrefix()+'/'+getName()+'/';
|
||||
}
|
||||
|
||||
@Exposed(visibility=2,name="url")
|
||||
public final String getAbsoluteUrl() {
|
||||
StaplerRequest request = Stapler.getCurrentRequest();
|
||||
if(request==null)
|
||||
throw new IllegalStateException("Not processing a HTTP request");
|
||||
return request.getRootPath()+'/'+getUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the settings to a file.
|
||||
*/
|
||||
|
||||
@@ -1,34 +1,27 @@
|
||||
package hudson.model;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.mapper.MapperWrapper;
|
||||
import hudson.util.XStream2;
|
||||
import net.sf.json.JSONObject;
|
||||
import net.sf.json.util.JSONBuilder;
|
||||
import hudson.api.DataWriter;
|
||||
import hudson.api.Exposed;
|
||||
import hudson.api.Parser;
|
||||
import hudson.api.ParserBuilder;
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
import org.kohsuke.stapler.StaplerResponse;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Calendar;
|
||||
import java.io.Writer;
|
||||
import java.util.Stack;
|
||||
import java.beans.Introspector;
|
||||
|
||||
/**
|
||||
* Used to expose remote access API for ".../api/"
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
* @see Exposed
|
||||
*/
|
||||
public class Api extends AbstractModelObject {
|
||||
/**
|
||||
* Value bean to be exposed as XML/JSON/etc.
|
||||
* Model object to be exposed as XML/JSON/etc.
|
||||
*/
|
||||
private final Object bean;
|
||||
|
||||
@@ -43,140 +36,143 @@ public class Api extends AbstractModelObject {
|
||||
/**
|
||||
* Exposes the bean as XML.
|
||||
*/
|
||||
public void doXml(StaplerRequest req, StaplerResponse rsp) throws IOException {
|
||||
public void doXml(StaplerRequest req, final StaplerResponse rsp) throws IOException {
|
||||
rsp.setContentType("application/xml;charset=UTF-8");
|
||||
RestXStream.INSTANCE.toXML(bean, rsp.getWriter());
|
||||
|
||||
write(new DataWriter() {
|
||||
private String name = Introspector.decapitalize(bean.getClass().getSimpleName());
|
||||
private final Stack<String> objectNames = new Stack<String>();
|
||||
private final Stack<Boolean> arrayState = new Stack<Boolean>();
|
||||
private final Writer out = rsp.getWriter();
|
||||
public boolean isArray;
|
||||
|
||||
public void name(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void valuePrimitive(Object v) throws IOException {
|
||||
value(v.toString());
|
||||
}
|
||||
|
||||
public void value(String v) throws IOException {
|
||||
String n = adjustName();
|
||||
out.write('<'+n+'>');
|
||||
out.write(v);
|
||||
out.write("</"+n+'>');
|
||||
}
|
||||
|
||||
public void valueNull() {
|
||||
// use absence to indicate null.
|
||||
}
|
||||
|
||||
public void startArray() {
|
||||
// use repeated element to display array
|
||||
// this means nested arrays are not supported
|
||||
isArray = true;
|
||||
}
|
||||
|
||||
public void endArray() {
|
||||
isArray = false;
|
||||
}
|
||||
|
||||
public void startObject() throws IOException {
|
||||
objectNames.push(name);
|
||||
arrayState.push(isArray);
|
||||
out.write('<'+adjustName()+'>');
|
||||
}
|
||||
|
||||
public void endObject() throws IOException {
|
||||
name = objectNames.pop();
|
||||
out.write("</"+adjustName()+'>');
|
||||
isArray = arrayState.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name to be used as an element name
|
||||
* by considering {@link #isArray}
|
||||
*/
|
||||
private String adjustName() {
|
||||
if(isArray) {
|
||||
if(name.endsWith("s"))
|
||||
return name.substring(0,name.length()-1);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Exposes the bean as JSON.
|
||||
*/
|
||||
public void doJson(StaplerRequest req, StaplerResponse rsp) throws IOException {
|
||||
public void doJson(StaplerRequest req, final StaplerResponse rsp) throws IOException {
|
||||
rsp.setContentType("text/javascript;charset=UTF-8");
|
||||
|
||||
String pad = req.getParameter("jsonp");
|
||||
PrintWriter w = rsp.getWriter();
|
||||
if(pad!=null) w.print(pad+'(');
|
||||
JSONSerializer.write(bean,new JSONBuilder(w));
|
||||
|
||||
write(new DataWriter() {
|
||||
private boolean needComma;
|
||||
private final Writer out = rsp.getWriter();
|
||||
|
||||
public void name(String name) throws IOException {
|
||||
comma();
|
||||
out.write(name+':');
|
||||
needComma = false;
|
||||
}
|
||||
|
||||
private void data(String v) throws IOException {
|
||||
comma();
|
||||
out.write(v);
|
||||
}
|
||||
|
||||
private void comma() throws IOException {
|
||||
if(needComma) out.write(',');
|
||||
needComma = true;
|
||||
}
|
||||
|
||||
public void valuePrimitive(Object v) throws IOException {
|
||||
data(v.toString());
|
||||
}
|
||||
|
||||
public void value(String v) throws IOException {
|
||||
data('\"'+v+'\"');
|
||||
}
|
||||
|
||||
public void valueNull() throws IOException {
|
||||
data("null");
|
||||
}
|
||||
|
||||
public void startArray() throws IOException {
|
||||
comma();
|
||||
out.write('[');
|
||||
needComma = false;
|
||||
}
|
||||
|
||||
public void endArray() throws IOException {
|
||||
out.write(']');
|
||||
needComma = true;
|
||||
}
|
||||
|
||||
public void startObject() throws IOException {
|
||||
comma();
|
||||
out.write('{');
|
||||
needComma=false;
|
||||
}
|
||||
|
||||
public void endObject() throws IOException {
|
||||
out.write('}');
|
||||
needComma=true;
|
||||
}
|
||||
});
|
||||
|
||||
if(pad!=null) w.print(')');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link XStream} customized for writing XML for the REST API.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
private static class RestXStream extends XStream2 {
|
||||
private RestXStream() {
|
||||
setMode(NO_REFERENCES);
|
||||
registerConverter(Result.conv);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MapperWrapper wrapMapper(MapperWrapper next) {
|
||||
return new MapperWrapper(next) {
|
||||
// always use the short name
|
||||
public String serializedClass(Class type) {
|
||||
return Introspector.decapitalize(type.getSimpleName());
|
||||
}
|
||||
|
||||
// don't serialize the outer class
|
||||
public boolean shouldSerializeMember(Class definedIn, String fieldName) {
|
||||
return !fieldName.startsWith("this$") && !fieldName.startsWith("val$")
|
||||
&& super.shouldSerializeMember(definedIn, fieldName);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static final RestXStream INSTANCE = new RestXStream();
|
||||
private void write(DataWriter writer) throws IOException {
|
||||
Parser p = parserBuilder.get(bean.getClass());
|
||||
p.writeTo(bean,writer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Works like {@link JSONObject#fromBean(Object)} except
|
||||
* this uses fields instead of properties.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
private static class JSONSerializer {
|
||||
|
||||
private static final Set<Class> LEAF_TYPES = new HashSet<Class>(Arrays.asList(
|
||||
String.class,
|
||||
URL.class,
|
||||
Boolean.class,
|
||||
Integer.class,
|
||||
Short.class,
|
||||
Long.class,
|
||||
Float.class,
|
||||
Double.class
|
||||
));
|
||||
|
||||
public static void write(Object bean, JSONBuilder builder) {
|
||||
if(bean==null) {
|
||||
builder.value(null);
|
||||
return;
|
||||
}
|
||||
|
||||
Class c = bean.getClass();
|
||||
|
||||
if(LEAF_TYPES.contains(c)) {
|
||||
builder.value(bean);
|
||||
return;
|
||||
}
|
||||
if(c.getComponentType()!=null) { // array
|
||||
builder.array();
|
||||
for (Object item : (Object[])bean)
|
||||
write(item,builder);
|
||||
builder.endArray();
|
||||
return;
|
||||
}
|
||||
if(bean instanceof Collection) {
|
||||
builder.array();
|
||||
for (Object item : (Collection) bean)
|
||||
write(item,builder);
|
||||
builder.endArray();
|
||||
return;
|
||||
}
|
||||
if(bean instanceof Map) {
|
||||
builder.object();
|
||||
for (Map.Entry e : ((Map<?,?>) bean).entrySet()) {
|
||||
builder.key(e.getKey().toString());
|
||||
write(e.getValue(),builder);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(bean instanceof Calendar) {
|
||||
builder.value(((Calendar)bean).getTimeInMillis());
|
||||
return;
|
||||
}
|
||||
if(bean instanceof Enum) {
|
||||
builder.value(bean);
|
||||
return;
|
||||
}
|
||||
|
||||
// otherwise handle it as a bean
|
||||
writeBean(builder, bean);
|
||||
}
|
||||
|
||||
private static void writeBean(JSONBuilder builder, Object bean) {
|
||||
builder.object();
|
||||
|
||||
for( Field f : bean.getClass().getFields() ) {
|
||||
try {
|
||||
if(Modifier.isStatic(f.getModifiers()))
|
||||
continue;
|
||||
f.setAccessible(true);
|
||||
Object value = f.get(bean);
|
||||
if(value!=null) {
|
||||
builder.key(f.getName());
|
||||
write(value,builder);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
// impossible given that this is a public field
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
||||
private static final ParserBuilder parserBuilder = new ParserBuilder();
|
||||
}
|
||||
@@ -88,16 +88,7 @@ public final class Build extends AbstractBuild<Project,Build> {
|
||||
}
|
||||
|
||||
public Api getApi(final StaplerRequest req) {
|
||||
// TODO: think of a way to compose this to push some of this up
|
||||
// the inheritance tree.
|
||||
class build {
|
||||
public int number = getNumber();
|
||||
public Calendar timestamp = getTimestamp();
|
||||
public String builtOn = getBuiltOnStr();
|
||||
public Result result = getResult();
|
||||
public long duration = getDuration();
|
||||
}
|
||||
return new Api(new build());
|
||||
return new Api(this);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@@ -13,6 +13,7 @@ import hudson.TcpSlaveAgentListener;
|
||||
import hudson.Util;
|
||||
import static hudson.Util.fixEmpty;
|
||||
import hudson.XmlFile;
|
||||
import hudson.api.Exposed;
|
||||
import hudson.model.Descriptor.FormException;
|
||||
import hudson.model.listeners.ItemListener;
|
||||
import hudson.model.listeners.JobListener;
|
||||
@@ -38,9 +39,9 @@ import hudson.triggers.Triggers;
|
||||
import hudson.util.CopyOnWriteList;
|
||||
import hudson.util.CopyOnWriteMap;
|
||||
import hudson.util.FormFieldValidator;
|
||||
import hudson.util.XStream2;import hudson.util.MultipartFormDataParser;
|
||||
import hudson.util.MultipartFormDataParser;
|
||||
import hudson.util.XStream2;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.FileUploadException;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
@@ -404,6 +405,7 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node
|
||||
*
|
||||
* @see #getAllItems(Class)
|
||||
*/
|
||||
@Exposed(name="jobs")
|
||||
public List<TopLevelItem> getItems() {
|
||||
return new ArrayList<TopLevelItem>(items.values());
|
||||
}
|
||||
@@ -1366,33 +1368,7 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node
|
||||
}
|
||||
|
||||
public Api getApi(final StaplerRequest req) {
|
||||
class hudson {
|
||||
public List<job> jobs = new ArrayList<job>();
|
||||
|
||||
class job {
|
||||
public String name;
|
||||
public String url;
|
||||
public BallColor color;
|
||||
public Integer lastBuild;
|
||||
|
||||
public job(TopLevelItem item) {
|
||||
this.name = item.getName();
|
||||
this.url = req.getRootPath()+'/'+item.getUrl();
|
||||
if (item instanceof Job) {
|
||||
Job job = (Job) item;
|
||||
color = job.getIconColor();
|
||||
lastBuild =job.getLastBuild()!=null ? job.getLastBuild().getNumber() : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hudson() {
|
||||
for (TopLevelItem item : getItems())
|
||||
jobs.add(new job(item));
|
||||
}
|
||||
}
|
||||
|
||||
return new Api(new hudson());
|
||||
return new Api(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package hudson.model;
|
||||
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -87,6 +89,18 @@ public interface Item extends PersistenceRoot {
|
||||
*/
|
||||
String getShortUrl();
|
||||
|
||||
/**
|
||||
* Returns the absolute URL of this item. This relies on the current
|
||||
* {@link StaplerRequest} to figure out what the host name is,
|
||||
* so can be used only during processing client requests.
|
||||
*
|
||||
* @return
|
||||
* absolute URL.
|
||||
* @throws IllegalStateException
|
||||
* if the method is invoked outside the HTTP request processing.
|
||||
*/
|
||||
String getAbsoluteUrl();
|
||||
|
||||
/**
|
||||
* Called right after when a {@link Item} is loaded from disk.
|
||||
* This is an opporunity to do a post load processing.
|
||||
|
||||
@@ -2,6 +2,8 @@ package hudson.model;
|
||||
|
||||
import hudson.ExtensionPoint;
|
||||
import hudson.Util;
|
||||
import hudson.api.Exposed;
|
||||
import hudson.api.ExposedBean;
|
||||
import hudson.model.Descriptor.FormException;
|
||||
import hudson.tasks.BuildTrigger;
|
||||
import hudson.tasks.LogRotator;
|
||||
@@ -116,6 +118,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
getNextBuildNumberFile().write(String.valueOf(nextBuildNumber)+'\n');
|
||||
}
|
||||
|
||||
@Exposed
|
||||
public boolean isInQueue() {
|
||||
return false;
|
||||
}
|
||||
@@ -123,6 +126,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
/**
|
||||
* If this job is in the build queue, return its item.
|
||||
*/
|
||||
@Exposed
|
||||
public Queue.Item getQueueItem() {
|
||||
return null;
|
||||
}
|
||||
@@ -130,6 +134,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
/**
|
||||
* If true, it will keep all the build logs of dependency components.
|
||||
*/
|
||||
@Exposed
|
||||
public boolean isKeepDependencies() {
|
||||
return keepDependencies;
|
||||
}
|
||||
@@ -146,6 +151,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
/**
|
||||
* Peeks the next build number.
|
||||
*/
|
||||
@Exposed
|
||||
public int getNextBuildNumber() {
|
||||
return nextBuildNumber;
|
||||
}
|
||||
@@ -281,6 +287,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
/**
|
||||
* Returns true if we should display "build now" icon
|
||||
*/
|
||||
@Exposed
|
||||
public abstract boolean isBuildable();
|
||||
|
||||
/**
|
||||
@@ -388,6 +395,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
/**
|
||||
* Returns the last build.
|
||||
*/
|
||||
@Exposed
|
||||
public RunT getLastBuild() {
|
||||
SortedMap<Integer,? extends RunT> runs = _getRuns();
|
||||
|
||||
@@ -398,6 +406,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
/**
|
||||
* Returns the oldest build in the record.
|
||||
*/
|
||||
@Exposed
|
||||
public RunT getFirstBuild() {
|
||||
SortedMap<Integer,? extends RunT> runs = _getRuns();
|
||||
|
||||
@@ -410,6 +419,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
* A stable build would include either {@link Result#SUCCESS} or {@link Result#UNSTABLE}.
|
||||
* @see #getLastStableBuild()
|
||||
*/
|
||||
@Exposed
|
||||
public RunT getLastSuccessfulBuild() {
|
||||
RunT r = getLastBuild();
|
||||
// temporary hack till we figure out what's causing this bug
|
||||
@@ -421,6 +431,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
/**
|
||||
* Returns the last stable build, if any. Otherwise null.
|
||||
*/
|
||||
@Exposed
|
||||
public RunT getLastStableBuild() {
|
||||
RunT r = getLastBuild();
|
||||
while(r!=null && (r.isBuilding() || r.getResult().isWorseThan(Result.SUCCESS)))
|
||||
@@ -431,6 +442,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
/**
|
||||
* Returns the last failed build, if any. Otherwise null.
|
||||
*/
|
||||
@Exposed
|
||||
public RunT getLastFailedBuild() {
|
||||
RunT r = getLastBuild();
|
||||
while(r!=null && (r.isBuilding() || r.getResult()!=Result.FAILURE))
|
||||
@@ -441,6 +453,7 @@ public abstract class Job<JobT extends Job<JobT,RunT>, RunT extends Run<JobT,Run
|
||||
/**
|
||||
* Used as the color of the status ball for the project.
|
||||
*/
|
||||
@Exposed(visibility=2,name="color")
|
||||
public BallColor getIconColor() {
|
||||
RunT lastBuild = getLastBuild();
|
||||
while(lastBuild!=null && lastBuild.hasntStartedYet())
|
||||
|
||||
@@ -8,6 +8,8 @@ import hudson.FilePath;
|
||||
import hudson.Util;
|
||||
import static hudson.Util.combine;
|
||||
import hudson.XmlFile;
|
||||
import hudson.api.Exposed;
|
||||
import hudson.api.ExposedBean;
|
||||
import hudson.tasks.BuildStep;
|
||||
import hudson.tasks.LogRotator;
|
||||
import hudson.tasks.test.AbstractTestResultAction;
|
||||
@@ -45,6 +47,7 @@ import java.util.logging.Logger;
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
@ExposedBean
|
||||
public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,RunT>>
|
||||
extends Actionable implements ExtensionPoint, Comparable<RunT> {
|
||||
|
||||
@@ -158,6 +161,7 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
|
||||
* When a build is {@link #isBuilding() in progress}, this method
|
||||
* may return null or a temporary intermediate result.
|
||||
*/
|
||||
@Exposed
|
||||
public Result getResult() {
|
||||
return result;
|
||||
}
|
||||
@@ -240,6 +244,7 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
|
||||
/**
|
||||
* When the build is scheduled.
|
||||
*/
|
||||
@Exposed
|
||||
public Calendar getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
@@ -301,6 +306,7 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
|
||||
/**
|
||||
* Gets the millisecond it took to build.
|
||||
*/
|
||||
@Exposed
|
||||
public long getDuration() {
|
||||
return duration;
|
||||
}
|
||||
@@ -339,6 +345,7 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
|
||||
return "#"+number;
|
||||
}
|
||||
|
||||
@Exposed(visibility=2)
|
||||
public int getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package hudson.model;
|
||||
|
||||
import hudson.Util;
|
||||
import hudson.api.ExposedBean;
|
||||
import hudson.scm.ChangeLogSet.Entry;
|
||||
import hudson.util.RunList;
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
@@ -24,6 +25,7 @@ import java.util.Map;
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
@ExposedBean
|
||||
public abstract class View extends AbstractModelObject {
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user