Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.background.announcements; |
michael@0 | 6 | |
michael@0 | 7 | import java.net.URI; |
michael@0 | 8 | import java.net.URISyntaxException; |
michael@0 | 9 | |
michael@0 | 10 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 11 | import org.mozilla.gecko.sync.ExtendedJSONObject; |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * Represents a retrieved product announcement. |
michael@0 | 15 | * |
michael@0 | 16 | * Instances of this class are immutable. |
michael@0 | 17 | */ |
michael@0 | 18 | public class Announcement { |
michael@0 | 19 | private static final String LOG_TAG = "Announcement"; |
michael@0 | 20 | |
michael@0 | 21 | private static final String KEY_ID = "id"; |
michael@0 | 22 | private static final String KEY_TITLE = "title"; |
michael@0 | 23 | private static final String KEY_URL = "url"; |
michael@0 | 24 | private static final String KEY_TEXT = "text"; |
michael@0 | 25 | |
michael@0 | 26 | private final int id; |
michael@0 | 27 | private final String title; |
michael@0 | 28 | private final URI uri; |
michael@0 | 29 | private final String text; |
michael@0 | 30 | |
michael@0 | 31 | public Announcement(int id, String title, String text, URI uri) { |
michael@0 | 32 | this.id = id; |
michael@0 | 33 | this.title = title; |
michael@0 | 34 | this.uri = uri; |
michael@0 | 35 | this.text = text; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | public static Announcement parseAnnouncement(ExtendedJSONObject body) throws URISyntaxException, IllegalArgumentException { |
michael@0 | 39 | final Integer id = body.getIntegerSafely(KEY_ID); |
michael@0 | 40 | if (id == null) { |
michael@0 | 41 | throw new IllegalArgumentException("No id provided in JSON."); |
michael@0 | 42 | } |
michael@0 | 43 | final String title = body.getString(KEY_TITLE); |
michael@0 | 44 | if (title == null || title.trim().length() == 0) { |
michael@0 | 45 | throw new IllegalArgumentException("Missing or empty announcement title."); |
michael@0 | 46 | } |
michael@0 | 47 | final String uri = body.getString(KEY_URL); |
michael@0 | 48 | if (uri == null) { |
michael@0 | 49 | // Empty or otherwise unhappy URI will throw a URISyntaxException. |
michael@0 | 50 | throw new IllegalArgumentException("Missing announcement URI."); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | final String text = body.getString(KEY_TEXT); |
michael@0 | 54 | if (text == null) { |
michael@0 | 55 | throw new IllegalArgumentException("Missing announcement body."); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | return new Announcement(id, title, text, new URI(uri)); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | public int getId() { |
michael@0 | 62 | return id; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | public String getTitle() { |
michael@0 | 66 | return title; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | public String getText() { |
michael@0 | 70 | return text; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | public URI getUri() { |
michael@0 | 74 | return uri; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | public ExtendedJSONObject asJSON() { |
michael@0 | 78 | ExtendedJSONObject out = new ExtendedJSONObject(); |
michael@0 | 79 | out.put(KEY_ID, id); |
michael@0 | 80 | out.put(KEY_TITLE, title); |
michael@0 | 81 | out.put(KEY_URL, uri.toASCIIString()); |
michael@0 | 82 | out.put(KEY_TEXT, text); |
michael@0 | 83 | return out; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | /** |
michael@0 | 87 | * Return false if the provided Announcement is in some way invalid, |
michael@0 | 88 | * regardless of being well-formed. |
michael@0 | 89 | */ |
michael@0 | 90 | public static boolean isValidAnnouncement(final Announcement an) { |
michael@0 | 91 | final URI uri = an.getUri(); |
michael@0 | 92 | if (uri == null) { |
michael@0 | 93 | Logger.warn(LOG_TAG, "No URI: announcement not valid."); |
michael@0 | 94 | return false; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | final String scheme = uri.getScheme(); |
michael@0 | 98 | if (scheme == null) { |
michael@0 | 99 | Logger.warn(LOG_TAG, "Null scheme: announcement not valid."); |
michael@0 | 100 | return false; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | // Only allow HTTP and HTTPS URLs. |
michael@0 | 104 | if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) { |
michael@0 | 105 | Logger.warn(LOG_TAG, "Scheme '" + scheme + "' forbidden: announcement not valid."); |
michael@0 | 106 | return false; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | return true; |
michael@0 | 110 | } |
michael@0 | 111 | } |