Get timesheet code sample

This sample Java code shows the process of getting an access token and using the token in a request for timesheet data.

package code_samples.org.hpe.apm.timesheet.ApmUIServices; 
import org.apache.commons.codec.binary.Base64; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import org.junit.Test; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.text.ParseException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

public class TimesheetCode_Example {

	public void getTimesheetExample() throws IOException, JSONException, ParseException {

		//Get OAuth data from Agile Manager. See the Help Center for details.
		String clientId = "API@1000.PPM"; 
		String clientSecret = "1UpW32lFc7X3H7t"; 
		AccessToken token = getAccessTokenWithFormFormat(clientId, clientSecret); 
		getTimeSheetData(token); 
	} 

	private void getTimeSheetData(AccessToken token) throws IOException, JSONException, ParseException {

		String method = "GET"; 
		String url = "http://agm_server:8080/agm/api/reports/timesheet?login-names=mary_developer@MyCompany.com&start-date=2015-01-01&end-date=2016-01-01&workspace-ids=1000"; 
		Map<String, String> headers = new HashMap<>(); 
		headers.put("Authorization", "bearer " + token.getAccessToken()); 
		String response = sendRequest(url, method, null, headers);
		List<TimesheetItem> items = parseTimesheetItems(response); 
	} 

	private AccessToken getAccessTokenWithFormFormat(String clientId, String clientSecret) throws IOException, JSONException {
		/* 
		Header : Content-Type=application/x-www-form-urlencoded 
		Post : http://agm_server:8080/agm/oauth/token 
		Data : client_id=<id from OAuth>&client_secret=<secret from OAuth>&grant_type=client_credentials
		Response :  {"access_token":"777856517_09a705b4-a4b7-4812-98b6-136210de7e4c","token_type":"bearer","expires_in":3599,"scope":"read trust write"} 
		*/ 

		String method = "POST"; 
		String url = "http://agm_server:8080/agm/oauth/token"; 
		String data = String.format("client_id=%s&client_secret=%s&grant_type=client_credentials", clientId, clientSecret); 
		Map<String, String> headers = new HashMap<>(); 
		headers.put("Content-Type", "application/x-www-form-urlencoded"); 
		String response = sendRequest(url, method, data, headers); 
		return parseAccessToken(response); 
	} 


	private AccessToken parseAccessToken(String json) throws JSONException {
		JSONObject obj = new JSONObject(json); 
		return new AccessToken(obj.getString("access_token"), obj.getString("token_type"), obj.getInt("expires_in"), obj.getString("scope")); 
	} 

	private List<TimesheetItem> parseTimesheetItems(String json) throws JSONException, ParseException {
		JSONObject obj = new JSONObject(json); 
		JSONArray arr = obj.getJSONArray("data"); 
		List<TimesheetItem> items = new ArrayList<>(); 
		for (int i = 0; i < arr.length(); i++) {
			JSONObject rawItem = (JSONObject) arr.get(i); 
			TimesheetItem item = new TimesheetItem(); 
			items.add(item); 
			/* see reports/timesheet topic 
			for full list of fields. */ 
			item.setEntityId(rawItem.getInt("entityId")); 
			item.setEntityName(rawItem.getString("entityName")); 
			item.setLoginName(rawItem.getString("loginName")); 
			item.setFullName(rawItem.getString("userFullName")); 
			item.setInvested(rawItem.getInt("investedHours")); 
			item.setDate(rawItem.getString("date")); //(format yyyy-MM-dd)
		} 
		return items; 
	} 

	private String sendRequest(String url, String method, String data, Map<String, String> headers) throws IOException {

		URL obj = new URL(url); 
		HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
		con.setRequestMethod(method);//"GET" or "POST"	
		
		//set headers
		if (headers != null) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				con.setRequestProperty(entry.getKey(), entry.getValue()); 
			} 
		} 

		//set data
		if (data != null) {
			// Send post request 
			con.setDoOutput(true); 
			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
			wr.writeBytes(data); 
			wr.flush(); 
			wr.close(); 
		} 

		int responseCode = con.getResponseCode(); 
		// System.out.print("Response code is " + responseCode); 

		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
		String inputLine; 
		StringBuffer response = new StringBuffer(); 

		while ((inputLine = in.readLine()) != null) { 
			response.append(inputLine); 
		} 
		in.close(); 

		String output = response.toString(); 
		return output; 
	} 

	public static class AccessToken {

		private String accessToken; 
		private String tokenType; 
		private int expiresIn;
		private String scope; 

		public AccessToken(String accessToken, String tokenType, int expiresIn, String scope) {
			this.accessToken = accessToken; 
			this.tokenType = tokenType; 
			this.expiresIn = expiresIn; 
			this.scope = scope; 
		} 

		public String getAccessToken() { 
			return accessToken; 
		} 

		public String getTokenType() { 
			return tokenType; 
		} 

		public int getExpiresIn() {
			return expiresIn; 
		} 

		public String getScope() { 
			return scope; 
		} 
	} 

	public static class TimesheetItem {

		private String loginName; 
		private String fullName; 
		private int invested;
		private String date; 
		private String entityName; 
		private String entityId; 


		public String getLoginName() { 
			return loginName; 
		} 

		public void setLoginName(String loginName) {
			this.loginName = loginName; 
		} 

		public String getFullName() { 
			return fullName; 
		} 

		public void setFullName(String fullName) {
			this.fullName = fullName; 
		} 

		public int getInvested() {
			return invested; 
		} 

		public void setInvested(int invested) {
			this.invested = invested; 
		} 

		public String getDate() { 
			return date; 
		} 

		public void setDate(String date) {
			this.date = date; 
		} 

		public String getEntityName() { 
			return entityName; 
		} 

		public void setEntityName(String entityName) {
			this.entityName = entityName; 
		} 

		public String getEntityId() { 
			return entityId; 
		} 

		public void setEntityId(String entityId) {
			this.entityId = entityId; 
		} 
	} 
}