    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.oauth;
    003
    004
    005import java.io.IOException;
    006import java.util.HashMap;
    007import java.util.Map;
    008
    009import org.openstreetmap.josm.plugins.streetside.utils.StreetsideProperties;
    010import org.openstreetmap.josm.plugins.streetside.utils.StreetsideURL;
    011import org.openstreetmap.josm.tools.Logging;
    012
    013/**
    014* Represents the current logged in user and stores its data.
    015*
    016* @author nokutu
    017*
    018*/
    019public final class StreetsideUser {
    020
    021private static String username;
    022private static String imagesPolicy;
    023private static String imagesHash;
    024/** If the stored token is valid or not. */
    025private static boolean isTokenValid = true;
    026
    027private StreetsideUser() {
    028 // Private constructor to avoid instantiation
    029}
    030
    031/**
    032* @return The username of the logged in user.
    033*/
    034public static synchronized String getUsername() {
    035 if (!isTokenValid) {
    036   return null;
    037 }
    038 if (username == null) {
    039   try {
    040     username = OAuthUtils
    041         .getWithHeader(StreetsideURL.APIv3.userURL())
    042         .getString("username");
    043   } catch (IOException e) {
    044     Logging.log(Logging.LEVEL_WARN, "Invalid Streetside token, resetting field", e);
    045     reset();
    046   }
    047 }
    048 return username;
    049}
    050
    051/**
    052* @return A HashMap object containing the images_policy and images_hash
    053*         strings.
    054*/
    055public static synchronized Map<String, String> getSecrets() {
    056 if (!isTokenValid)
    057   return null;
    058 Map<String, String> hash = new HashMap<>();
    059 try {
    060   if (imagesHash == null)
    061     imagesHash = OAuthUtils
    062         .getWithHeader(StreetsideURL.uploadSecretsURL())
    063         .getString("images_hash", null);
    064   hash.put("images_hash", imagesHash);
    065   if (imagesPolicy == null)
    066     imagesPolicy = OAuthUtils
    067         .getWithHeader(StreetsideURL.uploadSecretsURL())
    068         .getString("images_policy");
    069 } catch (IOException e) {
    070   Logging.log(Logging.LEVEL_WARN, "Invalid Streetside token, resetting field", e);
    071   reset();
    072 }
    073 hash.put("images_policy", imagesPolicy);
    074 return hash;
    075}
    076
    077/**
    078* Resets the MapillaryUser to null values.
    079*/
    080public static synchronized void reset() {
    081 username = null;
    082 imagesPolicy = null;
    083 imagesHash = null;
    084 isTokenValid = false;
    085 StreetsideProperties.ACCESS_TOKEN.put(StreetsideProperties.ACCESS_TOKEN.getDefaultValue());
    086}
    087
    088public static synchronized void setTokenValid(boolean value) {
    089 isTokenValid = value;
    090}
    091}