From 5ca3f63fa9be863e97f9eddd55bc9e7e420c2aa7 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 12 Apr 2007 06:09:39 +0000 Subject: [PATCH] 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 --- core/pom.xml | 6 - core/src/main/java/hudson/api/DataWriter.java | 35 +++ core/src/main/java/hudson/api/Exposed.java | 48 ++++ .../src/main/java/hudson/api/ExposedBean.java | 26 ++ .../main/java/hudson/api/FieldProperty.java | 20 ++ .../main/java/hudson/api/MethodProperty.java | 32 +++ core/src/main/java/hudson/api/Parser.java | 75 +++++ .../main/java/hudson/api/ParserBuilder.java | 30 ++ core/src/main/java/hudson/api/Property.java | 124 ++++++++ .../main/java/hudson/api/package-info.java | 25 ++ .../main/java/hudson/model/AbstractBuild.java | 2 + .../main/java/hudson/model/AbstractItem.java | 15 + core/src/main/java/hudson/model/Api.java | 268 +++++++++--------- core/src/main/java/hudson/model/Build.java | 11 +- core/src/main/java/hudson/model/Hudson.java | 34 +-- core/src/main/java/hudson/model/Item.java | 14 + core/src/main/java/hudson/model/Job.java | 13 + core/src/main/java/hudson/model/Run.java | 7 + core/src/main/java/hudson/model/View.java | 2 + 19 files changed, 606 insertions(+), 181 deletions(-) create mode 100644 core/src/main/java/hudson/api/DataWriter.java create mode 100644 core/src/main/java/hudson/api/Exposed.java create mode 100644 core/src/main/java/hudson/api/ExposedBean.java create mode 100644 core/src/main/java/hudson/api/FieldProperty.java create mode 100644 core/src/main/java/hudson/api/MethodProperty.java create mode 100644 core/src/main/java/hudson/api/Parser.java create mode 100644 core/src/main/java/hudson/api/ParserBuilder.java create mode 100644 core/src/main/java/hudson/api/Property.java create mode 100644 core/src/main/java/hudson/api/package-info.java diff --git a/core/pom.xml b/core/pom.xml index 581f79e2b4..980e4505c6 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -142,12 +142,6 @@ xstream 1.1.3 - - net.sf.json-lib - json-lib - jdk13 - 1.0.1 - jfree jfreechart diff --git a/core/src/main/java/hudson/api/DataWriter.java b/core/src/main/java/hudson/api/DataWriter.java new file mode 100644 index 0000000000..abc8b5c2d8 --- /dev/null +++ b/core/src/main/java/hudson/api/DataWriter.java @@ -0,0 +1,35 @@ +package hudson.api; + +import java.io.IOException; + +/** + * Receives the event callback on the model data to be exposed. + * + *

+ * The call sequence is: + * + *

+ * EVENTS := startObject PROPERTY* endObject
+ * PROPERTY := name VALUE
+ * VALUE := valuePrimitive
+ *        | value
+ *        | valueNull
+ *        | startArray VALUE* endArray
+ *        | EVENTS
+ * 
+ * + * @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; +} diff --git a/core/src/main/java/hudson/api/Exposed.java b/core/src/main/java/hudson/api/Exposed.java new file mode 100644 index 0000000000..38d6ce9846 --- /dev/null +++ b/core/src/main/java/hudson/api/Exposed.java @@ -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. + * + *

+ * 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. + *

+ * 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. + *

+ * And the rest goes in the same way. If the value is N, the object + * is exposed as the Nth level object. + * + *

+ * So bigger the number, more important the property is. + */ + int visibility() default 1; + + /** + * Name of the exposed property. + *

+ * 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 ""; +} diff --git a/core/src/main/java/hudson/api/ExposedBean.java b/core/src/main/java/hudson/api/ExposedBean.java new file mode 100644 index 0000000000..f399e55fd6 --- /dev/null +++ b/core/src/main/java/hudson/api/ExposedBean.java @@ -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. + * + *

+ * 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 { +} diff --git a/core/src/main/java/hudson/api/FieldProperty.java b/core/src/main/java/hudson/api/FieldProperty.java new file mode 100644 index 0000000000..9ca9184851 --- /dev/null +++ b/core/src/main/java/hudson/api/FieldProperty.java @@ -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); + } +} diff --git a/core/src/main/java/hudson/api/MethodProperty.java b/core/src/main/java/hudson/api/MethodProperty.java new file mode 100644 index 0000000000..088e731b17 --- /dev/null +++ b/core/src/main/java/hudson/api/MethodProperty.java @@ -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); + } +} diff --git a/core/src/main/java/hudson/api/Parser.java b/core/src/main/java/hudson/api/Parser.java new file mode 100644 index 0000000000..7cd2bfb885 --- /dev/null +++ b/core/src/main/java/hudson/api/Parser.java @@ -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 { + private final Class type; + + /** + * {@link Parser} for the super class. + */ + private final Parser superParser; + + private final Property[] properties; + + /*package*/ Parser(ParserBuilder parent, Class type) { + this.type = type; + if(type.getAnnotation(ExposedBean.class)==null) + throw new IllegalArgumentException(type+" doesn't have @ExposedBean"); + + parent.parsers.put(type,this); + + Class sc = type.getSuperclass(); + if(sc!=null && sc.getAnnotation(ExposedBean.class)!=null) + superParser = parent.get(sc); + else + superParser = null; + + List properties = new ArrayList(); + + // 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); + } +} diff --git a/core/src/main/java/hudson/api/ParserBuilder.java b/core/src/main/java/hudson/api/ParserBuilder.java new file mode 100644 index 0000000000..c3919a204f --- /dev/null +++ b/core/src/main/java/hudson/api/ParserBuilder.java @@ -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 parsers = new ConcurrentHashMap(); + + public Parser get(Class type) { + Parser p = parsers.get(type); + if(p==null) { + synchronized(this) { + p = parsers.get(type); + if(p==null) + p = new Parser(this,type); + } + } + return p; + } +} diff --git a/core/src/main/java/hudson/api/Property.java b/core/src/main/java/hudson/api/Property.java new file mode 100644 index 0000000000..efdb1b6db0 --- /dev/null +++ b/core/src/main/java/hudson/api/Property.java @@ -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 { + 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) 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 STRING_TYPES = new HashSet(Arrays.asList( + String.class, + URL.class + )); + + private static final Set PRIMITIVE_TYPES = new HashSet(Arrays.asList( + Integer.class, + Long.class, + Boolean.class + )); +} diff --git a/core/src/main/java/hudson/api/package-info.java b/core/src/main/java/hudson/api/package-info.java new file mode 100644 index 0000000000..2a8e58ebc7 --- /dev/null +++ b/core/src/main/java/hudson/api/package-info.java @@ -0,0 +1,25 @@ +/** + * Hudson remote XML/JSON API support. + * + *

Design

+ *

+ * Hudson provides the JSON/XML dump of its model objects + * (mostly {@link ModelObject}s but can be others.) See + * for the user-level + * documentation of this feature. + * + *

+ * 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. + * + *

+ * 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; \ No newline at end of file diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java index 2d52b4b80c..c62e5927cc 100644 --- a/core/src/main/java/hudson/model/AbstractBuild.java +++ b/core/src/main/java/hudson/model/AbstractBuild.java @@ -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

,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; } diff --git a/core/src/main/java/hudson/model/AbstractItem.java b/core/src/main/java/hudson/model/AbstractItem.java index d2292cc563..c769d54614 100644 --- a/core/src/main/java/hudson/model/AbstractItem.java +++ b/core/src/main/java/hudson/model/AbstractItem.java @@ -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. */ diff --git a/core/src/main/java/hudson/model/Api.java b/core/src/main/java/hudson/model/Api.java index fa22165861..b7c79a03a4 100644 --- a/core/src/main/java/hudson/model/Api.java +++ b/core/src/main/java/hudson/model/Api.java @@ -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 objectNames = new Stack(); + private final Stack arrayState = new Stack(); + 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("'); + } + + 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("'); + 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 LEAF_TYPES = new HashSet(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(); } diff --git a/core/src/main/java/hudson/model/Build.java b/core/src/main/java/hudson/model/Build.java index e2afc7efa4..84175a1932 100644 --- a/core/src/main/java/hudson/model/Build.java +++ b/core/src/main/java/hudson/model/Build.java @@ -88,16 +88,7 @@ public final class Build extends AbstractBuild { } 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); } // diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index 04f36f8c61..054e1ea986 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -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, Node * * @see #getAllItems(Class) */ + @Exposed(name="jobs") public List getItems() { return new ArrayList(items.values()); } @@ -1366,33 +1368,7 @@ public final class Hudson extends View implements ItemGroup, Node } public Api getApi(final StaplerRequest req) { - class hudson { - public List jobs = new ArrayList(); - - 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); } diff --git a/core/src/main/java/hudson/model/Item.java b/core/src/main/java/hudson/model/Item.java index d2f73bdf01..b86cb37197 100644 --- a/core/src/main/java/hudson/model/Item.java +++ b/core/src/main/java/hudson/model/Item.java @@ -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. diff --git a/core/src/main/java/hudson/model/Job.java b/core/src/main/java/hudson/model/Job.java index 04d3a479cc..83c07364c8 100644 --- a/core/src/main/java/hudson/model/Job.java +++ b/core/src/main/java/hudson/model/Job.java @@ -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, RunT extends Run, RunT extends Run, RunT extends Run, RunT extends Run, RunT extends Run, RunT extends Run runs = _getRuns(); @@ -398,6 +406,7 @@ public abstract class Job, RunT extends Run runs = _getRuns(); @@ -410,6 +419,7 @@ public abstract class Job, RunT extends Run, RunT extends Run, RunT extends Run, RunT extends Run,RunT extends Run> extends Actionable implements ExtensionPoint, Comparable { @@ -158,6 +161,7 @@ public abstract class Run ,RunT extends Run,RunT extends Run,RunT extends Run,RunT extends Run