From 2ebf9a909326b6b0515c8b33a5f4aa1c7ac944af Mon Sep 17 00:00:00 2001 From: Guilhem Legal Date: Wed, 15 Mar 2017 12:23:28 +0100 Subject: [PATCH] Add basic authentication for secure SOS --- .../main/resources/import-configuration.xsd | 9 ++++++ .../sos/importer/feeder/Configuration.java | 32 ++++++++++++++++--- .../org/n52/sos/importer/feeder/Feeder.java | 12 +++++++ .../sos/importer/feeder/util/FtpClient.java | 2 +- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/bindings/src/main/resources/import-configuration.xsd b/bindings/src/main/resources/import-configuration.xsd index 93cfbe3e..8987ce33 100644 --- a/bindings/src/main/resources/import-configuration.xsd +++ b/bindings/src/main/resources/import-configuration.xsd @@ -1230,6 +1230,15 @@ + + + + If the sos server is protected by Basic + authentication, the user name and password has to + be defined. + + + diff --git a/feeder/src/main/java/org/n52/sos/importer/feeder/Configuration.java b/feeder/src/main/java/org/n52/sos/importer/feeder/Configuration.java index 91ceeddd..6ad135b4 100644 --- a/feeder/src/main/java/org/n52/sos/importer/feeder/Configuration.java +++ b/feeder/src/main/java/org/n52/sos/importer/feeder/Configuration.java @@ -334,20 +334,44 @@ public URL getSosUrl() throws MalformedURLException { } /** - *

getUser.

+ *

getSosUser.

* * @return a {@link java.lang.String} object. */ - public String getUser() { + public String getSosUser() { + if (importConf.getSosMetadata().getCredentials() != null) { + return importConf.getSosMetadata().getCredentials().getUserName(); + } + return null; + } + + /** + *

getSosPassword.

+ * + * @return a {@link java.lang.String} object. + */ + public String getSosPassword() { + if (importConf.getSosMetadata().getCredentials() != null) { + return importConf.getSosMetadata().getCredentials().getPassword(); + } + return null; + } + + /** + *

getFtpUser.

+ * + * @return a {@link java.lang.String} object. + */ + public String getFtpUser() { return importConf.getDataFile().getRemoteFile().getCredentials().getUserName(); } /** - *

getPassword.

+ *

getFtpPassword.

* * @return a {@link java.lang.String} object. */ - public String getPassword() { + public String getFtpPassword() { return importConf.getDataFile().getRemoteFile().getCredentials().getPassword(); } diff --git a/feeder/src/main/java/org/n52/sos/importer/feeder/Feeder.java b/feeder/src/main/java/org/n52/sos/importer/feeder/Feeder.java index 25719723..012e161d 100644 --- a/feeder/src/main/java/org/n52/sos/importer/feeder/Feeder.java +++ b/feeder/src/main/java/org/n52/sos/importer/feeder/Feeder.java @@ -35,6 +35,8 @@ import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.Charset; import java.text.ParseException; @@ -268,6 +270,16 @@ public Feeder(final Configuration config) sweArrayObservationTimeOutBuffer); } isUseLastTimestamp = config.isUseLastTimestamp(); + if (config.getSosUser() != null && config.getSosPassword()!= null) { + try { + URI u = sosUrl.toURI(); + sosWrapper.setBasicAuthHost(u.getScheme() + "://" + u.getHost() + ':' + u.getPort()); + sosWrapper.setBasicAuthUser(config.getSosUser()); + sosWrapper.setBasicAuthPassword(config.getSosPassword()); + } catch (URISyntaxException ex) { + throw new OXFException(ex); + } + } } private Binding getBinding(final String binding) throws OXFException { diff --git a/feeder/src/main/java/org/n52/sos/importer/feeder/util/FtpClient.java b/feeder/src/main/java/org/n52/sos/importer/feeder/util/FtpClient.java index e6d28e72..63a8c055 100644 --- a/feeder/src/main/java/org/n52/sos/importer/feeder/util/FtpClient.java +++ b/feeder/src/main/java/org/n52/sos/importer/feeder/util/FtpClient.java @@ -75,7 +75,7 @@ public DataFile download() { try (FileOutputStream fos = new FileOutputStream(file);) { client.connect(config.getRemoteFileURL()); if (config.areRemoteFileCredentialsSet()) { - client.login(config.getUser(), config.getPassword()); + client.login(config.getFtpUser(), config.getFtpPassword()); } client.enterLocalPassiveMode(); URL remoteFileURL = new URL(config.getRemoteFileURL());