Clean refactoring using Java 11
This commit is contained in:
parent
4a0c0b1e6a
commit
7626f48a06
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.idea
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
|
@ -1,3 +1,5 @@
|
||||
image: jdk11-maven3
|
||||
|
||||
stages:
|
||||
- test
|
||||
- sonar
|
||||
|
14
README.md
14
README.md
@ -1,11 +1,11 @@
|
||||
[](https://maven-badges.herokuapp.com/maven-central/de.svenkubiak/jpushover)
|
||||
[](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
|
78
pom.xml
78
pom.xml
@ -18,9 +18,8 @@
|
||||
</developer>
|
||||
</developers>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<log4j.version>2.11.1</log4j.version>
|
||||
<httpcomponents.version>4.5.6</httpcomponents.version>
|
||||
</properties>
|
||||
<scm>
|
||||
<connection>scm:git:git@github.com:svenkubiak/JPushover.git</connection>
|
||||
@ -29,7 +28,7 @@
|
||||
<tag>HEAD</tag>
|
||||
</scm>
|
||||
<name>JPushover</name>
|
||||
<description>Convenient class for sending messages to Pushover in Java project</description>
|
||||
<description>Minimalist convenient class for sending messages to Pushover in Java project</description>
|
||||
<url>https://github.com/svenkubiak/JPushover</url>
|
||||
<prerequisites>
|
||||
<maven>3.0</maven>
|
||||
@ -41,8 +40,8 @@
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<debug>false</debug>
|
||||
<optimize>true</optimize>
|
||||
</configuration>
|
||||
@ -63,7 +62,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.0.0-M1</version>
|
||||
<version>3.0.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
@ -76,8 +75,24 @@
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>sonar-maven-plugin</artifactId>
|
||||
<version>3.3.0.603</version>
|
||||
<version>3.5.0.1254</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.owasp</groupId>
|
||||
<artifactId>dependency-check-maven</artifactId>
|
||||
<version>3.3.2</version>
|
||||
<configuration>
|
||||
<cveValidForHours>12</cveValidForHours>
|
||||
<failBuildOnCVSS>4</failBuildOnCVSS>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-release-plugin</artifactId>
|
||||
@ -90,80 +105,55 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
<version>3.0.0-M1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>versions-maven-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<version>2.7</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.20</version>
|
||||
<version>2.22.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.6</version>
|
||||
<version>3.7.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
<version>3.0.0-M1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>${httpcomponents.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>fluent-hc</artifactId>
|
||||
<version>${httpcomponents.version}</version>
|
||||
<groupId>com.eclipsesource.minimal-json</groupId>
|
||||
<artifactId>minimal-json</artifactId>
|
||||
<version>0.9.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
<version>3.8.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
@ -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<NameValuePair> 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<NameValuePair> 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<String> 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());
|
||||
}
|
||||
}
|
6
src/main/java/module-info.java
Normal file
6
src/main/java/module-info.java
Normal file
@ -0,0 +1,6 @@
|
||||
module jpushover {
|
||||
requires java.net.http;
|
||||
requires minimal.json;
|
||||
requires org.apache.commons.lang3;
|
||||
exports de.svenkubiak.jpushover;
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Console" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user