PlotSquared/src/main/java/com/intellectualcrafters/plot/util/HastebinUtility.java

63 lines
2.0 KiB
Java
Raw Normal View History

package com.intellectualcrafters.plot.util;
2015-07-30 16:25:16 +02:00
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HastebinUtility {
public static final String BIN_URL = "http://hastebin.com/documents", USER_AGENT = "Mozilla/5.0";
public static final Pattern PATTERN = Pattern.compile("\\{\"key\":\"([\\S\\s]*)\"\\}");
2015-07-19 16:18:46 +02:00
public static String upload(final String string) throws IOException {
URL url = new URL(BIN_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
2015-07-19 16:18:46 +02:00
outputStream.write(string.getBytes());
outputStream.flush();
outputStream.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
Matcher matcher = PATTERN.matcher(response.toString());
if (matcher.matches()) {
return "http://hastebin.com/" + matcher.group(1);
} else {
2015-07-19 16:18:46 +02:00
throw new RuntimeException("Couldn't read response!");
}
}
public static String upload(final File file) throws IOException {
StringBuilder content = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
2015-07-19 16:18:46 +02:00
reader.close();
return upload(content.toString());
}
}