Skip to content

Commit

Permalink
Optimize URL connection processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ComBatVision committed Jul 18, 2022
1 parent 1dce5b7 commit 07df859
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;
Expand All @@ -20,7 +20,6 @@
import gov.nasa.worldwind.util.Logger;
import gov.nasa.worldwind.util.Retriever;
import gov.nasa.worldwind.util.SynchronizedPool;
import gov.nasa.worldwind.util.WWUtil;

public class ElevationRetriever extends Retriever<ImageSource, Void, ShortBuffer> {

Expand Down Expand Up @@ -64,13 +63,13 @@ protected ShortBuffer decodeUrl(String urlString) throws IOException {
// TODO retry absent resources, they are currently handled but suppressed entirely after the first failure
// TODO configurable connect and read timeouts

InputStream stream = null;
HttpURLConnection conn = null;
try {
URLConnection conn = new URL(urlString).openConnection();
conn = (HttpURLConnection) new URL(urlString).openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(30000);

stream = new BufferedInputStream(conn.getInputStream());
InputStream stream = new BufferedInputStream(conn.getInputStream());
String contentType = conn.getContentType();
if (contentType.equalsIgnoreCase("application/bil16")) {
return this.readInt16Data(stream);
Expand All @@ -81,7 +80,9 @@ protected ShortBuffer decodeUrl(String urlString) throws IOException {
Logger.logMessage(Logger.ERROR, "ElevationRetriever", "decodeUrl", "Format not supported"));
}
} finally {
WWUtil.closeSilently(stream);
if (conn != null) {
conn.disconnect();
}
}
}

Expand Down
23 changes: 13 additions & 10 deletions worldwind/src/main/java/gov/nasa/worldwind/layer/LayerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -53,7 +53,6 @@
import gov.nasa.worldwind.util.LevelSetConfig;
import gov.nasa.worldwind.util.Logger;
import gov.nasa.worldwind.util.TileFactory;
import gov.nasa.worldwind.util.WWUtil;

public class LayerFactory {

Expand Down Expand Up @@ -432,7 +431,7 @@ protected void createWmtsLayer(WmtsLayer wmtsLayer, Layer layer, Callback callba
}

protected WmsCapabilities retrieveWmsCapabilities(String serviceAddress) {
InputStream inputStream = null;
HttpURLConnection conn = null;
WmsCapabilities wmsCapabilities;
try {
// Build the appropriate request Uri given the provided service address
Expand All @@ -443,25 +442,27 @@ protected WmsCapabilities retrieveWmsCapabilities(String serviceAddress) {
.build();

// Open the connection as an input stream
URLConnection conn = new URL(serviceUri.toString()).openConnection();
conn = (HttpURLConnection) new URL(serviceUri.toString()).openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(30000);
inputStream = new BufferedInputStream(conn.getInputStream());
InputStream inputStream = new BufferedInputStream(conn.getInputStream());

// Parse and read the input stream
wmsCapabilities = WmsCapabilities.getCapabilities(inputStream);
} catch (Exception e) {
throw new RuntimeException(
Logger.makeMessage("LayerFactory", "retrieveWmsCapabilities", "Unable to open connection and read from service address"));
} finally {
WWUtil.closeSilently(inputStream);
if (conn != null) {
conn.disconnect();
}
}

return wmsCapabilities;
}

protected WmtsCapabilities retrieveWmtsCapabilities(String serviceAddress) {
InputStream inputStream = null;
HttpURLConnection conn = null;
WmtsCapabilities wmtsCapabilities;
try {
// Build the appropriate request Uri given the provided service address
Expand All @@ -472,18 +473,20 @@ protected WmtsCapabilities retrieveWmtsCapabilities(String serviceAddress) {
.build();

// Open the connection as an input stream
URLConnection conn = new URL(serviceUri.toString()).openConnection();
conn = (HttpURLConnection) new URL(serviceUri.toString()).openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(30000);
inputStream = new BufferedInputStream(conn.getInputStream());
InputStream inputStream = new BufferedInputStream(conn.getInputStream());

// Parse and read the input stream
wmtsCapabilities = WmtsCapabilities.getCapabilities(inputStream);
} catch (Exception e) {
throw new RuntimeException(
Logger.makeMessage("LayerFactory", "retrieveWmsCapabilities", "Unable to open connection and read from service address " + e));
} finally {
WWUtil.closeSilently(inputStream);
if (conn != null) {
conn.disconnect();
}
}

return wmtsCapabilities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.geom.Sector;
Expand All @@ -26,7 +26,6 @@
import gov.nasa.worldwind.ogc.wcs.Wcs201CoverageDescriptions;
import gov.nasa.worldwind.ogc.wcs.WcsXmlParser;
import gov.nasa.worldwind.util.Logger;
import gov.nasa.worldwind.util.WWUtil;

/**
* Generates elevations from OGC Web Coverage Service (WCS) version 2.0.1.
Expand Down Expand Up @@ -184,7 +183,7 @@ protected TileMatrixSet tileMatrixSetFromCoverageDescription(Wcs201CoverageDescr
}

protected Wcs201CoverageDescriptions describeCoverage(String serviceAddress, String coverageId) throws Exception {
InputStream inputStream = null;
HttpURLConnection conn = null;
Object responseXml;
try {
// Build the appropriate request Uri given the provided service address
Expand All @@ -196,7 +195,7 @@ protected Wcs201CoverageDescriptions describeCoverage(String serviceAddress, Str
.build();

// Open the connection as an input stream
URLConnection conn = new URL(serviceUri.toString()).openConnection();
conn = (HttpURLConnection) new URL(serviceUri.toString()).openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(30000);

Expand All @@ -207,7 +206,7 @@ protected Wcs201CoverageDescriptions describeCoverage(String serviceAddress, Str
}

// Parse and read the input stream
inputStream = new BufferedInputStream(conn.getInputStream());
InputStream inputStream = new BufferedInputStream(conn.getInputStream());
responseXml = WcsXmlParser.parse(inputStream);
if (responseXml instanceof OwsExceptionReport) {
throw new OgcException((OwsExceptionReport) responseXml);
Expand All @@ -216,7 +215,9 @@ protected Wcs201CoverageDescriptions describeCoverage(String serviceAddress, Str
Logger.makeMessage("Wcs201ElevationCoverage", "describeCoverage", "Response is not a WCS DescribeCoverage document"));
}
} finally {
WWUtil.closeSilently(inputStream);
if (conn != null) {
conn.disconnect();
}
}

return (Wcs201CoverageDescriptions) responseXml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.util.Logger;
import gov.nasa.worldwind.util.Retriever;
import gov.nasa.worldwind.util.WWUtil;

public class ImageRetriever extends Retriever<ImageSource, ImageOptions, Bitmap> {

Expand Down Expand Up @@ -93,13 +92,13 @@ protected Bitmap decodeUrl(String urlString, ImageOptions imageOptions, ImageSou
// TODO retry absent resources, they are currently handled but suppressed entirely after the first failure
// TODO configurable connect and read timeouts

InputStream stream = null;
HttpURLConnection conn = null;
try {
URLConnection conn = new URL(urlString).openConnection();
conn = (HttpURLConnection) new URL(urlString).openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(30000);

stream = new BufferedInputStream(conn.getInputStream());
InputStream stream = new BufferedInputStream(conn.getInputStream());

BitmapFactory.Options factoryOptions = this.bitmapFactoryOptions(imageOptions);
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, factoryOptions);
Expand All @@ -111,7 +110,9 @@ protected Bitmap decodeUrl(String urlString, ImageOptions imageOptions, ImageSou

return bitmap;
} finally {
WWUtil.closeSilently(stream);
if (conn != null) {
conn.disconnect();
}
}
}

Expand Down

0 comments on commit 07df859

Please sign in to comment.