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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     5 package org.mozilla.gecko.background.announcements;
     7 import java.net.URI;
     8 import java.net.URISyntaxException;
    10 import org.mozilla.gecko.background.common.log.Logger;
    11 import org.mozilla.gecko.sync.ExtendedJSONObject;
    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";
    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";
    26   private final int id;
    27   private final String title;
    28   private final URI uri;
    29   private final String text;
    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   }
    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     }
    53     final String text = body.getString(KEY_TEXT);
    54     if (text == null) {
    55       throw new IllegalArgumentException("Missing announcement body.");
    56     }
    58     return new Announcement(id, title, text, new URI(uri));
    59   }
    61   public int getId() {
    62     return id;
    63   }
    65   public String getTitle() {
    66     return title;
    67   }
    69   public String getText() {
    70     return text;
    71   }
    73   public URI getUri() {
    74     return uri;
    75   }
    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   }
    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     }
    97     final String scheme = uri.getScheme();
    98     if (scheme == null) {
    99       Logger.warn(LOG_TAG, "Null scheme: announcement not valid.");
   100       return false;
   101     }
   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     }
   109     return true;
   110   }
   111 }

mercurial