Changeset 33965 in osm for applications/editors/josm/plugins
- Timestamp:
- 2017-12-28T19:35:04+01:00 (7 years ago)
- 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 5 5 6 6 import java.awt.GridBagLayout; 7 import java.io.BufferedReader;8 7 import java.io.IOException; 9 8 import java.io.InputStream; 10 import java.io.InputStreamReader;11 import java.io.OutputStream;12 9 import java.net.CookieHandler; 13 10 import java.net.HttpURLConnection; … … 20 17 import java.util.HashMap; 21 18 import java.util.List; 19 import java.util.Map.Entry; 22 20 23 21 import javax.swing.JComboBox; … … 31 29 import org.openstreetmap.josm.gui.MainApplication; 32 30 import org.openstreetmap.josm.gui.layer.Layer; 33 import org.openstreetmap.josm.gui.progress.NullProgressMonitor;34 31 import org.openstreetmap.josm.gui.util.GuiHelper; 35 32 import org.openstreetmap.josm.io.OsmTransferException; 36 import org.openstreetmap.josm.io.ProgressInputStream;37 33 import org.openstreetmap.josm.plugins.fr.cadastre.CadastrePlugin; 38 34 import org.openstreetmap.josm.tools.GBC; 35 import org.openstreetmap.josm.tools.HttpClient; 36 import org.openstreetmap.josm.tools.HttpClient.Response; 39 37 import org.openstreetmap.josm.tools.Logging; 40 38 41 39 public class CadastreInterface { 42 40 public boolean downloadCanceled; 43 private HttpURLConnectionurlConn;41 private Response urlConn; 44 42 45 43 private String csrfToken; … … 130 128 searchFormURL = new URL(BASE_URL + "/scpc/accueil.do"); 131 129 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(); 136 131 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(); 142 134 success = true; 143 135 // See https://bugs.openjdk.java.net/browse/JDK-8036017 … … 149 141 } 150 142 } 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))) { 154 146 break; 155 147 } … … 204 196 } 205 197 206 p rivate 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); 212 204 } 213 205 … … 255 247 // finally, open the interface on server side giving access to the wms server 256 248 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(); 261 250 if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) { 262 251 throw new IOException("Cannot open Cadastre interface. GET response:"+urlConn.getResponseCode()); 263 252 } 264 Logging.info("GET "+interfaceURL);265 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8));266 253 // 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(); 274 255 if (Logging.isDebugEnabled()) { 275 256 Logging.debug(lines.toString()); … … 318 299 content += "&x=0&y=0"; 319 300 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(); 338 303 urlConn.disconnect(); 339 304 if (lines != null) { … … 410 375 private String getFeuillesList(String csrfToken) { 411 376 // get all images in one html page 412 String ln = null;413 StringBuilder lines = new StringBuilder();414 HttpURLConnection urlConn2 = null;415 377 try { 416 378 URL getAllImagesURL = new URL(BASE_URL + "/scpc/listerFeuillesParcommune.do?CSRF_TOKEN=" + 417 379 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(); 428 381 } catch (IOException e) { 429 382 listOfFeuilles.clear(); 430 383 Logging.error(e); 431 384 } 432 return lines.toString();385 return ""; 433 386 } 434 387 … … 518 471 return; 519 472 // 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(); 540 475 parseBBoxCommune(wmsLayer, line); 541 476 if (wmsLayer.isRaster() && !wmsLayer.isAlreadyGeoreferenced()) { … … 610 545 public void cancel() { 611 546 if (urlConn != null) { 612 urlConn.setConnectTimeout(1); 613 urlConn.setReadTimeout(1); 547 urlConn.disconnect(); 614 548 } 615 549 downloadCanceled = true; … … 618 552 619 553 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(); 625 556 } 626 557 }
Note:
See TracChangeset
for help on using the changeset viewer.