Ignore:
Timestamp:
2017-12-28T19:35:04+01:00 (7 years ago)
Author:
donvip
Message:

use JOSM HttpClient

File:
1 edited

Legend:

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

    r33682 r33965  
    55
    66import java.awt.GridBagLayout;
    7 import java.io.BufferedReader;
    87import java.io.IOException;
    98import java.io.InputStream;
    10 import java.io.InputStreamReader;
    11 import java.io.OutputStream;
    129import java.net.CookieHandler;
    1310import java.net.HttpURLConnection;
     
    2017import java.util.HashMap;
    2118import java.util.List;
     19import java.util.Map.Entry;
    2220
    2321import javax.swing.JComboBox;
     
    3129import org.openstreetmap.josm.gui.MainApplication;
    3230import org.openstreetmap.josm.gui.layer.Layer;
    33 import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    3431import org.openstreetmap.josm.gui.util.GuiHelper;
    3532import org.openstreetmap.josm.io.OsmTransferException;
    36 import org.openstreetmap.josm.io.ProgressInputStream;
    3733import org.openstreetmap.josm.plugins.fr.cadastre.CadastrePlugin;
    3834import org.openstreetmap.josm.tools.GBC;
     35import org.openstreetmap.josm.tools.HttpClient;
     36import org.openstreetmap.josm.tools.HttpClient.Response;
    3937import org.openstreetmap.josm.tools.Logging;
    4038
    4139public class CadastreInterface {
    4240    public boolean downloadCanceled;
    43     private HttpURLConnection urlConn;
     41    private Response urlConn;
    4442
    4543    private String csrfToken;
     
    130128            searchFormURL = new URL(BASE_URL + "/scpc/accueil.do");
    131129            while (!success && retries > 0) {
    132                 urlConn = (HttpURLConnection) searchFormURL.openConnection();
    133                 urlConn.setRequestProperty("Connection", "close");
    134                 urlConn.setRequestMethod("GET");
    135                 urlConn.connect();
     130                urlConn = getHttpClient(searchFormURL).setHeader("Connection", "close").connect();
    136131                if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    137                     Logging.info("GET "+searchFormURL);
    138                     BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8));
    139                     while (in.readLine() != null) {
    140                         // read the buffer otherwise we sent POST too early
    141                     }
     132                    // read the buffer otherwise we sent POST too early
     133                    urlConn.fetchContent();
    142134                    success = true;
    143135                    // See https://bugs.openjdk.java.net/browse/JDK-8036017
     
    149141                        }
    150142                    } else {
    151                         String headerName;
    152                         for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) {
    153                             if ("Set-Cookie".equals(headerName) && handleCookie(urlConn.getHeaderField(i))) {
     143                        for (Entry<String, List<String>> e : urlConn.getHeaderFields().entrySet()) {
     144                            if ("Set-Cookie".equals(e.getKey()) && e.getValue() != null && !e.getValue().isEmpty()
     145                                    && handleCookie(e.getValue().get(0))) {
    154146                                break;
    155147                            }
     
    204196    }
    205197
    206     private void setCookie() {
    207         this.urlConn.setRequestProperty("Cookie", this.cookie);
    208     }
    209 
    210     public void setCookie(HttpURLConnection urlConn) {
    211         urlConn.setRequestProperty("Cookie", this.cookie);
     198    public HttpClient getHttpClient(URL url) {
     199        return HttpClient.create(url).setHeader("Cookie", cookie);
     200    }
     201
     202    public HttpClient getHttpClient(URL url, String method) {
     203        return HttpClient.create(url, method).setHeader("Cookie", cookie);
    212204    }
    213205
     
    255247            // finally, open the interface on server side giving access to the wms server
    256248            URL interfaceURL = new URL(BASE_URL + "/scpc/"+interfaceRef);
    257             urlConn = (HttpURLConnection) interfaceURL.openConnection();
    258             urlConn.setRequestMethod("GET");
    259             setCookie();
    260             urlConn.connect();
     249            urlConn = getHttpClient(interfaceURL).connect();
    261250            if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
    262251                throw new IOException("Cannot open Cadastre interface. GET response:"+urlConn.getResponseCode());
    263252            }
    264             Logging.info("GET "+interfaceURL);
    265             BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8));
    266253            // read the buffer otherwise we sent POST too early
    267             StringBuilder lines = new StringBuilder();
    268             String ln;
    269             while ((ln = in.readLine()) != null) {
    270                 if (Logging.isDebugEnabled()) {
    271                     lines.append(ln);
    272                 }
    273             }
     254            String lines = urlConn.fetchContent();
    274255            if (Logging.isDebugEnabled()) {
    275256                Logging.debug(lines.toString());
     
    318299            content += "&x=0&y=0";
    319300            searchFormURL = new URL(BASE_URL + "/scpc/rechercherPlan.do");
    320             urlConn = (HttpURLConnection) searchFormURL.openConnection();
    321             urlConn.setRequestMethod("POST");
    322             urlConn.setDoOutput(true);
    323             urlConn.setDoInput(true);
    324             setCookie();
    325             try (OutputStream wr = urlConn.getOutputStream()) {
    326                 wr.write(content.getBytes(StandardCharsets.UTF_8));
    327                 Logging.info("POST "+content);
    328                 wr.flush();
    329             }
    330             String ln;
    331             StringBuilder sb = new StringBuilder();
    332             try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8))) {
    333                 while ((ln = rd.readLine()) != null) {
    334                     sb.append(ln);
    335                 }
    336             }
    337             String lines = sb.toString();
     301            urlConn = getHttpClient(searchFormURL, "POST").setRequestBody(content.getBytes(StandardCharsets.UTF_8)).connect();
     302            String lines = urlConn.fetchContent();
    338303            urlConn.disconnect();
    339304            if (lines != null) {
     
    410375    private String getFeuillesList(String csrfToken) {
    411376        // get all images in one html page
    412         String ln = null;
    413         StringBuilder lines = new StringBuilder();
    414         HttpURLConnection urlConn2 = null;
    415377        try {
    416378            URL getAllImagesURL = new URL(BASE_URL + "/scpc/listerFeuillesParcommune.do?CSRF_TOKEN=" +
    417379                    csrfToken + "&keepVolatileSession=&offset=2000");
    418             urlConn2 = (HttpURLConnection) getAllImagesURL.openConnection();
    419             setCookie(urlConn2);
    420             urlConn2.connect();
    421             Logging.info("GET "+getAllImagesURL);
    422             try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream(), StandardCharsets.UTF_8))) {
    423                 while ((ln = rd.readLine()) != null) {
    424                     lines.append(ln);
    425                 }
    426             }
    427             urlConn2.disconnect();
     380            return getHttpClient(getAllImagesURL).connect().fetchContent();
    428381        } catch (IOException e) {
    429382            listOfFeuilles.clear();
    430383            Logging.error(e);
    431384        }
    432         return lines.toString();
     385        return "";
    433386    }
    434387
     
    518471            return;
    519472        // send GET opening normally the small window with the commune overview
    520         String content = BASE_URL + "/scpc/" + interfaceRef;
    521         content += "&dontSaveLastForward&keepVolatileSession=";
    522         searchFormURL = new URL(content);
    523         urlConn = (HttpURLConnection) searchFormURL.openConnection();
    524         urlConn.setRequestMethod("GET");
    525         setCookie();
    526         urlConn.connect();
    527         if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
    528             throw new IOException("Cannot get Cadastre response.");
    529         }
    530         Logging.info("GET "+searchFormURL);
    531         String ln;
    532         StringBuilder sb = new StringBuilder();
    533         try (BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8))) {
    534             while ((ln = in.readLine()) != null) {
    535                 sb.append(ln);
    536             }
    537         }
    538         urlConn.disconnect();
    539         String line = sb.toString();
     473        searchFormURL = new URL(BASE_URL + "/scpc/" + interfaceRef + "&dontSaveLastForward&keepVolatileSession=");
     474        String line = getHttpClient(searchFormURL).connect().fetchContent();
    540475        parseBBoxCommune(wmsLayer, line);
    541476        if (wmsLayer.isRaster() && !wmsLayer.isAlreadyGeoreferenced()) {
     
    610545    public void cancel() {
    611546        if (urlConn != null) {
    612             urlConn.setConnectTimeout(1);
    613             urlConn.setReadTimeout(1);
     547            urlConn.disconnect();
    614548        }
    615549        downloadCanceled = true;
     
    618552
    619553    public InputStream getContent(URL url) throws IOException, OsmTransferException {
    620         urlConn = (HttpURLConnection) url.openConnection();
    621         urlConn.setRequestProperty("Connection", "close");
    622         urlConn.setRequestMethod("GET");
    623         setCookie();
    624         return new ProgressInputStream(urlConn, NullProgressMonitor.INSTANCE);
     554        urlConn = getHttpClient(url).setHeader("Connection", "close").connect();
     555        return urlConn.getContent();
    625556    }
    626557}
Note: See TracChangeset for help on using the changeset viewer.