NetworkCrawler.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.data;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.URISyntaxException;
  22. import java.net.URL;
  23. import java.net.URLConnection;

  24. import org.hipparchus.exception.DummyLocalizable;
  25. import org.orekit.errors.OrekitException;


  26. /** Provider for data files directly fetched from network.
  27.  * <p>
  28.  * This class handles a list of URLs pointing to data files or zip/jar on
  29.  * the net. Since the net is not a tree structure the list elements
  30.  * cannot be top elements recursively browsed as in {@link
  31.  * DirectoryCrawler}, they must be data files or zip/jar archives.
  32.  * </p>
  33.  * <p>
  34.  * The files fetched from network can be locally cached on disk. This prevents
  35.  * too frequent network access if the URLs are remote ones (for example
  36.  * original internet URLs).
  37.  * </p>
  38.  * <p>
  39.  * If the URL points to a remote server (typically on the web) on the other side
  40.  * of a proxy server, you need to configure the networking layer of your
  41.  * application to use the proxy. For a typical authenticating proxy as used in
  42.  * many corporate environments, this can be done as follows using for example
  43.  * the AuthenticatorDialog graphical authenticator class that can be found
  44.  * in the tests directories:
  45.  * <pre>
  46.  *   System.setProperty("http.proxyHost",     "proxy.your.domain.com");
  47.  *   System.setProperty("http.proxyPort",     "8080");
  48.  *   System.setProperty("http.nonProxyHosts", "localhost|*.your.domain.com");
  49.  *   Authenticator.setDefault(new AuthenticatorDialog());
  50.  * </pre>
  51.  *
  52.  * <p>
  53.  * All {@link FiltersManager#addFilter(DataFilter) registered}
  54.  * {@link DataFilter filters} are applied.
  55.  * </p>
  56.  * <p>
  57.  * Zip archives entries are supported recursively.
  58.  * </p>
  59.  * <p>
  60.  * This is a simple application of the <code>visitor</code> design pattern for
  61.  * list browsing.
  62.  * </p>
  63.  * @see DataProvidersManager
  64.  * @author Luc Maisonobe
  65.  */
  66. public class NetworkCrawler extends AbstractListCrawler<URL> {

  67.     /** Connection timeout (milliseconds). */
  68.     private int timeout;

  69.     /** Build a data classpath crawler.
  70.      * <p>The default timeout is set to 10 seconds.</p>
  71.      * @param inputs list of input file URLs
  72.      */
  73.     public NetworkCrawler(final URL... inputs) {
  74.         super(inputs);
  75.         timeout = 10000;
  76.     }

  77.     /** Set the timeout for connection.
  78.      * @param timeout connection timeout in milliseconds
  79.      */
  80.     public void setTimeout(final int timeout) {
  81.         this.timeout = timeout;
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     protected String getCompleteName(final URL input) {
  86.         try {
  87.             return input.toURI().toString();
  88.         } catch (URISyntaxException ue) {
  89.             throw new OrekitException(ue, new DummyLocalizable(ue.getMessage()));
  90.         }
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     protected String getBaseName(final URL input) {
  95.         return new File(input.getPath()).getName();
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     protected ZipJarCrawler getZipJarCrawler(final URL input) {
  100.         return new ZipJarCrawler(input);
  101.     }

  102.     /** {@inheritDoc} */
  103.     @Override
  104.     protected InputStream getStream(final URL input) throws IOException {
  105.         final URLConnection connection = input.openConnection();
  106.         connection.setConnectTimeout(timeout);
  107.         return connection.getInputStream();
  108.     }

  109. }