Skip to content

Bkalashi.basic auth support #256

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

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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added optional basic authentication
  • Loading branch information
bkalashi committed Sep 15, 2022
commit d7616832c0759b17d03ee84517917c1fd437de1b
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/gov/nasa/worldwind/avlist/AVKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ 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_ENCODED_STRING = "gov.nasa.worldwind.avkey.BasicAuthEncodedString";
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
37 changes: 33 additions & 4 deletions src/gov/nasa/worldwind/ogc/wcs/wcs100/WCS100Capabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,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);
}

/**
* Retrieves the WCS capabilities document from a specified WCS server.
*
* @param uri The URI of the server.
* @param basicAuthenticationEncodedStr Base 64 Encoded String holding username/password ("username: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 basicAuthenticationEncodedStr) throws Exception
{
try
{
CapabilitiesRequest request = new CapabilitiesRequest(uri, "WCS");
request.setVersion("1.0.0");

return new WCS100Capabilities(request.toString());
return new WCS100Capabilities(request.toString(), basicAuthenticationEncodedStr);
}
catch (URISyntaxException e)
{
Expand All @@ -56,10 +74,21 @@ public static WCS100Capabilities retrieve(URI uri) throws Exception
}

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

/**
* Constructs WCS100Capabilities with an optional Base64 Encoded String for Basic Authentication
*
* @param docSource
* @param basicAuthenticationEncodedStr Base 64 Encoded String
*/
public WCS100Capabilities(Object docSource, String basicAuthenticationEncodedStr)
{
super(OGCConstants.WCS_1_0_0_NAMESPACE_URI);

this.eventReader = this.createReader(docSource);
this.eventReader = this.createReader(docSource, basicAuthenticationEncodedStr);

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

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

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

public static WCS100DescribeCoverage retrieve(URI uri, final String coverageName) throws URISyntaxException
{
return retrieve(uri, coverageName, null);
}

public static WCS100DescribeCoverage retrieve(URI uri, final String coverageName, String basicAuthenticationEncodedStr) throws URISyntaxException
{
Request request = new Request(uri, "WCS")
{
Expand All @@ -42,14 +47,19 @@ protected void initialize(String service)
}
};

return new WCS100DescribeCoverage(request.toString());
return new WCS100DescribeCoverage(request.toString(), basicAuthenticationEncodedStr);
}

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

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

this.eventReader = this.createReader(docSource);
this.eventReader = this.createReader(docSource, basicAuthenticationEncodedStr);

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

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

protected XMLEventParserContext createParserContext(XMLEventReader reader)
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 @@ -64,4 +64,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 @@ -49,6 +49,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 @@ -311,6 +312,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 @@ -586,6 +593,10 @@ protected long getExpiration(URLConnection connection)
return expiration;
}

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

@Override
public boolean equals(Object o)
{
Expand Down
4 changes: 4 additions & 0 deletions src/gov/nasa/worldwind/terrain/BasicElevationModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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 @@ -124,6 +126,8 @@ public BasicElevationModel(AVList params)
if (b != null)
this.setValue(AVKey.DELETE_CACHE_ON_EXIT, true);

basicAuthorizationString = params.getStringValue(AVKey.BASIC_AUTH_ENCODED_STRING);

// 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 @@ -365,6 +365,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
Loading