How to Create an Asset with the Drools Guvnor REST API

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleGuvnorServlet", value = "/")
public final class SampleGuvnorServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("username", "password"));

    client.register(MultiPartFeature.class);

    final MultiPart multiPart = new FormDataMultiPart();

    multiPart.bodyPart(new FormDataBodyPart("asset", ..., MediaType.APPLICATION_XML_TYPE));

    multiPart.bodyPart(new FormDataBodyPart("binary", ...));

    client.target("http://localhost:8080/guvnor/rest/packages/samplePackage/assets").request(MediaType.MULTIPART_FORM_DATA_TYPE).post(Entity.entity(multiPart));

    client.close();
  }
 
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s