-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes AASXDeserializer getRelatedFiles crashes (#229)
* Fixes AASXDeserializer getRelatedFiles crashes * Only file URLs are tried to resolve * Non-existing files lead to a warning * Update AASXDeserializer.java * Update AASXUtils.java * Update TestAASXUtils.java --------- Signed-off-by: Frank Schnicke <frank.schnicke@iese.fraunhofer.de>
- Loading branch information
1 parent
072b0f9
commit 8967ad8
Showing
7 changed files
with
156 additions
and
54 deletions.
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
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
57 changes: 31 additions & 26 deletions
57
...sx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/internal/AASXUtils.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 |
---|---|---|
@@ -1,40 +1,45 @@ | ||
/* | ||
* Copyright (c) 2024 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.internal; | ||
|
||
public class AASXUtils { | ||
|
||
|
||
/** | ||
* Gets the path from a URL e.g "http://localhost:8080/path/to/test.file" | ||
* results in "/path/to/test.file" | ||
* Removes the file: or file:// suffix of an URI | ||
* | ||
* @param url URL to get the path for | ||
* @return the path from the URL | ||
* @param uri | ||
* URI to remove the file suffix from | ||
* @return the URI without the file suffix | ||
*/ | ||
public static String getPathFromURL(String url) { | ||
if (url == null) { | ||
public static String removeFilePartOfURI(String uri) { | ||
if (uri == null) { | ||
return null; | ||
} | ||
|
||
if (url.contains("://")) { | ||
|
||
// Find the ":" and and remove the "http://" from the url | ||
int index = url.indexOf(":") + 3; | ||
url = url.substring(index); | ||
|
||
// Find the first "/" from the URL (now without the "http://") and remove | ||
// everything before that | ||
index = url.indexOf("/"); | ||
url = url.substring(index); | ||
|
||
// Recursive call to deal with more than one server parts | ||
// (e.g. basyx://127.0.0.1:6998//https://localhost/test/) | ||
return getPathFromURL(url); | ||
} else { | ||
// Make sure the path has a / at the start | ||
if (!url.startsWith("/")) { | ||
url = "/" + url; | ||
} | ||
return url; | ||
if (uri.startsWith("file://")) { | ||
return uri.replaceFirst("file://", ""); | ||
} else if (uri.startsWith("file:")) { | ||
return uri.replaceFirst("file:", ""); | ||
} | ||
|
||
return uri; | ||
} | ||
|
||
public static boolean isFilePath(String uri) { | ||
return uri.startsWith("/") || uri.startsWith("file:") || uri.startsWith("./") || uri.startsWith("../"); | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...rc/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/internal/TestAASXUtils.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,57 @@ | ||
/* | ||
* Copyright (c) 2024 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.internal; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
public class TestAASXUtils { | ||
|
||
@Test | ||
public void isFilePath() { | ||
// Cf. RFC8089 | ||
String[] filePaths = { | ||
"file://a", "file:a", "./b/c", "../b/c/d", "/a" | ||
}; | ||
|
||
String[] notFilePaths = { | ||
"http://admin-shell.io/example", "ftp://admin-shell.io/example" | ||
}; | ||
|
||
for (String filePath : filePaths) { | ||
assertTrue(AASXUtils.isFilePath(filePath)); | ||
} | ||
|
||
for (String filePath : notFilePaths) { | ||
assertFalse(AASXUtils.isFilePath(filePath)); | ||
} | ||
} | ||
|
||
@Test | ||
public void removeFilePartOfURI() { | ||
String[] filePaths = { | ||
"file:///a", "file:/a", "/a" | ||
}; | ||
|
||
for (String filePath : filePaths) { | ||
assertEquals("/a", AASXUtils.removeFilePartOfURI(filePath)); | ||
} | ||
} | ||
} |
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