Permalink
Browse files

add notification classes

  • Loading branch information...
1 parent 06f8302 commit 9feff7ec99d7a3a30f11532acd9ce93bfebc5208 @notnoop committed Jun 23, 2011
@@ -37,6 +37,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.util.regex.Pattern;
public final class Utilities {
private Utilities() { throw new AssertionError("Uninstantiable class"); }
@@ -69,4 +70,28 @@ public static ThreadSafeClientConnManager poolManager(int maxConnections) {
return cm;
}
+ public static String ifNonNull(Object cond, String value) {
+ return cond != null ? value : "";
+ }
+
+ public static String escapeXml(String value) {
+ if (value == null) {
+ return null;
+ }
+
+ StringBuilder sb = new StringBuilder(value.length());
+ for (int i = 0; i < value.length(); ++i) {
+ char ch = value.charAt(i);
+ switch (ch) {
+ case '&': sb.append("&amp;"); break;
+ case '<': sb.append("&lt;"); break;
+ case '>': sb.append("&gt;"); break;
+ case '"': sb.append("&quot;"); break;
+ case '\'': sb.append("&apos"); break;
+ default: sb.append(ch);
+ }
+ }
+
+ return sb.toString();
+ }
}
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2011, Mahmood Ali.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Mahmood Ali. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.notnoop.mpns.notifications;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map.Entry;
+
+import com.notnoop.mpns.MpnsNotification;
+
+public class RawNotification implements MpnsNotification {
+ private final String xml;
+
+ private final List<? extends Entry<String, String>> headers;
+
+ public RawNotification(String xml,
+ List<? extends Entry<String, String>> headers) {
+ this.xml = xml;
+
+ this.headers = headers;
+ }
+
+ public String getRequestBody() {
+ return xml;
+ }
+
+ public List<? extends Entry<String, String>> getHttpHeaders() {
+ return Collections.unmodifiableList(this.headers);
+ }
+}
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2011, Mahmood Ali.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Mahmood Ali. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.notnoop.mpns.notifications;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map.Entry;
+
+import com.notnoop.mpns.MpnsNotification;
+import static com.notnoop.mpns.internal.Utilities.ifNonNull;
+import static com.notnoop.mpns.internal.Utilities.escapeXml;
+
+public class TileNotification implements MpnsNotification {
+ private final String backgroundImage;
+ private final String title;
+ private int count;
+
+ private final String backBackgroundImage;
+ private final String backTitle;
+ private final String backContent;
+
+ private final List<? extends Entry<String, String>> headers;
+
+ public TileNotification(String backgroundImage, String title, int count,
+ String backBackgroundImage, String backTitle, String backContent,
+ List<? extends Entry<String, String>> headers) {
+ this.backgroundImage = backgroundImage;
+ this.title = title;
+ this.count = count;
+
+ this.backBackgroundImage = backBackgroundImage;
+ this.backTitle = backTitle;
+ this.backContent = backContent;
+
+ this.headers = headers;
+ }
+
+ public String getRequestBody() {
+ String tileMessage =
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
+ "<wp:Notification xmlns:wp=\"WPNotification\">" +
+ "<wp:Tile>" +
+ ifNonNull(backgroundImage, "<wp:BackgroundImage>" + escapeXml(backgroundImage) + "</wp:BackgroundImage>") +
+ "<wp:Count>" + count + "</wp:Count>" +
+ ifNonNull(title, "<wp:Title>" + escapeXml(title) + "</wp:Title>") +
+ ifNonNull(backBackgroundImage, "<wp:BackBackgroundImage><back side of tile image path></wp:BackBackgroundImage>") +
+ ifNonNull(backTitle, "<wp:BackTitle><back side of tile title></wp:BackTitle>") +
+ ifNonNull(backContent, "<wp:BackContent><back side of tile text></wp:BackContent>") +
+ "</wp:Tile> " +
+ "</wp:Notification>";
+
+ return tileMessage;
+ }
+
+ public List<? extends Entry<String, String>> getHttpHeaders() {
+ return Collections.unmodifiableList(this.headers);
+ }
+}
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2011, Mahmood Ali.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Mahmood Ali. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.notnoop.mpns.notifications;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map.Entry;
+
+import com.notnoop.mpns.MpnsNotification;
+import static com.notnoop.mpns.internal.Utilities.ifNonNull;
+import static com.notnoop.mpns.internal.Utilities.escapeXml;
+
+public class ToastNotification implements MpnsNotification {
+ private final String title;
+ private final String subtitle;
+ private final String parameter;
+
+ private final List<? extends Entry<String, String>> headers;
+
+ public ToastNotification(String title, String subtitle, String parameter,
+ List<? extends Entry<String, String>> headers) {
+ this.title = title;
+ this.subtitle = subtitle;
+ this.parameter = parameter;
+
+ this.headers = headers;
+ }
+
+ public String getRequestBody() {
+ String message =
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
+ "<wp:Notification xmlns:wp=\"WPNotification\">" +
+ "<wp:Toast>" +
+ ifNonNull(title, "<wp:Text1>" + escapeXml(title) + "</wp:Text1>") +
+ ifNonNull(subtitle, "<wp:Text2>" + escapeXml(subtitle) + "</wp:Text2>") +
+ ifNonNull(parameter, "<wp:Param>" + escapeXml(parameter) + "</wp:Param>") +
+ "</wp:Toast> " +
+ "</wp:Notification>";
+
+ return message;
+ }
+
+ public List<? extends Entry<String, String>> getHttpHeaders() {
+ return Collections.unmodifiableList(this.headers);
+ }
+}

0 comments on commit 9feff7e

Please sign in to comment.