Skip to content

Commit b85acfa

Browse files
Fixed the improvements
1 parent 6ddc6f2 commit b85acfa

4 files changed

Lines changed: 37 additions & 18 deletions

File tree

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<spotless-maven-plugin.version>2.44.2</spotless-maven-plugin.version>
4949
<maven-enforcer-plugin.version>3.5.0</maven-enforcer-plugin.version>
5050
<maven-javadoc-plugin.version>3.11.2</maven-javadoc-plugin.version>
51+
5152
</properties>
5253

5354
<dependencyManagement>
@@ -180,6 +181,7 @@
180181
<artifactId>system-stubs-jupiter</artifactId>
181182
<scope>test</scope>
182183
</dependency>
184+
183185
</dependencies>
184186

185187
<build>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package land.oras.auth;
2+
3+
public class FileStoreAuthenticationProvider implements AuthProvider{
4+
5+
@Override
6+
public String getAuthHeader() {
7+
return "";
8+
}
9+
}

src/main/java/land/oras/credentials/FileStore.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.FileReader;
44
import java.io.IOException;
55
import java.nio.file.Files;
6+
import java.nio.file.Path;
67
import java.nio.file.Paths;
78
import java.util.Map;
89
import java.util.Objects;
@@ -113,26 +114,20 @@ public static class Config {
113114
*/
114115
public static Config load(String configPath) throws ConfigLoadingException {
115116

116-
try (FileReader reader = new FileReader(Paths.get(configPath).toFile())) {
117-
// Read the file content and deserialize into a Map<String, Credential>
118-
Map<String, Credential> credentials = JsonUtils.fromJson(reader, new TypeToken<Map<String, Credential>>(){}.getType());
117+
Map<String, Credential> credentials = JsonUtils.fromJson(Path.of(configPath), new TypeToken<Map<String, Credential>>(){}.getType());
119118

120-
Config config = new Config();
119+
Config config = new Config();
121120

122-
for (Map.Entry<String, Credential> entry : credentials.entrySet()) {
121+
for (Map.Entry<String, Credential> entry : credentials.entrySet()) {
123122

124-
String serverAddress = entry.getKey();
125-
Credential credential = entry.getValue();
126-
// Put the serverAddress and Credential into the credentialStore
127-
config.credentialStore.put(serverAddress, credential);
128-
}
123+
String serverAddress = entry.getKey();
124+
Credential credential = entry.getValue();
125+
// Put the serverAddress and Credential into the credentialStore
126+
config.credentialStore.put(serverAddress, credential);
127+
}
129128

130-
return config;
129+
return config;
131130

132-
} catch (IOException e) {
133-
// Handle issues related to file reading or file not found
134-
throw new ConfigLoadingException("Failed to read the configuration file: " + configPath, e);
135-
}
136131
}
137132

138133
public Credential getCredential(String serverAddress) {

src/main/java/land/oras/utils/JsonUtils.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ private JsonUtils() {
4242
// Hide constructor
4343
}
4444

45-
public static Map<String, FileStore.Credential> fromJson(FileReader reader, Type type) {
46-
return gson.fromJson(reader, type);
47-
}
4845

4946
/**
5047
* Type adapter for ZonedDateTime
@@ -101,4 +98,20 @@ public static <T> T fromJson(Path path, Class<T> clazz) {
10198
throw new OrasException("Unable to read JSON file due to IO error", e);
10299
}
103100
}
101+
102+
/**
103+
* Convert a JSON string to an object
104+
* @param path The path to the JSON file
105+
* @param type The class of the object
106+
* @return The object
107+
*/
108+
public static <T> T fromJson(Path path, Type type) {
109+
try {
110+
return gson.fromJson(Files.readString(path, StandardCharsets.UTF_8), type);
111+
} catch (IOException e) {
112+
throw new OrasException("Unable to read JSON file due to IO error", e);
113+
}
114+
}
115+
116+
104117
}

0 commit comments

Comments
 (0)