diff --git a/client-libraries/java/rest-client/src/com/google/android/gcm/server/Sender.java b/client-libraries/java/rest-client/src/com/google/android/gcm/server/Sender.java index f509bbb..f717dff 100644 --- a/client-libraries/java/rest-client/src/com/google/android/gcm/server/Sender.java +++ b/client-libraries/java/rest-client/src/com/google/android/gcm/server/Sender.java @@ -15,61 +15,22 @@ */ package com.google.android.gcm.server; -import static com.google.android.gcm.server.Constants.JSON_CANONICAL_IDS; -import static com.google.android.gcm.server.Constants.JSON_ERROR; -import static com.google.android.gcm.server.Constants.JSON_FAILURE; -import static com.google.android.gcm.server.Constants.JSON_MESSAGE_ID; -import static com.google.android.gcm.server.Constants.JSON_MULTICAST_ID; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_BADGE; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_BODY; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_BODY_LOC_ARGS; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_BODY_LOC_KEY; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_CLICK_ACTION; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_COLOR; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_ICON; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_SOUND; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_TAG; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_TITLE; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_TITLE_LOC_ARGS; -import static com.google.android.gcm.server.Constants.JSON_NOTIFICATION_TITLE_LOC_KEY; -import static com.google.android.gcm.server.Constants.JSON_PAYLOAD; -import static com.google.android.gcm.server.Constants.JSON_REGISTRATION_IDS; -import static com.google.android.gcm.server.Constants.JSON_TO; -import static com.google.android.gcm.server.Constants.JSON_RESULTS; -import static com.google.android.gcm.server.Constants.JSON_SUCCESS; -import static com.google.android.gcm.server.Constants.PARAM_COLLAPSE_KEY; -import static com.google.android.gcm.server.Constants.PARAM_DELAY_WHILE_IDLE; -import static com.google.android.gcm.server.Constants.PARAM_DRY_RUN; -import static com.google.android.gcm.server.Constants.PARAM_PRIORITY; -import static com.google.android.gcm.server.Constants.PARAM_CONTENT_AVAILABLE; -import static com.google.android.gcm.server.Constants.PARAM_RESTRICTED_PACKAGE_NAME; -import static com.google.android.gcm.server.Constants.PARAM_TIME_TO_LIVE; -import static com.google.android.gcm.server.Constants.TOKEN_CANONICAL_REG_ID; -import static com.google.android.gcm.server.Constants.TOPIC_PREFIX; - import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; -import java.io.BufferedReader; -import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; +import java.io.*; import java.net.HttpURLConnection; +import java.net.Proxy; import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; +import static com.google.android.gcm.server.Constants.*; + /** * Helper class to send messages to the GCM service using an API Key. */ @@ -91,8 +52,9 @@ public class Sender { Logger.getLogger(Sender.class.getName()); private final String key; + private Proxy proxy; - private String endpoint; + private String endpoint; private int connectTimeout; private int readTimeout; @@ -112,11 +74,24 @@ public Sender(String key) { * @param key FCM Server Key obtained through the Firebase Web Console. * @param endpoint Endpoint to use when sending the message. */ - public Sender(String key, String endpoint) { + public Sender(String key, String endpoint) { this.key = nonNull(key); this.endpoint = nonNull(endpoint); } + /** + * Full options constructor. + * + * @param key FCM Server Key obtained through the Firebase Web Console. + * @param Proxy proxy to use when sending the message. + * @param endpoint Endpoint to use when sending the message. + */ + public Sender(String key, Proxy proxy, String endpoint) { + this.key = nonNull(key); + this.endpoint = nonNull(endpoint); + this.proxy = proxy; + } + public String getEndpoint() { return endpoint; } @@ -671,8 +646,18 @@ protected static void addParameter(StringBuilder body, String name, /** * Gets an {@link HttpURLConnection} given an URL. */ - protected HttpURLConnection getConnection(String url) throws IOException { - HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + protected HttpURLConnection getConnection(String url) throws IOException + { + + HttpURLConnection conn; + if( proxy != null) + { + conn = (HttpURLConnection) new URL(url).openConnection(proxy); + } + else + { + conn = (HttpURLConnection) new URL(url).openConnection(); + } conn.setConnectTimeout(connectTimeout); conn.setReadTimeout(readTimeout); return conn;