import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public class AdAstraApi {
private String BaseUrl;
private String ResponderUrl;
private String responderParameters;
/*
*Constructor sets BaseUrl, ResponderUrl and ResponderParameters properties
*/
public AdAstraApi() {
this.BaseUrl = "https://www.aaiscloud.com/INSTANCE/"; //TODO: change this to your instance URL
this.ResponderUrl = "~api/calendar/calendarList?action=get";
this.responderParameters = "fields=Event.Name%2C+ContactLastName%3AEventMeetingByActivityId.Event.PrimaryCustomerContact.Person.LastName%2C+ContactFirstName%3AEventMeetingByActivityId.Event.PrimaryCustomerContact.Person.FirstName%2C+EventMeetingByActivityId.Event.Customer.Name%2C+BuildingCode%2C+RoomNumber%2C+EventMeetingByActivityId.Event.EventType.Name%2C+EventMeetingByActivityId.Event.EventMeetings.Name%2C+StartDate%2C+EndDate%2C+StartMinute%2C+EndMinute%2C+Description%2C+Event.IsFeatured%2C+Event.IsPrivate&filter=%28%28StartDate%3E%3D%222019-08-06T00%3A00%3A00.000Z%22%29%26%26%28EndDate%3E%3D%222019-08-27T00%3A00%3A00.000Z%22%29%26%26%28EndDate%3C%3D%222019-09-03T00%3A00%3A00.000Z%22%29%29";
}
public String Login() throws MalformedURLException, IOException {
/*
* Open an HTTP Connection to the Logon.ashx page
*/
HttpURLConnection httpcon = (HttpURLConnection) ((new URL(BaseUrl+"Logon.ashx").openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();
/*
* Output user credentials over HTTP Output Stream
*/
byte[] outputBytes = "{'username': 'USERNAME_HERE', 'password':'PASSWORD_HERE'}".getBytes("UTF-8");
//TODO: change this to the username and password set up for your site, preferably one specifically for API calls
OutputStream os = httpcon.getOutputStream();
os.write(outputBytes);
os.close();
/*
* Call Function setCookie and pass the HttpUrlConnection. Set Function
* will return a Cookie String used to authenticate user.
*/
return setCookie(httpcon);
}
public String setCookie(HttpURLConnection httpcon) {
/*
* Process the HTTP Response Cookies from successful credentials
*/
List<String> cookieHeaders = httpcon.getHeaderFields().get("Set-Cookie");
String cookie = "";
for (String cookieEntry : cookieHeaders){
cookie += cookieEntry.substring(0,cookieEntry.indexOf(";")+1);
}
httpcon.disconnect();
return cookie;
}
public void ApiResponder(String cookie) throws MalformedURLException, IOException {
/*
* Create a new HTTP Connection request to responder, pass along Session_ID Cookie
*/
HttpURLConnection httpcon = (HttpURLConnection) ((new URL(this.BaseUrl+this.ResponderUrl).openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestProperty("Cookie", cookie);
httpcon.setRequestMethod("POST");
httpcon.connect();
byte[] outputBytes = responderParameters.getBytes("UTF-8");
OutputStream os = httpcon.getOutputStream();
os.write(outputBytes);
os.close();
/*
* Read/Output response from server
*/
BufferedReader inreader = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String decodedString;
while ((decodedString = inreader.readLine()) != null) {
System.out.println(decodedString);
}
inreader.close();
httpcon.disconnect();
}
public static void main(String[] args) throws Exception {
AdAstraApi api = new AdAstraApi();
System.out.println(api.Login());
api.ApiResponder(api.Login());
}
}
Comments
Please sign in to leave a comment.