Initial commit
This commit is contained in:
32
Paste/pom.xml
Normal file
32
Paste/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>utils</artifactId>
|
||||
<groupId>eu.mikroskeem</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>paste</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20160212</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations-java5</artifactId>
|
||||
<version>RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,7 @@
|
||||
package eu.mikroskeem.utils.paste;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface IPaste {
|
||||
String paste(String content) throws IOException;
|
||||
}
|
||||
43
Paste/src/main/java/eu/mikroskeem/utils/paste/Paste.java
Normal file
43
Paste/src/main/java/eu/mikroskeem/utils/paste/Paste.java
Normal 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));
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user