Initial commit

This commit is contained in:
2016-08-27 16:18:28 +03:00
commit 1b16a1d67e
29 changed files with 1374 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
package eu.mikroskeem.utils.paste;
import java.io.IOException;
public interface IPaste {
String paste(String content) throws IOException;
}

View File

@@ -0,0 +1,43 @@
package eu.mikroskeem.utils.paste;
import eu.mikroskeem.utils.paste.providers.Hastebin;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
*
* @author Mark
*/
public class Paste {
private final IPaste pasteProvider;
public Paste(){
/* Currently only supported provider */
this.pasteProvider = new Hastebin();
}
/**
* Format exception into proper string
*
* @param e Exception to format
* @return Exception as string
*/
public static String formatException(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
/**
* Paste stacktrace to pastebin
*
* @param e Exception to paste
* @return Paste url
*/
public String pasteException(Exception e) throws IOException {
return pasteProvider.paste(formatException(e));
}
}

View File

@@ -0,0 +1,39 @@
package eu.mikroskeem.utils.paste.providers;
import eu.mikroskeem.utils.paste.IPaste;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Hastebin implements IPaste {
@Nullable
public String paste(@NotNull String content) throws IOException {
HttpURLConnection connection = null;
try {
URL url = new URL("https://paste.nightsnack.cf/documents");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(content);
wr.flush();
wr.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
return String.format("https://paste.nightsnack.cf/raw/%s", new JSONObject(rd.readLine()).get("key"));
} catch (MalformedURLException e) {
return null;
} finally {
if (connection == null) { return null; }
connection.disconnect();
}
}
}

View File

@@ -0,0 +1,29 @@
package eu.mikroskeem.utils.test.paste;
import eu.mikroskeem.utils.paste.Paste;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
public class PasteTest {
@Test
public void testExceptionPaste(){
Paste paste = new Paste();
try {
throw new RuntimeException("Fake exception");
} catch (RuntimeException e){
String url;
try {
url = paste.pasteException(e);
} catch (IOException ioex){
/* Test paste server down? */
Assert.fail("Paste server seems to be down, unable to test");
return;
}
Assert.assertNotNull(url);
System.out.println("Example paste:");
System.out.println(url);
}
}
}