/**
 * JBoss, Home of Professional Open Source
 * Copyright Red Hat, Inc., and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.aerogear.unifiedpush.service.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;

import org.jboss.aerogear.unifiedpush.api.AndroidVariant;
import org.jboss.aerogear.unifiedpush.api.Installation;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  Utility used for Android installations which need to be unsubscribed from topics
 */
public class GCMTopicManager {

    private final Logger logger = LoggerFactory.getLogger(GCMTopicManager.class);

    // Instance ID API URL
    public static final String IIDURL = "https://iid.googleapis.com/iid/v1/";

    private AndroidVariant variant;

    public GCMTopicManager(AndroidVariant variant) {
        this.variant = variant;
    }

    public Set<String> getSubscribedCategories(Installation installation) {
        String url = IIDURL + "info/" + installation.getDeviceToken() + "?details=true";
        String deviceInfo;
        try {
            deviceInfo = get(url);
        } catch (IOException e) {
            logger.debug("Couldn't get list of subscribed topics from Instance ID service.");
            return new HashSet<String>(0);
        }
        JSONParser parser = new JSONParser();
        JSONObject info;
        try {
            info = (JSONObject) parser.parse(deviceInfo);
        } catch (ParseException e) {
            logger.debug("Couldn't parse list of subscribed topics from Instance ID service.");
            return new HashSet<String>(0);
        }
        JSONObject rel = (JSONObject) info.get("rel");
        if (rel == null){
        	logger.debug("Couldn't parse rel object Instance ID service.");
            return new HashSet<String>(0);
        }
        JSONObject topics = (JSONObject) rel.get("topics");
        return topics.keySet();
    }


    /**
     * Unsubscribes device from single category(topic)
     *
     * @param installation Installation object containing correct variant property of AndroidVariant class
     * @param categoryToUnsubscribe category(topic) that device should be unsubscribed from
     */
    public void unsubscribe(Installation installation, String categoryToUnsubscribe) {

        String url = "";
        try {
            url = IIDURL + installation.getDeviceToken() + "/rel/topics/" + URLEncoder.encode(categoryToUnsubscribe, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            //
        }

        try {
            delete(url);
        } catch (IOException e) {
            logger.debug("Unregistering device from topic was unsuccessfull");
        }
    }

    /**
     * Sends DELETE HTTP request to provided URL. Request is authorized using Google API key.
     *
     * @param urlS target URL string
     */
    private int delete(String urlS) throws IOException {
        URL url = new URL(urlS);
        HttpURLConnection conn = prepareAuthorizedConnection(url);
        conn.setRequestMethod("DELETE");
        conn.connect();
        return conn.getResponseCode();
    }

    /**
     * Sends GET HTTP request to provided URL. Request is authorized using Google API key.
     *
     * @param urlS target URL string
     */
    private String get(String urlS) throws IOException {
        URL url = new URL(urlS);
        HttpURLConnection conn = prepareAuthorizedConnection(url);
        conn.setRequestMethod("GET");
        // Read response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder result = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
           result.append(line);
        }
        rd.close();
        return result.toString();
    }

    private HttpURLConnection prepareAuthorizedConnection(URL url) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded" );
        conn.setRequestProperty("Authorization", "key=" + variant.getGoogleKey());
        return conn;
    }

}
@@ -952,23 +952,27 @@
 va.util.
-HashSet
+Collections
 ;%0Aimport
@@ -2114,37 +2114,37 @@
  return 
-new HashSet%3CString%3E(0
+Collections.emptySet(
 );%0A     
@@ -2443,37 +2443,37 @@
  return 
-new HashSet%3CString%3E(0
+Collections.emptySet(
 );%0A     
@@ -2654,29 +2654,29 @@
 urn 
-new HashSet%3CString%3E(0
+Collections.emptySet(
 );%0A 

Apply code review comments by @idelpivnitskiy