feat: 添加StickyNotes提交类

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2016-09-18 19:18:36 +08:00
parent 21a1798872
commit 40a9caa956
1 changed files with 225 additions and 0 deletions

View File

@ -0,0 +1,225 @@
package pw.yumc.YumCore.misc;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
/**
*
*
* @since 2016918 6:57:34
* @author
*/
public class StickyNotes {
private final static String DOMAIN = "http://paste.yumc.pw";
private final String POST_URL = DOMAIN + "/api/json/create";
private final String VIEW_URL = DOMAIN + "/%s/%s";
public static void main(final String[] args) {
final StickyNotes p = new StickyNotes();
final Paste paste = new Paste();
paste.addLine("异常提交测试!");
paste.addThrowable(new Throwable());
System.out.println(p.post(StickyNotes.Expire.HalfHour, paste));;
}
/**
*
*
* @param expire
*
* @param content
*
* @return
*/
public String post(final StickyNotes.Expire expire, final StickyNotes.Paste content) {
return post("YumCore-" + System.currentTimeMillis(), StickyNotes.Format.JAVA, expire, content);
}
/**
*
*
* @param content
*
* @return
*/
public String post(final StickyNotes.Paste content) {
return post("YumCore-" + System.currentTimeMillis(), StickyNotes.Format.JAVA, StickyNotes.Expire.Never, content);
}
/**
*
*
* @param title
*
* @param format
*
* @param expire
*
* @param content
*
* @return
*/
public String post(final String title, final StickyNotes.Format format, final StickyNotes.Expire expire, final StickyNotes.Paste content) {
return post(title, format.toString(), expire.getExpire(), content.toString());
}
/**
*
*
* @param title
*
* @param format
*
* @param expire
*
* @param content
*
* @return
*/
public String post(final String title, final String format, final int expire, final String content) {
String result = "Failed to post!";
try {
final HttpURLConnection connection = (HttpURLConnection) new URL(POST_URL).openConnection();
connection.setConnectTimeout(20000);
connection.setReadTimeout(20000);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-type", "application/x-www-form-urlencoded");
connection.setInstanceFollowRedirects(false);
connection.setDoOutput(true);
final OutputStream outputStream = connection.getOutputStream();
final byte[] outByte = String.format("title=%s&language=%s&expire=%s&data=%s", title, format, expire, content).getBytes();
outputStream.write(outByte);
outputStream.flush();
outputStream.close();
final BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
final StringBuffer request = new StringBuffer();
String temp;
while ((temp = br.readLine()) != null) {
request.append(temp);
}
br.close();
result = request.toString().trim();
JSONObject object = (JSONObject) JSONValue.parse(result);
object = (JSONObject) object.get("result");
if (object.containsKey("error")) {
return object.get("error").toString();
}
return String.format(VIEW_URL, object.get("id"), object.get("hash"));
} catch (final Exception e) {
e.printStackTrace();
}
return result;
}
/**
*
*
* @since 2016918 7:00:09
* @author
*/
public enum Expire {
HalfHour(1800),
Hour(21600),
Day(86400),
Week(604800),
Mouth(2592000),
Year(31536000),
Never(31536000);
int expire;
private Expire(final int expire) {
this.expire = expire;
}
public int getExpire() {
return expire;
}
}
/**
*
*
* @since 2016918 7:00:15
* @author
*/
public enum Format {
JAVA("java"),
JAVASCRIPT("javascript"),
HTML("html"),
YAML("yaml");
String format;
private Format(final String format) {
this.format = format;
}
@Override
public String toString() {
return format;
}
}
/**
*
*
* @since 2016918 7:00:21
* @author
*/
public static class Paste {
private final static String errN = "异常名称: %s";
private final static String errM = "异常说明: %s";
private final static String errInfo = "简易错误信息如下:";
private final static String errStackTrace = " 位于 %s.%s(%s:%s)";
private final List<String> TEXT = new ArrayList<>();
public void addFile(final File file) throws IOException {
if (file == null) {
throw new IllegalArgumentException("文件不得为Null!");
}
addLines(Files.readAllLines(file.toPath()));
}
public void addLine(final String str) {
this.TEXT.add(str);
}
public void addLines(final List<String> str) {
this.TEXT.addAll(str);
}
public void addThrowable(final Throwable e) {
Throwable temp = e;
while (temp.getCause() != null) {
temp = temp.getCause();
}
TEXT.add(String.format(errN, e.getClass().getName()));
TEXT.add(String.format(errM, e.getMessage()));
TEXT.add(errInfo);
for (final StackTraceElement ste : e.getStackTrace()) {
TEXT.add(String.format(errStackTrace, ste.getClassName(), ste.getMethodName(), ste.getFileName(), ste.getLineNumber() < 0 ? "未知" : ste.getLineNumber()));
}
}
@Override
public String toString() {
final StringBuilder text = new StringBuilder();
for (final String str : TEXT) {
text.append(str + '\n');
}
return text.toString();
}
}
}