Error when using REST API for updating job configuration

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Error when using REST API for updating job configuration

Yosi Kalmanson (kyosi)
Hi,
 
I'm trying to update job configuration through REST API. I'm posting the configuration file using the following code:
 
        HttpClient client = new HttpClient();
       
        String location = "http://localhost:8080/hudson/job/test10/config.xml";
          
        File f = new File("C:\\tmp\\configuration.xml");
        PostMethod filePost = new PostMethod(location);
       
        Part[] parts = {
            new FilePart(f.getName(), f)
        };
       
        filePost.setRequestEntity(
            new MultipartRequestEntity(parts, filePost.getParams())
        );
       
       
        client.executeMethod(filePost);
        System.out.println(filePost.getStatusCode() + "\n" + filePost.getResponseBodyAsString());
 
However, configuration is not getting updated. When i look into the response body i see:
 
javax.servlet.ServletException: hudson.util.IOException2: Failed to persist configuration.xml
org.kohsuke.stapler.Stapler.invoke(Stapler.java:498)
org.kohsuke.stapler.MetaClass$6.doDispatch(MetaClass.java:179)
org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:30)
....
 
and root cause:
 
org.xml.sax.SAXParseException: Content is not allowed in prolog.
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:610)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:708)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
hudson.model.Job.doConfigDotXml(Job.java:989)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:176)
org.kohsuke.stapler.Function.bindAndInvoke(Function.java:92)
org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:53)
org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:73)
org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:30)
org.kohsuke.stapler.Stapler.invoke(Stapler.java:486)
org.kohsuke.stapler.MetaClass$6.doDispatch(MetaClass.java:179)
org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:30)
org.kohsuke.stapler.Stapler.invoke(Stapler.java:486)
org.kohsuke.stapler.Stapler.invoke(Stapler.java:404)
org.kohsuke.stapler.Stapler.service(Stapler.java:117)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:91)
hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:83)
hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:47)
hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:76)
hudson.security.HudsonFilter.doFilter(HudsonFilter.java:155)
 
Any ideas?
 
Thanks,
Yosi

Reply | Threaded
Open this post in threaded view
|

RE: Error when using REST API for updating job configuration

justinedelson
I don't think you should use MultipartRequestEntity. Try using FileRequestEntity instead.
 
You might want to look at the code for the maven-hudson-plugin's add-job mojo: https://svn.dev.java.net/svn/hudson/trunk/hudson/tools/maven-hudson-plugin/src/main/java/org/jvnet/hudson/maven/plugins/hudson/AddJobMojo.java. This uses StringRequestEntity, but it's the same basic idea.
 
Justin


From: Yosi Kalmanson (kyosi) [mailto:[hidden email]]
Sent: Tuesday, June 30, 2009 9:01 AM
To: [hidden email]
Subject: Error when using REST API for updating job configuration

Hi,
 
I'm trying to update job configuration through REST API. I'm posting the configuration file using the following code:
 
        HttpClient client = new HttpClient();
       
        String location = "http://localhost:8080/hudson/job/test10/config.xml";
          
        File f = new File("C:\\tmp\\configuration.xml");
        PostMethod filePost = new PostMethod(location);
       
        Part[] parts = {
            new FilePart(f.getName(), f)
        };
       
        filePost.setRequestEntity(
            new MultipartRequestEntity(parts, filePost.getParams())
        );
       
       
        client.executeMethod(filePost);
        System.out.println(filePost.getStatusCode() + "\n" + filePost.getResponseBodyAsString());
 
However, configuration is not getting updated. When i look into the response body i see:
 
javax.servlet.ServletException: hudson.util.IOException2: Failed to persist configuration.xml
org.kohsuke.stapler.Stapler.invoke(Stapler.java:498)
org.kohsuke.stapler.MetaClass$6.doDispatch(MetaClass.java:179)
org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:30)
....
 
and root cause:
 
