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

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 5.61 KB  |  hits: 19  |  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. import java.net.URLEncoder;
  4.  
  5. import oauth.signpost.basic.DefaultOAuthConsumer;
  6. import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
  7. import oauth.signpost.exception.OAuthCommunicationException;
  8. import oauth.signpost.exception.OAuthExpectationFailedException;
  9. import oauth.signpost.exception.OAuthMessageSignerException;
  10.  
  11. import org.apache.http.HttpResponse;
  12. import org.apache.http.client.ClientProtocolException;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.conn.ssl.SSLSocketFactory;
  16. import org.apache.http.impl.client.DefaultHttpClient;
  17. import org.apache.http.util.EntityUtils;
  18. import org.apache.xerces.parsers.DOMParser;
  19. import org.w3c.dom.Document;
  20. import org.w3c.dom.Node;
  21. import org.w3c.dom.NodeList;
  22. import org.xml.sax.InputSource;
  23. import org.xml.sax.SAXException;
  24.  
  25. public class DeleteAllContributions {
  26.     private static CommonsHttpOAuthConsumer commonsHttpConsumer;
  27.     private static final String CONSUMERKEY = "3b69b13c-768a-4be5-a1bd-99f76953a3d5";
  28.     private static final String CONSUMERSECRETE = "2d05893b3e5a48f3deb7d05f6448a5ba";
  29.     private static final String TOKENKEY = "48cabfa8-8f11-4e40-ac33-8fb9f9865943";
  30.     private static final String TOKENSECRETE = "26520ed899f78c69bc38cd102cb0c0a6";
  31.     private static String host = "https://preview.indicee.com";
  32.  
  33.     public static void main(String args[]) throws Exception {
  34.         //oauth initialization
  35.         DefaultOAuthConsumer oconsumer = new DefaultOAuthConsumer(CONSUMERKEY, CONSUMERSECRETE);
  36.         oconsumer.setTokenWithSecret(TOKENKEY, TOKENSECRETE);
  37.  
  38.         commonsHttpConsumer = new CommonsHttpOAuthConsumer(oconsumer.getConsumerKey(), oconsumer.getConsumerSecret());
  39.         commonsHttpConsumer.setTokenWithSecret(oconsumer.getToken(), oconsumer.getTokenSecret());
  40.  
  41.         //deletes all data contributions
  42.         if (cleanup()) {
  43.             System.out.println("Successfully deleted all datasets");
  44.         } else {
  45.             System.out.println("Failed to delete all datasets");
  46.         }
  47.     }
  48.  
  49.     private static boolean cleanup() {
  50.         try {
  51.             Document doc = getItems("datasets");
  52.             NodeList elements = doc.getElementsByTagName("dataset");
  53.             for (int i = 0; i < elements.getLength(); i++) {
  54.                 Node node = elements.item(i);
  55.                 NodeList children = node.getChildNodes();
  56.                 for (int j = 0; j < children.getLength(); j++) {
  57.                     Node childNode = children.item(j);
  58.                     if (childNode.getNodeName().equals("name")) {
  59.                         String name = childNode.getFirstChild().getNodeValue();
  60.                         if (name == null || name.trim().isEmpty()) {
  61.                             continue;
  62.                         }
  63.                         boolean rtn = deleteItem("datasets", name);
  64.                         if (!rtn)
  65.                             return false;
  66.                     }
  67.                 }
  68.             }
  69.         } catch (Exception e) {
  70.             e.printStackTrace();
  71.             return false;
  72.         }
  73.         return true;
  74.     }
  75.  
  76.     private static Document getItems(String type) throws OAuthMessageSignerException, OAuthExpectationFailedException,
  77.             OAuthCommunicationException, ClientProtocolException, IOException {
  78.         HttpGet httpGet = new HttpGet(host + "/api/models/" + type + "/list.xml");
  79.  
  80.         // init a http client
  81.         DefaultHttpClient httpClient = new DefaultHttpClient();
  82.  
  83.         SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme(
  84.                 "https").getSocketFactory();
  85.         sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  86.  
  87.         commonsHttpConsumer.sign(httpGet);
  88.  
  89.         // send the data to get the response
  90.         HttpResponse httpResponse = httpClient.execute(httpGet);
  91.         int status = httpResponse.getStatusLine().getStatusCode();
  92.         if (status != 200) {
  93.             return null;
  94.         }
  95.  
  96.         InputStream body = httpResponse.getEntity().getContent();
  97.         DOMParser parser = new DOMParser();
  98.         try {
  99.             parser.parse(new InputSource(body));
  100.         } catch (SAXException e) {
  101.             e.printStackTrace();
  102.             return null;
  103.         } catch (IOException e) {
  104.             e.printStackTrace();
  105.             return null;
  106.         }
  107.         return parser.getDocument();
  108.     }
  109.  
  110.     private static boolean deleteItem(String type, String name) throws OAuthMessageSignerException,
  111.             OAuthExpectationFailedException, OAuthCommunicationException, ClientProtocolException, IOException {
  112.         name = URLEncoder.encode(name, "utf-8").replaceAll("\\+", "%20");
  113.         String url = host + "/api/models/" + type + "/" + name + "/delete.xml";
  114.         HttpPost httpPost = new HttpPost(url);
  115.  
  116.         // init a http client
  117.         DefaultHttpClient httpClient = new DefaultHttpClient();
  118.  
  119.         SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme(
  120.                 "https").getSocketFactory();
  121.         sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  122.  
  123.         commonsHttpConsumer.sign(httpPost);
  124.  
  125.         // send the data to get the response
  126.         HttpResponse httpResponse = httpClient.execute(httpPost);
  127.         int status = httpResponse.getStatusLine().getStatusCode();
  128.         String response = EntityUtils.toString(httpResponse.getEntity());
  129.         if (status == 200) {
  130.             //logger.info(response);
  131.             return true;
  132.         } else {
  133.             //logger.info(status + ":" + response);
  134.             return false;
  135.         }
  136.     }
  137. }