10 Replies Latest reply on Mar 14, 2013 1:26 PM by rockytriton

    Create Guvnor Asset Using Rest API

    dme

      Hi,

       

      Is there an example of how to create an Asset in Guvnor using the Rest API? I have tried to use the following code:

      static String widxml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><asset><checkInComment>Test</checkInComment><description>This is a Work Item Definition Configuration File</description><metadata><format>wid</format><title>Sample3</title></metadata><type></type><version>1</version></asset>";

       

          WebClient client = WebClient.create("http://127.0.0.1:8080/");
          String content = client.path("drools-guvnor/rest/packages/demo/assets/Sample2").accept("application/xml").get(String.class);

       

          System.out.println("content = " + content);

       

          String authorizationHeader = "Basic " + org.apache.cxf.common.util.Base64Utility.encode("guest:".getBytes());
          WebClient client2 = WebClient.create("http://127.0.0.1:8080/");
          client2.header("Authorization", authorizationHeader);
          Response response= client2.path("drools-guvnor/rest/packages/demo/assets").accept("application/atom+xml").post(widxml);
          System.out.println("response = " + response.getStatus());

       

        This throws a HTTP 415 exception.

       

      If I change the type to "application/xml" I get a 406 error.

       

      Can someone tell me whats wrong with my code, and if there is a sample which shows how I can add an asset using the REST API.

       

      Thanks.

        • 1. Re: Create Guvnor Asset Using Rest API
          skitch

          Hey dme,

           

          The problem seems to be that the Guvnor API only accepts application/atom+xml for the "Create Asset" Restful POST call.  Problematic on my end since I'd prefer to use JSON.  This is kind of discouraging as well, since all other REST calls accept JSON/XML/Atom.

          • 2. Re: Create Guvnor Asset Using Rest API
            dme

            The Atom/XML worked for me. I was missing the SLUG header, once I added that, I could create the WID thru the REST interface.

            • 3. Re: Create Guvnor Asset Using Rest API
              leosoubeste

              Could you explain how to add the Slug header? How did you modify your code in this example?

              Thanks

              • 4. Re: Create Guvnor Asset Using Rest API
                dme

                https://community.jboss.org/message/724446. This post has some information on how to add the Slug header. The other post uses HttpConnection, you can convert to CXF if thats what you are using.

                • 5. Re: Create Guvnor Asset Using Rest API
                  rockypulley

                  I am able to create an asset, but I cannot link to a global asset.  For instance I want to link to a global model with the REST API, how would I go about doing that?

                  • 6. Re: Create Guvnor Asset Using Rest API
                    leosoubeste

                    That was helpful, thanks! This is how I did it in case someone else wonders.

                     

                    WebClient client = WebClient.create("http://" + guvnorHost + ":" + guvnorPort);

                                MultivaluedMap<String, String> map = new MetadataMap<String, String>();

                                List<String> slugHeader = new ArrayList<String>();

                                slugHeader.add(assetName);

                                List<String> authHeader = new ArrayList<String>();

                                authHeader.add(authorizationHeader);

                                List<String> contentTypeHeader = new ArrayList<String>();

                                contentTypeHeader.add("application/octet-stream");

                                map.put("Slug", slugHeader);

                                map.put("Authorization", authHeader);

                                map.put("Content-Type", contentTypeHeader);

                                client.headers(map);

                                Response response = client.path("drools-guvnor/rest/packages/" + packageName + "/assets").accept("application/atom+xml").post(asset);

                    • 7. Re: Create Guvnor Asset Using Rest API
                      richard916

                      does anyone know how to create an POJO model asset with REST API?

                      I post

                      "

                      <entry xml:base="http://localhost:9080/repository/packages/package1/assets">

                         <title>testAsset1</title>

                         <summary>desc1</summary>

                      </entry>

                       

                      " but the format is by default "txt"...

                      • 8. Re: Create Guvnor Asset Using Rest API
                        ravinandan

                        I too am interested in solution to this problem.

                        Has anyone found workaround for this?

                        • 9. Re: Create Guvnor Asset Using Rest API
                          magick93

                          I want to link to a global model with the REST API, how would I go about doing that?

                           

                          Im also looking to do the same. Did anyone find a solution to creating a new rule that uses a pojo in the global package?

                          • 10. Re: Create Guvnor Asset Using Rest API
                            rockytriton

                            Sorry, it can't be done with the REST API.  I had to use java code to do it and I wrapped it in my own REST API.  I would post the code but I'm no longer working on that project and don't have access to it anymore.