Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bkalashi.basic auth support #256

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/gov/nasa/worldwind/avlist/AVKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public interface AVKey // TODO: Eliminate unused constants, if any
final String BALLOON = "gov.nasa.worldwind.avkey.Balloon";
final String BALLOON_TEXT = "gov.nasa.worldwind.avkey.BalloonText";
final String BACK = "gov.nasa.worldwind.avkey.Back";
final String BASIC_AUTH_USERNAME = "gov.nasa.worldwind.avkey.BasicAuthUsername";
final String BASIC_AUTH_PASSWORD = "gov.nasa.worldwind.avkey.BasicAuthPassword";
final String BEGIN = "gov.nasa.worldwind.avkey.Begin";
final String BIG_ENDIAN = "gov.nasa.worldwind.avkey.BigEndian";
final String BOTTOM = "gov.nasa.worldwind.avkey.Bottom";
Expand Down
7 changes: 7 additions & 0 deletions src/gov/nasa/worldwind/layers/rpf/RPFRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class RPFRetriever extends WWObjectImpl implements Retriever
public static final int RESPONSE_CODE_OK = 1;
public static final int RESPONSE_CODE_NO_CONTENT = 2;

protected String basicAuthenticationEncodedString;

public RPFRetriever(RPFGenerator.RPFServiceInstance service, URL url, RetrievalPostProcessor postProcessor)
{
if (service == null)
Expand Down Expand Up @@ -196,6 +198,11 @@ public void setStaleRequestLimit(int staleRequestLimit)
this.staleRequestLimit = staleRequestLimit;
}

@Override
public void setBasicAuthentication(String basicAuthorizationString) {
this.basicAuthenticationEncodedString = basicAuthorizationString;
}

public final RPFGenerator.RPFServiceInstance getService()
{
return this.service;
Expand Down
38 changes: 34 additions & 4 deletions src/gov/nasa/worldwind/ogc/wcs/wcs100/WCS100Capabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,31 @@ public class WCS100Capabilities extends AbstractXMLEventParser
* if an error occurs retrieving the document.
*/
public static WCS100Capabilities retrieve(URI uri) throws Exception
{
return retrieve(uri, null, null);
}

/**
* Retrieves the WCS capabilities document from a specified WCS server.
*
* @param uri The URI of the server.
* @param basicAuthUsername Username for Basic Authentication
* @param basicAuthPassword Password for Basic Authentication
*
* @return The WCS capabilities document for the specified server.
*
* @throws IllegalArgumentException if the specified URI is invalid.
* @throws gov.nasa.worldwind.exception.WWRuntimeException
* if an error occurs retrieving the document.
*/
public static WCS100Capabilities retrieve(URI uri, String basicAuthUsername, String basicAuthPassword) throws Exception
{
try
{
CapabilitiesRequest request = new CapabilitiesRequest(uri, "WCS");
request.setVersion("1.0.0");

return new WCS100Capabilities(request.toString());
return new WCS100Capabilities(request.toString(), basicAuthUsername, basicAuthPassword);
}
catch (URISyntaxException e)
{
Expand All @@ -79,10 +97,22 @@ public static WCS100Capabilities retrieve(URI uri) throws Exception
}

public WCS100Capabilities(Object docSource)
{
this(docSource, null, null);
}

/**
* Constructs WCS100Capabilities with an optional Base64 Encoded String for Basic Authentication
*
* @param docSource
* @param basicAuthUsername Username for Basic Authentication
* @param basicAuthUsername Password for Basic Authentication
*/
public WCS100Capabilities(Object docSource, String basicAuthUsername, String basicAuthPassword)
{
super(OGCConstants.WCS_1_0_0_NAMESPACE_URI);

this.eventReader = this.createReader(docSource);
this.eventReader = this.createReader(docSource, basicAuthUsername, basicAuthPassword);

this.initialize();
}
Expand All @@ -92,9 +122,9 @@ protected void initialize()
this.parserContext = this.createParserContext(this.eventReader);
}

protected XMLEventReader createReader(Object docSource)
protected XMLEventReader createReader(Object docSource, String basicAuthUsername, String basicAuthPassword)
{
return WWXML.openEventReader(docSource);
return WWXML.openEventReader(docSource, basicAuthUsername, basicAuthPassword);
}

protected XMLEventParserContext createParserContext(XMLEventReader reader)
Expand Down
37 changes: 32 additions & 5 deletions src/gov/nasa/worldwind/ogc/wcs/wcs100/WCS100DescribeCoverage.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,29 @@ public class WCS100DescribeCoverage extends AbstractXMLEventParser
protected XMLEventParserContext parserContext;
protected List<WCS100CoverageOffering> coverageOfferings = new ArrayList<WCS100CoverageOffering>(1);

public static WCS100DescribeCoverage retrieve(URI uri, final String coverageName) throws URISyntaxException
/**
* Retrieve WCS 100 Coverage
*
* @param uri uri
* @param coverageName name of coverage
* @return WCS100DescribeCoverage
* @throws URISyntaxException URI Syntax incorrect
*/
public static WCS100DescribeCoverage retrieve(URI uri, final String coverageName) throws URISyntaxException{
return retrieve(uri, coverageName, null, null);
}

/**
* Retrieve WCS 100 Coverage
*
* @param uri uri
* @param coverageName name of coverage
* @param basicAuthUsername Basic Authentication Username
* @param basicAuthPassword Basic Authentication Password
* @return WCS100DescribeCoverage
* @throws URISyntaxException URI Syntax incorrect
*/
public static WCS100DescribeCoverage retrieve(URI uri, final String coverageName, String basicAuthUsername, String basicAuthPassword) throws URISyntaxException
{
Request request = new Request(uri, "WCS")
{
Expand All @@ -64,14 +86,19 @@ protected void initialize(String service)
}
};

return new WCS100DescribeCoverage(request.toString());
return new WCS100DescribeCoverage(request.toString(), basicAuthUsername, basicAuthPassword);
}

