Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 4.27 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3.  
  4. import oauth.signpost.basic.DefaultOAuthConsumer;
  5. import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
  6.  
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.conn.ssl.SSLSocketFactory;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.xerces.parsers.DOMParser;
  12. import org.w3c.dom.Document;
  13. import org.w3c.dom.Node;
  14. import org.w3c.dom.NodeList;
  15. import org.xml.sax.InputSource;
  16. import org.xml.sax.SAXException;
  17.  
  18. public class ListReports {
  19.         private static CommonsHttpOAuthConsumer commonsHttpConsumer;
  20.         private static final String CONSUMERKEY = "3b69b13c-768a-4be5-a1bd-99f76953a3d5";
  21.         private static final String CONSUMERSECRETE = "2d05893b3e5a48f3deb7d05f6448a5ba";
  22.         private static final String TOKENKEY = "b5085a99-2b35-478b-8258-1537641740d3";
  23.         private static final String TOKENSECRETE = "5c2c8163f4ebcdf84db5ce3681d642bc";
  24.         private static String host = "https://preview.indicee.com";
  25.  
  26.         public static void main(String args[]) throws Exception {
  27.                 //oauth initialization
  28.                 DefaultOAuthConsumer oconsumer = new DefaultOAuthConsumer(CONSUMERKEY, CONSUMERSECRETE);
  29.                 oconsumer.setTokenWithSecret(TOKENKEY, TOKENSECRETE);
  30.  
  31.                 commonsHttpConsumer = new CommonsHttpOAuthConsumer(oconsumer.getConsumerKey(), oconsumer.getConsumerSecret());
  32.                 commonsHttpConsumer.setTokenWithSecret(oconsumer.getToken(), oconsumer.getTokenSecret());
  33.  
  34.                 //gets the list of reports
  35.                 Document reports = listReports();
  36.                 System.out.println(reports);
  37.                 NodeList elements = reports.getElementsByTagName("report");
  38.                 Node node = elements.item(0);
  39.                 NodeList children = node.getChildNodes();
  40.                 //find the first report id
  41.                 String id = null;
  42.                 for (int j = 0; j < children.getLength(); j++) {
  43.                         Node childNode = children.item(j);
  44.                         if (childNode.getNodeName().equals("id")) {
  45.                                 id = childNode.getFirstChild().getNodeValue();
  46.                                 if (id != null && !id.trim().isEmpty()) {
  47.                                         break;
  48.                                 }
  49.                         }
  50.                 }
  51.  
  52.                 //print the first report data
  53.                 if (id != null && !id.trim().isEmpty()) {
  54.                         Document data = reportData(Integer.parseInt(id));
  55.                         System.out.println(data);
  56.                 } else {
  57.                         System.out.println("Could not find report id.");
  58.                 }
  59.         }
  60.  
  61.         public static Document listReports() throws Exception {
  62.                 HttpGet httpGet = new HttpGet(host + "/api/reports");
  63.  
  64.                 // init a http client
  65.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  66.  
  67.                 SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
  68.                 sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  69.  
  70.                 //authenticate the request with oauth
  71.                 commonsHttpConsumer.sign(httpGet);
  72.  
  73.                 // send the data to get the response
  74.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  75.                 int status = httpResponse.getStatusLine().getStatusCode();
  76.                 if (status != 200) {
  77.                         throw new Exception("Error retrieving reports list.");
  78.                 }
  79.                 InputStream body = httpResponse.getEntity().getContent();
  80.                 DOMParser parser = new DOMParser();
  81.                 try {
  82.                         parser.parse(new InputSource(body));
  83.                 } catch (SAXException e) {
  84.                         e.printStackTrace();
  85.                         return null;
  86.                 } catch (IOException e) {
  87.                         e.printStackTrace();
  88.                         return null;
  89.                 }
  90.                 return parser.getDocument();
  91.         }
  92.  
  93.         public static Document reportData(int reportID) throws Exception {
  94.                 HttpGet httpGet = new HttpGet(host + "/api/reports/" + reportID + "/data");
  95.  
  96.                 // init a http client
  97.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  98.  
  99.                 SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
  100.                 sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  101.  
  102.                 //authenticate the request with oauth
  103.                 commonsHttpConsumer.sign(httpGet);
  104.  
  105.                 // send the data to get the response
  106.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  107.                 int status = httpResponse.getStatusLine().getStatusCode();
  108.                 if (status != 200) {
  109.                         throw new Exception("Error retrieving reports list.");
  110.                 }
  111.                 InputStream body = httpResponse.getEntity().getContent();
  112.                 DOMParser parser = new DOMParser();
  113.                 try {
  114.                         parser.parse(new InputSource(body));
  115.                 } catch (SAXException e) {
  116.                         e.printStackTrace();
  117.                         return null;
  118.                 } catch (IOException e) {
  119.                         e.printStackTrace();
  120.                         return null;
  121.                 }
  122.                 return parser.getDocument();
  123.         }
  124. }