mobile/android/base/background/announcements/Announcement.java

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

mercurial