Skip to content

Instantly share code, notes, and snippets.

@chaitut715
Last active December 31, 2016 06:47
Show Gist options
  • Select an option

  • Save chaitut715/31a0b34dfd3d14fd6f92b82e4d93fb19 to your computer and use it in GitHub Desktop.

Select an option

Save chaitut715/31a0b34dfd3d14fd6f92b82e4d93fb19 to your computer and use it in GitHub Desktop.
This is code snippet to get the calendar events of a user in java code (tested)
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
package com;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
/**
* Created by Chaitanya on 11/14/2016.
*/
public class SakaiUtil{
public static void main(String ar[]) throws ClientProtocolException, IOException{
String url = "https://qa10-mysql.nightly.sakaiproject.org";
String username = "admin";
String password = "admin";
getEventsResponse(url, username, password);
}
public static final String TAG = "SakaiUtil";
ObjectMapper mapper = new ObjectMapper();
public static String getEventsResponse(final String url, final String username, final String password) {
try {
System.out.println("getEventsResponse");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("_username", username));
params.add(new BasicNameValuePair("_password", password));
//create httpclient
HttpClient httpClient = HttpClientBuilder.create().build();
//store cookies in context
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
//call login web service
String webserviceURL = url+"/direct/session/new";
HttpPost postRequest = new HttpPost(webserviceURL);
postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
postRequest.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpClient.execute(postRequest, httpContext);
//get the status code
int status = response.getStatusLine().getStatusCode();
//if status code is 201 login is successful. So reuse the httpContext for next requests.
//if status code is not 201, there is a problem with the request.
if(status!=201)
return null;
EntityUtils.consume( response.getEntity() );
System.out.println("status:"+status);
String responseVal = getCalendarData(httpContext, url);
System.out.println("responseVal:"+responseVal);
//Log.i(TAG, "responseVal:"+responseVal);
//CalendarResponse calendarResponse = formatJSON(responseVal);
return responseVal;
}catch(IOException e){
e.printStackTrace();
return null;
}
}
protected static String getCalendarData(HttpContext httpContext, final String url) throws IOException {
HttpClient httpClient = HttpClientBuilder.create().build();
String webserviceURL = url+"/direct/calendar/my.json";
HttpGet getRequest = new HttpGet(webserviceURL);
HttpResponse response = httpClient.execute(getRequest, httpContext);
HttpEntity httpEntity = response.getEntity();
String responseVal = EntityUtils.toString(httpEntity);
//Log.i(TAG, "responseVal:"+responseVal);
EntityUtils.consume(httpEntity);
return responseVal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment