001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins.streetside.oauth;
003
004import java.io.BufferedReader;
005import java.io.IOException;
006import java.io.InputStreamReader;
007import java.net.HttpURLConnection;
008import java.net.URL;
009
010import javax.json.Json;
011import javax.json.JsonException;
012import javax.json.JsonObject;
013import javax.json.JsonReader;
014
015import org.openstreetmap.josm.plugins.streetside.utils.StreetsideProperties;
016
017/**
018* A set of utilities related to OAuth.
019*
020* @author nokutu
021*
022*/
023public final class OAuthUtils {
024
025private OAuthUtils() {
026 // Private constructor to avoid instantiation
027}
028
029/**
030* Returns a JsonObject containing the result of making a GET request with the
031* authorization header.
032*
033* @param url
034*          The {@link URL} where the request must be made.
035* @return A JsonObject containing the result of the GET request.
036* @throws IOException
037*           Errors relating to the connection.
038*/
039public static JsonObject getWithHeader(URL url) throws IOException {
040 HttpURLConnection con = (HttpURLConnection) url.openConnection();
041 con.setRequestMethod("GET");
042 con.setRequestProperty("Authorization", "Bearer " + StreetsideProperties.ACCESS_TOKEN.get());
043
044 try (
045   JsonReader reader = Json.createReader(new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")))
046 ) {
047   return reader.readObject();
048 } catch (JsonException e) {
049   throw new IOException(e);
050 }
051}
052}