-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[Folderwatcher] [WIP] Azure blob storage containers monitoring support #14926
Open
goopilot
wants to merge
1
commit into
openhab:main
Choose a base branch
from
goopilot:azure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
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
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...derwatcher/src/main/java/org/openhab/binding/folderwatcher/internal/api/AzureActions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright (c) 2010-2025 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.folderwatcher.internal.api; | ||
|
||
import static org.eclipse.jetty.http.HttpHeader.*; | ||
import static org.eclipse.jetty.http.HttpMethod.*; | ||
|
||
import java.io.StringReader; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jetty.client.HttpClient; | ||
import org.eclipse.jetty.client.api.ContentResponse; | ||
import org.eclipse.jetty.client.api.Request; | ||
import org.openhab.binding.folderwatcher.internal.api.auth.Azure4SignerForAuthorizationHeader; | ||
import org.openhab.binding.folderwatcher.internal.api.exception.AuthException; | ||
import org.openhab.binding.folderwatcher.internal.api.util.HttpUtilException; | ||
import org.openhab.core.io.net.http.HttpClientFactory; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.InputSource; | ||
|
||
/** | ||
* The {@link AzureActions} class contains AWS S3 API implementation. | ||
* | ||
* @author Alexandr Salamatov - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class AzureActions { | ||
private final HttpClient httpClient; | ||
private static final Duration REQUEST_TIMEOUT = Duration.ofMinutes(1); | ||
private static final String CONTENT_TYPE = "application/xml"; | ||
private URL containerUri; | ||
private String azureAccessKey; | ||
private String accountName; | ||
private String containerName; | ||
|
||
public AzureActions(HttpClientFactory httpClientFactory, String accountName, String containerName) { | ||
this(httpClientFactory, accountName, containerName, ""); | ||
} | ||
|
||
public AzureActions(HttpClientFactory httpClientFactory, String accountName, String containerName, | ||
String azureAccessKey) { | ||
this.httpClient = httpClientFactory.getCommonHttpClient(); | ||
try { | ||
this.containerUri = new URL("https://" + accountName + ".blob.core.windows.net/" + containerName); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException("Unable to parse service endpoint: " + e.getMessage()); | ||
} | ||
this.azureAccessKey = azureAccessKey; | ||
this.accountName = accountName; | ||
this.containerName = containerName; | ||
} | ||
|
||
public List<String> listContainer(String prefix) throws Exception { | ||
Map<String, String> headers = new HashMap<String, String>(); | ||
Map<String, String> params = new HashMap<String, String>(); | ||
return listBlob(prefix, headers, params); | ||
} | ||
|
||
public List<String> listBlob(String prefix, Map<String, String> headers, Map<String, String> params) | ||
throws Exception { | ||
|
||
params.put("restype", "container"); | ||
params.put("comp", "list"); | ||
params.put("maxresults", "1000"); | ||
params.put("prefix", prefix); | ||
headers.put(ACCEPT.toString(), CONTENT_TYPE); | ||
|
||
if (!azureAccessKey.isEmpty()) { | ||
Azure4SignerForAuthorizationHeader signer = new Azure4SignerForAuthorizationHeader("GET", | ||
this.containerUri); | ||
String authorization; | ||
try { | ||
authorization = signer.computeSignature(headers, params, accountName, azureAccessKey, containerName); | ||
} catch (HttpUtilException e) { | ||
throw new AuthException(e); | ||
} | ||
headers.put("Authorization", authorization); | ||
} | ||
|
||
Request request = httpClient.newRequest(this.containerUri.toString()) // | ||
.method(GET) // | ||
.timeout(REQUEST_TIMEOUT.toNanos(), TimeUnit.NANOSECONDS); // | ||
|
||
for (String headerKey : headers.keySet()) { | ||
request.header(headerKey, headers.get(headerKey)); | ||
} | ||
for (String paramKey : params.keySet()) { | ||
request.param(paramKey, params.get(paramKey)); | ||
} | ||
|
||
ContentResponse contentResponse = request.send(); | ||
if (contentResponse.getStatus() != 200) { | ||
throw new Exception("HTTP Response is not 200"); | ||
} | ||
|
||
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); | ||
// This returns extra character before <xml. Need to find out why | ||
String sResponse = contentResponse.getContentAsString(); | ||
InputSource is = new InputSource(new StringReader(sResponse.substring(sResponse.indexOf("<")))); | ||
Document doc = docBuilder.parse(is); | ||
NodeList nameNodesList = doc.getElementsByTagName("Blob"); | ||
List<String> returnList = new ArrayList<>(); | ||
|
||
if (nameNodesList.getLength() == 0) { | ||
return returnList; | ||
} | ||
|
||
for (int i = 0; i < nameNodesList.getLength(); i++) { | ||
returnList.add(nameNodesList.item(i).getFirstChild().getTextContent()); | ||
} | ||
|
||
nameNodesList = doc.getElementsByTagName("NextMarker"); | ||
if (nameNodesList.getLength() > 0) { | ||
if (nameNodesList.item(0).getChildNodes().getLength() > 0) { | ||
String continueToken = nameNodesList.item(0).getFirstChild().getTextContent(); | ||
params.clear(); | ||
headers.clear(); | ||
params.put("marker", continueToken); | ||
returnList.addAll(listBlob(prefix, headers, params)); | ||
} | ||
} | ||
return returnList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm doing this, but I don't like it.
The reason why I'm doing this that Azure in the HTTP response has some additional character before <?xml statement. So the DOM parser fails.
Wireshark also shows it:
Did anybody experience that and if yes, what is the best solution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wborn did you experience similar issue with Azure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, maybe it's a BOM?
https://en.m.wikipedia.org/wiki/Byte_order_mark