1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/background/announcements/Announcement.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,111 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.background.announcements; 1.9 + 1.10 +import java.net.URI; 1.11 +import java.net.URISyntaxException; 1.12 + 1.13 +import org.mozilla.gecko.background.common.log.Logger; 1.14 +import org.mozilla.gecko.sync.ExtendedJSONObject; 1.15 + 1.16 +/** 1.17 + * Represents a retrieved product announcement. 1.18 + * 1.19 + * Instances of this class are immutable. 1.20 + */ 1.21 +public class Announcement { 1.22 + private static final String LOG_TAG = "Announcement"; 1.23 + 1.24 + private static final String KEY_ID = "id"; 1.25 + private static final String KEY_TITLE = "title"; 1.26 + private static final String KEY_URL = "url"; 1.27 + private static final String KEY_TEXT = "text"; 1.28 + 1.29 + private final int id; 1.30 + private final String title; 1.31 + private final URI uri; 1.32 + private final String text; 1.33 + 1.34 + public Announcement(int id, String title, String text, URI uri) { 1.35 + this.id = id; 1.36 + this.title = title; 1.37 + this.uri = uri; 1.38 + this.text = text; 1.39 + } 1.40 + 1.41 + public static Announcement parseAnnouncement(ExtendedJSONObject body) throws URISyntaxException, IllegalArgumentException { 1.42 + final Integer id = body.getIntegerSafely(KEY_ID); 1.43 + if (id == null) { 1.44 + throw new IllegalArgumentException("No id provided in JSON."); 1.45 + } 1.46 + final String title = body.getString(KEY_TITLE); 1.47 + if (title == null || title.trim().length() == 0) { 1.48 + throw new IllegalArgumentException("Missing or empty announcement title."); 1.49 + } 1.50 + final String uri = body.getString(KEY_URL); 1.51 + if (uri == null) { 1.52 + // Empty or otherwise unhappy URI will throw a URISyntaxException. 1.53 + throw new IllegalArgumentException("Missing announcement URI."); 1.54 + } 1.55 + 1.56 + final String text = body.getString(KEY_TEXT); 1.57 + if (text == null) { 1.58 + throw new IllegalArgumentException("Missing announcement body."); 1.59 + } 1.60 + 1.61 + return new Announcement(id, title, text, new URI(uri)); 1.62 + } 1.63 + 1.64 + public int getId() { 1.65 + return id; 1.66 + } 1.67 + 1.68 + public String getTitle() { 1.69 + return title; 1.70 + } 1.71 + 1.72 + public String getText() { 1.73 + return text; 1.74 + } 1.75 + 1.76 + public URI getUri() { 1.77 + return uri; 1.78 + } 1.79 + 1.80 + public ExtendedJSONObject asJSON() { 1.81 + ExtendedJSONObject out = new ExtendedJSONObject(); 1.82 + out.put(KEY_ID, id); 1.83 + out.put(KEY_TITLE, title); 1.84 + out.put(KEY_URL, uri.toASCIIString()); 1.85 + out.put(KEY_TEXT, text); 1.86 + return out; 1.87 + } 1.88 + 1.89 + /** 1.90 + * Return false if the provided Announcement is in some way invalid, 1.91 + * regardless of being well-formed. 1.92 + */ 1.93 + public static boolean isValidAnnouncement(final Announcement an) { 1.94 + final URI uri = an.getUri(); 1.95 + if (uri == null) { 1.96 + Logger.warn(LOG_TAG, "No URI: announcement not valid."); 1.97 + return false; 1.98 + } 1.99 + 1.100 + final String scheme = uri.getScheme(); 1.101 + if (scheme == null) { 1.102 + Logger.warn(LOG_TAG, "Null scheme: announcement not valid."); 1.103 + return false; 1.104 + } 1.105 + 1.106 + // Only allow HTTP and HTTPS URLs. 1.107 + if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) { 1.108 + Logger.warn(LOG_TAG, "Scheme '" + scheme + "' forbidden: announcement not valid."); 1.109 + return false; 1.110 + } 1.111 + 1.112 + return true; 1.113 + } 1.114 +}