Implementing automated bug creation

In this section we’ll be building the foundations for a Java service that can automatically create a new bug in Bugzilla whenever a build or a test (from another program) fails. Here’s roughly how the entire system would work:

  1. The build scripts (or testing programs) will be configured to report bugs against a product in Bugzilla; with the ID for the product, these applications can retrieve the OSLC Service Provider that represents that product.
  2. The build scripts will retrieve the Service Provider for the product and then parse it to find the Creation Factory URL, or the URL to which you can POST to create new bugs
  3. The scripts will create an RDF/XML representation of a new bug to be created; if there is an OSLC Resource Shape, they will use that to determine any required fields
  4. The script will send the RDF/XML representation to the Creation Factory URL using HTTP POST; the adapter will then interact with Bugzilla to create a new bug.

As with the last section, we won’t create an entire service; instead, we’ll focus on the OSLC-specific parts, like parsing RDF resources and POSTing to an OSLC Creation Factory.

Using a Service Provider Catalog to find a Service Provider.

For our OSLC-CM Bugzilla Adapter (or any OSLC provider), the starting point for exploring OSLC capabilities is the Service Provider Catalog document.

You can read more about implementing Service Provider Catalogs for our Bugzilla Adapter here. In short, we represent every Bugzilla Product as a Service Provider resource, and we collect all of those Service Providers in one Service Provider Catalog.

The general principle is that clients should only need to know the URL for the Catalog; from the Catalog, clients can navigate to the other OSLC services. In other words, clients should not have to hard-code URLs to individual OSLC services.

Here’s a sample Service Provider Catalog document. You can see something similar if you’re running the Bugzilla Adapter and run an HTTP GET request to http://localhost:8080/OSLC4JBugzilla/services/catalog/singleton with an Accept header of application/rdf+xml:

<?xml version="1.0"?>
<rdf:RDF xmlns:oslc="" 
  <oslc:ServiceProviderCatalog rdf:about="http://localhost:8080/OSLC4JBugzilla/catalog">
    <dcterms:title>OSLC-CM Adapter/Bugzilla Service Provider Catalog</dcterms:title>
    Enables navigation to Service Provider for each Product
    against which bugs may be reported.
    <oslc:domain rdf:resource=""/>

      <oslc:ServiceProvider rdf:about=

      <oslc:ServiceProvider rdf:about=
        <dcterms:title>Sam's Widget</dcterms:title>


