Created
December 4, 2015 10:38
-
-
Save peterkir/7f593a2a494f28c3c62a to your computer and use it in GitHub Desktop.
Deleting via JFrog Bintray REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.klib.bintray; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.Base64; | |
import java.util.List; | |
import java.util.Scanner; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Delete { | |
private static final String BINTRAY_DOWNLOAD_URL = "https://dl.bintray.com"; | |
private static final String BINTRAY_SUBJECT = "/peterkir"; | |
private static final String BINTRAY_REPO = "/generic"; | |
private static final String BINTRAY_RESTAPI_URL = "https://api.bintray.com"; | |
private static final String BINTRAY_DELETE = "/content"; | |
private static final Pattern nav = Pattern.compile(".*?href=\":(.*?)\"", Pattern.MULTILINE); | |
public static void main(String[] args) { | |
Delete delete = new Delete(); | |
delete.visit("/org.eclipse.oomph/1.3.0/org.eclipse.oomph/").forEach(entry -> { | |
System.out.println("deleting " + entry + " ... successful: " + delete.httpDelete(entry)); | |
}); | |
delete.visit("/org.eclipse.oomph/1.3.0/peterkir/126/").forEach(entry -> { | |
System.out.println("deleting " + entry + " ... successful: " + delete.httpDelete(entry)); | |
}); | |
System.out.println("execution completed!"); | |
} | |
/** | |
* | |
* @param url for bintray location | |
* @return list of artifact file urls found recursively inside provided url | |
*/ | |
private List<String> visit(String url) { | |
List<String> rv = new ArrayList<String>(); | |
try { | |
URL dlUrl = new URL(BINTRAY_DOWNLOAD_URL + BINTRAY_SUBJECT + BINTRAY_REPO + url); | |
Scanner s = new Scanner(dlUrl.openStream()); | |
while (s.hasNext()) { | |
String next = s.next(); | |
Matcher m = nav.matcher(next); | |
while (m.find()) { | |
for (int i = 0; i < m.groupCount(); i++) { | |
String match = m.group(i + 1); | |
if (match.endsWith("/")) { | |
rv.addAll(visit(url + match)); | |
} else { | |
rv.add(BINTRAY_RESTAPI_URL + BINTRAY_DELETE + BINTRAY_SUBJECT + BINTRAY_REPO + "/" + url | |
+ match); | |
} | |
} | |
} | |
} | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return rv; | |
} | |
private boolean httpDelete(String entry) { | |
boolean rv = false; | |
try { | |
URL url = new URL(entry); | |
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); | |
httpCon.setDoOutput(true); | |
httpCon.setRequestMethod("DELETE"); | |
String userCredentials = "peterkir:<enter_valid_api_key>"; | |
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes())); | |
httpCon.setRequestProperty("Authorization", basicAuth); | |
httpCon.connect(); | |
int rc = httpCon.getResponseCode(); | |
if (rc == 200) { | |
rv = true; | |
} else { | |
System.out.println(" ... " + rc + " " + httpCon.getResponseMessage()); | |
} | |
} catch (ProtocolException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return rv; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment