Building VersionOne Integrations with Java (Part 7: Submitting Data)

December 2, 2015 CollabNet VersionOne

Building VersionOne Integrations with Java (Part 7: Submitting Data)

In this part of the series, we’re going to discuss how to submit data against the VersionOne API, with particular focus on doing so with the rest-1.v1/Data endpoint.

Here’s where we are at in this series:
Part 1: Introduction
Part 2: Handling Configurations
Part 3: Logging
Part 4: Parsing Data
Part 5: Using a HTTP Client
Part 6: Getting Data
Part 7: Submitting Data (YOU ARE HERE)
Part 8: Executing Operations
Part 9: Authenticating with OAuth
Part 10: Conclusion

If you’ve been following along with this series of articles, you should now have the basic building blocks of an integration including configurations, logging, parsing, a HTTP client used to make requests and getting data from the VersionOne API.

In order to create a new asset, we need to send an HTTP POST to the rest-1.v1/Data endpoint; we’ll use the following URL:

~/VersionOne/rest-1.v1/Data/Story

VersionOne has defined required fields so in order to have the correct format, the new Story definition should have at least the following info (using XML notation it should look like this):

<Asset href="/VersionOne/rest-1.v1/New/Story">
          <Attribute name="Name" act="set">Create a New Asset from Java</Attribute>
           <Relation name="Scope" act="set">
             <Asset href="/VersionOne/rest-1.v1/Data/Scope/0" idref="Scope:0"/>
           </Relation>
    </Asset>

Note: With the release of VersionOne Summer 2014, the rest.v1 endpoint supports all forms of authentication including Basic, Windows, and OAuth. See the Security topic in the VersionOne Developer Community for a complete list of endpoints and the authentication methods that they currently support.

The result of this http post will resemble the following.

<Asset href="/VersionOne/rest-1.v1/Data/Story/14191/21510" id="Story:14191:21510">
           <Attribute name="Name">Create a New Asset from Java</Attribute>
           <Relation name="Scope">
             <Asset href="/VersionOne/rest-1.v1/Data/Scope/0" idref="Scope:0" />
           </Relation>
     </Asset>

This result confirms the new asset was created.

Here’s what that looks like:

public static void createNewAsset() throws IOException {

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(instaceUrl);
CloseableHttpResponse httpResponse = null;

String newAssetString =    "<Asset href='/VersionOne/rest-1.v1/New/Story'><Attribute name='Name' act='set'>Create a New Asset from Java</Attribute><Relation name='Scope' act='set'><Asset href='/VersionOne/rest-1.oauth.v1/Data/Scope/0' idref='Scope:0'/></Relation></Asset>";

try {

     String authString = "admin" + ":" + "admin";
     byte[] authEncodedBytes = Base64.encodeBase64(authString.getBytes());
     String authEncodedString = new String(authEncodedBytes);

     httpPost.setHeader("Authorization", "Basic " + authEncodedString);
     httpPost.setHeader("User-Agent", "com.mycompany.myapp/1.0");
     StringEntity newAsset = new StringEntity(newAssetString);
     httpPost.setEntity(newAsset);
     httpResponse = httpclient.execute(httpPost);

     HttpEntity entity = httpResponse.getEntity();
     String responseBody = EntityUtils.toString(entity, "UTF-8");
     System.out.println(httpResponse.getStatusLine());
     System.out.println(responseBody);

     checkResponseStatus(httpResponse, responseBody);
  } catch (ClientProtocolException e) {
                           e.printStackTrace();
  } catch (IOException e) {
                           e.printStackTrace();
  } finally {
              httpResponse.close();
}

In this code sample, I’m using an HttpPost object, and then after setting the header values for the request, I use the setEntity method to set the payload of the request.

Note: There is far more to the rest.v1 endpoint than can be covered in a blog post, be sure to see the tour of rest.v1 in the VersionOne Developer Community for more in-depth coverage.

Hopefully you now have a sense of how you can make use of different endpoints to submit data to VersionOne.

As always, stay agile my friend…

Previous Article
CollabNet @ Gerrit User Summit 2015
CollabNet @ Gerrit User Summit 2015

On Saturday, November 7 and Sunday, November 8 the Gerrit User Summit took place in Mountain View, CA. This...

Next Article
How Unified Software Development and Delivery Makes the Vision of DevOps a Reality
How Unified Software Development and Delivery Makes the Vision of DevOps a Reality

  Are you finding that DevOps is more vision than reality? Here’s how you can unify the systems that DevOps...