mirror of
https://github.com/BGHDDevelopment/PlayerBalancer.git
synced 2024-11-27 05:05:19 +01:00
Reformat of the source
This commit is contained in:
parent
355bbcf180
commit
a3a760ef08
4
pom.xml
4
pom.xml
@ -1,6 +1,6 @@
|
||||
<?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"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -12,6 +12,11 @@ import java.util.Map;
|
||||
public class AdapterFix implements ConfigurationAdapter {
|
||||
private static final Map<String, ServerInfo> fakeServers = new HashMap<>();
|
||||
private static AdapterFix instance = null;
|
||||
private final ConfigurationAdapter adapter;
|
||||
|
||||
public AdapterFix(ConfigurationAdapter adapter) {
|
||||
this.adapter = adapter;
|
||||
}
|
||||
|
||||
public static void inject(ProxyServer server) {
|
||||
if (instance == null) {
|
||||
@ -32,12 +37,6 @@ public class AdapterFix implements ConfigurationAdapter {
|
||||
return fakeServers;
|
||||
}
|
||||
|
||||
private final ConfigurationAdapter adapter;
|
||||
|
||||
public AdapterFix(ConfigurationAdapter adapter) {
|
||||
this.adapter = adapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
adapter.load();
|
||||
|
@ -91,8 +91,10 @@ public enum PasteHelper {
|
||||
}
|
||||
};
|
||||
|
||||
public static final String DEVELOPER_KEY = "e3ff18d8fb001a3ece08ae0d7d4a87bd";
|
||||
private String link;
|
||||
private ScheduledTask task = null;
|
||||
|
||||
public void send(Plugin plugin, CommandSender sender, String message) {
|
||||
try {
|
||||
sender.sendMessage(new ComponentBuilder(message.replace("{link}", link == null ? link = paste(plugin) : link)
|
||||
@ -108,12 +110,11 @@ public enum PasteHelper {
|
||||
sender.sendMessage(new ComponentBuilder("An unexpected error occurred while pasting the file").color(ChatColor.RED).create());
|
||||
}
|
||||
e.printStackTrace();
|
||||
} catch(Exception e) {
|
||||
} catch (Exception e) {
|
||||
sender.sendMessage(new ComponentBuilder("An internal error occurred while attempting to perform this command").color(ChatColor.RED).create());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract String paste(Plugin plugin) throws Exception;
|
||||
public static final String DEVELOPER_KEY = "e3ff18d8fb001a3ece08ae0d7d4a87bd";
|
||||
}
|
||||
|
@ -13,10 +13,10 @@ import static me.jaimemartz.lobbybalancer.LobbyBalancer.printStartupInfo;
|
||||
|
||||
public class PingManager {
|
||||
private final LobbyBalancer plugin;
|
||||
private final Map<ServerInfo, ServerStatus> storage = new HashMap<>();
|
||||
private boolean stopped = true;
|
||||
private PingTacticType tactic;
|
||||
private ScheduledTask task;
|
||||
private final Map<ServerInfo, ServerStatus> storage = new HashMap<>();
|
||||
|
||||
public PingManager(LobbyBalancer plugin) {
|
||||
this.plugin = plugin;
|
||||
|
@ -3,57 +3,52 @@ package org.jpaste;
|
||||
import org.jpaste.exceptions.PasteException;
|
||||
|
||||
/**
|
||||
*
|
||||
* An abstract representation of a paste.
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* An abstract paste holds the paste contents and the {@link #paste()}
|
||||
* operation.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @param <P> PasteResult implementation
|
||||
* @author Brian B
|
||||
*
|
||||
* @param <P>
|
||||
* PasteResult implementation
|
||||
*/
|
||||
public abstract class AbstractPaste<P extends AbstractPasteLink> {
|
||||
private String contents;
|
||||
private String contents;
|
||||
|
||||
/**
|
||||
* Creates a new abstract <code>AbstractPaste</code> instance.
|
||||
*
|
||||
* @param contents
|
||||
* the contents of the paste
|
||||
*/
|
||||
public AbstractPaste(String contents) {
|
||||
this.contents = contents;
|
||||
}
|
||||
/**
|
||||
* Creates a new abstract <code>AbstractPaste</code> instance.
|
||||
*
|
||||
* @param contents the contents of the paste
|
||||
*/
|
||||
public AbstractPaste(String contents) {
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets paste contents
|
||||
*
|
||||
* @return paste contents
|
||||
*/
|
||||
public String getContents() {
|
||||
return contents;
|
||||
}
|
||||
/**
|
||||
* Gets paste contents
|
||||
*
|
||||
* @return paste contents
|
||||
*/
|
||||
public String getContents() {
|
||||
return contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the paste contents
|
||||
*
|
||||
* @param contents
|
||||
* contents of the paste
|
||||
*/
|
||||
public void setContents(String contents) {
|
||||
this.contents = contents;
|
||||
}
|
||||
/**
|
||||
* Sets the paste contents
|
||||
*
|
||||
* @param contents contents of the paste
|
||||
*/
|
||||
public void setContents(String contents) {
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to paste this paste and returns the results
|
||||
*
|
||||
* @return paste result
|
||||
* @throws PasteException if it failed to paste the paste
|
||||
*/
|
||||
public abstract P paste() throws PasteException;
|
||||
/**
|
||||
* Attempts to paste this paste and returns the results
|
||||
*
|
||||
* @return paste result
|
||||
* @throws PasteException if it failed to paste the paste
|
||||
*/
|
||||
public abstract P paste() throws PasteException;
|
||||
|
||||
}
|
||||
|
@ -3,23 +3,21 @@ package org.jpaste;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
*
|
||||
* An representation of an abstract PasteLink
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* An AbstractPasteLink holds the link/URL to a paste.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractPasteLink {
|
||||
|
||||
/**
|
||||
* Gets the URL to this paste
|
||||
*
|
||||
* @return URL to paste
|
||||
*/
|
||||
public abstract URL getLink();
|
||||
/**
|
||||
* Gets the URL to this paste
|
||||
*
|
||||
* @return URL to paste
|
||||
*/
|
||||
public abstract URL getLink();
|
||||
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
package org.jpaste.exceptions;
|
||||
|
||||
public class PasteException extends Exception {
|
||||
private static final long serialVersionUID = -4230960075582953775L;
|
||||
private static final long serialVersionUID = -4230960075582953775L;
|
||||
|
||||
public PasteException() {
|
||||
}
|
||||
public PasteException() {
|
||||
}
|
||||
|
||||
public PasteException(String message) {
|
||||
super(message);
|
||||
}
|
||||
public PasteException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,60 +1,56 @@
|
||||
package org.jpaste.pastebin;
|
||||
|
||||
/**
|
||||
*
|
||||
* Represents a paste's expire date
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* A list of all expire dates/times can be found at the <a
|
||||
* href="http://pastebin.com/api">pastebin API manual</a>.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public enum PasteExpireDate {
|
||||
NEVER("N", -1), TEN_MINUTES("10M", 10 * 60), ONE_HOUR("1H", 60 * 60), ONE_DAY(
|
||||
"1D", 60 * 60 * 24), ONE_WEEK("1W", 60 * 60 * 24 * 7), TWO_WEEKS(
|
||||
"2W", 60 * 60 * 24 * 14), ONE_MONTH("1M", -1);
|
||||
NEVER("N", -1), TEN_MINUTES("10M", 10 * 60), ONE_HOUR("1H", 60 * 60), ONE_DAY(
|
||||
"1D", 60 * 60 * 24), ONE_WEEK("1W", 60 * 60 * 24 * 7), TWO_WEEKS(
|
||||
"2W", 60 * 60 * 24 * 14), ONE_MONTH("1M", -1);
|
||||
|
||||
private String val;
|
||||
private int timeSeconds;
|
||||
private String val;
|
||||
private int timeSeconds;
|
||||
|
||||
/**
|
||||
* Creates a new <code>PasteExpireDate</code> instance.
|
||||
*
|
||||
* @param val
|
||||
* a valid expire date value
|
||||
*/
|
||||
PasteExpireDate(String val, int timeSeconds) {
|
||||
this.val = val;
|
||||
this.timeSeconds = timeSeconds;
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PasteExpireDate</code> instance.
|
||||
*
|
||||
* @param val a valid expire date value
|
||||
*/
|
||||
PasteExpireDate(String val, int timeSeconds) {
|
||||
this.val = val;
|
||||
this.timeSeconds = timeSeconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get's the valid value for the 'api_paste_expire_date' parameter
|
||||
*
|
||||
* @return the valid value for the 'api_paste_expire_date' parameter
|
||||
*/
|
||||
public String getValue() {
|
||||
return val;
|
||||
}
|
||||
/**
|
||||
* Gets PasteExpireDate based on: paste expire date minus paste date (in
|
||||
* seconds)
|
||||
*
|
||||
* @param timeSeconds seconds between expire date and paste date
|
||||
* @return PasteExpireDate
|
||||
*/
|
||||
public static PasteExpireDate getExpireDate(int timeSeconds) {
|
||||
for (PasteExpireDate date : PasteExpireDate.values()) {
|
||||
if (date.timeSeconds == timeSeconds) {
|
||||
return date;
|
||||
}
|
||||
}
|
||||
return ONE_MONTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets PasteExpireDate based on: paste expire date minus paste date (in
|
||||
* seconds)
|
||||
*
|
||||
* @param timeSeconds
|
||||
* seconds between expire date and paste date
|
||||
* @return PasteExpireDate
|
||||
*/
|
||||
public static PasteExpireDate getExpireDate(int timeSeconds) {
|
||||
for (PasteExpireDate date : PasteExpireDate.values()) {
|
||||
if (date.timeSeconds == timeSeconds) {
|
||||
return date;
|
||||
}
|
||||
}
|
||||
return ONE_MONTH;
|
||||
}
|
||||
/**
|
||||
* Get's the valid value for the 'api_paste_expire_date' parameter
|
||||
*
|
||||
* @return the valid value for the 'api_paste_expire_date' parameter
|
||||
*/
|
||||
public String getValue() {
|
||||
return val;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,189 +19,172 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
* A global representation of the pastebin site
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* Holds various constants and method shortcuts
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public class Pastebin {
|
||||
/**
|
||||
* Used to interact with the pastebin API
|
||||
*/
|
||||
public static final String API_POST_LINK = "http://pastebin.com/api/api_post.php";
|
||||
/**
|
||||
* Used for fetching an user session id
|
||||
*/
|
||||
public static final String API_LOGIN_LINK = "http://pastebin.com/api/api_login.php";
|
||||
/**
|
||||
* Used to interact with the pastebin API
|
||||
*/
|
||||
public static final String API_POST_LINK = "http://pastebin.com/api/api_post.php";
|
||||
/**
|
||||
* Used for fetching an user session id
|
||||
*/
|
||||
public static final String API_LOGIN_LINK = "http://pastebin.com/api/api_login.php";
|
||||
|
||||
/**
|
||||
* Fetches a paste text from pastebin
|
||||
*
|
||||
* @param pasteKey
|
||||
* the unique paste key
|
||||
* @return contents of the paste
|
||||
*/
|
||||
public static String getContents(String pasteKey) {
|
||||
return PastebinLink.getContents(pasteKey);
|
||||
}
|
||||
/**
|
||||
* Fetches a paste text from pastebin
|
||||
*
|
||||
* @param pasteKey the unique paste key
|
||||
* @return contents of the paste
|
||||
*/
|
||||
public static String getContents(String pasteKey) {
|
||||
return PastebinLink.getContents(pasteKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a paste on pastebin and returns the URL to it
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents
|
||||
* contents of the paste
|
||||
* @return URL to paste
|
||||
* @throws PasteException
|
||||
* if it failed to push the paste
|
||||
*/
|
||||
public static URL pastePaste(String developerKey, String contents)
|
||||
throws PasteException {
|
||||
return pastePaste(developerKey, contents, null);
|
||||
}
|
||||
/**
|
||||
* Generates a paste on pastebin and returns the URL to it
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents contents of the paste
|
||||
* @return URL to paste
|
||||
* @throws PasteException if it failed to push the paste
|
||||
*/
|
||||
public static URL pastePaste(String developerKey, String contents)
|
||||
throws PasteException {
|
||||
return pastePaste(developerKey, contents, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a paste on pastebin and returns the URL to it
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents
|
||||
* contents of the paste
|
||||
* @param title
|
||||
* title of the paste
|
||||
* @return URL to paste
|
||||
* @throws PasteException
|
||||
* if it failed to push the paste
|
||||
*/
|
||||
public static URL pastePaste(String developerKey, String contents,
|
||||
String title) throws PasteException {
|
||||
return newPaste(developerKey, contents, title).paste().getLink();
|
||||
}
|
||||
/**
|
||||
* Generates a paste on pastebin and returns the URL to it
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents contents of the paste
|
||||
* @param title title of the paste
|
||||
* @return URL to paste
|
||||
* @throws PasteException if it failed to push the paste
|
||||
*/
|
||||
public static URL pastePaste(String developerKey, String contents,
|
||||
String title) throws PasteException {
|
||||
return newPaste(developerKey, contents, title).paste().getLink();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new paste and returns it
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents
|
||||
* contents of the paste
|
||||
* @param title
|
||||
* title of the paste
|
||||
* @return a new paste
|
||||
*/
|
||||
public static PastebinPaste newPaste(String developerKey, String contents,
|
||||
String title) {
|
||||
PastebinPaste paste = new PastebinPaste(developerKey, contents);
|
||||
paste.setPasteTitle(title);
|
||||
return paste;
|
||||
}
|
||||
/**
|
||||
* Generates a new paste and returns it
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents contents of the paste
|
||||
* @param title title of the paste
|
||||
* @return a new paste
|
||||
*/
|
||||
public static PastebinPaste newPaste(String developerKey, String contents,
|
||||
String title) {
|
||||
PastebinPaste paste = new PastebinPaste(developerKey, contents);
|
||||
paste.setPasteTitle(title);
|
||||
return paste;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new paste and returns it
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents
|
||||
* contents of the paste
|
||||
* @return a new paste
|
||||
*/
|
||||
public static PastebinPaste newPaste(String developerKey, String contents) {
|
||||
return newPaste(developerKey, contents, null);
|
||||
}
|
||||
/**
|
||||
* Generates a new paste and returns it
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents contents of the paste
|
||||
* @return a new paste
|
||||
*/
|
||||
public static PastebinPaste newPaste(String developerKey, String contents) {
|
||||
return newPaste(developerKey, contents, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current trending pastebin pastes
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @return an array of {@link PastebinLink}
|
||||
* @throws ParseException
|
||||
* if it failed to parse the trending pastes
|
||||
*/
|
||||
public static PastebinLink[] getTrending(String developerKey)
|
||||
throws ParseException {
|
||||
if (developerKey == null || developerKey.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Developer key can't be null or empty.");
|
||||
}
|
||||
Post post = new Post();
|
||||
post.put("api_dev_key", developerKey);
|
||||
post.put("api_option", "trends");
|
||||
/**
|
||||
* Gets the current trending pastebin pastes
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @return an array of {@link PastebinLink}
|
||||
* @throws ParseException if it failed to parse the trending pastes
|
||||
*/
|
||||
public static PastebinLink[] getTrending(String developerKey)
|
||||
throws ParseException {
|
||||
if (developerKey == null || developerKey.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Developer key can't be null or empty.");
|
||||
}
|
||||
Post post = new Post();
|
||||
post.put("api_dev_key", developerKey);
|
||||
post.put("api_option", "trends");
|
||||
|
||||
String response = Web.getContents(API_POST_LINK, post);
|
||||
String response = Web.getContents(API_POST_LINK, post);
|
||||
|
||||
if (response.startsWith("<paste>")) {
|
||||
// success
|
||||
try {
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
|
||||
.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
response = "<dummy>" + response + "</dummy>"; // requires root
|
||||
// element
|
||||
Document doc = dBuilder.parse(new InputSource(
|
||||
new ByteArrayInputStream(response.getBytes("utf-8"))));
|
||||
doc.getDocumentElement().normalize();
|
||||
if (response.startsWith("<paste>")) {
|
||||
// success
|
||||
try {
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
|
||||
.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
response = "<dummy>" + response + "</dummy>"; // requires root
|
||||
// element
|
||||
Document doc = dBuilder.parse(new InputSource(
|
||||
new ByteArrayInputStream(response.getBytes("utf-8"))));
|
||||
doc.getDocumentElement().normalize();
|
||||
|
||||
NodeList nodes = doc.getElementsByTagName("paste");
|
||||
ArrayList<PastebinLink> pastes = new ArrayList<PastebinLink>(
|
||||
nodes.getLength());
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
Node node = nodes.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
NodeList nodes = doc.getElementsByTagName("paste");
|
||||
ArrayList<PastebinLink> pastes = new ArrayList<PastebinLink>(
|
||||
nodes.getLength());
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
Node node = nodes.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
|
||||
String pasteFormat = XMLUtils.getText(element,
|
||||
"paste_format_short");
|
||||
String title = XMLUtils.getText(element, "paste_title");
|
||||
int visibility = Integer.parseInt(XMLUtils.getText(
|
||||
element, "paste_private"));
|
||||
int hits = Integer.parseInt(XMLUtils.getText(element,
|
||||
"paste_hits"));
|
||||
String pasteFormat = XMLUtils.getText(element,
|
||||
"paste_format_short");
|
||||
String title = XMLUtils.getText(element, "paste_title");
|
||||
int visibility = Integer.parseInt(XMLUtils.getText(
|
||||
element, "paste_private"));
|
||||
int hits = Integer.parseInt(XMLUtils.getText(element,
|
||||
"paste_hits"));
|
||||
|
||||
long expireDate = Long.parseLong(XMLUtils.getText(
|
||||
element, "paste_expire_date"));
|
||||
long pasteDate = Long.parseLong(XMLUtils.getText(
|
||||
element, "paste_date"));
|
||||
long expireDate = Long.parseLong(XMLUtils.getText(
|
||||
element, "paste_expire_date"));
|
||||
long pasteDate = Long.parseLong(XMLUtils.getText(
|
||||
element, "paste_date"));
|
||||
|
||||
URL pasteURL = new URL(XMLUtils.getText(element,
|
||||
"paste_url"));
|
||||
URL pasteURL = new URL(XMLUtils.getText(element,
|
||||
"paste_url"));
|
||||
|
||||
PastebinPaste paste = new PastebinPaste();
|
||||
paste.setPasteFormat(pasteFormat);
|
||||
paste.setPasteTitle(title);
|
||||
paste.setVisibility(visibility);
|
||||
paste.setPasteExpireDate(expireDate == 0L ? PasteExpireDate.NEVER
|
||||
: PasteExpireDate
|
||||
.getExpireDate((int) (expireDate - pasteDate)));
|
||||
PastebinPaste paste = new PastebinPaste();
|
||||
paste.setPasteFormat(pasteFormat);
|
||||
paste.setPasteTitle(title);
|
||||
paste.setVisibility(visibility);
|
||||
paste.setPasteExpireDate(expireDate == 0L ? PasteExpireDate.NEVER
|
||||
: PasteExpireDate
|
||||
.getExpireDate((int) (expireDate - pasteDate)));
|
||||
|
||||
PastebinLink pastebinLink = new PastebinLink(paste,
|
||||
pasteURL, new Date(pasteDate * 1000));
|
||||
pastebinLink.setHits(hits);
|
||||
PastebinLink pastebinLink = new PastebinLink(paste,
|
||||
pasteURL, new Date(pasteDate * 1000));
|
||||
pastebinLink.setHits(hits);
|
||||
|
||||
pastes.add(pastebinLink);
|
||||
}
|
||||
}
|
||||
pastes.add(pastebinLink);
|
||||
}
|
||||
}
|
||||
|
||||
return pastes.toArray(new PastebinLink[pastes.size()]);
|
||||
} catch (Exception e) {
|
||||
throw new ParseException("Failed to parse pastes: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
return pastes.toArray(new PastebinLink[pastes.size()]);
|
||||
} catch (Exception e) {
|
||||
throw new ParseException("Failed to parse pastes: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
throw new ParseException("Failed to parse pastes: " + response);
|
||||
}
|
||||
throw new ParseException("Failed to parse pastes: " + response);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,174 +11,165 @@ import java.net.URL;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
* A representation of an online pastebin paste.
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* <p>
|
||||
* This class represents an existing pastebin paste URL
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public class PastebinLink extends AbstractPasteLink {
|
||||
private PastebinPaste paste;
|
||||
private URL link;
|
||||
private int hits;
|
||||
private String key;
|
||||
private Date pasteDate;
|
||||
private PastebinPaste paste;
|
||||
private URL link;
|
||||
private int hits;
|
||||
private String key;
|
||||
private Date pasteDate;
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinLink</code> object, representing an existing
|
||||
* paste
|
||||
*
|
||||
* @param paste
|
||||
* paste details
|
||||
* @param url
|
||||
* link to paste
|
||||
*/
|
||||
public PastebinLink(PastebinPaste paste, URL url) {
|
||||
this(paste, url, new Date((System.currentTimeMillis() / 1000) * 1000));
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinLink</code> object, representing an existing
|
||||
* paste
|
||||
*
|
||||
* @param paste paste details
|
||||
* @param url link to paste
|
||||
*/
|
||||
public PastebinLink(PastebinPaste paste, URL url) {
|
||||
this(paste, url, new Date((System.currentTimeMillis() / 1000) * 1000));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinLink</code> object, representing an existing
|
||||
* paste
|
||||
*
|
||||
* @param paste
|
||||
* paste details
|
||||
* @param url
|
||||
* link to paste
|
||||
* @param pasteDate
|
||||
* date the paste has been pasted
|
||||
*/
|
||||
public PastebinLink(PastebinPaste paste, URL url, Date pasteDate) {
|
||||
this.paste = paste;
|
||||
this.link = url;
|
||||
this.pasteDate = pasteDate;
|
||||
try {
|
||||
this.key = url.toURI().getPath().substring(1);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinLink</code> object, representing an existing
|
||||
* paste
|
||||
*
|
||||
* @param paste paste details
|
||||
* @param url link to paste
|
||||
* @param pasteDate date the paste has been pasted
|
||||
*/
|
||||
public PastebinLink(PastebinPaste paste, URL url, Date pasteDate) {
|
||||
this.paste = paste;
|
||||
this.link = url;
|
||||
this.pasteDate = pasteDate;
|
||||
try {
|
||||
this.key = url.toURI().getPath().substring(1);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public URL getLink() {
|
||||
return link;
|
||||
}
|
||||
/**
|
||||
* Fetches a paste text from pastebin
|
||||
*
|
||||
* @param pasteKey the unique paste key
|
||||
* @return contents of the paste
|
||||
*/
|
||||
public static String getContents(String pasteKey) {
|
||||
return Web.getContents("http://pastebin.com/raw.php?i=" + pasteKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets pastebin paste details
|
||||
*
|
||||
* @return paste details
|
||||
*/
|
||||
public PastebinPaste getPaste() {
|
||||
return paste;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets pastebin unique paste key
|
||||
* @return unique paste key
|
||||
*/
|
||||
public String getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the pastebin link content
|
||||
* <p>
|
||||
* After parsing use the following methods: {@link #getPaste()} {@link PastebinPaste#getContents()}
|
||||
*/
|
||||
public void fetchContent() {
|
||||
if(getPaste().getContents() != null) {
|
||||
throw new IllegalStateException("Contents already fetched.");
|
||||
}
|
||||
getPaste().setContents(getContents(getKey()));
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public URL getLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the paste page hits
|
||||
*
|
||||
* @param hits
|
||||
* amount of times paste has been visited
|
||||
*/
|
||||
public void setHits(int hits) {
|
||||
if (hits < 0) {
|
||||
throw new IllegalArgumentException("Hits must be positive: " + hits);
|
||||
}
|
||||
this.hits = hits;
|
||||
}
|
||||
/**
|
||||
* Gets pastebin paste details
|
||||
*
|
||||
* @return paste details
|
||||
*/
|
||||
public PastebinPaste getPaste() {
|
||||
return paste;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the paste page hits
|
||||
*
|
||||
* @return paste page hits
|
||||
*/
|
||||
public int getHits() {
|
||||
return this.hits;
|
||||
}
|
||||
/**
|
||||
* Gets pastebin unique paste key
|
||||
*
|
||||
* @return unique paste key
|
||||
*/
|
||||
public String getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the paste date
|
||||
*
|
||||
* @return paste date
|
||||
*/
|
||||
public Date getPasteDate() {
|
||||
return this.pasteDate;
|
||||
}
|
||||
/**
|
||||
* Fetches the pastebin link content
|
||||
* <p>
|
||||
* After parsing use the following methods: {@link #getPaste()} {@link PastebinPaste#getContents()}
|
||||
*/
|
||||
public void fetchContent() {
|
||||
if (getPaste().getContents() != null) {
|
||||
throw new IllegalStateException("Contents already fetched.");
|
||||
}
|
||||
getPaste().setContents(getContents(getKey()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes this paste
|
||||
*
|
||||
* @param account
|
||||
* the account which was used to create this paste
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @throws PasteException if it failed to delete the paste
|
||||
*/
|
||||
public void delete(String developerKey, PastebinAccount account) throws PasteException {
|
||||
if(developerKey == null || developerKey.isEmpty()) {
|
||||
throw new IllegalArgumentException("Developer key can't be null or empty.");
|
||||
}
|
||||
if(account.getUserSessionId() == null || account.getUserSessionId().isEmpty()) {
|
||||
throw new IllegalArgumentException("Account user session id is missing.");
|
||||
}
|
||||
Post post = new Post();
|
||||
post.put("api_dev_key", developerKey);
|
||||
post.put("api_user_key", account.getUserSessionId());
|
||||
post.put("api_paste_key", getKey());
|
||||
post.put("api_option", "delete");
|
||||
|
||||
String response = Web.getContents(Pastebin.API_POST_LINK, post);
|
||||
if(response.equals("Paste Removed")) {
|
||||
return;
|
||||
}
|
||||
throw new PasteException("Failed to delete paste: " + response);
|
||||
}
|
||||
/**
|
||||
* Gets the paste page hits
|
||||
*
|
||||
* @return paste page hits
|
||||
*/
|
||||
public int getHits() {
|
||||
return this.hits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes this paste
|
||||
* @throws PasteException if it failed to delete the paste
|
||||
*/
|
||||
public void delete() throws PasteException {
|
||||
delete(getPaste().getDeveloperKey(), getPaste().getAccount());
|
||||
}
|
||||
/**
|
||||
* Sets the paste page hits
|
||||
*
|
||||
* @param hits amount of times paste has been visited
|
||||
*/
|
||||
public void setHits(int hits) {
|
||||
if (hits < 0) {
|
||||
throw new IllegalArgumentException("Hits must be positive: " + hits);
|
||||
}
|
||||
this.hits = hits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a paste text from pastebin
|
||||
*
|
||||
* @param pasteKey
|
||||
* the unique paste key
|
||||
* @return contents of the paste
|
||||
*/
|
||||
public static String getContents(String pasteKey) {
|
||||
return Web.getContents("http://pastebin.com/raw.php?i=" + pasteKey);
|
||||
}
|
||||
/**
|
||||
* Gets the paste date
|
||||
*
|
||||
* @return paste date
|
||||
*/
|
||||
public Date getPasteDate() {
|
||||
return this.pasteDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes this paste
|
||||
*
|
||||
* @param account the account which was used to create this paste
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @throws PasteException if it failed to delete the paste
|
||||
*/
|
||||
public void delete(String developerKey, PastebinAccount account) throws PasteException {
|
||||
if (developerKey == null || developerKey.isEmpty()) {
|
||||
throw new IllegalArgumentException("Developer key can't be null or empty.");
|
||||
}
|
||||
if (account.getUserSessionId() == null || account.getUserSessionId().isEmpty()) {
|
||||
throw new IllegalArgumentException("Account user session id is missing.");
|
||||
}
|
||||
Post post = new Post();
|
||||
post.put("api_dev_key", developerKey);
|
||||
post.put("api_user_key", account.getUserSessionId());
|
||||
post.put("api_paste_key", getKey());
|
||||
post.put("api_option", "delete");
|
||||
|
||||
String response = Web.getContents(Pastebin.API_POST_LINK, post);
|
||||
if (response.equals("Paste Removed")) {
|
||||
return;
|
||||
}
|
||||
throw new PasteException("Failed to delete paste: " + response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes this paste
|
||||
*
|
||||
* @throws PasteException if it failed to delete the paste
|
||||
*/
|
||||
public void delete() throws PasteException {
|
||||
delete(getPaste().getDeveloperKey(), getPaste().getAccount());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,303 +10,289 @@ import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
*
|
||||
* A representation of a new or existing paste.
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* This class holds the contents of the paste itself. You can get and modify
|
||||
* settings and then 'push' this paste onto <a
|
||||
* href="http://pastebin.com/">pastebin.</a>
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* This class has been programmed with the help of the <a
|
||||
* href="http://pastebin.com/api/">pastebin API manual.</a>
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public class PastebinPaste extends AbstractPaste<PastebinLink> {
|
||||
/**
|
||||
* Makes a paste public.
|
||||
*/
|
||||
public static final int VISIBILITY_PUBLIC = 0;
|
||||
/**
|
||||
* Makes a paste unlisted.
|
||||
*/
|
||||
public static final int VISIBILITY_UNLISTED = 1;
|
||||
/**
|
||||
* Makes a paste private.
|
||||
* <p>
|
||||
* Requires an {@link PastebinAccount
|
||||
* org.jpaste.pastebin.account.PastebinAccount PastebinAccount}
|
||||
* </p>
|
||||
*/
|
||||
public static final int VISIBILITY_PRIVATE = 2;
|
||||
private String developerKey;
|
||||
private PastebinAccount account;
|
||||
private String pasteTitle;
|
||||
private String pasteFormat;
|
||||
private PasteExpireDate expireDate;
|
||||
private int visibility;
|
||||
/**
|
||||
* Makes a paste public.
|
||||
*/
|
||||
public static final int VISIBILITY_PUBLIC = 0;
|
||||
/**
|
||||
* Makes a paste unlisted.
|
||||
*/
|
||||
public static final int VISIBILITY_UNLISTED = 1;
|
||||
/**
|
||||
* Makes a paste private.
|
||||
* <p>
|
||||
* Requires an {@link PastebinAccount
|
||||
* org.jpaste.pastebin.account.PastebinAccount PastebinAccount}
|
||||
* </p>
|
||||
*/
|
||||
public static final int VISIBILITY_PRIVATE = 2;
|
||||
private String developerKey;
|
||||
private PastebinAccount account;
|
||||
private String pasteTitle;
|
||||
private String pasteFormat;
|
||||
private PasteExpireDate expireDate;
|
||||
private int visibility;
|
||||
|
||||
/**
|
||||
* Creates a new empty <code>PastebinPaste</code> instance.
|
||||
*/
|
||||
public PastebinPaste() {
|
||||
this(null, null, null);
|
||||
}
|
||||
/**
|
||||
* Creates a new empty <code>PastebinPaste</code> instance.
|
||||
*/
|
||||
public PastebinPaste() {
|
||||
this(null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinPaste</code> instance.
|
||||
*
|
||||
* @param contents
|
||||
* the paste contents
|
||||
*/
|
||||
public PastebinPaste(String contents) {
|
||||
this(null, contents, null);
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinPaste</code> instance.
|
||||
*
|
||||
* @param contents the paste contents
|
||||
*/
|
||||
public PastebinPaste(String contents) {
|
||||
this(null, contents, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinPaste</code> instance.
|
||||
*
|
||||
* @param account
|
||||
* a pastebin account
|
||||
*/
|
||||
public PastebinPaste(PastebinAccount account) {
|
||||
this(account.getDeveloperKey(), null, account);
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinPaste</code> instance.
|
||||
*
|
||||
* @param account a pastebin account
|
||||
*/
|
||||
public PastebinPaste(PastebinAccount account) {
|
||||
this(account.getDeveloperKey(), null, account);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinPaste</code> instance.
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents
|
||||
* the contents of the paste
|
||||
*/
|
||||
public PastebinPaste(String developerKey, String contents) {
|
||||
this(developerKey, contents, null);
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinPaste</code> instance.
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents the contents of the paste
|
||||
*/
|
||||
public PastebinPaste(String developerKey, String contents) {
|
||||
this(developerKey, contents, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinPaste</code> instance.
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents
|
||||
* the contents of the paste
|
||||
* @param account
|
||||
* a pastebin account
|
||||
*/
|
||||
public PastebinPaste(String developerKey, String contents,
|
||||
PastebinAccount account) {
|
||||
super(contents);
|
||||
this.developerKey = developerKey;
|
||||
this.account = account;
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinPaste</code> instance.
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param contents the contents of the paste
|
||||
* @param account a pastebin account
|
||||
*/
|
||||
public PastebinPaste(String developerKey, String contents,
|
||||
PastebinAccount account) {
|
||||
super(contents);
|
||||
this.developerKey = developerKey;
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pastebin account If you set an account the pastes will be listed
|
||||
* on your account.
|
||||
*
|
||||
* @param account
|
||||
* a pastebin account
|
||||
*/
|
||||
public void setAccount(PastebinAccount account) {
|
||||
this.account = account;
|
||||
}
|
||||
/**
|
||||
* Gets the pastebin account
|
||||
*
|
||||
* @return pastebin account
|
||||
*/
|
||||
public PastebinAccount getAccount() {
|
||||
return this.account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the pastebin account
|
||||
*
|
||||
* @return pastebin account
|
||||
*/
|
||||
public PastebinAccount getAccount() {
|
||||
return this.account;
|
||||
}
|
||||
/**
|
||||
* Sets the pastebin account If you set an account the pastes will be listed
|
||||
* on your account.
|
||||
*
|
||||
* @param account a pastebin account
|
||||
*/
|
||||
public void setAccount(PastebinAccount account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the developer key The developer key is required to paste contents on
|
||||
* pastebin
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
*/
|
||||
public void setDeveloperKey(String developerKey) {
|
||||
if (developerKey == null || developerKey.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Developer key can not be null or empty.");
|
||||
}
|
||||
this.developerKey = developerKey;
|
||||
}
|
||||
/**
|
||||
* Gets the developer key
|
||||
*
|
||||
* @return developer key
|
||||
*/
|
||||
public String getDeveloperKey() {
|
||||
return this.developerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the paste expire date
|
||||
*
|
||||
* @param date
|
||||
* date when the paste will be removed
|
||||
*/
|
||||
public void setPasteExpireDate(PasteExpireDate date) {
|
||||
this.expireDate = date;
|
||||
}
|
||||
/**
|
||||
* Sets the developer key The developer key is required to paste contents on
|
||||
* pastebin
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
*/
|
||||
public void setDeveloperKey(String developerKey) {
|
||||
if (developerKey == null || developerKey.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Developer key can not be null or empty.");
|
||||
}
|
||||
this.developerKey = developerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the developer key
|
||||
*
|
||||
* @return developer key
|
||||
*/
|
||||
public String getDeveloperKey() {
|
||||
return this.developerKey;
|
||||
}
|
||||
/**
|
||||
* Gets paste title
|
||||
*
|
||||
* @return paste title
|
||||
*/
|
||||
public String getPasteTitle() {
|
||||
return this.pasteTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the paste title
|
||||
*
|
||||
* @param title
|
||||
* title of the paste
|
||||
*/
|
||||
public void setPasteTitle(String title) {
|
||||
this.pasteTitle = title;
|
||||
}
|
||||
/**
|
||||
* Sets the paste title
|
||||
*
|
||||
* @param title title of the paste
|
||||
*/
|
||||
public void setPasteTitle(String title) {
|
||||
this.pasteTitle = title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets paste title
|
||||
*
|
||||
* @return paste title
|
||||
*/
|
||||
public String getPasteTitle() {
|
||||
return this.pasteTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets paste expire date
|
||||
* @return paste expire date
|
||||
*/
|
||||
public PasteExpireDate getPasteExpireDate() {
|
||||
return this.expireDate;
|
||||
}
|
||||
/**
|
||||
* Gets paste expire date
|
||||
*
|
||||
* @return paste expire date
|
||||
*/
|
||||
public PasteExpireDate getPasteExpireDate() {
|
||||
return this.expireDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the paste format The format is used for syntax highlighting
|
||||
*
|
||||
* @see <a href="http://pastebin.com/api#5">available syntax highlighting
|
||||
* formats</a>
|
||||
* @param format
|
||||
* format of the paste
|
||||
*/
|
||||
public void setPasteFormat(String format) {
|
||||
this.pasteFormat = format;
|
||||
}
|
||||
/**
|
||||
* Sets the paste expire date
|
||||
*
|
||||
* @param date date when the paste will be removed
|
||||
*/
|
||||
public void setPasteExpireDate(PasteExpireDate date) {
|
||||
this.expireDate = date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets paste format
|
||||
*
|
||||
* @return paste format
|
||||
*/
|
||||
public String getPasteFormat() {
|
||||
return this.pasteFormat;
|
||||
}
|
||||
/**
|
||||
* Gets paste format
|
||||
*
|
||||
* @return paste format
|
||||
*/
|
||||
public String getPasteFormat() {
|
||||
return this.pasteFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes this paste private, unlisted or public Default visibility is public
|
||||
* <p>
|
||||
* <strong>Valid visibilities</strong>
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_PUBLIC}
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_UNLISTED}
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_PRIVATE}
|
||||
* </p>
|
||||
*
|
||||
* @param visibility
|
||||
* the paste it's visibility
|
||||
*/
|
||||
public void setVisibility(int visibility) {
|
||||
if (visibility < 0 || visibility > 2) {
|
||||
throw new IllegalArgumentException("Unknown visibility: "
|
||||
+ visibility);
|
||||
}
|
||||
this.visibility = visibility;
|
||||
}
|
||||
/**
|
||||
* Sets the paste format The format is used for syntax highlighting
|
||||
*
|
||||
* @param format format of the paste
|
||||
* @see <a href="http://pastebin.com/api#5">available syntax highlighting
|
||||
* formats</a>
|
||||
*/
|
||||
public void setPasteFormat(String format) {
|
||||
this.pasteFormat = format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets this paste visibility
|
||||
* <p>
|
||||
* <strong>Valid visibilities</strong>
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_PUBLIC}
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_UNLISTED}
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_PRIVATE}
|
||||
* </p>
|
||||
*
|
||||
* @return visibility of this paste
|
||||
*/
|
||||
public int getVisibility() {
|
||||
return this.visibility;
|
||||
}
|
||||
/**
|
||||
* Gets this paste visibility
|
||||
* <p>
|
||||
* <strong>Valid visibilities</strong>
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_PUBLIC}
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_UNLISTED}
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_PRIVATE}
|
||||
* </p>
|
||||
*
|
||||
* @return visibility of this paste
|
||||
*/
|
||||
public int getVisibility() {
|
||||
return this.visibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public PastebinLink paste() throws PasteException {
|
||||
if (getContents() == null || getContents().isEmpty()) {
|
||||
throw new IllegalStateException("Paste can not be null or empty.");
|
||||
}
|
||||
if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) {
|
||||
throw new IllegalStateException("Developer key is missing.");
|
||||
}
|
||||
/**
|
||||
* Makes this paste private, unlisted or public Default visibility is public
|
||||
* <p>
|
||||
* <strong>Valid visibilities</strong>
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_PUBLIC}
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_UNLISTED}
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link PastebinPaste#VISIBILITY_PRIVATE}
|
||||
* </p>
|
||||
*
|
||||
* @param visibility the paste it's visibility
|
||||
*/
|
||||
public void setVisibility(int visibility) {
|
||||
if (visibility < 0 || visibility > 2) {
|
||||
throw new IllegalArgumentException("Unknown visibility: "
|
||||
+ visibility);
|
||||
}
|
||||
this.visibility = visibility;
|
||||
}
|
||||
|
||||
Post post = new Post();
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public PastebinLink paste() throws PasteException {
|
||||
if (getContents() == null || getContents().isEmpty()) {
|
||||
throw new IllegalStateException("Paste can not be null or empty.");
|
||||
}
|
||||
if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) {
|
||||
throw new IllegalStateException("Developer key is missing.");
|
||||
}
|
||||
|
||||
// required parameters
|
||||
post.put("api_dev_key", getDeveloperKey());
|
||||
post.put("api_option", "paste");
|
||||
post.put("api_paste_code", getContents());
|
||||
Post post = new Post();
|
||||
|
||||
// optional parameters
|
||||
if (this.account != null && this.account.getUserSessionId() != null) {
|
||||
post.put("api_user_key", this.account.getUserSessionId());
|
||||
}
|
||||
if (this.pasteTitle != null) {
|
||||
post.put("api_paste_name", getPasteTitle());
|
||||
}
|
||||
if (this.pasteFormat != null) {
|
||||
post.put("api_paste_format", getPasteFormat());
|
||||
}
|
||||
post.put("api_paste_private", Integer.toString(getVisibility()));
|
||||
if (this.expireDate != null) {
|
||||
post.put("api_paste_expire_date", expireDate.getValue());
|
||||
}
|
||||
// required parameters
|
||||
post.put("api_dev_key", getDeveloperKey());
|
||||
post.put("api_option", "paste");
|
||||
post.put("api_paste_code", getContents());
|
||||
|
||||
try {
|
||||
String pageResponse = Web.getContents(Pastebin.API_POST_LINK, post);
|
||||
if (pageResponse.startsWith("http")) {
|
||||
// success
|
||||
PastebinLink result = new PastebinLink(this, new URL(
|
||||
pageResponse));
|
||||
return result;
|
||||
}
|
||||
throw new PasteException("Failed to generate paste: "
|
||||
+ pageResponse);
|
||||
} catch (MalformedURLException e) {
|
||||
// shouldn't happen
|
||||
throw new PasteException("Failed to generate paste: " + e);
|
||||
}
|
||||
}
|
||||
// optional parameters
|
||||
if (this.account != null && this.account.getUserSessionId() != null) {
|
||||
post.put("api_user_key", this.account.getUserSessionId());
|
||||
}
|
||||
if (this.pasteTitle != null) {
|
||||
post.put("api_paste_name", getPasteTitle());
|
||||
}
|
||||
if (this.pasteFormat != null) {
|
||||
post.put("api_paste_format", getPasteFormat());
|
||||
}
|
||||
post.put("api_paste_private", Integer.toString(getVisibility()));
|
||||
if (this.expireDate != null) {
|
||||
post.put("api_paste_expire_date", expireDate.getValue());
|
||||
}
|
||||
|
||||
try {
|
||||
String pageResponse = Web.getContents(Pastebin.API_POST_LINK, post);
|
||||
if (pageResponse.startsWith("http")) {
|
||||
// success
|
||||
PastebinLink result = new PastebinLink(this, new URL(
|
||||
pageResponse));
|
||||
return result;
|
||||
}
|
||||
throw new PasteException("Failed to generate paste: "
|
||||
+ pageResponse);
|
||||
} catch (MalformedURLException e) {
|
||||
// shouldn't happen
|
||||
throw new PasteException("Failed to generate paste: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,347 +23,330 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
* Represents an account on <a href="http://pastebin.com/">Pastebin</a>.
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* This class can fetch a non-expiring user session id. This session id is used
|
||||
* for generating private pasted, fetching user details, fetching user his
|
||||
* pastes, adding pastes to his account & more account based interactions.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* A reference manual for generating a user session id can be found <a
|
||||
* href="http://pastebin.com/api#8">here</a>.
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public class PastebinAccount {
|
||||
private String username, password, userSessionId, developerKey;
|
||||
private String username, password, userSessionId, developerKey;
|
||||
|
||||
/**
|
||||
* Creates a new empty <code>PastebinAccount</code> instance.
|
||||
*/
|
||||
public PastebinAccount() {
|
||||
this(null, null, null);
|
||||
}
|
||||
/**
|
||||
* Creates a new empty <code>PastebinAccount</code> instance.
|
||||
*/
|
||||
public PastebinAccount() {
|
||||
this(null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinAccount</code> instance.
|
||||
*
|
||||
* When you use this constructor, you'll need to use the {@link #login()} to
|
||||
* fetch an user session id
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param username
|
||||
* the username of the pastebin account
|
||||
* @param password
|
||||
* the password of the pastebin account
|
||||
*/
|
||||
public PastebinAccount(String developerKey, String username, String password) {
|
||||
this.developerKey = developerKey;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinAccount</code> instance.
|
||||
* <p>
|
||||
* When you use this constructor, you'll need to use the {@link #login()} to
|
||||
* fetch an user session id
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param username the username of the pastebin account
|
||||
* @param password the password of the pastebin account
|
||||
*/
|
||||
public PastebinAccount(String developerKey, String username, String password) {
|
||||
this.developerKey = developerKey;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinAccount</code> instance.
|
||||
*
|
||||
* @param userSessionId
|
||||
* the user session id of the pastebin account.
|
||||
*/
|
||||
public PastebinAccount(String userSessionId) {
|
||||
this(null, userSessionId);
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinAccount</code> instance.
|
||||
*
|
||||
* @param userSessionId the user session id of the pastebin account.
|
||||
*/
|
||||
public PastebinAccount(String userSessionId) {
|
||||
this(null, userSessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinAccount</code> instance.
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param userSessionId
|
||||
* the user session id of the pastebin account.
|
||||
*/
|
||||
public PastebinAccount(String developerKey, String userSessionId) {
|
||||
this.developerKey = developerKey;
|
||||
this.userSessionId = userSessionId;
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinAccount</code> instance.
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
* @param userSessionId the user session id of the pastebin account.
|
||||
*/
|
||||
public PastebinAccount(String developerKey, String userSessionId) {
|
||||
this.developerKey = developerKey;
|
||||
this.userSessionId = userSessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the user session id The user session id can be used to paste private
|
||||
* pastes and will add pastes to your account
|
||||
*
|
||||
* @param userSessionId
|
||||
* the user session id of the pastebin account
|
||||
*/
|
||||
public void setUserSessionId(String userSessionId) {
|
||||
this.userSessionId = userSessionId;
|
||||
}
|
||||
/**
|
||||
* Gets the user session id
|
||||
*
|
||||
* @return user session id
|
||||
*/
|
||||
public String getUserSessionId() {
|
||||
return this.userSessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user session id
|
||||
*
|
||||
* @return user session id
|
||||
*/
|
||||
public String getUserSessionId() {
|
||||
return this.userSessionId;
|
||||
}
|
||||
/**
|
||||
* Sets the user session id The user session id can be used to paste private
|
||||
* pastes and will add pastes to your account
|
||||
*
|
||||
* @param userSessionId the user session id of the pastebin account
|
||||
*/
|
||||
public void setUserSessionId(String userSessionId) {
|
||||
this.userSessionId = userSessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the username
|
||||
*
|
||||
* @param username
|
||||
* the username of the pastebin account
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
/**
|
||||
* Gets username of the pastebin account
|
||||
*
|
||||
* @return username of the pastebin account. Could be null if only an user
|
||||
* session is was provided.
|
||||
*/
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets username of the pastebin account
|
||||
*
|
||||
* @return username of the pastebin account. Could be null if only an user
|
||||
* session is was provided.
|
||||
*/
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
/**
|
||||
* Sets the username
|
||||
*
|
||||
* @param username the username of the pastebin account
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the password
|
||||
*
|
||||
* @param password
|
||||
* the password of the pastebin account
|
||||
*/
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
/**
|
||||
* Gets password of the pastebin account
|
||||
*
|
||||
* @return password of the pastebin account. Could be null if only an user
|
||||
* session id was provided.
|
||||
*/
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets password of the pastebin account
|
||||
*
|
||||
* @return password of the pastebin account. Could be null if only an user
|
||||
* session id was provided.
|
||||
*/
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
/**
|
||||
* Sets the password
|
||||
*
|
||||
* @param password the password of the pastebin account
|
||||
*/
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the developer key The developer key is required to paste contents on
|
||||
* pastebin
|
||||
*
|
||||
* @param developerKey
|
||||
* a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
*/
|
||||
public void setDeveloperKey(String developerKey) {
|
||||
if (developerKey == null || developerKey.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Developer key can not be null or empty.");
|
||||
}
|
||||
this.developerKey = developerKey;
|
||||
}
|
||||
/**
|
||||
* Gets the developer key
|
||||
*
|
||||
* @return developer key
|
||||
*/
|
||||
public String getDeveloperKey() {
|
||||
return this.developerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the developer key
|
||||
*
|
||||
* @return developer key
|
||||
*/
|
||||
public String getDeveloperKey() {
|
||||
return this.developerKey;
|
||||
}
|
||||
/**
|
||||
* Sets the developer key The developer key is required to paste contents on
|
||||
* pastebin
|
||||
*
|
||||
* @param developerKey a developer key which can be fetched from the pastebin API
|
||||
* page
|
||||
*/
|
||||
public void setDeveloperKey(String developerKey) {
|
||||
if (developerKey == null || developerKey.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Developer key can not be null or empty.");
|
||||
}
|
||||
this.developerKey = developerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches an user session id.
|
||||
*
|
||||
* @throws LoginException
|
||||
* if fetching the user session id failed
|
||||
*/
|
||||
public void login() throws LoginException {
|
||||
if (getUserSessionId() != null) {
|
||||
throw new IllegalStateException("Already logged in.");
|
||||
}
|
||||
if (getUsername() == null || getPassword() == null) {
|
||||
throw new IllegalStateException("Username or password null.");
|
||||
}
|
||||
if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) {
|
||||
throw new IllegalStateException("Developer key is missing.");
|
||||
}
|
||||
/**
|
||||
* Fetches an user session id.
|
||||
*
|
||||
* @throws LoginException if fetching the user session id failed
|
||||
*/
|
||||
public void login() throws LoginException {
|
||||
if (getUserSessionId() != null) {
|
||||
throw new IllegalStateException("Already logged in.");
|
||||
}
|
||||
if (getUsername() == null || getPassword() == null) {
|
||||
throw new IllegalStateException("Username or password null.");
|
||||
}
|
||||
if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) {
|
||||
throw new IllegalStateException("Developer key is missing.");
|
||||
}
|
||||
|
||||
Post post = new Post();
|
||||
Post post = new Post();
|
||||
|
||||
post.put("api_dev_key", getDeveloperKey());
|
||||
post.put("api_user_name", username);
|
||||
post.put("api_user_password", password);
|
||||
post.put("api_dev_key", getDeveloperKey());
|
||||
post.put("api_user_name", username);
|
||||
post.put("api_user_password", password);
|
||||
|
||||
String response = Web.getContents(Pastebin.API_LOGIN_LINK, post);
|
||||
if (response == null || response.isEmpty()) {
|
||||
throw new LoginException("Empty response from login API server.");
|
||||
}
|
||||
if (response.toLowerCase().startsWith("bad")) {
|
||||
throw new LoginException("Failed to login: " + response);
|
||||
}
|
||||
String response = Web.getContents(Pastebin.API_LOGIN_LINK, post);
|
||||
if (response == null || response.isEmpty()) {
|
||||
throw new LoginException("Empty response from login API server.");
|
||||
}
|
||||
if (response.toLowerCase().startsWith("bad")) {
|
||||
throw new LoginException("Failed to login: " + response);
|
||||
}
|
||||
|
||||
this.userSessionId = response;
|
||||
}
|
||||
this.userSessionId = response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all pasted pastes by this user
|
||||
*
|
||||
* @param limit
|
||||
* maximum amount of pastes to receive
|
||||
* <p>
|
||||
* <code>0 > limit > 1000</code>
|
||||
* </p>
|
||||
* @return all pasted pastes made by this user/account
|
||||
* @throws ParseException
|
||||
* if it failed to parse the pastes
|
||||
*/
|
||||
public PastebinLink[] getPastes(int limit) throws ParseException {
|
||||
if (limit > 1000) {
|
||||
limit = 1000;
|
||||
}
|
||||
if (limit < 1) {
|
||||
limit = 1;
|
||||
}
|
||||
if (getUserSessionId() == null) {
|
||||
throw new IllegalStateException("User session id missing.");
|
||||
}
|
||||
if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) {
|
||||
throw new IllegalStateException("Developer key is missing.");
|
||||
}
|
||||
/**
|
||||
* Gets all pasted pastes by this user
|
||||
*
|
||||
* @param limit maximum amount of pastes to receive
|
||||
* <p>
|
||||
* <code>0 > limit > 1000</code>
|
||||
* </p>
|
||||
* @return all pasted pastes made by this user/account
|
||||
* @throws ParseException if it failed to parse the pastes
|
||||
*/
|
||||
public PastebinLink[] getPastes(int limit) throws ParseException {
|
||||
if (limit > 1000) {
|
||||
limit = 1000;
|
||||
}
|
||||
if (limit < 1) {
|
||||
limit = 1;
|
||||
}
|
||||
if (getUserSessionId() == null) {
|
||||
throw new IllegalStateException("User session id missing.");
|
||||
}
|
||||
if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) {
|
||||
throw new IllegalStateException("Developer key is missing.");
|
||||
}
|
||||
|
||||
Post post = new Post();
|
||||
Post post = new Post();
|
||||
|
||||
post.put("api_dev_key", getDeveloperKey());
|
||||
post.put("api_user_key", getUserSessionId());
|
||||
post.put("api_results_limit", Integer.toString(limit));
|
||||
post.put("api_option", "list");
|
||||
post.put("api_dev_key", getDeveloperKey());
|
||||
post.put("api_user_key", getUserSessionId());
|
||||
post.put("api_results_limit", Integer.toString(limit));
|
||||
post.put("api_option", "list");
|
||||
|
||||
String response = Web.getContents(Pastebin.API_POST_LINK, post);
|
||||
if (response.equals("No pastes found.")) {
|
||||
return null;
|
||||
}
|
||||
String response = Web.getContents(Pastebin.API_POST_LINK, post);
|
||||
if (response.equals("No pastes found.")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (response.startsWith("<paste>")) {
|
||||
// success
|
||||
try {
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
|
||||
.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
response = "<dummy>" + response + "</dummy>"; // requires root
|
||||
// element
|
||||
Document doc = dBuilder.parse(new InputSource(
|
||||
new ByteArrayInputStream(response.getBytes("utf-8"))));
|
||||
doc.getDocumentElement().normalize();
|
||||
if (response.startsWith("<paste>")) {
|
||||
// success
|
||||
try {
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
|
||||
.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
response = "<dummy>" + response + "</dummy>"; // requires root
|
||||
// element
|
||||
Document doc = dBuilder.parse(new InputSource(
|
||||
new ByteArrayInputStream(response.getBytes("utf-8"))));
|
||||
doc.getDocumentElement().normalize();
|
||||
|
||||
NodeList nodes = doc.getElementsByTagName("paste");
|
||||
ArrayList<PastebinLink> pastes = new ArrayList<PastebinLink>(
|
||||
nodes.getLength());
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
Node node = nodes.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
NodeList nodes = doc.getElementsByTagName("paste");
|
||||
ArrayList<PastebinLink> pastes = new ArrayList<PastebinLink>(
|
||||
nodes.getLength());
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
Node node = nodes.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
|
||||
String pasteFormat = XMLUtils.getText(element,
|
||||
"paste_format_short");
|
||||
String title = XMLUtils.getText(element, "paste_title");
|
||||
int visibility = Integer.parseInt(XMLUtils.getText(
|
||||
element, "paste_private"));
|
||||
int hits = Integer.parseInt(XMLUtils.getText(element,
|
||||
"paste_hits"));
|
||||
String pasteFormat = XMLUtils.getText(element,
|
||||
"paste_format_short");
|
||||
String title = XMLUtils.getText(element, "paste_title");
|
||||
int visibility = Integer.parseInt(XMLUtils.getText(
|
||||
element, "paste_private"));
|
||||
int hits = Integer.parseInt(XMLUtils.getText(element,
|
||||
"paste_hits"));
|
||||
|
||||
long expireDate = Long.parseLong(XMLUtils.getText(
|
||||
element, "paste_expire_date"));
|
||||
long pasteDate = Long.parseLong(XMLUtils.getText(
|
||||
element, "paste_date"));
|
||||
long expireDate = Long.parseLong(XMLUtils.getText(
|
||||
element, "paste_expire_date"));
|
||||
long pasteDate = Long.parseLong(XMLUtils.getText(
|
||||
element, "paste_date"));
|
||||
|
||||
URL pasteURL = new URL(XMLUtils.getText(element,
|
||||
"paste_url"));
|
||||
URL pasteURL = new URL(XMLUtils.getText(element,
|
||||
"paste_url"));
|
||||
|
||||
PastebinPaste paste = new PastebinPaste(this);
|
||||
paste.setPasteFormat(pasteFormat);
|
||||
paste.setPasteTitle(title);
|
||||
paste.setVisibility(visibility);
|
||||
paste.setPasteExpireDate(expireDate == 0L ? PasteExpireDate.NEVER
|
||||
: PasteExpireDate
|
||||
.getExpireDate((int) (expireDate - pasteDate)));
|
||||
PastebinPaste paste = new PastebinPaste(this);
|
||||
paste.setPasteFormat(pasteFormat);
|
||||
paste.setPasteTitle(title);
|
||||
paste.setVisibility(visibility);
|
||||
paste.setPasteExpireDate(expireDate == 0L ? PasteExpireDate.NEVER
|
||||
: PasteExpireDate
|
||||
.getExpireDate((int) (expireDate - pasteDate)));
|
||||
|
||||
PastebinLink pastebinLink = new PastebinLink(paste,
|
||||
pasteURL, new Date(pasteDate * 1000));
|
||||
pastebinLink.setHits(hits);
|
||||
PastebinLink pastebinLink = new PastebinLink(paste,
|
||||
pasteURL, new Date(pasteDate * 1000));
|
||||
pastebinLink.setHits(hits);
|
||||
|
||||
pastes.add(pastebinLink);
|
||||
}
|
||||
}
|
||||
pastes.add(pastebinLink);
|
||||
}
|
||||
}
|
||||
|
||||
return pastes.toArray(new PastebinLink[pastes.size()]);
|
||||
} catch (Exception e) {
|
||||
throw new ParseException("Failed to parse pastes: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
return pastes.toArray(new PastebinLink[pastes.size()]);
|
||||
} catch (Exception e) {
|
||||
throw new ParseException("Failed to parse pastes: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
throw new ParseException("Failed to parse pastes: " + response);
|
||||
}
|
||||
throw new ParseException("Failed to parse pastes: " + response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets pasted pastes (max 50) by this user
|
||||
*
|
||||
* @return all pasted pastes made by this user/account
|
||||
* @throws ParseException
|
||||
* if it failed to parse the pastes
|
||||
*/
|
||||
public PastebinLink[] getPastes() throws ParseException {
|
||||
return getPastes(50);
|
||||
}
|
||||
/**
|
||||
* Gets pasted pastes (max 50) by this user
|
||||
*
|
||||
* @return all pasted pastes made by this user/account
|
||||
* @throws ParseException if it failed to parse the pastes
|
||||
*/
|
||||
public PastebinLink[] getPastes() throws ParseException {
|
||||
return getPastes(50);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the account details of this account
|
||||
*
|
||||
* @return account details
|
||||
* @throws ParseException
|
||||
* if it failed to parse the account details
|
||||
*/
|
||||
public PastebinAccountDetails getAccountDetails() throws ParseException {
|
||||
if (getUserSessionId() == null) {
|
||||
throw new IllegalStateException("User session id missing.");
|
||||
}
|
||||
if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) {
|
||||
throw new IllegalStateException("Developer key is missing.");
|
||||
}
|
||||
/**
|
||||
* Fetches the account details of this account
|
||||
*
|
||||
* @return account details
|
||||
* @throws ParseException if it failed to parse the account details
|
||||
*/
|
||||
public PastebinAccountDetails getAccountDetails() throws ParseException {
|
||||
if (getUserSessionId() == null) {
|
||||
throw new IllegalStateException("User session id missing.");
|
||||
}
|
||||
if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) {
|
||||
throw new IllegalStateException("Developer key is missing.");
|
||||
}
|
||||
|
||||
Post post = new Post();
|
||||
post.put("api_dev_key", getDeveloperKey());
|
||||
post.put("api_user_key", getUserSessionId());
|
||||
post.put("api_option", "userdetails");
|
||||
Post post = new Post();
|
||||
post.put("api_dev_key", getDeveloperKey());
|
||||
post.put("api_user_key", getUserSessionId());
|
||||
post.put("api_option", "userdetails");
|
||||
|
||||
String response = Web.getContents(Pastebin.API_POST_LINK, post);
|
||||
String response = Web.getContents(Pastebin.API_POST_LINK, post);
|
||||
|
||||
if (!response.startsWith("<user>")) {
|
||||
throw new ParseException("Failed to parse account details: "
|
||||
+ response);
|
||||
}
|
||||
if (!response.startsWith("<user>")) {
|
||||
throw new ParseException("Failed to parse account details: "
|
||||
+ response);
|
||||
}
|
||||
|
||||
try {
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
|
||||
.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
Document doc = dBuilder.parse(new InputSource(
|
||||
new ByteArrayInputStream(response.getBytes("utf-8"))));
|
||||
doc.getDocumentElement().normalize();
|
||||
try {
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
|
||||
.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
Document doc = dBuilder.parse(new InputSource(
|
||||
new ByteArrayInputStream(response.getBytes("utf-8"))));
|
||||
doc.getDocumentElement().normalize();
|
||||
|
||||
return new PastebinAccountDetails((Element) doc
|
||||
.getElementsByTagName("user").item(0));
|
||||
} catch (Exception e) {
|
||||
throw new ParseException("Failed to parse account details: " + e);
|
||||
}
|
||||
}
|
||||
return new PastebinAccountDetails((Element) doc
|
||||
.getElementsByTagName("user").item(0));
|
||||
} catch (Exception e) {
|
||||
throw new ParseException("Failed to parse account details: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,125 +10,132 @@ import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
*
|
||||
* Holds information and settings of a pastebin account.
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public class PastebinAccountDetails {
|
||||
private String username, format, expiration, avatarURL, website, email, location;
|
||||
private int userPrivate, accountType;
|
||||
private String username, format, expiration, avatarURL, website, email, location;
|
||||
private int userPrivate, accountType;
|
||||
|
||||
/**
|
||||
* Creates a new <code>PastebinAccountDetails</code> instance.
|
||||
*
|
||||
* @param userElement
|
||||
* the 'user' xml elements received from the pastebin API
|
||||
*/
|
||||
public PastebinAccountDetails(Element userElement) {
|
||||
this.username = XMLUtils.getText(userElement, "user_name");
|
||||
this.format = XMLUtils.getText(userElement, "user_format_short");
|
||||
this.expiration = XMLUtils.getText(userElement, "user_expiration");
|
||||
this.avatarURL = XMLUtils.getText(userElement, "user_avatar_url");
|
||||
this.userPrivate = Integer.parseInt(XMLUtils.getText(userElement, "user_private"));
|
||||
this.website = XMLUtils.getText(userElement, "user_website");
|
||||
this.email = XMLUtils.getText(userElement, "user_email");
|
||||
this.location = XMLUtils.getText(userElement, "user_location");
|
||||
this.accountType = Integer.parseInt(XMLUtils.getText(userElement, "user_account_type"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the username of this account
|
||||
* @return username
|
||||
*/
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets user text format
|
||||
* @return user text format
|
||||
*/
|
||||
public String getFormat() {
|
||||
return this.format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets this account expiration time
|
||||
* <p>
|
||||
* <code>N = never (default)</code>
|
||||
* </p>
|
||||
* @return account expiration time
|
||||
*/
|
||||
public String getExpiration() {
|
||||
return this.expiration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets URL to avatar image
|
||||
* @return URL to avatar image
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public URL getAvatarURL() throws MalformedURLException {
|
||||
return new URL(this.avatarURL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets user visibility
|
||||
* <pre>
|
||||
* 0 = public
|
||||
* 1 = unlisted
|
||||
* 2 = private
|
||||
* </pre>
|
||||
* @return visibility of account
|
||||
*/
|
||||
public int getPrivate() {
|
||||
return this.userPrivate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user's set website
|
||||
* @return url to website
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public URL getWebsite() throws MalformedURLException {
|
||||
if(this.website.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return new URL(this.website);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user e-mail
|
||||
* @return user account e-mail
|
||||
*/
|
||||
public String getEmail() {
|
||||
return this.email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user's set location
|
||||
* @return location, city
|
||||
*/
|
||||
public String getLocation() {
|
||||
return this.location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if this account is a 'pro' account
|
||||
* @return <code>true</code> if this account is a pro account, otherwise <code>false</code>.
|
||||
*/
|
||||
public boolean isPro() {
|
||||
return accountType == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the user his avatar from {@link #getAvatarURL()}
|
||||
* @return image
|
||||
* @throws IOException if image was not read
|
||||
*/
|
||||
public BufferedImage getAvatar() throws IOException {
|
||||
return ImageIO.read(getAvatarURL());
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>PastebinAccountDetails</code> instance.
|
||||
*
|
||||
* @param userElement the 'user' xml elements received from the pastebin API
|
||||
*/
|
||||
public PastebinAccountDetails(Element userElement) {
|
||||
this.username = XMLUtils.getText(userElement, "user_name");
|
||||
this.format = XMLUtils.getText(userElement, "user_format_short");
|
||||
this.expiration = XMLUtils.getText(userElement, "user_expiration");
|
||||
this.avatarURL = XMLUtils.getText(userElement, "user_avatar_url");
|
||||
this.userPrivate = Integer.parseInt(XMLUtils.getText(userElement, "user_private"));
|
||||
this.website = XMLUtils.getText(userElement, "user_website");
|
||||
this.email = XMLUtils.getText(userElement, "user_email");
|
||||
this.location = XMLUtils.getText(userElement, "user_location");
|
||||
this.accountType = Integer.parseInt(XMLUtils.getText(userElement, "user_account_type"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the username of this account
|
||||
*
|
||||
* @return username
|
||||
*/
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets user text format
|
||||
*
|
||||
* @return user text format
|
||||
*/
|
||||
public String getFormat() {
|
||||
return this.format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets this account expiration time
|
||||
* <p>
|
||||
* <code>N = never (default)</code>
|
||||
* </p>
|
||||
*
|
||||
* @return account expiration time
|
||||
*/
|
||||
public String getExpiration() {
|
||||
return this.expiration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets URL to avatar image
|
||||
*
|
||||
* @return URL to avatar image
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public URL getAvatarURL() throws MalformedURLException {
|
||||
return new URL(this.avatarURL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets user visibility
|
||||
* <pre>
|
||||
* 0 = public
|
||||
* 1 = unlisted
|
||||
* 2 = private
|
||||
* </pre>
|
||||
*
|
||||
* @return visibility of account
|
||||
*/
|
||||
public int getPrivate() {
|
||||
return this.userPrivate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user's set website
|
||||
*
|
||||
* @return url to website
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public URL getWebsite() throws MalformedURLException {
|
||||
if (this.website.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return new URL(this.website);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user e-mail
|
||||
*
|
||||
* @return user account e-mail
|
||||
*/
|
||||
public String getEmail() {
|
||||
return this.email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user's set location
|
||||
*
|
||||
* @return location, city
|
||||
*/
|
||||
public String getLocation() {
|
||||
return this.location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if this account is a 'pro' account
|
||||
*
|
||||
* @return <code>true</code> if this account is a pro account, otherwise <code>false</code>.
|
||||
*/
|
||||
public boolean isPro() {
|
||||
return accountType == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the user his avatar from {@link #getAvatarURL()}
|
||||
*
|
||||
* @return image
|
||||
* @throws IOException if image was not read
|
||||
*/
|
||||
public BufferedImage getAvatar() throws IOException {
|
||||
return ImageIO.read(getAvatarURL());
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
package org.jpaste.pastebin.exceptions;
|
||||
|
||||
public class LoginException extends Exception {
|
||||
private static final long serialVersionUID = -4230960075582953775L;
|
||||
private static final long serialVersionUID = -4230960075582953775L;
|
||||
|
||||
public LoginException() {
|
||||
}
|
||||
public LoginException() {
|
||||
}
|
||||
|
||||
public LoginException(String message) {
|
||||
super(message);
|
||||
}
|
||||
public LoginException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
package org.jpaste.pastebin.exceptions;
|
||||
|
||||
public class ParseException extends Exception {
|
||||
private static final long serialVersionUID = -4230960075582953775L;
|
||||
private static final long serialVersionUID = -4230960075582953775L;
|
||||
|
||||
public ParseException() {
|
||||
}
|
||||
public ParseException() {
|
||||
}
|
||||
|
||||
public ParseException(String message) {
|
||||
super(message);
|
||||
}
|
||||
public ParseException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,61 +6,57 @@ import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
*
|
||||
* A representation of a HTTP post
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* Encodes parameters with the UTF-8 Charset.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <p>
|
||||
* <a href="http://en.wikipedia.org/wiki/POST_(HTTP)">Reference manual</a>
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public class Post {
|
||||
private static final String ENCODING = "UTF-8";
|
||||
private HashMap<String, String> post;
|
||||
private static final String ENCODING = "UTF-8";
|
||||
private HashMap<String, String> post;
|
||||
|
||||
/**
|
||||
* Creates a new <code>Post</code> instance.
|
||||
*/
|
||||
public Post() {
|
||||
post = new HashMap<String, String>();
|
||||
}
|
||||
/**
|
||||
* Creates a new <code>Post</code> instance.
|
||||
*/
|
||||
public Post() {
|
||||
post = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a key value pair to the post parameters
|
||||
*
|
||||
* @param key
|
||||
* the key
|
||||
* @param value
|
||||
* the value
|
||||
*/
|
||||
public void put(String key, String value) {
|
||||
try {
|
||||
this.post.put(URLEncoder.encode(key, ENCODING),
|
||||
URLEncoder.encode(value, ENCODING));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Adds a key value pair to the post parameters
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
public void put(String key, String value) {
|
||||
try {
|
||||
this.post.put(URLEncoder.encode(key, ENCODING),
|
||||
URLEncoder.encode(value, ENCODING));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The HTTP post string representation
|
||||
*
|
||||
* @return HTTP Post contents
|
||||
*/
|
||||
public String getPost() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Entry<String, String> entry : post.entrySet()) {
|
||||
builder.append(entry.getKey()).append('=').append(entry.getValue())
|
||||
.append('&');
|
||||
}
|
||||
builder.deleteCharAt(builder.length() - 1);
|
||||
return new String(builder);
|
||||
}
|
||||
/**
|
||||
* The HTTP post string representation
|
||||
*
|
||||
* @return HTTP Post contents
|
||||
*/
|
||||
public String getPost() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Entry<String, String> entry : post.entrySet()) {
|
||||
builder.append(entry.getKey()).append('=').append(entry.getValue())
|
||||
.append('&');
|
||||
}
|
||||
builder.deleteCharAt(builder.length() - 1);
|
||||
return new String(builder);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,67 +9,62 @@ import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
/**
|
||||
*
|
||||
* Web utility class
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public class Web {
|
||||
|
||||
/**
|
||||
* Submits a HTTP post and fetches and returns the response
|
||||
*
|
||||
* @param link
|
||||
* The link/URL
|
||||
* @param post
|
||||
* the HTTP post representation
|
||||
* @return response of the web page
|
||||
*/
|
||||
public static String getContents(String link, Post post) {
|
||||
try {
|
||||
URL url = new URL(link);
|
||||
/**
|
||||
* Submits a HTTP post and fetches and returns the response
|
||||
*
|
||||
* @param link The link/URL
|
||||
* @param post the HTTP post representation
|
||||
* @return response of the web page
|
||||
*/
|
||||
public static String getContents(String link, Post post) {
|
||||
try {
|
||||
URL url = new URL(link);
|
||||
|
||||
URLConnection connection = url.openConnection();
|
||||
URLConnection connection = url.openConnection();
|
||||
|
||||
if(post != null) {
|
||||
connection.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(
|
||||
connection.getOutputStream());
|
||||
wr.write(post.getPost());
|
||||
wr.flush();
|
||||
wr.close();
|
||||
}
|
||||
if (post != null) {
|
||||
connection.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(
|
||||
connection.getOutputStream());
|
||||
wr.write(post.getPost());
|
||||
wr.flush();
|
||||
wr.close();
|
||||
}
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||
connection.getInputStream()));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (builder.length() > 0) {
|
||||
builder.append('\n');
|
||||
}
|
||||
builder.append(line);
|
||||
}
|
||||
reader.close();
|
||||
return new String(builder);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new IllegalArgumentException("Malformed link: " + e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to fetch contents from link: "
|
||||
+ e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets text from a link
|
||||
*
|
||||
* @param link
|
||||
* The link/URL
|
||||
* @return response of the web page
|
||||
*/
|
||||
public static String getContents(String link) {
|
||||
return getContents(link, null);
|
||||
}
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||
connection.getInputStream()));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (builder.length() > 0) {
|
||||
builder.append('\n');
|
||||
}
|
||||
builder.append(line);
|
||||
}
|
||||
reader.close();
|
||||
return new String(builder);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new IllegalArgumentException("Malformed link: " + e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to fetch contents from link: "
|
||||
+ e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets text from a link
|
||||
*
|
||||
* @param link The link/URL
|
||||
* @return response of the web page
|
||||
*/
|
||||
public static String getContents(String link) {
|
||||
return getContents(link, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,25 +3,21 @@ package org.jpaste.utils.xml;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
*
|
||||
* Holds various XML utility methods
|
||||
*
|
||||
*
|
||||
* @author Brian B
|
||||
*
|
||||
*/
|
||||
public class XMLUtils {
|
||||
|
||||
/**
|
||||
* Fetches text from a element
|
||||
*
|
||||
* @param parent
|
||||
* the parent of the element you want to fetch text from
|
||||
* @param tagName
|
||||
* name of the element you want to fetch text from
|
||||
* @return text of tag
|
||||
*/
|
||||
public static String getText(Element parent, String tagName) {
|
||||
return parent.getElementsByTagName(tagName).item(0).getTextContent();
|
||||
}
|
||||
/**
|
||||
* Fetches text from a element
|
||||
*
|
||||
* @param parent the parent of the element you want to fetch text from
|
||||
* @param tagName name of the element you want to fetch text from
|
||||
* @return text of tag
|
||||
*/
|
||||
public static String getText(Element parent, String tagName) {
|
||||
return parent.getElementsByTagName(tagName).item(0).getTextContent();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user