The above example catalog has two oslc:serviceProvider values (i.e. The URL of each provider is specified in the rdf:about attribute, and its title is specified as a dcterms:title property value. For example, the OSLC Service Provider resource for the FoodReplicator product is located at http://localhost:8080/OSLC4JBugzilla/services/serviceProviders/2.

As we discussed in the previous section, you should use an RDF parser like Jena to parse these documents programmatically.

Using a Service Provider to find a Creation Factory

Once we’ve navigated from a Catalog to a Service Provider resource, here’s a sample of what you might see (as an RDF/XML document):

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf=""    
  <oslc:ServiceProvider rdf:about="http://localhost:8080/OSLC4JBugzilla/services/serviceProviders/2">
    <dcterms:title>OSLC-CM Adapter/Bugzilla Service Provider:
       Product FakePortal(2)</dcterms:title>
       Enables navigation to OSLC-CM Resource Creator and Selector Dialogs
    <oslc:service>  <!-- (1) -->
        <oslc:domain rdf:resource=""/>

        <!-- selection and creation dialog information deleted -->

        <oslc:creationFactory> <!-- (2) -->
            <dcterms:title>Change Request Creation Factory</dcterms:title>
            <oslc:resourceType rdf:resource=  <!-- (3) -->
            <oslc:creation rdf:resource=  <!-- (4) -->
            <oslc:resourceShape rdf:resource=  <!-- (5) -->
            <oslc:usage rdf:resource=  <!-- (6) -->

You can read more about implementing Service Providers and implementing creation factories for our Bugzilla adapter.

Of most interest to our team developing a way to automatically create bugs are the contents of the <oslc:service> element ((1)). The Service has an oslc:creationFactory property ((2)) with a value of oslc:CreationFactory. The creation factory has values that indicate it is for creating Change Requests ((3)), the URI for posting new Change Requests ((4)), and the URI of the Resource Shape ((5)) that lists the required fields for bug creation. The usage value ((5)) indicates that this is the default Creation Factory to use.

With this information, the build and testing scripts can parse the Service Provider document and discover the Creation Factory URL, which is the URL for posting new bugs.

First, though, let’s explore the Resource Shape document.

Using a Resource Shape to determine required properties

It’s not enough to just know the URL to POST bugs to; the testing scripts must also create a properly formatted OSLC-CM Change Request representation with the required property values and property values that are valid. Each Product defined in Bugzilla might have different required fields, custom fields and different allowed values.

It’s entirely possible confer with the Bugzilla system’s administration and figure out the required and allowed values, or you could use the OSLC OSLC Creation Factory’s Resource Shape document, which provides the same information.

An example Resource Shape document in RDF/XML form is below (you can see the Resource Shape from our Bugzilla Adapter at http://localhost:8080/OSLC4JBugzilla/services/resourceShapes/changeRequest):

<?xml version="1.0" encoding="UTF-8"?>
<oslc:Shape xmlns:rdf=""

   <dc:title>This is the resource shape for a new Bugzilla Bug</dc:title>

     <oslc:propertyDefinition rdf:resource=
      "" />
     <oslc:valueType rdf:resource=
      "" />
     <oslc:occurs rdf:resource=
      "" />

     <oslc:propertyDefinition rdf:resource=
      "" />
     <oslc:valueType rdf:resource=
      "" />
     <oslc:occurs rdf:resource=
      "" />
     <oslc:allowedValue>User Interface</oslc:allowedValue>      

  <!-- other properties omitted -->


Inside the root oslc:Shape element is the dc:title of the shape, which tells us the shape’s purpose (1). Next there is a series of property values for the oslc:property property. Each one describes the requirements for the property at creation time. In the listing, we omit all but two of the property values: dc:title and bugz:component.

Each property has a name (oslc:name), a link to the property definition (oslc:propertyDefinition), the acceptable type of value (oslc:valueType) and the cardinality (oslc:occurs), and allowed values (oslc:allowedValue). In our example above, the component property accepts String values; the occurs value of Exactly-one indicates that it is required; and the allowed values are either Installer or User Interface.

With this information from the Resource Shape document, we can now create and post new bugs.

Forming an RDF/XML representation of a Bugzilla bug

Note: The following discusses using an RDF/XML parser. If you are writing Java, you could also use – in fact, we recommend using – OSLC4J to handle conversions between RDF/XML and plain old Java objects. Consider the following to be guidance if you choose to approach this another way.

Although you could generate RDF/XML through a variety of techniques, we recommend using a dedicated RDF toolkit like Jena.

We’ll create a very simple method below: it accepts strings of various property values and returns an RDF/XML representation of a new bug.

To follow along, open the file in the org.eclipse.lyo.samples.ninacrm.examples package and search for the formNewBug() method.

First, OSLC-CM requires Change Requests to have an RDF Type and a title:

Property bugType =
  new PropertyImpl("");

Property titleProp =
  new PropertyImpl("");

Next, we know from the Resource Shape document that new bugs must have property values for the Bugzilla bug properties product, version, component, platform and operating system. So we set up Jena property objects for each of those:

Property versionProp = 
  new PropertyImpl("");

Property componentProp = 
  new PropertyImpl("");

Property platformProp = 
  new PropertyImpl("");

Property opsysProp = 
  new PropertyImpl("");
We are hard-coding required Bugzilla properties here. It would be better – more flexible and future-proof – to programmatically locate and parse a Resource Shape document to determine the required properties; for simplicity's sake, we do not do so here. We leave that as an exercise for the reader.

Note that we did not set the Bugzilla Product: the product can be determined by the choice of service that you use to post the new bug. (In other words, every Bugzilla product will have its own OSLC Creation Factory.)

Next, we set up a Jena Model object and adds namespace prefixes. These are not strictly necessary, but they will make the RDF/XML a little more readable and make it look more like the examples in the OSLC specifications, which is useful.

Model model = ModelFactory.createDefaultModel();
model.setNsPrefix("bugz",    "");
model.setNsPrefix("dcterms", "");
model.setNsPrefix("oslc_cm", "");

Once the Model is set up, we create a Resource object using a base URI that is the empty string. We won’t know the URI of the new bug until the OSLC-CM provider has created it and tells us the new URI via the HTTP Location header.

com.hp.hpl.jena.rdf.model.Resource resource = model.createResource("");

Once we have a Resource, we are ready to add property values for each of the required properties:

resource.addProperty(RDF.type,     bugType);
resource.addLiteral(titleProp,     title);
resource.addLiteral(versionProp,   version);
resource.addLiteral(componentProp, component);
resource.addLiteral(platformProp,  platform);
resource.addLiteral(opsysProp,     opsys);

Finally, we write out the RDF model in RDF/XML format and return it in string form:

StringWriter sw = new StringWriter();
RDFWriter writer = model.getWriter();
writer.write(model, sw, "/");
return sw.toString();

Using HTTP to POST a new bug

With the ability to build RDF/XML representations of a bug in place, we can write a simple example that posts an RDF/XML representation of a new bug to an OSLC-CM Provider:

public static void postNewBug(
                   String creationURL,
                   String title, 
                   String version, 
                   String component, 
                   String platform, 
                   String opsys) {

  String bug = formNewBug(title, version, component plaform, opsys); // (1) 

  try {
     URL createURL = new URL(creationURL);  // (2)

     HttpURLConnection conn = (HttpURLConnection)createURL.openConnection();
     conn.setRequestMethod("POST"); // (3) 
     conn.setRequestProperty("Content-Type", "application/rdf+xml");  // (4)

     BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream());
     out.write(bug.getBytes("UTF-8")); // (5)

     BufferedReader in = new BufferedReader( // (6)
      new InputStreamReader(conn.getInputStream()));          
     String s;
     while ((s = in.readLine()) != null) {

     int rc = conn.getResponseCode(); // (7)
     System.out.println("Return status: " + rc);
     System.out.println("Location: " + conn.getHeaderField("Location")); // (8)
   catch (IOException e) {

The above method accepts as arguments the URL for the creation factory and the required attributes.

First, we use the formNewBug() method (discussed above) to build the RDF/XML representation of the new bug from the passed property values ((1)).

Next, we create a URL object with the URL of the target Change Request Creation Factory ((2)) and use it to open an HTTP connection. We configure that connection for HTTP POST ((3)) and set the HTTP Content-Type to inform the server that we are sending RDF/XML data ((4)).

Finally, we write out the bug to the server ((5)). To confirm that the POST worked, we write out the results ((6)) and the response code ((7)) and the Location header ((8)). If all went well, the response code should be 201, which means Created, and the Location will be the URI of the newly created bug.

Try it out!

If you'd like to more details or want to try to post a bug using RDF/XML, see our walkthrough of implementing a Creation Factory for our Bugzilla adapter.

Any problems?

Ask a question on the forums or email the site administrator. If you have questions specifically about Eclipse Lyo, ask the lyo-dev mailing list.