Login


 

In this article we discuss how to login using the CloudBilling API tokens. It should be noted that these are indeed examples and not production-grade code

Login


package com.sample;
import javax.xml.soap.*;

public class TokenTest {

 public static void main(String[] args) {
  String soapEndpointUrl = "https://api.cloudbilling.nl/V1_3_2.svc";
  callSoapWebService(soapEndpointUrl); 
 }

 private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
   SOAPPart soapPart = soapMessage.getSOAPPart();
   String myNamespace = "api";
   String myNamespaceURI = "api.billing.inter8.V1_3_2";
 
  // SOAP Envelope
   SOAPEnvelope envelope = soapPart.getEnvelope();   
   envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

   /*<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="api.billing.inter8.V1_3_2"> 
       <SOAP-ENV:Header>
         <s:ApiToken xmlns:s="http://api.cloudbilling.nl/2018/11/auth/token">(enter token here)</s:ApiToken> 
       </SOAP-ENV:Header> 
       <SOAP-ENV:Body>
         <api:CustomerGetByCode> 
            <api:customerCode>(customer code)</api:customerCode>
         </api:CustomerGetByCode> 
       </SOAP-ENV:Body>
     </SOAP-ENV:Envelope>*/
    
    //SOAP Header
    if (envelope.getHeader() != null) {
     envelope.getHeader().detachNode();    
    }
    String token = "(enter token here)";
    SOAPHeader header = envelope.addHeader();
    SOAPElement apiToken = header.addChildElement("ApiToken", "s", "http://api.cloudbilling.nl/2018/11/auth/token");    
    apiToken.addTextNode(token);
    
    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", "api.billing.inter8.V1_3_ 2/IAPIServiceEndpoint/CustomerGetByCode");
    
    // SOAP Body
    SOAPBody soapBody = envelope.getBody();    
    SOAPElement soapBodyElem = soapBody.addChildElement("CustomerGetByCode", myNamespace);    
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("customerCode", myNamespace);    
    soapBodyElem1.addTextNode("(customer code)");
 }

 private static void callSoapWebService(String soapEndpointUrl) {    
    try {
     // Create SOAP Connection 
     SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
     SOAPConnection soapConnection = soapConnectionFactory.createConnection();
     // Send SOAP Message to SOAP Server 
     SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), soapEndpointUrl);
     // Print the SOAP Response      
     System.out.println("Response SOAP Message:");      
     soapResponse.writeTo(System.out);
     soapConnection.close();    
    } catch (Exception e) {
      System.err.println("\nError occurred while sending SOAP Request to Server! Make sure you have the correct endpoint URL and SOAPAction!\n");
      e.printStackTrace();     
    }
 }
   
   private static SOAPMessage createSOAPRequest() throws Exception {     
    MessageFactory messageFactory = MessageFactory.newInstance();     
    SOAPMessage soapMessage = messageFactory.createMessage();
    createSoapEnvelope(soapMessage);
    /* Print the request message, just for debugging purposes */     
    System.out.println("Request SOAP Message:");     
    soapMessage.writeTo(System.out);     
     return soapMessage;    
   }
}