Ignore:
Timestamp:
2015-07-16T13:56:24+02:00 (10 years ago)
Author:
nokutu
Message:

Login system implemented

Location:
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary
Files:
2 deleted
5 edited

Legend:

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

    r31386 r31390  
    1313/**
    1414 * Abstract superclass for all image objects. At the moment there is just 2,
    15  * {@code MapillaryImportedImage} and {@code MapillaryImage}.
     15 * {@link MapillaryImportedImage} and {@link MapillaryImage}.
    1616 *
    1717 * @author nokutu
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryImportedImage.java

    r31386 r31390  
    9595  private static String currentDate() {
    9696    Calendar cal = Calendar.getInstance();
    97 
    9897    SimpleDateFormat formatter = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
    9998    return formatter.format(cal.getTime());
    100 
    10199  }
    102100}
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/HyperlinkLabel.java

    r31387 r31390  
    109109  /**
    110110   * Fires an ActionEvent to all interested listeners.
     111   * @param evt
    111112   */
    112113  protected void fireActionPerformed(ActionEvent evt) {
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryPreferenceSetting.java

    r31389 r31390  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
     5import java.awt.Desktop;
    56import java.awt.FlowLayout;
    67import java.awt.event.ActionEvent;
     8import java.io.IOException;
     9import java.net.URI;
     10import java.net.URISyntaxException;
    711
    812import javax.swing.AbstractAction;
    913import javax.swing.JButton;
    1014import javax.swing.JCheckBox;
    11 import javax.swing.JDialog;
    12 import javax.swing.JOptionPane;
    1315import javax.swing.JPanel;
    1416
     
    1820import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
    1921import org.openstreetmap.josm.plugins.mapillary.MapillaryPlugin;
     22import org.openstreetmap.josm.plugins.mapillary.oauth.PortListener;
    2023
    2124/**
     
    97100    @Override
    98101    public void actionPerformed(ActionEvent arg0) {
    99       JButton login = new JButton();
    100       JButton cancel = new JButton();
    101       JOptionPane pane = new JOptionPane(new MapillaryOAuthUI(),
    102           JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null,
    103           new JButton[] { login, cancel });
    104       login.setAction(new LoginAction(pane));
    105       cancel.setAction(new CancelAction(pane));
    106       JDialog dlg = pane.createDialog(Main.parent, tr("Login"));
    107       dlg.setVisible(true);
    108       dlg.dispose();
    109     }
    110   }
     102      PortListener portListener = new PortListener();
     103      portListener.start();
    111104
    112   private class LoginAction extends AbstractAction {
    113 
    114     private static final long serialVersionUID = -7157028112711343289L;
    115 
    116     private JOptionPane pane;
    117 
    118     public LoginAction(JOptionPane pane) {
    119       putValue(NAME, tr("Login"));
    120       this.pane = pane;
    121     }
    122 
    123     @Override
    124     public void actionPerformed(ActionEvent e) {
    125       pane.setValue(JOptionPane.OK_OPTION);
    126     }
    127   }
    128 
    129   private class CancelAction extends AbstractAction {
    130 
    131     private static final long serialVersionUID = 2329066472975953270L;
    132 
    133     private JOptionPane pane;
    134 
    135     public CancelAction(JOptionPane pane) {
    136       putValue(NAME, tr("Cancel"));
    137       this.pane = pane;
    138     }
    139 
    140     @Override
    141     public void actionPerformed(ActionEvent e) {
    142       pane.setValue(JOptionPane.CANCEL_OPTION);
     105      String url = "http://www.mapillary.io/connect?redirect_uri=http:%2F%2Flocalhost:8763%2F&client_id=MkJKbDA0bnZuZlcxeTJHTmFqN3g1dzplZTlkZjQyYjYyZTczOTdi&response_type=token&scope=user:email";
     106      Desktop desktop = Desktop.getDesktop();
     107      try {
     108        desktop.browse(new URI(url));
     109      } catch (IOException | URISyntaxException ex) {
     110        ex.printStackTrace();
     111      } catch (UnsupportedOperationException ex) {
     112        Runtime runtime = Runtime.getRuntime();
     113        try {
     114          runtime.exec("xdg-open " + url);
     115        } catch (IOException exc) {
     116          exc.printStackTrace();
     117        }
     118      }
    143119    }
    144120  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/PortListener.java

    r31388 r31390  
    88import java.util.Scanner;
    99
    10 import javax.swing.JLabel;
    11 import javax.swing.SwingUtilities;
    12 
    1310import org.openstreetmap.josm.Main;
    1411
     
    1815 */
    1916public class PortListener extends Thread {
    20 
    21   JLabel text;
    22 
    23   /**
    24    * Main constructor.
    25    *
    26    * @param text
    27    */
    28   public PortListener(JLabel text) {
    29     this.text = text;
    30   }
    3117
    3218  @Override
     
    3824      Scanner in = new Scanner(new InputStreamReader(clientSocket.getInputStream()));
    3925      String s;
    40       String code = null;
     26      String accessToken = null;
    4127      while (in.hasNextLine()) {
    4228        s = in.nextLine();
    43         if (s.contains("?code=")) {
    44           code = s.substring(s.indexOf("=") + 1, s.indexOf("HTTP") - 1);
     29        if (s.contains("access_token=")) {
     30          String[] ss = s.split("&");
     31          for (int i = 0; i < ss.length; i++) {
     32            if (ss[i].contains("access_token=")) {
     33              accessToken = ss[i].substring(ss[i].indexOf("access_token=") + 13, ss[i].length());
     34              break;
     35            }
     36          }
    4537          break;
    4638        }
     
    4941      writeContent(out);
    5042
    51       System.out.println("The code is: " + code);
     43      Main.info("Successful login with Mapillary, the access token is: " + accessToken);
     44      Main.pref.put("mapillary.access-token", accessToken);
    5245
    5346      out.close();
     
    5750      Main.error(e);
    5851    }
    59     if (!SwingUtilities.isEventDispatchThread()) {
    60       SwingUtilities.invokeLater(new Runnable() {
    61         @Override
    62         public void run() {
    63           text.setText("Authorization successful");
    64         }
    65       });
    66     } else
    67       text.setText("Authorization successful");
    6852  }
    6953
    7054  private void writeContent(PrintWriter out) {
    71     String response = "";
    72     response += "<html><body>Authorization successful</body></html>";
     55    String response = "<html><head><title>Mapillary login</title></head><body>Login successful, return to JOSM.</body></html>";
    7356    out.println("HTTP/1.1 200 OK");
    7457    out.println("Content-Length: " + response.length());
Note: See TracChangeset for help on using the changeset viewer.