Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose underlying pastemanager funcs #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.kitteh</groupId>
<artifactId>paste-gg-api</artifactId>
<version>0.9.1</version>
<version>1.0.0-SNAPSHOT</version>

<name>paste.gg Java API</name>

Expand Down Expand Up @@ -45,14 +45,13 @@
<system>GitHub</system>
<url>https://github.com/KittehOrg/PasteGGAPI/issues</url>
</issueManagement>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<version>2.8.6</version>
<type>jar</type>
<scope>provided</scope>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -98,6 +97,11 @@
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
</plugins>
</build>
</project>
83 changes: 65 additions & 18 deletions src/main/java/org/kitteh/pastegg/ConnectionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,92 @@
*/
package org.kitteh.pastegg;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
* Created by Narimm on 28/02/2020.
*/
public class ConnectionProvider {
private static Integer responseCode = null;

static String processPasteRequest(String output) throws IOException{
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);
conn.setRequestProperty("Accept", "application/json");
try (OutputStream os = conn.getOutputStream()) {
byte[] input = output.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
StringBuilder content = new StringBuilder();
try (
InputStream stream = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(stream,StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader)) {
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 = in.readLine()) != null) {
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;
}

}
5 changes: 4 additions & 1 deletion src/main/java/org/kitteh/pastegg/GsonProviderLol.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
package org.kitteh.pastegg;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
* I'm a temporary class!
*/
public class GsonProviderLol {
public static final Gson GSON = new Gson();
public static final Gson GSON = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
}
41 changes: 41 additions & 0 deletions src/main/java/org/kitteh/pastegg/InvalidPasteException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* * 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;

/**
* Created by Narimm on 4/03/2020.
*/
public class InvalidPasteException extends RuntimeException {
private final String message;

public InvalidPasteException(String message) {
this.message = message;
}

@Override
public String getMessage() {
return message;
}

}
28 changes: 23 additions & 5 deletions src/main/java/org/kitteh/pastegg/Paste.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@
package org.kitteh.pastegg;


import com.google.gson.annotations.SerializedName;

import java.util.Date;
import java.util.Optional;

public class Paste {
private final String id;
private final String deletion_key;
private final Visibility visibility;

private Date expires;
@SerializedName("created_at")
private Date createdAt;
@SerializedName("updated_at")
private Date updatedAt;

/**
* Constructs a paste without a deletion key.
Expand All @@ -40,22 +47,33 @@ public class Paste {
public Paste(String id) {
this(id, null);
}

/**
* Constructs a paste.
*
* @param id id
* @param id id
* @param deletionKey deletion key, or null
*/
public Paste(String id, String deletionKey) {
this(id,deletionKey,Visibility.PUBLIC);
this(id, deletionKey, Visibility.PUBLIC);
}

public Paste(String id, String deletionKey, Visibility visibility) {
this.id = id;
this.deletion_key = deletionKey;
this.visibility = visibility;
}

public Date getExpires() {
return expires;
}

public Date getCreatedAt() {
return createdAt;
}

public Date getUpdatedAt() {
return updatedAt;
}

/**
* Gets the paste's id.
*
Expand Down
48 changes: 41 additions & 7 deletions src/main/java/org/kitteh/pastegg/PasteBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,27 @@ public static class PasteResult {
private Paste result;
private String message;


public Optional<Paste> getPaste() {
return Optional.ofNullable(this.result);
}

public Optional<String> getMessage() {
return Optional.ofNullable(this.message);
}

public String getStatus() {
return status;
}
}

private Visibility visibility = Visibility.getDefault();
private String name;
private String expires = null;
private boolean debug = false;
private String apiKey;
@SuppressWarnings({"TypeMayBeWeakened", "MismatchedQueryAndUpdateOfCollection"})
private List<PasteFile> files = new LinkedList<>();
private String expires;
private final List<PasteFile> files = new LinkedList<>();

public PasteBuilder name(String name) {
this.name = name;
Expand All @@ -66,24 +73,51 @@ public PasteBuilder expires(ZonedDateTime when) {
return this;
}

public PasteBuilder setApiKey(String key) {
this.apiKey = key;
return this;
}

public PasteBuilder visibility(Visibility visibility) {
this.visibility = visibility;
return this;
}

/**
* debug the connection.
* @param debug boolean
* @return PasteBuilder
*/
public PasteBuilder debug(boolean debug) {
this.debug = debug;
return this;
}


public PasteBuilder addFile(PasteFile file) {
files.add(file);
return this;
}

public PasteResult build() {
public PasteResult build() throws InvalidPasteException {
if (visibility == Visibility.PRIVATE && apiKey == null) {
throw new InvalidPasteException("No API Key Provided for Private Paste...");
}
String toString = GsonProviderLol.GSON.toJson(this);
try {
String result = ConnectionProvider.processPasteRequest(toString);
return GsonProviderLol.GSON.fromJson(result, PasteResult.class);
String result = ConnectionProvider.processPasteRequest(apiKey, toString,debug);
PasteResult pasteResult = GsonProviderLol.GSON.fromJson(result, PasteResult.class);
if (pasteResult.getPaste().isPresent()) {
PasteManager.addPaste(pasteResult.getPaste().get());
}
return pasteResult;
} catch (IOException e) {
e.printStackTrace();

InvalidPasteException invalid =
new InvalidPasteException("Paste could not be sent to past.gg: "
+ e.getMessage());
invalid.addSuppressed(e);
throw invalid;
}
return null;
}
}
6 changes: 4 additions & 2 deletions src/main/java/org/kitteh/pastegg/PasteContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public Function<String, String> getProcessor() {
}
}

private ContentType format;
private String value;
@SuppressWarnings("unused")
private final ContentType format;
@SuppressWarnings("unused")
private final String value;

private transient String processedValue;

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/kitteh/pastegg/PasteFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
*/
package org.kitteh.pastegg;

@SuppressWarnings("CanBeFinal")
public class PasteFile {
private String id;
private String name;
private PasteContent content;
private final String id;
private final String name;
private final PasteContent content;

public PasteFile(String id, String name, PasteContent content) {
this.id = id;
Expand Down
Loading