Changeset 19267 in osm


Ignore:
Timestamp:
2010-01-04T00:33:04+01:00 (15 years ago)
Author:
pieren
Message:

Several minor improvements.

Location:
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java

    r19078 r19267  
    1212import java.net.MalformedURLException;
    1313import java.net.URL;
     14import java.util.Date;
    1415import java.util.Vector;
    1516
     
    4344    }
    4445    private Vector<PlanImage> listOfFeuilles = new Vector<PlanImage>();
     46    private long cookieTimestamp;
    4547
    4648    final String baseURL = "http://www.cadastre.gouv.fr";
     
    5153    final String cOptionListEnd = "</option>";
    5254    final String cBBoxCommunStart = "new GeoBox(";
    53     final String cBBoxCommunEnd = ")";
     55    final String cBBoxCommunEnd = ")";   
    5456
    5557    final String cInterfaceVector = "afficherCarteCommune.do";
     
    5860    final String cImageLinkStart = "title=\"image\"><a href=\"#\" onClick=\"popup('afficherCarteFeuille.do?f=";
    5961    final String cImageNameStart = ">Feuille ";
     62   
     63    final static long cCookieExpiration = 30 * 60 * 1000; // 30 minutes expressed in milliseconds
     64
     65    final  int cRetriesGetCookie = 10; // 10 times every 3 seconds means 30 seconds trying to get a cookie
    6066
    6167    public boolean retrieveInterface(WMSLayer wmsLayer) throws DuplicateLayerException {
    6268        if (wmsLayer.getName().equals(""))
    6369            return false;
     70        if (wmsLayer.getName().equals(lastWMSLayerName))
     71            return true;
    6472        // open the session with the French Cadastre web front end
    6573        downloadCancelled = false;
    6674        try {
    67             if (cookie == null || !wmsLayer.getName().equals(lastWMSLayerName)) {
     75            if (cookie == null || isCookieExpired())
    6876                getCookie();
    69                 getInterface(wmsLayer);
    70                 this.lastWMSLayerName = wmsLayer.getName();
     77            if (cookie != null && interfaceRef == null) {
     78                    getInterface(wmsLayer);
     79                    this.lastWMSLayerName = wmsLayer.getName();
     80            } else {
     81                JOptionPane.showMessageDialog(Main.parent,
     82                        tr("Cannot open a new client session.\nServer in maintenance or temporary overloaded."));
     83                return false;
    7184            }
    7285            openInterface();
    7386        } catch (IOException e) {
    74             /*JOptionPane.showMessageDialog(Main.parent,
     87            JOptionPane.showMessageDialog(Main.parent,
    7588                    tr("Town/city {0} not found or not available\n" +
    76                             "or action canceled", wmsLayer.getLocation()));*/
    77             JOptionPane pane = new JOptionPane(
    78                     tr("Town/city {0} not found or not available\n" +
    79                             "or action canceled", wmsLayer.getLocation()),
    80                             JOptionPane.INFORMATION_MESSAGE);
    81             // this below is a temporary workaround to fix the "always on top" issue
    82             JDialog dialog = pane.createDialog(Main.parent, tr("Select commune"));
    83             CadastrePlugin.prepareDialog(dialog);
    84             dialog.setVisible(true);
    85             // till here
     89                            "or action canceled", wmsLayer.getLocation()));
    8690            return false;
    8791        }
     
    8993    }
    9094
     95    /**
     96     *
     97     * @return true if a cookie is delivered by WMS and false is WMS is not opening a client session
     98     *         (too many clients or in maintenance)
     99     * @throws IOException
     100     */
    91101    private void getCookie() throws IOException {
     102        boolean cookied = false;
     103        int retries = cRetriesGetCookie;
    92104        try {
    93             // first, get the cookie from Cadastre to allow next downloads
    94105            searchFormURL = new URL(baseURL + "/scpc/accueil.do");
    95             urlConn = (HttpURLConnection)searchFormURL.openConnection();
    96             urlConn.setRequestMethod("GET");
    97             urlConn.connect();
    98             if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
    99                 throw new IOException("Cannot get Cadastre cookie.");
    100             }
    101             System.out.println("GET "+searchFormURL);
    102             BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    103             while(in.readLine() != null) {}  // read the buffer otherwise we sent POST too early
    104             String headerName=null;
    105             for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
    106                 if (headerName.equals("Set-Cookie")) {
    107                     cookie = urlConn.getHeaderField(i);
    108                     cookie = cookie.substring(0, cookie.indexOf(";"));
    109                     System.out.println("Cookie="+cookie);
     106            while (cookied == false && retries > 0) {
     107                urlConn = (HttpURLConnection)searchFormURL.openConnection();
     108                urlConn.setRequestMethod("GET");
     109                urlConn.connect();
     110                if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
     111                    System.out.println("GET "+searchFormURL);
     112                    BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
     113                    while(in.readLine() != null) {}  // read the buffer otherwise we sent POST too early
     114                    String headerName=null;
     115                    for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
     116                        if (headerName.equals("Set-Cookie")) {
     117                            cookie = urlConn.getHeaderField(i);
     118                            cookie = cookie.substring(0, cookie.indexOf(";"));
     119                            cookieTimestamp = new Date().getTime();
     120                            System.out.println("received cookie=" + cookie + " at " + new Date(cookieTimestamp));
     121                            cookied = true;
     122                        }
     123                    }
     124                } else {
     125                    System.out.println("Request to home page failed. Http error:"+urlConn.getResponseCode()+". Try again "+retries+" times");
     126                    CadastrePlugin.safeSleep(3000);
     127                    retries --;
    110128                }
    111129            }
     
    118136    public void resetCookie() {
    119137        lastWMSLayerName = null;
    120     }
    121 
    122     public void resetCookieIfNewLayer(String newWMSLayerName) {
     138        cookie = null;
     139    }
     140   
     141    public boolean isCookieExpired() {
     142        long now = new Date().getTime();
     143        if ((now - cookieTimestamp) > cCookieExpiration) {
     144            System.out.println("cookie received at "+new Date(cookieTimestamp)+" expired (now is "+new Date(now)+")");
     145            return true;
     146        }
     147        return false;
     148    }
     149
     150    public void resetInterfaceRefIfNewLayer(String newWMSLayerName) {
    123151        if (!newWMSLayerName.equals(lastWMSLayerName)) {
    124             resetCookie();
     152            interfaceRef = null;
    125153        }
    126154    }
     
    154182                    int res = selectFeuilleDialog();
    155183                    if (res != -1) {
    156                         // TODO
    157184                        wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
    158185                        checkLayerDuplicates(wmsLayer);
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastrePlugin.java

    r19078 r19267  
    8787 *                 - removed autosourcing of empty new nodes
    8888 * 1.6 28-Nov-2009 - Fix minor issues if Grab is called without layer (possible since projection rework)
    89  * 1.7 12-Dec-2009 - Change URL's changes for cookie and downgrade imgs resolution due to WMS changes
     89 * 1.7 12-Dec-2009 - Change URL's changes for cookie and downgrade imgs resolution due to WMS changes
     90 * 1.8 xxx         - filter the mouse button 1 during georeferencing
     91 *                 - possibility to modify the auto-sourcing text just before upload
     92 *                 - retry if getting a new cookie failed (10 times during 30 seconds)
     93 *                 - cookie expiration automatically detected and renewed (after 30 minutes)
     94 *                 - proper WMS layer cleanup at destruction (workaround for memory leak)
    9095 */
    9196public class CadastrePlugin extends Plugin {
    92     static String VERSION = "1.7";
     97    static String VERSION = "1.8";
    9398
    9499    static JMenu cadastreJMenu;
     
    122127     */
    123128    public CadastrePlugin() throws Exception {
    124         System.out.println("Pluging \"cadastre-fr\" started...");
     129        System.out.println("Pluging cadastre-fr v"+VERSION+" started...");
    125130        if (Main.pref.get("cadastrewms.cacheDir").equals(""))
    126131            cacheDir = Main.pref.getPreferencesDir()+"plugins/cadastrewms/";
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionGrabPlanImage.java

    r18544 r19267  
    124124        else
    125125            mouseClickedTime = System.currentTimeMillis();
     126        if (e.getButton() != MouseEvent.BUTTON1)
     127            return;
    126128        countMouseClicked++;
    127129        EastNorth ea = Main.proj.latlon2eastNorth(Main.map.mapView.getLatLon(e.getX(), e.getY()));
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionNewLocation.java

    r18838 r19267  
    3939        String codeDepartement = "";
    4040        String codeCommune = "";
    41         boolean resetCookie = false;
     41        boolean changeInterface = false;
    4242        JLabel labelSectionNewLocation = new JLabel(tr("Add a new layer"));
    4343        JPanel p = new JPanel(new GridBagLayout());
     
    6666        if (!inputTown.getText().equals("")) {
    6767            location = inputTown.getText().toUpperCase();
    68             resetCookie = true;
     68            changeInterface = true;
    6969            Main.pref.put("cadastrewms.location", location);
    7070            Main.pref.put("cadastrewms.codeCommune", codeCommune);
     
    8989        } else if (existingLayers != null && existingLayers.size() > 0 && Main.map.mapView.getActiveLayer() instanceof WMSLayer) {
    9090            wmsLayer = (WMSLayer)Main.map.mapView.getActiveLayer();
    91             resetCookie = true;
     91            changeInterface = true;
    9292        }
    9393
    94         if (resetCookie)
    95             CadastrePlugin.cadastreGrabber.getWmsInterface().resetCookieIfNewLayer(wmsLayer.getName());
     94        if (changeInterface)
     95            CadastrePlugin.cadastreGrabber.getWmsInterface().resetInterfaceRefIfNewLayer(wmsLayer.getName());
    9696        return wmsLayer;
    9797    }
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSLayer.java

    r19149 r19267  
    8888    }
    8989
     90    public void destroy() {
     91        super.destroy();
     92        images = null;
     93        dividedBbox = null;
     94        System.out.println("Layer "+location+" destroyed");
     95    }
     96   
    9097    private static String buildName(String location, String codeCommune) {
    9198        String ret = new String(location.toUpperCase());
Note: See TracChangeset for help on using the changeset viewer.