/**
 * 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 org.jboss.aerogear.unifiedpush.api.AndroidVariant;
import org.jboss.aerogear.unifiedpush.api.Installation;
import org.jboss.aerogear.unifiedpush.utils.AeroGearLogger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

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;

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

    private final AeroGearLogger logger = AeroGearLogger.getInstance(FCMTopicManager.class);

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

    private AndroidVariant variant;

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

    public Set<String> getSubscribedCategories(Installation installation) {
        String url = IID_URL + "info/" + installation.getDeviceToken() + "?details=true";
        String deviceInfo;
        try {
            deviceInfo = get(url);
        } catch (IOException e) {
            logger.fine("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.fine("Couldn't parse list of subscribed topics from Instance ID service.");
            return new HashSet<String>(0);
        }
        JSONObject rel = (JSONObject) info.get("rel");
        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 = IID_URL + installation.getDeviceToken() + "/rel/topics/" + URLEncoder.encode(categoryToUnsubscribe, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            //
        }

        try {
            delete(url);
        } catch (IOException e) {
            logger.fine("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;
    }

}
@@ -1229,24 +1229,66 @@
 URLEncoder;%0A
+import java.nio.charset.StandardCharsets;%0A
 import java.
@@ -3210,15 +3210,41 @@
 be, 
-%22UTF-8%22
+StandardCharsets.UTF_8.toString()
 );%0A 
@@ -4994,8 +4994,9 @@
     %7D%0A%0A%7D
+%0A

Use StandardCharsets.UTF_8 instead of magic string