Changeset 31807 in osm for applications
- Timestamp:
- 2015-12-10T22:11:49+01:00 (9 years ago)
- Location:
- applications/editors/josm/plugins/mapillary
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/MapillaryUser.java
r31799 r31807 9 9 import org.openstreetmap.josm.Main; 10 10 import org.openstreetmap.josm.plugins.mapillary.MapillaryPlugin; 11 import org.openstreetmap.josm.plugins.mapillary.io.download.MapillaryDownloader; 11 12 12 13 /** … … 24 25 public static boolean isTokenValid = true; 25 26 27 private MapillaryUser() { 28 // Private constructor to avoid instantiation 29 } 30 26 31 /** 27 * Returns the username of the logged user.28 *29 32 * @return The username of the logged in user. 30 33 */ 31 34 public static String getUsername() { 32 if (!isTokenValid) 35 if (!isTokenValid) { 33 36 return null; 34 if (username == null) 37 } 38 if (username == null) { 35 39 try { 36 40 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)) 40 42 .getString("username"); 41 43 } catch (IOException e) { 42 Main.info("Invalid Mapillary token, reset ing field");44 Main.info("Invalid Mapillary token, resetting field"); 43 45 reset(); 44 46 } 47 } 45 48 return username; 46 49 } -
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListener.java
r31796 r31807 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.plugins.mapillary.oauth; 3 4 import static org.openstreetmap.josm.tools.I18n.tr;5 3 6 4 import java.io.IOException; … … 14 12 15 13 import org.openstreetmap.josm.Main; 14 import org.openstreetmap.josm.tools.I18n; 16 15 17 16 /** … … 23 22 */ 24 23 public class OAuthPortListener extends Thread { 24 public static final int PORT = 8763; 25 25 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 27 32 28 33 @Override 29 34 public void run() { 30 35 try { 31 ServerSocket serverSocket = new ServerSocket( 8763);36 ServerSocket serverSocket = new ServerSocket(PORT); 32 37 Socket clientSocket = serverSocket.accept(); 33 38 PrintWriter out = new PrintWriter(new OutputStreamWriter( … … 37 42 String s; 38 43 String accessToken = null; 39 while (in.hasNextLine() ) {44 while (in.hasNextLine() && accessToken == null) { 40 45 s = in.nextLine(); 41 46 if (s.contains("access_token=")) { 42 47 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()); 48 51 } 49 52 } 50 53 break; 51 } else if (s.contains("keep-alive")) 54 } else if (s.contains("keep-alive")) { 52 55 break; 56 } 53 57 } 54 58 … … 61 65 MapillaryUser.reset(); 62 66 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); 65 68 // Saves the access token in preferences. 66 69 MapillaryUser.isTokenValid = true; … … 70 73 } 71 74 } catch (BindException e) { 75 Main.warn(e); 72 76 return; 73 77 } catch (IOException e) { -
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthUtils.java
r31796 r31807 34 34 HttpURLConnection con = (HttpURLConnection) url.openConnection(); 35 35 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")); 38 37 39 38 try ( -
applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListenerTest.java
r31488 r31807 34 34 } 35 35 try { 36 URL url = new URL("http://localhost: 8763?access_token=access_token");36 URL url = new URL("http://localhost:"+OAuthPortListener.PORT+"?access_token=access_token"); 37 37 HttpURLConnection con = (HttpURLConnection) url.openConnection(); 38 38 BufferedReader in = new BufferedReader(new InputStreamReader(
Note:
See TracChangeset
for help on using the changeset viewer.