From 7626f48a0662306012fca4b130979723844d6992 Mon Sep 17 00:00:00 2001 From: Sven Kubiak Date: Sat, 6 Oct 2018 07:49:05 +0200 Subject: [PATCH] Clean refactoring using Java 11 --- .gitignore | 1 + .gitlab-ci.yml | 2 + README.md | 14 +- pom.xml | 78 ++++----- .../de/svenkubiak/jpushover/JPushover.java | 164 +++++------------- src/main/java/module-info.java | 6 + src/main/resources/log4j2.xml | 13 -- .../svenkubiak/jpushover/TestJPushover.java | 78 --------- 8 files changed, 98 insertions(+), 258 deletions(-) create mode 100644 src/main/java/module-info.java delete mode 100644 src/main/resources/log4j2.xml delete mode 100644 src/test/java/de/svenkubiak/jpushover/TestJPushover.java diff --git a/.gitignore b/.gitignore index 1229c22..aa30968 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea .classpath .project .settings/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7b1ad45..e63361d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,3 +1,5 @@ +image: jdk11-maven3 + stages: - test - sonar diff --git a/README.md b/README.md index ec35a48..22ede79 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ [![Maven Central](https://maven-badges.herokuapp.com/maven-central/de.svenkubiak/jpushover/badge.svg)](https://maven-badges.herokuapp.com/maven-central/de.svenkubiak/jpushover) -[![Build Status](https://secure.travis-ci.org/svenkubiak/JPushover.png?branch=master)](http://travis-ci.org/svenkubiak/JPushover) - JPushover ================ -Convenient class for sending messages to [Pushover][1] in Java. +Minimalist convenient class for sending messages to [Pushover][1] in Java. + +Starting with version 3.x JPushover is build for and requires Java 11. Usage ------------------ @@ -20,7 +20,7 @@ Usage ``` 2) Use the JPushover object with the required informations were you want ``` -JPushover.build() +JPushover.create() .withToken("MyToken") .withUser("MyUser") .withMessage("MyMessage") @@ -30,14 +30,14 @@ You can additionally add all available options from the official [Pushover docum You can also validate a user and token using the following method - boolean valid = JPushover.build() + boolean valid = JPushover.create() .withToken("MyToken") .withUser("MyUser") .validate(); If you want more information and/or the response from the Pushover API, use the JPushoverResponse object. - JPushoverResponse jPushoverResponse = JPushover.build() + JPushoverResponse jPushoverResponse = JPushover.create() .withToken("MyToken") .withUser("MyUser") .withMessage("MyMessage") @@ -46,4 +46,4 @@ If you want more information and/or the response from the Pushover API, use the The JPushoverResponse will return the raw HTTP status code, along with the raw JSON response and a convenient boolean if the request was successful or not. [1]: https://pushover.net -[2]: https://pushover.net/api +[2]: https://pushover.net/api \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1731cbe..0895980 100644 --- a/pom.xml +++ b/pom.xml @@ -18,9 +18,8 @@ + 11 UTF-8 - 2.11.1 - 4.5.6 scm:git:git@github.com:svenkubiak/JPushover.git @@ -29,7 +28,7 @@ HEAD JPushover - Convenient class for sending messages to Pushover in Java project + Minimalist convenient class for sending messages to Pushover in Java project https://github.com/svenkubiak/JPushover 3.0 @@ -41,8 +40,8 @@ maven-compiler-plugin 3.8.0 - 1.8 - 1.8 + ${java.version} + ${java.version} false true @@ -63,7 +62,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.0.0-M1 + 3.0.1 attach-javadocs @@ -76,8 +75,24 @@ org.codehaus.mojo sonar-maven-plugin - 3.3.0.603 + 3.5.0.1254 + + org.owasp + dependency-check-maven + 3.3.2 + + 12 + 4 + + + + + check + + + + org.apache.maven.plugins maven-release-plugin @@ -90,80 +105,55 @@ org.apache.maven.plugins maven-deploy-plugin - 2.8.2 + 3.0.0-M1 org.codehaus.mojo versions-maven-plugin - 2.4 + 2.7 org.apache.maven.plugins maven-clean-plugin - 3.0.0 + 3.1.0 org.apache.maven.plugins maven-surefire-plugin - 2.20 + 2.22.0 org.apache.maven.plugins maven-jar-plugin - 3.0.2 + 3.1.0 org.apache.maven.plugins maven-site-plugin - 3.6 + 3.7.1 org.apache.maven.plugins maven-install-plugin - 2.5.2 + 3.0.0-M1 org.apache.maven.plugins maven-resources-plugin - 3.0.2 + 3.1.0 - org.apache.httpcomponents - httpmime - ${httpcomponents.version} - - - commons-io - commons-io - 2.6 - - - org.apache.httpcomponents - fluent-hc - ${httpcomponents.version} + com.eclipsesource.minimal-json + minimal-json + 0.9.5 org.apache.commons commons-lang3 - 3.8 - - - org.apache.logging.log4j - log4j-api - ${log4j.version} - - - org.apache.logging.log4j - log4j-core - ${log4j.version} - - - org.apache.logging.log4j - log4j-slf4j-impl - ${log4j.version} + 3.8.1 junit diff --git a/src/main/java/de/svenkubiak/jpushover/JPushover.java b/src/main/java/de/svenkubiak/jpushover/JPushover.java index 5e17016..ccf39f6 100644 --- a/src/main/java/de/svenkubiak/jpushover/JPushover.java +++ b/src/main/java/de/svenkubiak/jpushover/JPushover.java @@ -1,19 +1,17 @@ package de.svenkubiak.jpushover; import java.io.IOException; -import java.util.List; +import java.net.InetSocketAddress; +import java.net.ProxySelector; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; import java.util.Objects; -import org.apache.commons.io.IOUtils; +import com.eclipsesource.json.Json; import org.apache.commons.lang3.StringUtils; -import org.apache.http.Consts; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.client.fluent.Form; -import org.apache.http.client.fluent.Request; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import de.svenkubiak.jpushover.enums.Constants; import de.svenkubiak.jpushover.enums.Priority; @@ -25,8 +23,9 @@ import de.svenkubiak.jpushover.enums.Sound; * */ public class JPushover { - private static final Logger LOG = LogManager.getLogger(JPushover.class); private static final int HTTP_OK = 200; + private Priority pushoverPriority; + private Sound pushoverSound; private String pushoverToken; private String pushoverUser; private String pushoverMessage; @@ -38,9 +37,8 @@ public class JPushover { private String pushoverRetry; private String pushoverExpire; private String pushoverCallback; - private Priority pushoverPriority; - private Sound pushoverSound; - private HttpHost proxy; + private String proxyHost; + private int proxyPort; private boolean pushoverHtml; public JPushover() { @@ -52,7 +50,7 @@ public class JPushover { * Creates a new JPushover instance * @return JPushover instance */ - public static JPushover build() { + public static JPushover create() { return new JPushover(); } @@ -230,11 +228,13 @@ public class JPushover { /** * Uses the given proxy for HTTP requests * - * @param proxy The host that should be used as Proxy + * @param proxyHost The host that should be used for the Proxy + * @param proxyPort The port that should be used for the Proxy * @return JPushover instance */ - public final JPushover withProxy(final HttpHost proxy) { - this.proxy = proxy; + public final JPushover withProxy(final String proxyHost, final int proxyPort) { + this.proxyHost = proxyHost; + this.proxyPort = proxyPort; return this; } @@ -248,36 +248,23 @@ public class JPushover { * * @return true if token and user are valid and at least on device is on the account, false otherwise */ - public boolean validate() { + public boolean validate() throws IOException, InterruptedException { Objects.requireNonNull(this.pushoverToken, "Token is required for validation"); Objects.requireNonNull(this.pushoverUser, "User is required for validation"); - final List params = Form.form() + var body = Json.object() .add(Constants.TOKEN.toString(), this.pushoverToken) .add(Constants.USER.toString(), this.pushoverUser) - .add(Constants.DEVICE.toString(), this.pushoverDevice) - .build(); + .add(Constants.DEVICE.toString(), this.pushoverDevice); - boolean valid = false; - try { - final Request request = Request.Post(Constants.VALIDATION_URL.toString()); - if (proxy != null) { - request.viaProxy(proxy); - } - - final HttpResponse httpResponse = request - .bodyForm(params, Consts.UTF_8) - .execute() - .returnResponse(); + var httpResponse = getResponse(body.toString(), Constants.VALIDATION_URL.toString()); - if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HTTP_OK) { - final String response = IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8); - if (StringUtils.isNotBlank(response) && response.contains("\"status\":1")) { - valid = true; - } + var valid = false; + if (httpResponse.statusCode() == HTTP_OK) { + var response = httpResponse.body(); + if (StringUtils.isNotBlank(response) && response.contains("\"status\":1")) { + valid = true; } - } catch (final IOException e) { - LOG.error("Failed to send validation requeste to pushover", e); } return valid; @@ -288,7 +275,7 @@ public class JPushover { * * @return JPushoverResponse instance */ - public final JPushoverResponse push() { + public final JPushoverResponse push() throws IOException, InterruptedException { Objects.requireNonNull(this.pushoverToken, "Token is required for a message"); Objects.requireNonNull(this.pushoverUser, "User is required for a message"); Objects.requireNonNull(this.pushoverMessage, "Message is required for a message"); @@ -298,7 +285,7 @@ public class JPushover { Objects.requireNonNull(this.pushoverExpire, "Expire is required on priority emergency"); } - final List params = Form.form() + var body = Json.object() .add(Constants.TOKEN.toString(), this.pushoverToken) .add(Constants.USER.toString(), this.pushoverUser) .add(Constants.MESSAGE.toString(), this.pushoverMessage) @@ -312,88 +299,33 @@ public class JPushover { .add(Constants.PRIORITY.toString(), this.pushoverPriority.toString()) .add(Constants.TIMESTAMP.toString(), this.pushoverTimestamp) .add(Constants.SOUND.toString(), this.pushoverSound.toString()) - .add(Constants.HTML.toString(), this.pushoverHtml ? "1" : "0") - .build(); + .add(Constants.HTML.toString(), this.pushoverHtml ? "1" : "0"); - final JPushoverResponse jPushoverResponse = new JPushoverResponse().isSuccessful(false); - try { - final HttpResponse httpResponse = Request.Post(Constants.MESSAGES_URL.toString()) - .bodyForm(params, Consts.UTF_8) - .execute() - .returnResponse(); + var httpResponse = getResponse(body.toString(), Constants.MESSAGES_URL.toString()); - if (httpResponse != null) { - final int status = httpResponse.getStatusLine().getStatusCode(); - - jPushoverResponse - .httpStatus(status) - .response(IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8)) - .isSuccessful((status == HTTP_OK) ? true : false); - } - } catch (final IOException e) { - LOG.error("Failed to send message to pushover", e); - } + JPushoverResponse jPushoverResponse = new JPushoverResponse().isSuccessful(false); + jPushoverResponse + .httpStatus(httpResponse.statusCode()) + .response(httpResponse.body()) + .isSuccessful((httpResponse.statusCode() == HTTP_OK) ? true : false); return jPushoverResponse; } - public String getToken() { - return pushoverToken; - } + private HttpResponse getResponse(String body, String url) throws IOException, InterruptedException { + HttpRequest httpRequest = HttpRequest.newBuilder() + .uri(URI.create(url)) + .timeout(Duration.ofSeconds(5)) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(body)) + .build(); - public String getUser() { - return pushoverUser; - } + HttpClient.Builder httpClientBuilder = HttpClient.newBuilder(); - public String getMessage() { - return pushoverMessage; - } + if (StringUtils.isNotBlank(this.proxyHost) && this.proxyPort > 0) { + httpClientBuilder.proxy(ProxySelector.of(new InetSocketAddress(this.proxyHost, this.proxyPort))); + } - public String getDevice() { - return pushoverDevice; - } - - public String getTitle() { - return pushoverTitle; - } - - public String getUrl() { - return pushoverUrl; - } - - public String getUrlTitle() { - return pushoverUrlTitle; - } - - public String getTimestamp() { - return pushoverTimestamp; - } - - public String getRetry() { - return pushoverRetry; - } - - public String getExpire() { - return pushoverExpire; - } - - public String getCallback() { - return pushoverCallback; - } - - public Priority getPriority() { - return pushoverPriority; - } - - public Sound getSound() { - return pushoverSound; - } - - public boolean isHtml() { - return pushoverHtml; - } - - public HttpHost getProxy() { - return proxy; + return httpClientBuilder.build().send(httpRequest, HttpResponse.BodyHandlers.ofString()); } } \ No newline at end of file diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java new file mode 100644 index 0000000..68316b9 --- /dev/null +++ b/src/main/java/module-info.java @@ -0,0 +1,6 @@ +module jpushover { + requires java.net.http; + requires minimal.json; + requires org.apache.commons.lang3; + exports de.svenkubiak.jpushover; +} \ No newline at end of file diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml deleted file mode 100644 index c8ea537..0000000 --- a/src/main/resources/log4j2.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/test/java/de/svenkubiak/jpushover/TestJPushover.java b/src/test/java/de/svenkubiak/jpushover/TestJPushover.java deleted file mode 100644 index c5f56a1..0000000 --- a/src/test/java/de/svenkubiak/jpushover/TestJPushover.java +++ /dev/null @@ -1,78 +0,0 @@ -package de.svenkubiak.jpushover; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import org.apache.http.HttpHost; -import org.junit.Test; - -import de.svenkubiak.jpushover.enums.Priority; -import de.svenkubiak.jpushover.enums.Sound; - -public class TestJPushover { - private static final String USER = "user"; - private static final String URL_TITLE = "urlTitle"; - private static final String URL = "url"; - private static final String TOKEN = "token"; - private static final String TITLE = "title"; - private static final String TIMESTAMP = "timestamp"; - private static final String RETRY = "retry"; - private static final String MESSAGE = "message"; - private static final String EXPIRE = "expire"; - private static final String DEVICE = "device"; - private static final String CALLBACK = "callback"; - private static final HttpHost PROXY = new HttpHost("localhost"); - - @Test - public void TestValues(){ - final JPushover push = new JPushover(); - - push.withCallback(CALLBACK); - assertEquals(push.getCallback(), CALLBACK); - - push.withDevice(DEVICE); - assertEquals(push.getDevice(), DEVICE); - - push.withExpire(EXPIRE); - assertEquals(push.getExpire(), EXPIRE); - - push.withMessage(MESSAGE); - assertEquals(push.getMessage(), MESSAGE); - - push.withPriority(Priority.HIGH); - assertEquals(push.getPriority(), Priority.HIGH); - - push.withRetry(RETRY); - assertEquals(push.getRetry(), RETRY); - - push.withSound(Sound.ALIEN); - assertEquals(push.getSound(), Sound.ALIEN); - - push.withTimestamp(TIMESTAMP); - assertEquals(push.getTimestamp(), TIMESTAMP); - - push.withTitle(TITLE); - assertEquals(push.getTitle(), TITLE); - - push.withToken(TOKEN); - assertEquals(push.getToken(), TOKEN); - - push.withUrl(URL); - assertEquals(push.getUrl(), URL); - - push.withUrlTitle(URL_TITLE); - assertEquals(push.getUrlTitle(), URL_TITLE); - - push.withUser(USER); - assertEquals(push.getUser(), USER); - - push.enableHtml(); - assertTrue(push.isHtml()); - - push.withProxy(PROXY); - assertEquals(push.getProxy(), PROXY); - - final JPushoverResponse response = push.push(); - assertTrue(response != null); - } -} \ No newline at end of file