Skip to content

Commit 56a3efb

Browse files
committed
chore(logging): improve debug and error messages for artifact code handling
1 parent 7c45996 commit 56a3efb

5 files changed

Lines changed: 60 additions & 18 deletions

File tree

src/main/java/net/trustyuri/ArtifactCodeImpl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public class ArtifactCodeImpl implements ArtifactCode {
2525
if (TrustyUriUtils.isPotentialArtifactCode(artifactCode)) {
2626
this.dataHash = TrustyUriUtils.getDataPart(artifactCode);
2727
this.module = ModuleDirectory.getModule(TrustyUriUtils.getModuleId(artifactCode));
28+
logger.debug("Created ArtifactCodeImpl from code '{}': module='{}', dataHash='{}'", artifactCode, module.getModuleId(), dataHash);
2829
} else {
29-
logger.error("Invalid artifact code: {}", artifactCode);
30+
logger.error("Invalid artifact code (failed format/length check): '{}'", artifactCode);
3031
throw new IllegalArgumentException("Invalid artifact code: " + artifactCode);
3132
}
3233
}
@@ -39,19 +40,20 @@ public class ArtifactCodeImpl implements ArtifactCode {
3940
*/
4041
ArtifactCodeImpl(TrustyUriModule module, String dataHash) {
4142
if (module == null) {
42-
logger.error("Module cannot be null");
43+
logger.error("Cannot create artifact code: module is null (dataHash='{}')", dataHash);
4344
throw new IllegalArgumentException("Module cannot be null");
4445
}
4546
if (dataHash == null || dataHash.isEmpty()) {
46-
logger.error("Data hash cannot be null or empty");
47+
logger.error("Cannot create artifact code: data hash is null or empty (module='{}')", module.getModuleId());
4748
throw new IllegalArgumentException("Data hash cannot be null or empty");
4849
}
4950
if (module.getDataPartLength() != dataHash.length()) {
50-
logger.error("Data hash length does not match module requirements: expected {}, got {}", module.getDataPartLength(), dataHash.length());
51+
logger.error("Cannot create artifact code for module '{}': data hash '{}' has length {}, expected {}", module.getModuleId(), dataHash, dataHash.length(), module.getDataPartLength());
5152
throw new IllegalArgumentException("Data hash length does not match module requirements: expected " + module.getDataPartLength() + ", got " + dataHash.length());
5253
}
5354
this.module = module;
5455
this.dataHash = dataHash;
56+
logger.debug("Created ArtifactCodeImpl: module='{}', dataHash='{}'", module.getModuleId(), dataHash);
5557
}
5658

5759
@Override

src/main/java/net/trustyuri/TrustyUriResource.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ public TrustyUriResource(URL url, String artifactCode) throws IOException {
120120
logger.debug("Creating TrustyUriResource from URL '{}' with explicit artifact code '{}'", url, artifactCode);
121121
URLConnection conn = url.openConnection();
122122
String contentType = conn.getContentType();
123-
logger.debug("Content-Type reported by server for '{}': '{}'", url, contentType);
123+
if (contentType == null) {
124+
logger.warn("Server did not report a Content-Type for URL '{}'; mimetype will be unknown", url);
125+
} else {
126+
logger.debug("Content-Type reported by server for '{}': '{}'", url, contentType);
127+
}
124128
init(url.toString(), contentType, conn.getInputStream(), artifactCode);
125129
}
126130

@@ -136,7 +140,11 @@ public TrustyUriResource(URL url) throws IOException {
136140
logger.debug("Creating TrustyUriResource from URL '{}'; derived artifact code: '{}'", n, ac);
137141
URLConnection conn = url.openConnection();
138142
String contentType = conn.getContentType();
139-
logger.debug("Content-Type reported by server for '{}': '{}'", n, contentType);
143+
if (contentType == null) {
144+
logger.warn("Server did not report a Content-Type for URL '{}'; mimetype will be unknown", n);
145+
} else {
146+
logger.debug("Content-Type reported by server for '{}': '{}'", n, contentType);
147+
}
140148
init(n, contentType, conn.getInputStream(), ac);
141149
}
142150

@@ -145,11 +153,17 @@ private void init(String filename, String mimetype, InputStream in, String artif
145153
this.mimetype = mimetype;
146154
this.artifactCode = artifactCode;
147155
if (filename.matches(".*\\.(gz|gzip)")) {
148-
logger.debug("Detected compressed resource '{}', wrapping stream in GZIPInputStream", filename);
149-
this.in = new GZIPInputStream(in);
156+
logger.debug("Detected compressed resource '{}' (.gz/.gzip extension), wrapping stream in GZIPInputStream", filename);
157+
try {
158+
this.in = new GZIPInputStream(in);
159+
} catch (IOException ex) {
160+
logger.error("Failed to read '{}' as a GZIP stream despite .gz/.gzip extension: {}", filename, ex.getMessage());
161+
throw ex;
162+
}
150163
} else {
151164
this.in = in;
152165
}
166+
logger.debug("Initialized TrustyUriResource: filename='{}', mimetype='{}', artifactCode='{}', compressed={}", filename, mimetype, artifactCode, compressed);
153167
}
154168

155169
/**
@@ -212,7 +226,9 @@ public boolean isCompressed() {
212226
* @return the module identifier of the resource, which is derived from the artifact code
213227
*/
214228
public String getModuleId() {
215-
return TrustyUriUtils.getModuleId(artifactCode);
229+
String moduleId = TrustyUriUtils.getModuleId(artifactCode);
230+
logger.debug("Resolved module ID for artifact code '{}': '{}'", artifactCode, moduleId);
231+
return moduleId;
216232
}
217233

218234
/**

src/main/java/net/trustyuri/TrustyUriUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ public static String getNiUri(String s, boolean withAuthority) {
122122
// TODO For future modules, hash might not be equal to data part:
123123
String hash = getDataPart(ac);
124124
TrustyUriModule module = ModuleDirectory.getModule(moduleId);
125+
if (module == null) {
126+
logger.warn("Cannot convert to ni URI — no module registered for ID '{}' (artifact code '{}', from '{}')", moduleId, ac, s);
127+
return null;
128+
}
125129
String tail = "/" + module.getAlgorithmId() + ";" + hash + "?module=" + moduleId;
126130
if (withAuthority) {
127131
try {
@@ -161,7 +165,9 @@ public static String getBase64(byte[] bytes) {
161165
public static String getBase64Hash(String s) {
162166
MessageDigest md = RdfHasher.getDigest();
163167
md.update(s.getBytes());
164-
return getBase64(md.digest());
168+
String hash = getBase64(md.digest());
169+
logger.debug("Computed base64 hash for string of length {}: '{}'", s.length(), hash);
170+
return hash;
165171
}
166172

167173
/**

src/main/java/net/trustyuri/file/FileHasher.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,31 @@ public FileHasher() {
3535
* @throws IOException if an I/O error occurs while reading the input stream
3636
*/
3737
public ArtifactCode makeArtifactCode(InputStream in) throws IOException {
38-
logger.debug("Computing {} artifact code from input stream", HASH_ALGORITHM);
39-
MessageDigest md = null;
38+
logger.debug("Starting {} hash computation from input stream", HASH_ALGORITHM);
39+
MessageDigest md;
4040
try {
4141
md = MessageDigest.getInstance(HASH_ALGORITHM);
4242
} catch (NoSuchAlgorithmException ex) {
43+
logger.error("{} algorithm not available on this JVM", HASH_ALGORITHM, ex);
4344
throw new IllegalStateException(HASH_ALGORITHM + " algorithm not available on this JVM", ex);
4445
}
46+
47+
long bytesRead = 0;
4548
DigestInputStream d = null;
4649
try {
4750
d = new DigestInputStream(in, md);
4851
while (d.read() != -1) {
52+
bytesRead++;
4953
}
54+
} catch (IOException ex) {
55+
logger.error("I/O error while hashing input stream after {} byte(s) read", bytesRead, ex);
56+
throw ex;
5057
} finally {
5158
d.close();
5259
}
60+
61+
logger.debug("Read and hashed {} byte(s) from input stream", bytesRead);
62+
5363
ArtifactCode code = ArtifactCode.of(ModuleDirectory.getModule(FileModule.MODULE_ID), TrustyUriUtils.getBase64(md.digest()));
5464
logger.debug("Computed artifact code: {}", code);
5565
return code;

src/main/java/net/trustyuri/file/FileModule.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,40 @@ public int getDataPartLength() {
4141

4242
@Override
4343
public boolean hasCorrectHash(TrustyUriResource r) throws IOException {
44-
logger.debug("Verifying hash for resource: {}", r);
44+
logger.debug("Verifying hash for resource '{}' (claimed artifact code: '{}')", r.getFilename(), r.getArtifactCode());
4545
FileHasher hasher = new FileHasher();
4646
ArtifactCode ac = hasher.makeArtifactCode(new BufferedInputStream(r.getInputStream()));
4747
boolean matches = r.getArtifactCode().equals(ac.toString());
4848
if (matches) {
49-
logger.debug("Hash verification passed for resource: {}", r);
49+
logger.debug("Hash verification passed for resource '{}'", r.getFilename());
5050
} else {
51-
logger.warn("Hash verification FAILED for resource: {} — expected {}, got {}",
52-
r, r.getArtifactCode(), ac);
51+
logger.warn("Hash verification FAILED for resource '{}' — claimed '{}', actual computed hash '{}'", r.getFilename(), r.getArtifactCode(), ac);
5352
}
5453
return matches;
5554
}
5655

5756
@Override
5857
public void fixTrustyFile(File file) throws IOException {
5958
logger.info("Fixing trusty URI filename for: {}", file.getAbsolutePath());
59+
6060
TrustyUriResource r = new TrustyUriResource(file);
61+
String artifactCode = r.getArtifactCode();
62+
if (artifactCode == null || artifactCode.isEmpty()) {
63+
logger.warn("No artifact code derived from filename '{}'; nothing to strip before reprocessing", r.getFilename());
64+
}
65+
6166
File renamedFile = new File(r.getFilename().replaceAll(r.getArtifactCode(), ""));
62-
logger.debug("Stripping artifact code '{}' — renaming to: {}", r.getArtifactCode(), renamedFile.getName());
67+
logger.debug("Stripping artifact code '{}' from '{}' — renaming to: '{}'", artifactCode, file.getName(), renamedFile.getName());
68+
6369
boolean renamed = file.renameTo(renamedFile);
6470
if (!renamed) {
6571
logger.error("Failed to rename '{}' to '{}' before reprocessing", file.getName(), renamedFile.getName());
6672
throw new IOException("Could not rename file: " + file.getAbsolutePath());
6773
}
68-
logger.debug("Renamed successfully, reprocessing: {}", renamedFile.getName());
74+
logger.debug("Renamed '{}' to '{}', reprocessing", file.getName(), renamedFile.getName());
75+
6976
ProcessFile.process(renamedFile);
77+
logger.info("Finished fixing trusty file (now reprocessed as: '{}')", renamedFile.getName());
7078
}
7179

7280
}

0 commit comments

Comments
 (0)