Skip to main content

This post is from the CollabNet VersionOne blog and has not been updated since the original publish date.

Last Updated Dec 01, 2015 — Enterprise Agile Planning expert

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

Enterprise Agile Planning

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…

More from the Blog

View more
Feb 14, 2021

Reflecting on the 20th anniversary of the Agile Manifesto

Enterprise Agile Planning
Over the past 20 years, it’s been amazing to watch an idea from ...
Read More
Feb 08, 2021

How does agile apply to an entire organization?

Enterprise Agile Planning
Before we dive into the main subject of this blog post, it is importan ...
Read More
Feb 03, 2021

It took a pandemic to realize why digital transformation actually matters

Enterprise Agile Planning
Before anyone had ever heard of COVID-19, businesses across the globe ...
Read More
Jan 27, 2021

Improve visibility, reduce costs, and take back control of your scaled out container and cloud deployments with the latest releases of Digital.ai Agility and DevOps solutions

Enterprise Agile Planning
We’re thrilled to announce the latest releases of our Digital.ai Agili ...
Read More
Contact Us