From a3a760ef08e0a8c3dbf416ccbfecad07958cf018 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Rincon Date: Sat, 14 Jan 2017 12:24:09 +0100 Subject: [PATCH] Reformat of the source --- pom.xml | 4 +- .../lobbybalancer/manager/AdapterFix.java | 11 +- .../lobbybalancer/manager/PasteHelper.java | 5 +- .../lobbybalancer/ping/PingManager.java | 2 +- src/main/java/org/jpaste/AbstractPaste.java | 75 ++- .../java/org/jpaste/AbstractPasteLink.java | 18 +- .../org/jpaste/exceptions/PasteException.java | 12 +- .../org/jpaste/pastebin/PasteExpireDate.java | 82 ++- .../java/org/jpaste/pastebin/Pastebin.java | 301 +++++----- .../org/jpaste/pastebin/PastebinLink.java | 295 +++++---- .../org/jpaste/pastebin/PastebinPaste.java | 514 ++++++++-------- .../pastebin/account/PastebinAccount.java | 565 +++++++++--------- .../account/PastebinAccountDetails.java | 239 ++++---- .../pastebin/exceptions/LoginException.java | 12 +- .../pastebin/exceptions/ParseException.java | 12 +- src/main/java/org/jpaste/utils/web/Post.java | 82 ++- src/main/java/org/jpaste/utils/web/Web.java | 103 ++-- .../java/org/jpaste/utils/xml/XMLUtils.java | 26 +- 18 files changed, 1142 insertions(+), 1216 deletions(-) diff --git a/pom.xml b/pom.xml index f1defef..1f10820 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,6 @@ - 4.0.0 diff --git a/src/main/java/me/jaimemartz/lobbybalancer/manager/AdapterFix.java b/src/main/java/me/jaimemartz/lobbybalancer/manager/AdapterFix.java index 684faa0..a96cd7f 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/manager/AdapterFix.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/manager/AdapterFix.java @@ -12,6 +12,11 @@ import java.util.Map; public class AdapterFix implements ConfigurationAdapter { private static final Map 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(); diff --git a/src/main/java/me/jaimemartz/lobbybalancer/manager/PasteHelper.java b/src/main/java/me/jaimemartz/lobbybalancer/manager/PasteHelper.java index 0ba92ca..93112aa 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/manager/PasteHelper.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/manager/PasteHelper.java @@ -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"; } diff --git a/src/main/java/me/jaimemartz/lobbybalancer/ping/PingManager.java b/src/main/java/me/jaimemartz/lobbybalancer/ping/PingManager.java index 0f5b53c..976d6c6 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/ping/PingManager.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/ping/PingManager.java @@ -13,10 +13,10 @@ import static me.jaimemartz.lobbybalancer.LobbyBalancer.printStartupInfo; public class PingManager { private final LobbyBalancer plugin; + private final Map storage = new HashMap<>(); private boolean stopped = true; private PingTacticType tactic; private ScheduledTask task; - private final Map storage = new HashMap<>(); public PingManager(LobbyBalancer plugin) { this.plugin = plugin; diff --git a/src/main/java/org/jpaste/AbstractPaste.java b/src/main/java/org/jpaste/AbstractPaste.java index 50f8b6f..f7e7152 100644 --- a/src/main/java/org/jpaste/AbstractPaste.java +++ b/src/main/java/org/jpaste/AbstractPaste.java @@ -3,57 +3,52 @@ package org.jpaste; import org.jpaste.exceptions.PasteException; /** - * * An abstract representation of a paste. - * + *

*

* An abstract paste holds the paste contents and the {@link #paste()} * operation. *

- * + * + * @param

PasteResult implementation * @author Brian B - * - * @param

- * PasteResult implementation */ public abstract class AbstractPaste

{ - private String contents; + private String contents; - /** - * Creates a new abstract AbstractPaste instance. - * - * @param contents - * the contents of the paste - */ - public AbstractPaste(String contents) { - this.contents = contents; - } + /** + * Creates a new abstract AbstractPaste 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; } diff --git a/src/main/java/org/jpaste/AbstractPasteLink.java b/src/main/java/org/jpaste/AbstractPasteLink.java index 36f3226..3138148 100644 --- a/src/main/java/org/jpaste/AbstractPasteLink.java +++ b/src/main/java/org/jpaste/AbstractPasteLink.java @@ -3,23 +3,21 @@ package org.jpaste; import java.net.URL; /** - * * An representation of an abstract PasteLink - * + *

*

* An AbstractPasteLink holds the link/URL to a paste. *

- * + * * @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(); } diff --git a/src/main/java/org/jpaste/exceptions/PasteException.java b/src/main/java/org/jpaste/exceptions/PasteException.java index dffa5f7..a12d72a 100644 --- a/src/main/java/org/jpaste/exceptions/PasteException.java +++ b/src/main/java/org/jpaste/exceptions/PasteException.java @@ -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); + } } diff --git a/src/main/java/org/jpaste/pastebin/PasteExpireDate.java b/src/main/java/org/jpaste/pastebin/PasteExpireDate.java index ff3e029..c9a356d 100644 --- a/src/main/java/org/jpaste/pastebin/PasteExpireDate.java +++ b/src/main/java/org/jpaste/pastebin/PasteExpireDate.java @@ -1,60 +1,56 @@ package org.jpaste.pastebin; /** - * * Represents a paste's expire date - * + *

*

* A list of all expire dates/times can be found at the pastebin API manual. *

- * + * * @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 PasteExpireDate instance. - * - * @param val - * a valid expire date value - */ - PasteExpireDate(String val, int timeSeconds) { - this.val = val; - this.timeSeconds = timeSeconds; - } + /** + * Creates a new PasteExpireDate 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; + } } diff --git a/src/main/java/org/jpaste/pastebin/Pastebin.java b/src/main/java/org/jpaste/pastebin/Pastebin.java index fc3baa1..f3ae5af 100644 --- a/src/main/java/org/jpaste/pastebin/Pastebin.java +++ b/src/main/java/org/jpaste/pastebin/Pastebin.java @@ -19,189 +19,172 @@ import java.util.ArrayList; import java.util.Date; /** - * * A global representation of the pastebin site - * + *

*

* Holds various constants and method shortcuts *

- * + * * @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("")) { - // success - try { - DocumentBuilderFactory dbFactory = DocumentBuilderFactory - .newInstance(); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - response = "" + response + ""; // requires root - // element - Document doc = dBuilder.parse(new InputSource( - new ByteArrayInputStream(response.getBytes("utf-8")))); - doc.getDocumentElement().normalize(); + if (response.startsWith("")) { + // success + try { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + response = "" + response + ""; // requires root + // element + Document doc = dBuilder.parse(new InputSource( + new ByteArrayInputStream(response.getBytes("utf-8")))); + doc.getDocumentElement().normalize(); - NodeList nodes = doc.getElementsByTagName("paste"); - ArrayList pastes = new ArrayList( - 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 pastes = new ArrayList( + 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); + } } diff --git a/src/main/java/org/jpaste/pastebin/PastebinLink.java b/src/main/java/org/jpaste/pastebin/PastebinLink.java index 462713f..002a3ea 100644 --- a/src/main/java/org/jpaste/pastebin/PastebinLink.java +++ b/src/main/java/org/jpaste/pastebin/PastebinLink.java @@ -11,174 +11,165 @@ import java.net.URL; import java.util.Date; /** - * * A representation of an online pastebin paste. - * - * + *

+ *

*

* This class represents an existing pastebin paste URL *

- * + * * @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 PastebinLink 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 PastebinLink 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 PastebinLink 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 PastebinLink 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 - *

- * 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 + *

+ * 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()); + } } diff --git a/src/main/java/org/jpaste/pastebin/PastebinPaste.java b/src/main/java/org/jpaste/pastebin/PastebinPaste.java index d225e39..455f8af 100644 --- a/src/main/java/org/jpaste/pastebin/PastebinPaste.java +++ b/src/main/java/org/jpaste/pastebin/PastebinPaste.java @@ -10,303 +10,289 @@ import java.net.MalformedURLException; import java.net.URL; /** - * * A representation of a new or existing paste. - * + *

*

* This class holds the contents of the paste itself. You can get and modify * settings and then 'push' this paste onto pastebin. *

- * + *

*

* This class has been programmed with the help of the pastebin API manual. *

- * + * * @author Brian B - * */ public class PastebinPaste extends AbstractPaste { - /** - * 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. - *

- * Requires an {@link PastebinAccount - * org.jpaste.pastebin.account.PastebinAccount PastebinAccount} - *

- */ - 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. + *

+ * Requires an {@link PastebinAccount + * org.jpaste.pastebin.account.PastebinAccount PastebinAccount} + *

+ */ + 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 PastebinPaste instance. - */ - public PastebinPaste() { - this(null, null, null); - } + /** + * Creates a new empty PastebinPaste instance. + */ + public PastebinPaste() { + this(null, null, null); + } - /** - * Creates a new PastebinPaste instance. - * - * @param contents - * the paste contents - */ - public PastebinPaste(String contents) { - this(null, contents, null); - } + /** + * Creates a new PastebinPaste instance. + * + * @param contents the paste contents + */ + public PastebinPaste(String contents) { + this(null, contents, null); + } - /** - * Creates a new PastebinPaste instance. - * - * @param account - * a pastebin account - */ - public PastebinPaste(PastebinAccount account) { - this(account.getDeveloperKey(), null, account); - } + /** + * Creates a new PastebinPaste instance. + * + * @param account a pastebin account + */ + public PastebinPaste(PastebinAccount account) { + this(account.getDeveloperKey(), null, account); + } - /** - * Creates a new PastebinPaste 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 PastebinPaste 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 PastebinPaste 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 PastebinPaste 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 available syntax highlighting - * formats - * @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 - *

- * Valid visibilities - *

- *

- * {@link PastebinPaste#VISIBILITY_PUBLIC} - *

- *

- * {@link PastebinPaste#VISIBILITY_UNLISTED} - *

- *

- * {@link PastebinPaste#VISIBILITY_PRIVATE} - *

- * - * @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 available syntax highlighting + * formats + */ + public void setPasteFormat(String format) { + this.pasteFormat = format; + } - /** - * Gets this paste visibility - *

- * Valid visibilities - *

- *

- * {@link PastebinPaste#VISIBILITY_PUBLIC} - *

- *

- * {@link PastebinPaste#VISIBILITY_UNLISTED} - *

- *

- * {@link PastebinPaste#VISIBILITY_PRIVATE} - *

- * - * @return visibility of this paste - */ - public int getVisibility() { - return this.visibility; - } + /** + * Gets this paste visibility + *

+ * Valid visibilities + *

+ *

+ * {@link PastebinPaste#VISIBILITY_PUBLIC} + *

+ *

+ * {@link PastebinPaste#VISIBILITY_UNLISTED} + *

+ *

+ * {@link PastebinPaste#VISIBILITY_PRIVATE} + *

+ * + * @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 + *

+ * Valid visibilities + *

+ *

+ * {@link PastebinPaste#VISIBILITY_PUBLIC} + *

+ *

+ * {@link PastebinPaste#VISIBILITY_UNLISTED} + *

+ *

+ * {@link PastebinPaste#VISIBILITY_PRIVATE} + *

+ * + * @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); + } + } } diff --git a/src/main/java/org/jpaste/pastebin/account/PastebinAccount.java b/src/main/java/org/jpaste/pastebin/account/PastebinAccount.java index e080bd1..5e13102 100644 --- a/src/main/java/org/jpaste/pastebin/account/PastebinAccount.java +++ b/src/main/java/org/jpaste/pastebin/account/PastebinAccount.java @@ -23,347 +23,330 @@ import java.util.ArrayList; import java.util.Date; /** - * * Represents an account on Pastebin. - * + *

*

* 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. *

- * + *

*

* A reference manual for generating a user session id can be found here. - * + * * @author Brian B - * */ public class PastebinAccount { - private String username, password, userSessionId, developerKey; + private String username, password, userSessionId, developerKey; - /** - * Creates a new empty PastebinAccount instance. - */ - public PastebinAccount() { - this(null, null, null); - } + /** + * Creates a new empty PastebinAccount instance. + */ + public PastebinAccount() { + this(null, null, null); + } - /** - * Creates a new PastebinAccount 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 PastebinAccount 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 PastebinAccount instance. - * - * @param userSessionId - * the user session id of the pastebin account. - */ - public PastebinAccount(String userSessionId) { - this(null, userSessionId); - } + /** + * Creates a new PastebinAccount instance. + * + * @param userSessionId the user session id of the pastebin account. + */ + public PastebinAccount(String userSessionId) { + this(null, userSessionId); + } - /** - * Creates a new PastebinAccount 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 PastebinAccount 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 - *

- * 0 > limit > 1000 - *

- * @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 + *

+ * 0 > limit > 1000 + *

+ * @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("")) { - // success - try { - DocumentBuilderFactory dbFactory = DocumentBuilderFactory - .newInstance(); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - response = "" + response + ""; // requires root - // element - Document doc = dBuilder.parse(new InputSource( - new ByteArrayInputStream(response.getBytes("utf-8")))); - doc.getDocumentElement().normalize(); + if (response.startsWith("")) { + // success + try { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + response = "" + response + ""; // requires root + // element + Document doc = dBuilder.parse(new InputSource( + new ByteArrayInputStream(response.getBytes("utf-8")))); + doc.getDocumentElement().normalize(); - NodeList nodes = doc.getElementsByTagName("paste"); - ArrayList pastes = new ArrayList( - 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 pastes = new ArrayList( + 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("")) { - throw new ParseException("Failed to parse account details: " - + response); - } + if (!response.startsWith("")) { + 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); + } + } } diff --git a/src/main/java/org/jpaste/pastebin/account/PastebinAccountDetails.java b/src/main/java/org/jpaste/pastebin/account/PastebinAccountDetails.java index 6e6bc6a..8685e21 100644 --- a/src/main/java/org/jpaste/pastebin/account/PastebinAccountDetails.java +++ b/src/main/java/org/jpaste/pastebin/account/PastebinAccountDetails.java @@ -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 PastebinAccountDetails 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 - *

- * N = never (default) - *

- * @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 - *
-	 * 0 = public
-	 * 1 = unlisted
-	 * 2 = private
-	 * 
- * @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 true if this account is a pro account, otherwise false. - */ - 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 PastebinAccountDetails 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 + *

+ * N = never (default) + *

+ * + * @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 + *
+     * 0 = public
+     * 1 = unlisted
+     * 2 = private
+     * 
+ * + * @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 true if this account is a pro account, otherwise false. + */ + 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()); + } } diff --git a/src/main/java/org/jpaste/pastebin/exceptions/LoginException.java b/src/main/java/org/jpaste/pastebin/exceptions/LoginException.java index 093ff4a..fc4f77a 100644 --- a/src/main/java/org/jpaste/pastebin/exceptions/LoginException.java +++ b/src/main/java/org/jpaste/pastebin/exceptions/LoginException.java @@ -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); + } } diff --git a/src/main/java/org/jpaste/pastebin/exceptions/ParseException.java b/src/main/java/org/jpaste/pastebin/exceptions/ParseException.java index f7ea231..fb5802c 100644 --- a/src/main/java/org/jpaste/pastebin/exceptions/ParseException.java +++ b/src/main/java/org/jpaste/pastebin/exceptions/ParseException.java @@ -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); + } } diff --git a/src/main/java/org/jpaste/utils/web/Post.java b/src/main/java/org/jpaste/utils/web/Post.java index 3f64259..18fdfe2 100644 --- a/src/main/java/org/jpaste/utils/web/Post.java +++ b/src/main/java/org/jpaste/utils/web/Post.java @@ -6,61 +6,57 @@ import java.util.HashMap; import java.util.Map.Entry; /** - * * A representation of a HTTP post - * + *

*

* Encodes parameters with the UTF-8 Charset. *

- * + *

*

* Reference manual *

- * + * * @author Brian B - * */ public class Post { - private static final String ENCODING = "UTF-8"; - private HashMap post; + private static final String ENCODING = "UTF-8"; + private HashMap post; - /** - * Creates a new Post instance. - */ - public Post() { - post = new HashMap(); - } + /** + * Creates a new Post instance. + */ + public Post() { + post = new HashMap(); + } - /** - * 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 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 entry : post.entrySet()) { + builder.append(entry.getKey()).append('=').append(entry.getValue()) + .append('&'); + } + builder.deleteCharAt(builder.length() - 1); + return new String(builder); + } } diff --git a/src/main/java/org/jpaste/utils/web/Web.java b/src/main/java/org/jpaste/utils/web/Web.java index 7055bef..0bf5eff 100644 --- a/src/main/java/org/jpaste/utils/web/Web.java +++ b/src/main/java/org/jpaste/utils/web/Web.java @@ -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); + } } diff --git a/src/main/java/org/jpaste/utils/xml/XMLUtils.java b/src/main/java/org/jpaste/utils/xml/XMLUtils.java index cdae712..4ba7a6d 100644 --- a/src/main/java/org/jpaste/utils/xml/XMLUtils.java +++ b/src/main/java/org/jpaste/utils/xml/XMLUtils.java @@ -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(); + } }