public WCS100DescribeCoverage(Object docSource)
{
this(docSource, null, null);
}

public WCS100DescribeCoverage(Object docSource, String basicAuthUsername, String basicAuthPassword)
{
super(OGCConstants.WCS_1_0_0_NAMESPACE_URI);

this.eventReader = this.createReader(docSource);
this.eventReader = this.createReader(docSource, basicAuthUsername, basicAuthPassword);

this.initialize();
}
Expand All @@ -81,9 +108,9 @@ protected void initialize()
this.parserContext = this.createParserContext(this.eventReader);
}

protected XMLEventReader createReader(Object docSource)
protected XMLEventReader createReader(Object docSource, String basicAuthUsername, String basicAuthPassword)
{
return WWXML.openEventReader(docSource);
return WWXML.openEventReader(docSource, basicAuthUsername, basicAuthPassword);
}

protected XMLEventParserContext createParserContext(XMLEventReader reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class LocalRasterServerRetriever extends WWObjectImpl implements Retrieve
protected long submitTime;
protected long beginTime;
protected long endTime;

protected String basicAuthenticationEncodedString;
public LocalRasterServerRetriever(AVList params, RasterServer rasterServer, RetrievalPostProcessor postProcessor)
{
if (null != params)
Expand Down Expand Up @@ -177,6 +177,11 @@ public void setStaleRequestLimit(int staleRequestLimit)
this.staleRequestLimit = staleRequestLimit;
}

@Override
public void setBasicAuthentication(String basicAuthorizationString) {
this.basicAuthenticationEncodedString = basicAuthorizationString;
}

public Retriever call() throws Exception
{
try
Expand Down
2 changes: 2 additions & 0 deletions src/gov/nasa/worldwind/retrieve/Retriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,6 @@ public interface Retriever extends WWObject, java.util.concurrent.Callable<Retri
int getStaleRequestLimit();

void setStaleRequestLimit(int staleRequestLimit);

void setBasicAuthentication(String basicAuthorizationString);
}
11 changes: 11 additions & 0 deletions src/gov/nasa/worldwind/retrieve/URLRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public abstract class URLRetriever extends WWObjectImpl implements Retriever
protected long submitTime;
protected long beginTime;
protected long endTime;
protected String basicAuthenticationEncodedStr;

/**
* Create the appropriate retriever for a URL's protocol.
Expand Down Expand Up @@ -333,6 +334,12 @@ protected URLConnection openConnection() throws IOException
this.connection = this.url.openConnection(proxy);
else
this.connection = this.url.openConnection();

// Set optional basic authentication
if (basicAuthenticationEncodedStr != null)
{
this.connection.setRequestProperty("Authorization", "Basic " + basicAuthenticationEncodedStr);
}
}
catch (java.io.IOException e)
{
Expand Down Expand Up @@ -608,6 +615,10 @@ protected long getExpiration(URLConnection connection)
return expiration;
}

public void setBasicAuthentication(String basicAuthenticationEncodedStr){
this.basicAuthenticationEncodedStr = basicAuthenticationEncodedStr;
}

@Override
public boolean equals(Object o)
{
Expand Down
9 changes: 9 additions & 0 deletions src/gov/nasa/worldwind/terrain/BasicElevationModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public class BasicElevationModel extends AbstractElevationModel implements BulkR
// Model resource properties.
protected static final int RESOURCE_ID_OGC_CAPABILITIES = 1;

protected String basicAuthorizationString;

public BasicElevationModel(AVList params)
{
if (params == null)
Expand Down Expand Up @@ -146,6 +148,13 @@ public BasicElevationModel(AVList params)
if (b != null)
this.setValue(AVKey.DELETE_CACHE_ON_EXIT, true);

String basicAuthUsername = params.getStringValue(AVKey.BASIC_AUTH_USERNAME);
String basicAuthPassword = params.getStringValue(AVKey.BASIC_AUTH_PASSWORD);
if(basicAuthUsername != null && basicAuthPassword != null) {
basicAuthorizationString = Base64.getEncoder().encodeToString(
(basicAuthUsername + ":" + basicAuthPassword).getBytes());
}

// Set some fallback values if not already set.
setFallbacks(params);

Expand Down
1 change: 1 addition & 0 deletions src/gov/nasa/worldwind/terrain/WCSElevationModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ protected void downloadElevations(WMSBasicElevationModel.ElevationCompositionTil

Retriever retriever = new HTTPRetriever(url,
new WMSBasicElevationModel.CompositionRetrievalPostProcessor(tile.getFile()));
retriever.setBasicAuthentication(basicAuthorizationString);
retriever.setConnectTimeout(10000);
retriever.setReadTimeout(60000);
retriever.call();
Expand Down
70 changes: 65 additions & 5 deletions src/gov/nasa/worldwind/util/WWXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import java.awt.*;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.*;
import java.nio.ByteBuffer;
import java.text.*;
import java.util.*;
Expand Down Expand Up @@ -450,7 +450,25 @@ public static XMLEventReader openEventReaderFile(String filePath, Class c, boole
* @throws WWRuntimeException if an exception or error occurs while opening and parsing the url. The causing
* exception is included in this exception's {@link Throwable#initCause(Throwable)}.
*/
public static XMLEventReader openEventReaderURL(URL url, boolean isNamespaceAware)
public static XMLEventReader openEventReaderURL(URL url, boolean isNamespaceAware){
return openEventReaderURL(url, isNamespaceAware, null, null);
}

/**
* Open an XML event stream given a generic {@link java.net.URL} reference.
*
* @param url the URL to the document.
* @param isNamespaceAware true to enable namespace-aware processing and false to disable it.
* @param basicAuthUsername Username for Basic Authentication
* @param basicAuthPassword Password for Basic Authentication
*
* @return an XMLEventReader for the URL.
*
* @throws IllegalArgumentException if the url is null.
* @throws WWRuntimeException if an exception or error occurs while opening and parsing the url. The causing
* exception is included in this exception's {@link Throwable#initCause(Throwable)}.
*/
public static XMLEventReader openEventReaderURL(URL url, boolean isNamespaceAware, String basicAuthUsername, String basicAuthPassword)
{
if (url == null)
{
Expand All @@ -461,7 +479,14 @@ public static XMLEventReader openEventReaderURL(URL url, boolean isNamespaceAwar

try
{
InputStream inputStream = url.openStream();
URLConnection urlConnection = url.openConnection();

// Set optional basic authentication
if(basicAuthUsername != null && basicAuthPassword != null) {
urlConnection.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString(
(basicAuthUsername + ":" + basicAuthPassword).getBytes()));
}
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
return openEventReaderStream(inputStream, isNamespaceAware);
}
catch (IOException e)
Expand All @@ -483,7 +508,24 @@ public static XMLEventReader openEventReaderURL(URL url, boolean isNamespaceAwar
*/
public static XMLEventReader openEventReader(Object docSource)
{
return openEventReader(docSource, true);
return openEventReader(docSource, true, null, null);
}

/**
* Open a namespace-aware XML event stream from a general source. The source type may be one of the following: <ul>
* <li>{@link URL}</li> <li>{@link InputStream}</li> <li>{@link File}</li> <li>{@link String} containing a valid URL
* description or a file or resource name available on the classpath.</li> </ul>
*
* @param docSource the source of the XML document.
* @param basicAuthPassword Username for Basic Authentication
* @param basicAuthUsername Password for Basic Authentication
*
* @return the source document as a {@link javax.xml.stream.XMLEventReader}, or null if the source object is a
* string that does not identify a URL, a file or a resource available on the classpath.
*/
public static XMLEventReader openEventReader(Object docSource, String basicAuthUsername, String basicAuthPassword)
{
return openEventReader(docSource, true, basicAuthUsername, basicAuthPassword);
}

/**
Expand All @@ -498,6 +540,24 @@ public static XMLEventReader openEventReader(Object docSource)
* string that does not identify a URL, a file or a resource available on the classpath.
*/
public static XMLEventReader openEventReader(Object docSource, boolean isNamespaceAware)
{
return openEventReader(docSource, isNamespaceAware, null, null);
}

/**
* Open an XML event stream from a general source. The source type may be one of the following: <ul> <li>{@link
* URL}</li> <li>{@link InputStream}</li> <li>{@link File}</li> <li>{@link String} containing a valid URL
* description or a file or resource name available on the classpath.</li> </ul>
*
* @param docSource the source of the XML document.
* @param isNamespaceAware true to enable namespace-aware processing and false to disable it.
* @param basicAuthUsername Username for Basic Authentication
* @param basicAuthPassword Password for Basic Authentication
*
* @return the source document as a {@link javax.xml.stream.XMLEventReader}, or null if the source object is a
* string that does not identify a URL, a file or a resource available on the classpath.
*/
public static XMLEventReader openEventReader(Object docSource, boolean isNamespaceAware, String basicAuthUsername, String basicAuthPassword)
{
if (docSource == null || WWUtil.isEmpty(docSource))
{
Expand Down Expand Up @@ -534,7 +594,7 @@ else if (!(docSource instanceof String))

URL url = WWIO.makeURL(sourceName);
if (url != null)
return openEventReaderURL(url, isNamespaceAware);
return openEventReaderURL(url, isNamespaceAware, basicAuthUsername, basicAuthPassword);

return openEventReaderFile(sourceName, null, isNamespaceAware);
}
Expand Down