Merge pull request #3 from danielhaas/master

Added Proxy support
This commit is contained in:
Sven Kubiak 2018-08-16 11:23:08 +02:00 committed by GitHub
commit 2f5002d8d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 16 deletions

View File

@ -7,6 +7,7 @@ import java.util.Objects;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts; import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.fluent.Form; import org.apache.http.client.fluent.Form;
@ -40,6 +41,7 @@ public class JPushover {
private boolean pushoverHtml; private boolean pushoverHtml;
private Priority pushoverPriority; private Priority pushoverPriority;
private Sound pushoverSound; private Sound pushoverSound;
private HttpHost proxy;
public JPushover() { public JPushover() {
this.withSound(Sound.PUSHOVER); this.withSound(Sound.PUSHOVER);
@ -61,7 +63,7 @@ public class JPushover {
* @param token The pushover API token * @param token The pushover API token
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withToken(String token) { public final JPushover withToken(final String token) {
this.pushoverToken = token; this.pushoverToken = token;
return this; return this;
} }
@ -74,7 +76,7 @@ public class JPushover {
* @param user The username * @param user The username
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withUser(String user) { public final JPushover withUser(final String user) {
this.pushoverUser = user; this.pushoverUser = user;
return this; return this;
} }
@ -86,7 +88,7 @@ public class JPushover {
* @param retry Number of seconds * @param retry Number of seconds
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withRetry(String retry) { public final JPushover withRetry(final String retry) {
this.pushoverRetry = retry; this.pushoverRetry = retry;
return this; return this;
} }
@ -98,7 +100,7 @@ public class JPushover {
* @param expire Number of seconds * @param expire Number of seconds
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withExpire(String expire) { public final JPushover withExpire(final String expire) {
this.pushoverExpire = expire; this.pushoverExpire = expire;
return this; return this;
} }
@ -110,7 +112,7 @@ public class JPushover {
* @param message The message to sent * @param message The message to sent
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withMessage(String message) { public final JPushover withMessage(final String message) {
this.pushoverMessage = message; this.pushoverMessage = message;
return this; return this;
} }
@ -123,7 +125,7 @@ public class JPushover {
* @param device The device name * @param device The device name
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withDevice(String device) { public final JPushover withDevice(final String device) {
this.pushoverDevice = device; this.pushoverDevice = device;
return this; return this;
} }
@ -135,7 +137,7 @@ public class JPushover {
* @param title The title * @param title The title
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withTitle(String title) { public final JPushover withTitle(final String title) {
this.pushoverTitle = title; this.pushoverTitle = title;
return this; return this;
} }
@ -147,7 +149,7 @@ public class JPushover {
* @param url The url * @param url The url
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withUrl(String url) { public final JPushover withUrl(final String url) {
this.pushoverUrl = url; this.pushoverUrl = url;
return this; return this;
} }
@ -169,7 +171,7 @@ public class JPushover {
* @param urlTitle The url title * @param urlTitle The url title
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withUrlTitle(String urlTitle) { public final JPushover withUrlTitle(final String urlTitle) {
this.pushoverUrlTitle = urlTitle; this.pushoverUrlTitle = urlTitle;
return this; return this;
} }
@ -181,7 +183,7 @@ public class JPushover {
* @param timestamp The Unix timestamp * @param timestamp The Unix timestamp
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withTimestamp(String timestamp) { public final JPushover withTimestamp(final String timestamp) {
this.pushoverTimestamp = timestamp; this.pushoverTimestamp = timestamp;
return this; return this;
} }
@ -193,7 +195,7 @@ public class JPushover {
* @param priority The priority enum * @param priority The priority enum
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withPriority(Priority priority) { public final JPushover withPriority(final Priority priority) {
this.pushoverPriority = priority; this.pushoverPriority = priority;
return this; return this;
} }
@ -206,7 +208,7 @@ public class JPushover {
* @param sound THe sound enum * @param sound THe sound enum
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withSound(Sound sound) { public final JPushover withSound(final Sound sound) {
this.pushoverSound = sound; this.pushoverSound = sound;
return this; return this;
} }
@ -220,11 +222,22 @@ public class JPushover {
* @param callback The callback URL * @param callback The callback URL
* @return JPushover instance * @return JPushover instance
*/ */
public final JPushover withCallback(String callback) { public final JPushover withCallback(final String callback) {
this.pushoverCallback = callback; this.pushoverCallback = callback;
return this; return this;
} }
/**
* Uses the given proxy for http communication
*
* @param proxy The host that should be used as Proxy
* @return JPushover instance
*/
public final JPushover withProxy(final HttpHost proxy) {
this.proxy = proxy;
return this;
}
/** /**
* Sends a validation request to pushover ensuring that the token and user * Sends a validation request to pushover ensuring that the token and user
* is correct, that there is at least one active device on the account. * is correct, that there is at least one active device on the account.
@ -247,7 +260,12 @@ public class JPushover {
boolean valid = false; boolean valid = false;
try { try {
final HttpResponse httpResponse = Request.Post(Constants.VALIDATION_URL.toString()) final Request request = Request.Post(Constants.VALIDATION_URL.toString());
if (proxy!=null)
request.viaProxy(proxy);
final HttpResponse httpResponse = request
.bodyForm(params, Consts.UTF_8) .bodyForm(params, Consts.UTF_8)
.execute() .execute()
.returnResponse(); .returnResponse();
@ -297,7 +315,7 @@ public class JPushover {
.add(Constants.HTML.toString(), this.pushoverHtml ? "1" : "0") .add(Constants.HTML.toString(), this.pushoverHtml ? "1" : "0")
.build(); .build();
JPushoverResponse jPushoverResponse = new JPushoverResponse().isSuccessful(false); final JPushoverResponse jPushoverResponse = new JPushoverResponse().isSuccessful(false);
try { try {
final HttpResponse httpResponse = Request.Post(Constants.MESSAGES_URL.toString()) final HttpResponse httpResponse = Request.Post(Constants.MESSAGES_URL.toString())
.bodyForm(params, Consts.UTF_8) .bodyForm(params, Consts.UTF_8)
@ -374,4 +392,8 @@ public class JPushover {
public boolean isHtml() { public boolean isHtml() {
return pushoverHtml; return pushoverHtml;
} }
public HttpHost getProxy() {
return proxy;
}
} }

View File

@ -3,6 +3,7 @@ package de.svenkubiak.jpushover;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.apache.http.HttpHost;
import org.junit.Test; import org.junit.Test;
import de.svenkubiak.jpushover.enums.Priority; import de.svenkubiak.jpushover.enums.Priority;
@ -20,6 +21,7 @@ public class TestJPushover {
private static final String EXPIRE = "expire"; private static final String EXPIRE = "expire";
private static final String DEVICE = "device"; private static final String DEVICE = "device";
private static final String CALLBACK = "callback"; private static final String CALLBACK = "callback";
private static final HttpHost PROXY = new HttpHost("localhost");
@Test @Test
public void TestValues(){ public void TestValues(){
@ -67,7 +69,10 @@ public class TestJPushover {
push.enableHtml(); push.enableHtml();
assertTrue(push.isHtml()); assertTrue(push.isHtml());
JPushoverResponse response = push.push(); push.withProxy(PROXY);
assertEquals(push.getProxy(), PROXY);
final JPushoverResponse response = push.push();
assertTrue(response != null); assertTrue(response != null);
} }
} }