- import java.io.IOException;
- import java.io.InputStream;
- import oauth.signpost.basic.DefaultOAuthConsumer;
- import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.conn.ssl.SSLSocketFactory;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.xerces.parsers.DOMParser;
- import org.w3c.dom.Document;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- public class ListReports {
- private static CommonsHttpOAuthConsumer commonsHttpConsumer;
- private static final String CONSUMERKEY = "3b69b13c-768a-4be5-a1bd-99f76953a3d5";
- private static final String CONSUMERSECRETE = "2d05893b3e5a48f3deb7d05f6448a5ba";
- private static final String TOKENKEY = "b5085a99-2b35-478b-8258-1537641740d3";
- private static final String TOKENSECRETE = "5c2c8163f4ebcdf84db5ce3681d642bc";
- private static String host = "https://preview.indicee.com";
- public static void main(String args[]) throws Exception {
- //oauth initialization
- DefaultOAuthConsumer oconsumer = new DefaultOAuthConsumer(CONSUMERKEY, CONSUMERSECRETE);
- oconsumer.setTokenWithSecret(TOKENKEY, TOKENSECRETE);
- commonsHttpConsumer = new CommonsHttpOAuthConsumer(oconsumer.getConsumerKey(), oconsumer.getConsumerSecret());
- commonsHttpConsumer.setTokenWithSecret(oconsumer.getToken(), oconsumer.getTokenSecret());
- //gets the list of reports
- Document reports = listReports();
- System.out.println(reports);
- NodeList elements = reports.getElementsByTagName("report");
- Node node = elements.item(0);
- NodeList children = node.getChildNodes();
- //find the first report id
- String id = null;
- for (int j = 0; j < children.getLength(); j++) {
- Node childNode = children.item(j);
- if (childNode.getNodeName().equals("id")) {
- id = childNode.getFirstChild().getNodeValue();
- if (id != null && !id.trim().isEmpty()) {
- break;
- }
- }
- }
- //print the first report data
- if (id != null && !id.trim().isEmpty()) {
- Document data = reportData(Integer.parseInt(id));
- System.out.println(data);
- } else {
- System.out.println("Could not find report id.");
- }
- }
- public static Document listReports() throws Exception {
- HttpGet httpGet = new HttpGet(host + "/api/reports");
- // init a http client
- DefaultHttpClient httpClient = new DefaultHttpClient();
- SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
- sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- //authenticate the request with oauth
- commonsHttpConsumer.sign(httpGet);
- // send the data to get the response
- HttpResponse httpResponse = httpClient.execute(httpGet);
- int status = httpResponse.getStatusLine().getStatusCode();
- if (status != 200) {
- throw new Exception("Error retrieving reports list.");
- }
- InputStream body = httpResponse.getEntity().getContent();
- DOMParser parser = new DOMParser();
- try {
- parser.parse(new InputSource(body));
- } catch (SAXException e) {
- e.printStackTrace();
- return null;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- return parser.getDocument();
- }
- public static Document reportData(int reportID) throws Exception {
- HttpGet httpGet = new HttpGet(host + "/api/reports/" + reportID + "/data");
- // init a http client
- DefaultHttpClient httpClient = new DefaultHttpClient();
- SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
- sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- //authenticate the request with oauth
- commonsHttpConsumer.sign(httpGet);
- // send the data to get the response
- HttpResponse httpResponse = httpClient.execute(httpGet);
- int status = httpResponse.getStatusLine().getStatusCode();
- if (status != 200) {
- throw new Exception("Error retrieving reports list.");
- }
- InputStream body = httpResponse.getEntity().getContent();
- DOMParser parser = new DOMParser();
- try {
- parser.parse(new InputSource(body));
- } catch (SAXException e) {
- e.printStackTrace();
- return null;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- return parser.getDocument();
- }
- }