michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.background.announcements; michael@0: michael@0: import java.net.URI; michael@0: import java.net.URISyntaxException; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.ExtendedJSONObject; michael@0: michael@0: /** michael@0: * Represents a retrieved product announcement. michael@0: * michael@0: * Instances of this class are immutable. michael@0: */ michael@0: public class Announcement { michael@0: private static final String LOG_TAG = "Announcement"; michael@0: michael@0: private static final String KEY_ID = "id"; michael@0: private static final String KEY_TITLE = "title"; michael@0: private static final String KEY_URL = "url"; michael@0: private static final String KEY_TEXT = "text"; michael@0: michael@0: private final int id; michael@0: private final String title; michael@0: private final URI uri; michael@0: private final String text; michael@0: michael@0: public Announcement(int id, String title, String text, URI uri) { michael@0: this.id = id; michael@0: this.title = title; michael@0: this.uri = uri; michael@0: this.text = text; michael@0: } michael@0: michael@0: public static Announcement parseAnnouncement(ExtendedJSONObject body) throws URISyntaxException, IllegalArgumentException { michael@0: final Integer id = body.getIntegerSafely(KEY_ID); michael@0: if (id == null) { michael@0: throw new IllegalArgumentException("No id provided in JSON."); michael@0: } michael@0: final String title = body.getString(KEY_TITLE); michael@0: if (title == null || title.trim().length() == 0) { michael@0: throw new IllegalArgumentException("Missing or empty announcement title."); michael@0: } michael@0: final String uri = body.getString(KEY_URL); michael@0: if (uri == null) { michael@0: // Empty or otherwise unhappy URI will throw a URISyntaxException. michael@0: throw new IllegalArgumentException("Missing announcement URI."); michael@0: } michael@0: michael@0: final String text = body.getString(KEY_TEXT); michael@0: if (text == null) { michael@0: throw new IllegalArgumentException("Missing announcement body."); michael@0: } michael@0: michael@0: return new Announcement(id, title, text, new URI(uri)); michael@0: } michael@0: michael@0: public int getId() { michael@0: return id; michael@0: } michael@0: michael@0: public String getTitle() { michael@0: return title; michael@0: } michael@0: michael@0: public String getText() { michael@0: return text; michael@0: } michael@0: michael@0: public URI getUri() { michael@0: return uri; michael@0: } michael@0: michael@0: public ExtendedJSONObject asJSON() { michael@0: ExtendedJSONObject out = new ExtendedJSONObject(); michael@0: out.put(KEY_ID, id); michael@0: out.put(KEY_TITLE, title); michael@0: out.put(KEY_URL, uri.toASCIIString()); michael@0: out.put(KEY_TEXT, text); michael@0: return out; michael@0: } michael@0: michael@0: /** michael@0: * Return false if the provided Announcement is in some way invalid, michael@0: * regardless of being well-formed. michael@0: */ michael@0: public static boolean isValidAnnouncement(final Announcement an) { michael@0: final URI uri = an.getUri(); michael@0: if (uri == null) { michael@0: Logger.warn(LOG_TAG, "No URI: announcement not valid."); michael@0: return false; michael@0: } michael@0: michael@0: final String scheme = uri.getScheme(); michael@0: if (scheme == null) { michael@0: Logger.warn(LOG_TAG, "Null scheme: announcement not valid."); michael@0: return false; michael@0: } michael@0: michael@0: // Only allow HTTP and HTTPS URLs. michael@0: if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) { michael@0: Logger.warn(LOG_TAG, "Scheme '" + scheme + "' forbidden: announcement not valid."); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: }