AnalyticalGraphicsInc / czml-writer

A library for writing CZML content for use with Cesium.
Other
395 stars 142 forks source link

CesiumStreamWriter fails if using external network resource from behind firewall #137

Closed docjason closed 7 years ago

docjason commented 7 years ago

If trying to create CZML from behind HTTP Proxy with network resource then CesiumStreamWriter fails.

Example:

    BillboardCesiumWriter billboard = packet.openBillboardProperty();
    billboard.writeColorProperty(123, 67, 0, 255);
    billboard.writeImageProperty(URI.create("http://cesiumjs.org/images/CesiumHeaderLogo.png"), CesiumResourceBehavior.EMBED);
    billboard.close();

One simple fix is adding check if http.proxy of https.proxy system property are set in System.property then define in proxy before creating HttpWebRequest in CesiumFormattingHelper.

a/Java/CesiumLanguageWriter/translatedSrc/cesiumlanguagewriter/advanced/CesiumFormattingHelper.java
+++ b/Java/CesiumLanguageWriter/translatedSrc/cesiumlanguagewriter/advanced/CesiumFormattingHelper.java
@@ -16,6 +16,7 @@ import agi.foundation.compatibility.WebResponse;
 import cesiumlanguagewriter.*;
 import java.awt.image.RenderedImage;
 import java.io.InputStream;
+import java.net.Proxy;

 /**
  *
@@ -105,6 +106,21 @@ public final class CesiumFormattingHelper {
             return uri;
         }
         WebRequest request = WebRequest.create(uri);
+
+        // patch starts here 
+        final boolean isHttps = uri.startsWith("https");
+        String proxyHost = System.getProperty(isHttps ? "https.proxyHost" : "http.proxyHost", "");
+        if (!proxyHost.isEmpty()) {
+               String proxyPort = System.getProperty(isHttps ? "https.proxyPort" : "http.proxyPort", "");
+               if (!proxyPort.isEmpty()) {
+                       int port = Integer.parseInt(proxyPort);
+                       Proxy proxy = new Proxy(Proxy.Type.HTTP, new java.net.InetSocketAddress(proxyHost, port));
+                       request.setProxy(proxy);
+               }
+        }
+        // patch ends here
+
         HttpWebRequest httpWebRequest = (request instanceof HttpWebRequest) ? (HttpWebRequest) request : null;
         if (httpWebRequest != null) {
             httpWebRequest.setUserAgent("CesiumWriter");
shunter commented 7 years ago

I tested this and the current code works correctly with an HTTP proxy. The code eventually calls URL.openConnection() - https://docs.oracle.com/javase/7/docs/api/java/net/URL.html#openConnection() which uses the configured Java proxy by default. Basically, all of the code you include to detect the Java system proxy configuration happens by default.

In my testing I can see that the connection is correctly made through the proxy when configured properly, either by using -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8888 on the command line, or by using Properties.setProperty.

If you're still having trouble, please provide more information about how your application is configured.