Ignore:
Timestamp:
2015-12-10T22:11:49+01:00 (9 years ago)
Author:
floscher
Message:

[mapillary] Display OAuth response in UTF-8 (previously had problems with Umlauts) and make some code more readable

Location:
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/MapillaryUser.java

    r31799 r31807  
    99import org.openstreetmap.josm.Main;
    1010import org.openstreetmap.josm.plugins.mapillary.MapillaryPlugin;
     11import org.openstreetmap.josm.plugins.mapillary.io.download.MapillaryDownloader;
    1112
    1213/**
     
    2425  public static boolean isTokenValid = true;
    2526
     27  private MapillaryUser() {
     28    // Private constructor to avoid instantiation
     29  }
     30
    2631  /**
    27    * Returns the username of the logged user.
    28    *
    2932   * @return The username of the logged in user.
    3033   */
    3134  public static String getUsername() {
    32     if (!isTokenValid)
     35    if (!isTokenValid) {
    3336      return null;
    34     if (username == null)
     37    }
     38    if (username == null) {
    3539      try {
    3640        username = OAuthUtils
    37             .getWithHeader(
    38                 new URL(
    39                     "https://a.mapillary.com/v2/me?client_id="+MapillaryPlugin.CLIENT_ID))
     41            .getWithHeader(new URL(MapillaryDownloader.BASE_URL+"me?client_id="+MapillaryPlugin.CLIENT_ID))
    4042            .getString("username");
    4143      } catch (IOException e) {
    42         Main.info("Invalid Mapillary token, reseting field");
     44        Main.info("Invalid Mapillary token, resetting field");
    4345        reset();
    4446      }
     47    }
    4548    return username;
    4649  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListener.java

    r31796 r31807  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.plugins.mapillary.oauth;
    3 
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    53
    64import java.io.IOException;
     
    1412
    1513import org.openstreetmap.josm.Main;
     14import org.openstreetmap.josm.tools.I18n;
    1615
    1716/**
     
    2322 */
    2423public class OAuthPortListener extends Thread {
     24  public static final int PORT = 8763;
    2525
    26   protected static final String RESPONSE = tr("<html><head><title>Mapillary login</title></head><body>Login successful, return to JOSM.</body></html>");
     26  protected static final String RESPONSE = String.format(
     27      "<!DOCTYPE html><html><head><meta charset=\"utf8\"><title>%s</title></head><body>%s</body></html>",
     28      I18n.tr("Mapillary login"),
     29      I18n.tr("Login successful, return to JOSM.")
     30  );
     31
    2732
    2833  @Override
    2934  public void run() {
    3035    try {
    31       ServerSocket serverSocket = new ServerSocket(8763);
     36      ServerSocket serverSocket = new ServerSocket(PORT);
    3237      Socket clientSocket = serverSocket.accept();
    3338      PrintWriter out = new PrintWriter(new OutputStreamWriter(
     
    3742      String s;
    3843      String accessToken = null;
    39       while (in.hasNextLine()) {
     44      while (in.hasNextLine() && accessToken == null) {
    4045        s = in.nextLine();
    4146        if (s.contains("access_token=")) {
    4247          String[] ss = s.split("&");
    43           for (int i = 0; i < ss.length; i++) {
    44             if (ss[i].contains("access_token=")) {
    45               accessToken = ss[i].substring(
    46                   ss[i].indexOf("access_token=") + 13, ss[i].length());
    47               break;
     48          for (int i = 0; i < ss.length && accessToken == null; i++) {
     49            if (ss[i].startsWith("access_token=")) {
     50              accessToken = ss[i].substring(ss[i].indexOf("access_token=") + 13, ss[i].length());
    4851            }
    4952          }
    5053          break;
    51         } else if (s.contains("keep-alive"))
     54        } else if (s.contains("keep-alive")) {
    5255          break;
     56        }
    5357      }
    5458
     
    6165      MapillaryUser.reset();
    6266
    63       Main.info("Successful login with Mapillary, the access token is: "
    64           + accessToken);
     67      Main.info("Successful login with Mapillary, the access token is: " + accessToken);
    6568      // Saves the access token in preferences.
    6669      MapillaryUser.isTokenValid = true;
     
    7073      }
    7174    } catch (BindException e) {
     75      Main.warn(e);
    7276      return;
    7377    } catch (IOException e) {
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthUtils.java

    r31796 r31807  
    3434    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    3535    con.setRequestMethod("GET");
    36     con.setRequestProperty("Authorization",
    37         "Bearer " + Main.pref.get("mapillary.access-token"));
     36    con.setRequestProperty("Authorization", "Bearer " + Main.pref.get("mapillary.access-token"));
    3837
    3938    try (
Note: See TracChangeset for help on using the changeset viewer.