コード例 #1
0
ファイル: OsmServerReader.java プロジェクト: harrymahar/josm
 /**
  * Open a connection to the given url and return a reader on the input stream from that
  * connection. In case of user cancel, return <code>null</code>. Relative URL's are directed to
  * API base URL.
  *
  * @param urlStr The url to connect to.
  * @param progressMonitor progress monitoring and abort handler
  * @return An reader reading the input stream (servers answer) or <code>null</code>.
  * @throws OsmTransferException thrown if data transfer errors occur
  */
 protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor)
     throws OsmTransferException {
   try {
     api.initialize(progressMonitor);
     urlStr = urlStr.startsWith("http") ? urlStr : (getBaseUrl() + urlStr);
     return getInputStreamRaw(urlStr, progressMonitor);
   } finally {
     progressMonitor.invalidate();
   }
 }
コード例 #2
0
  private void infoSync(LatLon pos, ProgressMonitor progressMonitor) {

    progressMonitor.beginTask(null, 3);
    try {
      mRuian.prepareData(pos);
      htmlText = mRuian.getHtml();
      coordinatesText = PointInfoUtils.formatCoordinates(pos.lat(), pos.lon());

    } finally {
      progressMonitor.finishTask();
    }
    progressMonitor.invalidate();
    if (htmlText.length() == 0) {
      GuiHelper.runInEDTAndWait(
          () ->
              PointInfoUtils.showNotification(
                  tr("Data not available.") + "\n(" + pos.toDisplayString() + ")", "warning"));
      return;
    }
  }
コード例 #3
0
ファイル: OsmServerReader.java プロジェクト: harrymahar/josm
  /**
   * Open a connection to the given url and return a reader on the input stream from that
   * connection. In case of user cancel, return <code>null</code>.
   *
   * @param urlStr The exact url to connect to.
   * @param progressMonitor progress monitoring and abort handler
   * @return An reader reading the input stream (servers answer) or <code>null</code>.
   * @throws OsmTransferException thrown if data transfer errors occur
   */
  protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor)
      throws OsmTransferException {
    try {
      URL url = null;
      try {
        url = new URL(urlStr.replace(" ", "%20"));
      } catch (MalformedURLException e) {
        throw new OsmTransferException(e);
      }
      try {
        // fix #7640, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive
        activeConnection = Utils.openHttpConnection(url, false);
      } catch (Exception e) {
        throw new OsmTransferException(
            tr("Failed to open connection to API {0}.", url.toExternalForm()), e);
      }
      if (cancel) {
        activeConnection.disconnect();
        return null;
      }

      if (doAuthenticate) {
        addAuth(activeConnection);
      }
      if (cancel) throw new OsmTransferCanceledException();
      if (Main.pref.getBoolean("osm-server.use-compression", true)) {
        activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
      }

      activeConnection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect", 15) * 1000);

      try {
        Main.info("GET " + url);
        activeConnection.connect();
      } catch (Exception e) {
        e.printStackTrace();
        OsmTransferException ote =
            new OsmTransferException(
                tr("Could not connect to the OSM server. Please check your internet connection."),
                e);
        ote.setUrl(url.toString());
        throw ote;
      }
      try {
        if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
          throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null);

        if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
          throw new OsmTransferCanceledException();

        String encoding = activeConnection.getContentEncoding();
        if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
          String errorHeader = activeConnection.getHeaderField("Error");
          StringBuilder errorBody = new StringBuilder();
          try {
            InputStream i = FixEncoding(activeConnection.getErrorStream(), encoding);
            if (i != null) {
              BufferedReader in = new BufferedReader(new InputStreamReader(i));
              String s;
              while ((s = in.readLine()) != null) {
                errorBody.append(s);
                errorBody.append("\n");
              }
            }
          } catch (Exception e) {
            errorBody.append(tr("Reading error text failed."));
          }

          throw new OsmApiException(
              activeConnection.getResponseCode(),
              errorHeader,
              errorBody.toString(),
              url.toString());
        }

        return FixEncoding(new ProgressInputStream(activeConnection, progressMonitor), encoding);
      } catch (OsmTransferException e) {
        throw e;
      } catch (Exception e) {
        throw new OsmTransferException(e);
      }
    } finally {
      progressMonitor.invalidate();
    }
  }