Permalink
Browse files

use byte[] instead of String

  • Loading branch information...
1 parent 6ca8624 commit 7d9403bfabe61692e8ad3ef038a18e82d86f82e1 @notnoop committed Jun 23, 2011
View
2 src/main/java/com/notnoop/mpns/MpnsNotification.java
@@ -42,7 +42,7 @@
* Returns the binary representation of the payload.
*
*/
- public String getRequestBody();
+ public byte[] getRequestBody();
/**
* Returns the identifier of the current message. The
View
16 src/main/java/com/notnoop/mpns/internal/AbstractMpnsService.java
@@ -30,14 +30,12 @@
*/
package com.notnoop.mpns.internal;
-import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Map.Entry;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.protocol.HTTP;
+import org.apache.http.entity.ByteArrayEntity;
import com.notnoop.mpns.MpnsNotification;
import com.notnoop.mpns.MpnsService;
@@ -51,14 +49,10 @@ protected AbstractMpnsService(HttpClient httpClient) {
this.httpClient = httpClient;
}
- protected HttpPost postMessage(String subscriptionUri, String requestBody, Collection<? extends Entry<String, String>> headers) {
+ protected HttpPost postMessage(String subscriptionUri, byte[] requestBody,
+ Collection<? extends Entry<String, String>> headers) {
HttpPost method = new HttpPost(subscriptionUri);
- try {
- method.setEntity(new StringEntity(requestBody,
- "text/xml", HTTP.UTF_8));
- } catch (UnsupportedEncodingException e) {
- throw new AssertionError("UTF-8 isn't supported!!!");
- }
+ method.setEntity(new ByteArrayEntity(requestBody));
for (Entry<String, String> header: headers) {
method.addHeader(header.getKey(), header.getValue());
@@ -72,7 +66,7 @@ protected HttpPost postMessage(String subscriptionUri, String requestBody, Colle
public void push(String subscriptionUri, String payload,
Collection<? extends Entry<String, String>> headers)
throws NetworkIOException {
- this.push(postMessage(subscriptionUri, payload, headers));
+ this.push(postMessage(subscriptionUri, Utilities.toUTF8(payload), headers));
}
public void push(String subscriptionUri, MpnsNotification message)
View
11 src/main/java/com/notnoop/mpns/internal/Utilities.java
@@ -35,10 +35,13 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.io.UnsupportedEncodingException;
public final class Utilities {
private Utilities() { throw new AssertionError("Uninstantiable class"); }
+ public static String XML_CONTENT_TYPE = "text/xml";
+
public static String readFully(InputStream is) {
final char[] buffer = new char[0x10000];
StringBuilder out = new StringBuilder();
@@ -91,4 +94,12 @@ public static String escapeXml(String value) {
return sb.toString();
}
+
+ public static byte[] toUTF8(String content) {
+ try {
+ return content.getBytes("UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ throw new AssertionError("The world is coming to an end! No UTF-8 support");
+ }
+ }
}
View
11 src/main/java/com/notnoop/mpns/notifications/AbstractNotificationBuilder.java
@@ -114,6 +114,17 @@ public A callbackUri(String callbackUri) {
return (A)this;
}
+ /**
+ * Sets the notification body content type
+ *
+ * @param contentType the content type of the body
+ * @return this
+ */
+ protected A contentType(String contentType) {
+ this.headers.add(Pair.of("Content-Type", contentType));
+ return (A)this;
+ }
+
protected abstract int deliveryValueOf(DeliveryClass delivery);
public abstract B build();
View
26 src/main/java/com/notnoop/mpns/notifications/RawNotification.java
@@ -36,36 +36,44 @@
import com.notnoop.mpns.DeliveryClass;
import com.notnoop.mpns.MpnsNotification;
+import com.notnoop.mpns.internal.Utilities;
public class RawNotification implements MpnsNotification {
- private final String xml;
+ private final byte[] body;
private final List<? extends Entry<String, String>> headers;
- public RawNotification(String xml,
+ public RawNotification(byte[] body,
List<? extends Entry<String, String>> headers) {
- this.xml = xml;
+ this.body = body;
this.headers = headers;
}
- public String getRequestBody() {
- return xml;
+ public byte[] getRequestBody() {
+ return body;
}
public List<? extends Entry<String, String>> getHttpHeaders() {
return Collections.unmodifiableList(this.headers);
}
public static class Builder extends AbstractNotificationBuilder<Builder, RawNotification> {
- private String xml;
+ private byte[] body;
public Builder() {
super("raw");
}
- public Builder xml(String xml) {
- this.xml = xml;
+ public Builder body(String body) {
+ this.body = Utilities.toUTF8(body);
+ return this;
+ }
+
+ public Builder body(byte[] body) {
+ byte[] copy = new byte[body.length];
+ System.arraycopy(body, 0, copy, 0, body.length);
+ this.body = body;
return this;
}
@@ -82,7 +90,7 @@ protected int deliveryValueOf(DeliveryClass delivery) {
@Override
public RawNotification build() {
- return new RawNotification(xml, headers);
+ return new RawNotification(body, headers);
}
}
}
View
6 src/main/java/com/notnoop/mpns/notifications/TileNotification.java
@@ -36,6 +36,7 @@
import com.notnoop.mpns.DeliveryClass;
import com.notnoop.mpns.MpnsNotification;
+import com.notnoop.mpns.internal.Utilities;
import static com.notnoop.mpns.internal.Utilities.ifNonNull;
import static com.notnoop.mpns.internal.Utilities.escapeXml;
@@ -65,7 +66,7 @@ public TileNotification(String backgroundImage, String title, int count,
this.headers = headers;
}
- public String getRequestBody() {
+ public byte[] getRequestBody() {
String tileMessage =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
@@ -79,7 +80,7 @@ public String getRequestBody() {
"</wp:Tile> " +
"</wp:Notification>";
- return tileMessage;
+ return Utilities.toUTF8(tileMessage);
}
public List<? extends Entry<String, String>> getHttpHeaders() {
@@ -92,6 +93,7 @@ public String getRequestBody() {
public Builder() {
super("token"); // TODO: Check whether it is "tile"
+ contentType(Utilities.XML_CONTENT_TYPE);
}
public Builder backgroundImage(String backgroundImage) {
View
6 src/main/java/com/notnoop/mpns/notifications/ToastNotification.java
@@ -36,6 +36,7 @@
import com.notnoop.mpns.DeliveryClass;
import com.notnoop.mpns.MpnsNotification;
+import com.notnoop.mpns.internal.Utilities;
import static com.notnoop.mpns.internal.Utilities.ifNonNull;
import static com.notnoop.mpns.internal.Utilities.escapeXml;
@@ -56,7 +57,7 @@ public ToastNotification(String title, String subtitle, String parameter,
this.headers = headers;
}
- public String getRequestBody() {
+ public byte[] getRequestBody() {
String message =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
@@ -67,7 +68,7 @@ public String getRequestBody() {
"</wp:Toast> " +
"</wp:Notification>";
- return message;
+ return Utilities.toUTF8(message);
}
public List<? extends Entry<String, String>> getHttpHeaders() {
@@ -79,6 +80,7 @@ public String getRequestBody() {
public Builder() {
super("toast");
+ contentType(Utilities.XML_CONTENT_TYPE);
}
public Builder title(String title) {

0 comments on commit 7d9403b

Please sign in to comment.