org.xml.sax.SAXParseException: Content is not allowed in prolog.
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:610)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:708)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
hudson.model.Job.doConfigDotXml(Job.java:989)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:176)
org.kohsuke.stapler.Function.bindAndInvoke(Function.java:92)
org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:53)
org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:73)
org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:30)
org.kohsuke.stapler.Stapler.invoke(Stapler.java:486)
org.kohsuke.stapler.MetaClass$6.doDispatch(MetaClass.java:179)
org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:30)
org.kohsuke.stapler.Stapler.invoke(Stapler.java:486)
org.kohsuke.stapler.Stapler.invoke(Stapler.java:404)
org.kohsuke.stapler.Stapler.service(Stapler.java:117)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:91)
hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:83)
hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:47)
hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:76)
hudson.security.HudsonFilter.doFilter(HudsonFilter.java:155)
 
Any ideas?
 
Thanks,
Yosi

Reply | Threaded
Open this post in threaded view
|

RE: RE: Error when using REST API for updating job configuration

Yosi Kalmanson (kyosi)
Hi Justin,
 
That worked!!!
 
Thanks,
Yosi
From: Edelson, Justin [mailto:[hidden email]]
Sent: Tuesday, June 30, 2009 5:09 PM
To: [hidden email]
Subject: RE: Error when using REST API for updating job configuration

I don't think you should use MultipartRequestEntity. Try using FileRequestEntity instead.
 
You might want to look at the code for the maven-hudson-plugin's add-job mojo: https://svn.dev.java.net/svn/hudson/trunk/hudson/tools/maven-hudson-plugin/src/main/java/org/jvnet/hudson/maven/plugins/hudson/AddJobMojo.java. This uses StringRequestEntity, but it's the same basic idea.
 
Justin


From: Yosi Kalmanson (kyosi) [mailto:[hidden email]]
Sent: Tuesday, June 30, 2009 9:01 AM
To: [hidden email]
Subject: Error when using REST API for updating job configuration

Hi,
 
I'm trying to update job configuration through REST API. I'm posting the configuration file using the following code:
 
        HttpClient client = new HttpClient();
       
        String location = "http://localhost:8080/hudson/job/test10/config.xml";
          
        File f = new File("C:\\tmp\\configuration.xml");
        PostMethod filePost = new PostMethod(location);
       
        Part[] parts = {
            new FilePart(f.getName(), f)
        };
       
        filePost.setRequestEntity(
            new MultipartRequestEntity(parts, filePost.getParams())
        );
       
       
        client.executeMethod(filePost);
        System.out.println(filePost.getStatusCode() + "\n" + filePost.getResponseBodyAsString());
 
However, configuration is not getting updated. When i look into the response body i see:
 
javax.servlet.ServletException: hudson.util.IOException2: Failed to persist configuration.xml
org.kohsuke.stapler.Stapler.invoke(Stapler.java:498)
org.kohsuke.stapler.MetaClass$6.doDispatch(MetaClass.java:179)
org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:30)
....
 
and root cause:
 
org.xml.sax.SAXParseException: Content is not allowed in prolog.
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:610)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:708)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
hudson.model.Job.doConfigDotXml(Job.java:989)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:176)
org.kohsuke.stapler.Function.bindAndInvoke(Function.java:92)
org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:53)
org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:73)
org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:30)
org.kohsuke.stapler.Stapler.invoke(Stapler.java:486)
org.kohsuke.stapler.MetaClass$6.doDispatch(MetaClass.java:179)
org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:30)
org.kohsuke.stapler.Stapler.invoke(Stapler.java:486)
org.kohsuke.stapler.Stapler.invoke(Stapler.java:404)
org.kohsuke.stapler.Stapler.service(Stapler.java:117)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:91)
hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:83)
hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:47)
hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:76)
hudson.security.HudsonFilter.doFilter(HudsonFilter.java:155)
 
Any ideas?
 
Thanks,
Yosi