-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConnectionProvider.java
More file actions
114 lines (107 loc) · 4.65 KB
/
ConnectionProvider.java
File metadata and controls
114 lines (107 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* * Copyright (C) 2018-2020 Matt Baxter https://kitteh.org
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.kitteh.pastegg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
/**
* Created by Narimm on 28/02/2020.
*/
public class ConnectionProvider {
private static Integer responseCode = null;
public static Integer getLastResponseCode() {
return responseCode;
}
static String processPasteRequest(String key, String output) throws IOException {
return processPasteRequest(key, output,false);
}
static String processPasteRequest(String key, String output, boolean debug) throws IOException {
URL url = new URL("https://api.paste.gg/v1/pastes");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=" + StandardCharsets.UTF_8);
conn.setDoOutput(true);
if (key != null) {
conn.setRequestProperty("Authorization", "Key " + key);
}
conn.setRequestProperty("Accept", "application/json");
if (debug) {
System.out.println("----------Connection--------------");
System.out.println(conn.toString());
System.out.println("----------Output--------------");
System.out.println(output);
System.out.println("------------------------------");
}
try (OutputStream os = conn.getOutputStream()) {
byte[] input = output.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
StringBuilder content = new StringBuilder();
try {
responseCode = conn.getResponseCode();
}catch (IOException e){
InputStream in = conn.getErrorStream();
if (in != null) {
InputStreamReader reader = new InputStreamReader(in,StandardCharsets.UTF_8);
BufferedReader errorIn = new BufferedReader(reader);
String inputLine;
while ((inputLine = errorIn.readLine()) != null) {
content.append(inputLine);
}
if ( debug ) {
System.out.println("----------Error Response--------------");
System.out.println(content.toString());
System.out.println("------------------------------");
}
throw new IOException(e.getMessage() + " Error Data: " + content.toString());
}
throw e;
}
try (
InputStream stream = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader)) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
}
return content.toString();
}
public static boolean deletePaste(String pasteId, String deletionKey) throws IOException{
URL url = new URL("https://api.paste.gg/v1/pastes/"+pasteId);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("DELETE");
String key = "Key "+deletionKey;
conn.setRequestProperty("Authorization",key);
conn.connect();
int responseCode = conn.getResponseCode();
return responseCode == 204;
}
}