1 Reply Latest reply on Dec 4, 2014 1:00 PM by hernsys

    Create a new asset in Guvnor through REST API (POST method)

    neokiing

      Hi All,

      I am trying to create an asset (.txt file ) in GUVNOR through REST API in JAVA. But the problem is when using the post method to create asset .. program is executing with no error but i am having SERVER_ERROR(http 500  error..) as a response.

      Steps I followed :-

      1.Created an asset named SayanFileTest

      2.Got the Asset Info in Atom -XML format ..

      3.Replaced the asset name With new one (FileUploadTest1)

      4.Called the post method to create a new entry in GUVNOR.

      Can anybody please help me with this one?  I am providing the code.. Please tell me what i am doing wrong in this and how to solve it.

      (N.B. - I am using jboss-as-7.1.1.Final and drools-guvnor 5.3.0)

       

      ***********************

      //package imported..

      import org.apache.abdera.model.Document;

      import org.apache.abdera.model.Entry;

      import org.apache.abdera.protocol.Response.ResponseType;

      import org.apache.abdera.protocol.client.AbderaClient;

      import org.apache.abdera.protocol.client.ClientResponse;

      import org.apache.abdera.protocol.client.RequestOptions;

       

       

      // Post method

       

      Abdera abdera= new Abdera();

      AbderaClient client = new AbderaClient(abdera);


      UsernamePasswordCredentials userpass = new UsernamePasswordCredentials("admin","admin");

      client.addCredentials(url.toExternalForm(),null,"Basic",userpass);

       

      ClientResponse resp = client.get(new URL("http://localhost:8080/drools-guvnor/rest/packages/defaultPackage/assets/SayanFileTest").toExternalForm());

      Document<Entry> doc = resp.getDocument();

          

              Entry prevEntry = doc.getRoot();  // have done this to get the atom-entry at the first place

             

              Entry newEntry = abdera.newEntry();

           

      // copied the prevEntry to populate the below string

       

       

              String newResponse ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns=\"http://www.w3.org/XML/1998/namespace\" xml:base=\"http://localhost:8080/drools-guvnor/rest/packages/defaultPackage/assets/SayanFileTest\"><atom:title xmlns:atom=\"http://www.w3.org/2005/Atom\" type=\"text\">SayanFileTest</atom:title><atom:id xmlns:atom=\"http://localhost:8080/drools-guvnor/rest/packages/defaultPackage/assets/SayanFileTesthttp://www.w3.org/2005/Atom\">http://localhost:8080/drools-guvnor/rest/packages/defaultPackage/assets/SayanFileTest</atom:id><atom:published xmlns:atom=\"2013-01-23T14:10:33.173+05:30http://www.w3.org/2005/Atom\">2013-01-23T14:10:33.173+05:30</atom:published><atom:author xmlns:atom=\"http://www.w3.org/2005/Atom\"><atom:name xmlns:atom=\"adminhttp://www.w3.org/2005/Atom\">admin</atom:name></atom:author><atom:content xmlns:atom=\"http://www.w3.org/2005/Atom\" type=\"application/octet-stream\" src=\"http://localhost:8080/drools-guvnor/rest/packages/defaultPackage/assets/SayanFileTest/binary\"></atom:content><atom:summary xmlns:atom=\"http://www.w3.org/2005/Atom\" type=\"text\"></atom:summary><metadata xmlns=\"\"><uuid><value></value></uuid><categories></categories><format><value>.txt</value></format><state><value>Draft</value></state><versionNumber><value>2</value></versionNumber><checkinComment><value></value></checkinComment><archived><value>false</value></archived></metadata></atom:entry>";

        

      // replaced the name of the previous asset  

        newResponse=newResponse.replaceAll("SayanFileTest","FileUploadTest1");
             

              System.out.println(newResponse);
            

              newEntry.setContent(newResponse);


              client = new AbderaClient(abdera);
              client.addCredentials(new URL("http://localhost:8080/drools-guvnor/").toExternalForm(), null,"Basic",new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin"));
               
              RequestOptions  options = client.getDefaultRequestOptions();
              options.setContentType(MediaType.APPLICATION_ATOM_XML);

              ClientResponse clientResponsePost = client.post(new URL("http://localhost:8080/drools-guvnor/rest/packages/defaultPackage/assets").toExternalForm(), newEntry, options);
        
              System.out.println(clientResponsePost.getType()+ "__________ " + clientResponsePost.getStatus());

      *****************

      I am a newbie in this. Any help will be really appreciated.

      Thanks in advance ,

      Sayan

        • 1. Re: Create a new asset in Guvnor through REST API (POST method)
          hernsys

          Hi.

           

          You can try running this method.

           

          private static void addingNewAsset() {

                  WebClient client = WebClient.create("http://localhost:8080");

                  byte[] fileBytes = null;

                  try {

                      fileBytes = Files

                              .readAllBytes(Paths

                                      .get("src/main/resources/com/resources/guvnor/assets/guvnorRuleExample.drl"));

                  } catch (IOException e) {

                      // TODO Auto-generated catch block

                      e.printStackTrace();

                  }

                  String fileSource = new String(fileBytes);

                  Response response = client

                          .path("guvnor/rest/packages/testPackage/assets")

                          .accept(MediaType.APPLICATION_ATOM_XML_TYPE)

                          .header(AUTHORIZATION, AUTHORIZATION_VALUE)

                          .type(MediaType.APPLICATION_OCTET_STREAM_TYPE)

                          .header("slug", "guvnorRuleExample.drl").post(fileSource);

                  System.out.println("Status:" + response.getStatus());

              }

           

          * You should have running guvnor in local instance (localhost:8080/guvnor)

          * you should have a package called "testPackage"

           

          Cheers