How to connect to BMC Discovery Rest API from Java

Introduction

Please follow the official BMC documentation, create a valid user and generate a permanent API token. From this point you can use this tutorial. As in some environments setting up a valid Java keystore might be difficult, this example shows a way how to disable the default SSL checks performed by the client. If you would like to configure it properly, please google for how to add your root CA into java keystore by using java keytool and build the client using the commented out method: ClientBuilder.newClient().

Versioning

  • BMC Discovery 11.1.0.5
  • Eclipse Version: Neon.3 Release (4.6.3)
  • Java 1.8.0_131
  • javax.ws.rs: 2.1
  • jersey: 2.26

Setting up the project

  • From the File menu → New → Java Project
  • Project Name: BmcdApiClient
  • Next... Next...
  • Finish
  • Converting to maven

    From the Project Explorer view right click on the Project → configure → Convert to Maven Project → Finish

    Adding dependencies

    <dependencies> 
       <dependency>  
          <groupid>javax.ws.rs</groupid> 
          <artifactid>javax.ws.rs-api</artifactid> 
          <version>2.1</version> 
       </dependency>
       <dependency> 
          <groupid>org.glassfish.jersey.core</groupid>
          <artifactid>jersey-client</artifactid>
          <version>2.26</version>
       </dependency> 
       <dependency>
          <groupid>org.glassfish.jersey.inject</groupid> 
          <artifactid>jersey-hk2</artifactid>
          <version>2.26</version>
       </dependency>
    </dependencies>
    
    jersey-hk2 dependency might not be necesary for newer versions. At least is not needed for versions prior to 2.26, it's just look like a feature of this release.

    Create BMCDClient class

    Create new class Name: BMCDClient Package: com.blogspot.itsystemengineer Paste its content and replace "xxx" with valid values.
    package com.blogspot.itsystemengineer;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    import javax.net.ssl.SSLContext; 
    import javax.net.ssl.TrustManager; 
    import javax.net.ssl.X509TrustManager;
    import javax.ws.rs.client.Client;
    import javax.ws.rs.client.ClientBuilder;
    import javax.ws.rs.client.Entity;
    import javax.ws.rs.client.WebTarget;
    import javax.ws.rs.core.HttpHeaders;
    import javax.ws.rs.core.MediaType;
    
    public class BMCDClient {
    
     public static Client IgnoreSSLClient() throws Exception {
         SSLContext sslcontext = SSLContext.getInstance("TLS");
         sslcontext.init(null, new TrustManager[]{new X509TrustManager() {
             public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
             public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
             public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
    
         }}, new java.security.SecureRandom());
         return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true).build();
     }
     
     public static void main(String[] args) throws Exception {
      
            String appliance = "xxx";
            String token = "xxx";
            String jsonString = "{\"label\" : \"test\", \"ranges\" : [\"127.0.0.1\"]}";
            
            Client client = IgnoreSSLClient();
            // Client client = ClientBuilder.newClient();
            String target = "https://"+appliance+"/api/v1.0/discovery/runs";
            WebTarget myResource = client.target(target);
            String responseEntity = myResource.request(MediaType.TEXT_PLAIN)
              .header(HttpHeaders.AUTHORIZATION,
                "Bearer "
                +token)
              .post(Entity.json(jsonString), String.class);
            
            System.out.println(responseEntity);
     }
    
    }
    
    If you now run your project, on the screen you should see a response from the API which is a JSON that contains uri and uuid values.

    Comments