- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URLEncoder;
- import oauth.signpost.basic.DefaultOAuthConsumer;
- import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
- import oauth.signpost.exception.OAuthCommunicationException;
- import oauth.signpost.exception.OAuthExpectationFailedException;
- import oauth.signpost.exception.OAuthMessageSignerException;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.ssl.SSLSocketFactory;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- 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 DeleteAllContributions {
- 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 = "48cabfa8-8f11-4e40-ac33-8fb9f9865943";
- private static final String TOKENSECRETE = "26520ed899f78c69bc38cd102cb0c0a6";
- 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());
- //deletes all data contributions
- if (cleanup()) {
- System.out.println("Successfully deleted all datasets");
- } else {
- System.out.println("Failed to delete all datasets");
- }
- }
- private static boolean cleanup() {
- try {
- Document doc = getItems("datasets");
- NodeList elements = doc.getElementsByTagName("dataset");
- for (int i = 0; i < elements.getLength(); i++) {
- Node node = elements.item(i);
- NodeList children = node.getChildNodes();
- for (int j = 0; j < children.getLength(); j++) {
- Node childNode = children.item(j);
- if (childNode.getNodeName().equals("name")) {
- String name = childNode.getFirstChild().getNodeValue();
- if (name == null || name.trim().isEmpty()) {
- continue;
- }
- boolean rtn = deleteItem("datasets", name);
- if (!rtn)
- return false;
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- private static Document getItems(String type) throws OAuthMessageSignerException, OAuthExpectationFailedException,
- OAuthCommunicationException, ClientProtocolException, IOException {
- HttpGet httpGet = new HttpGet(host + "/api/models/" + type + "/list.xml");
- // init a http client
- DefaultHttpClient httpClient = new DefaultHttpClient();
- SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme(
- "https").getSocketFactory();
- sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- commonsHttpConsumer.sign(httpGet);
- // send the data to get the response
- HttpResponse httpResponse = httpClient.execute(httpGet);
- int status = httpResponse.getStatusLine().getStatusCode();
- if (status != 200) {
- return null;
- }
- 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();
- }
- private static boolean deleteItem(String type, String name) throws OAuthMessageSignerException,
- OAuthExpectationFailedException, OAuthCommunicationException, ClientProtocolException, IOException {
- name = URLEncoder.encode(name, "utf-8").replaceAll("\\+", "%20");
- String url = host + "/api/models/" + type + "/" + name + "/delete.xml";
- HttpPost httpPost = new HttpPost(url);
- // init a http client
- DefaultHttpClient httpClient = new DefaultHttpClient();
- SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme(
- "https").getSocketFactory();
- sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- commonsHttpConsumer.sign(httpPost);
- // send the data to get the response
- HttpResponse httpResponse = httpClient.execute(httpPost);
- int status = httpResponse.getStatusLine().getStatusCode();
- String response = EntityUtils.toString(httpResponse.getEntity());
- if (status == 200) {
- //logger.info(response);
- return true;
- } else {
- //logger.info(status + ":" + response);
- return false;
- }
- }
- }