Difference between revisions of "REST API:Login"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
 
(4 intermediate revisions by the same user not shown)
Line 10: Line 10:
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpStatus;


// Utility Classes
// Apache Utilities
import org.apache.commons.lang.StringUtils; 
 
// Java Utilities
import java.util.List;
import java.io.InputStream;
import java.io.InputStream;
import javax.ws.rs.core.MultivaluedMap;


/*
/*
Line 26: Line 31:
   RestClient client;
   RestClient client;
    
    
   String baseUrl = "http://{domain}";
   String baseUrl = "https://{{domain}}";
   String username = "yourName";
   String username = "yourName";
   String password = "yourPassword";
   String password = "yourPassword";
Line 47: Line 52:
       resource.accept("application/xml");
       resource.accept("application/xml");
       ClientResponse response = resource.post(xml);
       ClientResponse response = resource.post(xml);
       String responseXml = response.getEntity(String.class);  
 
       this.sessionId = getValue("/platform/login/sessionId", responseXml);
      // Combine cookie parameters from the response header,
       return this.sessionId;
      // making a cookie string for use in subsequent requests.
       MultivaluedMap<String,String> headers = response.getHeaders(); 
      String cookieString = "";
      List<String> cookieParams = headers.get("Set-Cookie");
       cookieString = StringUtils.join(cookieParams, "; ");
 
       return cookieString;
     }
     }
     catch (Exception e)
     catch (Exception e)
Line 58: Line 69:
   }
   }


  /*
   // ...Various utility methods...
  * Get a value from XML returned in a response.
  *
  * @param xpath_expr  An XPATH expression.
  *                    For example: "/platform/login/sessionId"
  *                   
  * @param xml The XML string to be parsed. Generally obtained using
  *        ClientResponse.getEntity(String.class) --Apache wink RestClient
  *        httpMethod.getResponseBodyAsString()  --Apache HttpClient
  */
  public static String getValue(String xpath_expr, String xml)
  {
    return BaseUtil.xpath_result(xpath_expr, xml);
  }
 
   // ...Other utility methods used by subclasses...


   public static void main(String args[])
   public static void main(String args[])
   {
   {
     BaseClient client = new BaseClient();
     BaseClient client = new BaseClient();
     String sessionId = client.login();
     String cookie = client.login();
     System.out.println("SessionId is: " + sessionId);
     System.out.println("Session Cookie is: " + cookie);
     client.logout();
     client.logout();
   }
   }
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 22:29, 18 May 2015

This code from the BaseClient sample program uses the Apache wink RestClient to make a REST login request and get a sessionId. It uses the Apache Wink client to post the login request, and calls a utility method defined in the BaseUtil] class to extract the sessionId from the response.
<syntaxhighlight lang="java" enclose="div" style="overflow: auto">

package demo.rest;

//HTTP Classes import org.apache.wink.client.RestClient; import org.apache.wink.client.Resource; import org.apache.wink.client.ClientResponse; import org.apache.commons.httpclient.HttpStatus;

// Apache Utilities import org.apache.commons.lang.StringUtils;

// Java Utilities import java.util.List; import java.io.InputStream; import javax.ws.rs.core.MultivaluedMap;

/*

* A base client that handles login and logout and provides utility
* methods for programs that use REST APIs.
* 
* Note:
* This class uses the Apache wink RestClient, which makes it
* pretty easy to make requests and handle responses. 
*/

public class BaseClient {

 String sessionId;
 RestClient client;
 
 String baseUrl = "https://{yourDomain}";
 String username = "yourName";
 String password = "yourPassword";
 
 public String login()
 { 
   String url = baseUrl + "/networking/rest/login";
   String xml = "<platform>"
       + "<login>"
           + "<userName>"+username+"</userName>"
           + "<password>"+password+"</password>"
       + "</login>"
       + "</platform>";
   try
   {
     System.out.println("Logging in");
     this.client = new RestClient();
     Resource resource = client.resource(url);
     resource.contentType("application/xml");
     resource.accept("application/xml");
     ClientResponse response = resource.post(xml);
     // Combine cookie parameters from the response header,
     // making a cookie string for use in subsequent requests.
     MultivaluedMap<String,String> headers = response.getHeaders();  
     String cookieString = "";
     List<String> cookieParams = headers.get("Set-Cookie");
     cookieString = StringUtils.join(cookieParams, "; ");
     return cookieString;
   }
   catch (Exception e)
   {
     e.printStackTrace();
   }
   return null;
 }
 // ...Various utility methods...
 public static void main(String args[])
 {
   BaseClient client = new BaseClient();
   String cookie = client.login();
   System.out.println("Session Cookie is: " + cookie);
   client.logout();
 }

} </syntaxhighlight>