Skip to content

Commit

Permalink
Issue 1827: The repository URL is misleading and gets stripped.
Browse files Browse the repository at this point in the history
Partially restore the previous behavior of using the `/servicedocument`.
It turns out that while the repository URL gets used, it gets stripped to just the host name and port number during deposit.
The swordv1 library ends up adding the deposit location from the service document manifest.

Using the Repository URL in the error logs is misleading and confusing.
Change the behavior to better describe what the given URL is on error.
The sword handles the actual deposit location internally and is not as readily available for use in the error log.
  • Loading branch information
kaladay committed Jul 7, 2023
1 parent d4551cd commit ff98465
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions src/main/java/org/tdl/vireo/model/depositor/SWORDv1Depositor.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Map<String, String> getCollections(DepositLocation depLocation) {
}

ServiceDocument serviceDocument = null;
String serviceDocumentUrl = depLocation.getRepository() + "/servicedocument";
String serviceDocumentUrl = depLocation.getRepository();

try {
Map<String, String> foundCollections = new HashMap<String, String>();
Expand Down Expand Up @@ -96,8 +96,8 @@ public Map<String, String> getCollections(DepositLocation depLocation) {
foundCollections.put(collection.getTitle(), collection.getLocation());
}
}
return foundCollections;

return foundCollections;
} catch (MalformedURLException murle) {
logger.debug("The repository is an invalid URL", murle);
throw new SwordDepositException("The repository is an invalid URL", murle);
Expand All @@ -109,7 +109,7 @@ public Map<String, String> getCollections(DepositLocation depLocation) {
} else if (messageContainsCode(re.getMessage(), "401")) {
throw new SwordDepositUnauthorizedException("Unauthorized credentials.", re);
} else if (messageContainsCode(re.getMessage(), "404")) {
throw new SwordDepositNotFoundException("Repository URL not found.", re);
throw new SwordDepositNotFoundException("Repository URL is not found.", re);
} else if (messageContainsCode(re.getMessage(), "408")) {
throw new SwordDepositRequestTimeoutException("Request timed out.", re);
} else if (messageContainsCode(re.getMessage(), "409")) {
Expand Down Expand Up @@ -142,23 +142,29 @@ public String deposit(DepositLocation depLocation, ExportPackage exportPackage)
throw new SwordDepositInternalServerErrorException("Bad deposit location or repository URL when trying to deposit().");
}

String depositUrl = depLocation.getRepository() + "/deposit";

// Only the host and port are used from the repository URL for sword depositing.
URI sword = null;
String depositUrl = null;
try {
sword = new URI(depLocation.getRepository());
depositUrl = sword.getScheme() + "://" + sword.getHost();
if (sword.getPort() != -1) {
depositUrl += ":" + sword.getPort();
}
} catch (URISyntaxException e) {
throw new SwordDepositBadRequestException("Unable to publish, cannot parse URI from repository URL: " + depLocation.getRepository(), e);
}

URL repositoryURL = new URL(depositUrl);

try {
FileHelperUtility fileHelperUtility = new FileHelperUtility();

File exportFile = (File) exportPackage.getPayload();

String exportMimeType = fileHelperUtility.getMimeType(exportFile);

// Building the client
Client client = new Client();
// get the timeout from the location, or default it to the default
client.setSocketTimeout(depLocation.getTimeout() == null ? DepositLocation.DEFAULT_TIMEOUT : (depLocation.getTimeout() * 1000));
client.setServer(repositoryURL.getHost(), repositoryURL.getPort());
client.setServer(sword.getHost(), sword.getPort());
client.setUserAgent(USER_AGENT);

// If the credentials include a username and password, set those on the client.
Expand All @@ -167,7 +173,6 @@ public String deposit(DepositLocation depLocation, ExportPackage exportPackage)
}

PostMessage message = new PostMessage();

message.setFilepath(exportFile.getAbsolutePath());
message.setDestination(depLocation.getCollection());
message.setFiletype(exportMimeType);
Expand All @@ -185,32 +190,21 @@ public String deposit(DepositLocation depLocation, ExportPackage exportPackage)
DepositResponse response = client.postFile(message);

if (response.getHttpResponse() < 200 || response.getHttpResponse() > 204) {
throw new SwordDepositException("Sword server responed with a non success HTTP status code: " + response.getHttpResponse());
throw new SWORDClientException("Sword server responded with a non-success HTTP Status Code: " + response.getHttpResponse());
}

try {
URI sword = new URI(depositUrl);
URI handle = new URI(response.getEntry().getId());
URI handle = new URI(response.getEntry().getId());
depositUrl += "/handle" + handle.getPath();

String depositURL = sword.getScheme() + "://" + sword.getHost();
if (sword.getPort() != -1) {
depositURL += ":" + sword.getPort();
}
depositURL += "/handle" + handle.getPath();

return new URI(depositURL).toString();
} catch (URISyntaxException e) {
throw new SwordDepositException("Unable to publish to " + depositUrl, e);
}
} catch (MalformedURLException | SWORDClientException re) {
return new URI(depositUrl).toString();
} catch (SWORDClientException | URISyntaxException re) {
logger.debug(re.getMessage(), re);
String message = re.getMessage();
String repoMessage = ", unable to publish to " + depositUrl;
String repoMessage = ", unable to publish using deposit URL " + depositUrl;

if (re.getMessage().contains("Unable to parse the XML")) {
repoMessage += ", SWORD server cannot parse the XML.";
}
else {
} else {
repoMessage += ".";
}

Expand All @@ -219,7 +213,7 @@ public String deposit(DepositLocation depLocation, ExportPackage exportPackage)
} else if (messageContainsCode(re.getMessage(), "401")) {
throw new SwordDepositUnauthorizedException("Unauthorized credentials" + repoMessage, re);
} else if (messageContainsCode(re.getMessage(), "404")) {
throw new SwordDepositNotFoundException("Repository URL not found" + repoMessage, re);
throw new SwordDepositNotFoundException("Repository URL or Deposit URL is not found" + repoMessage, re);
} else if (messageContainsCode(re.getMessage(), "408")) {
throw new SwordDepositRequestTimeoutException("Request timed out" + repoMessage, re);
} else if (messageContainsCode(re.getMessage(), "409")) {
Expand All @@ -237,10 +231,9 @@ public String deposit(DepositLocation depLocation, ExportPackage exportPackage)
} else if (re.getMessage().contains("Connection refused")) {
throw new SwordDepositForbiddenException("Connection refused" + repoMessage, re);
} else if (re.getMessage().contains("Unable to parse the XML")) {
throw new SwordDepositUnprocessableEntityException("XML parse failure, unable to publish to " + depositUrl + ".", re);
}
else {
message = "Unable to publish to " + depositUrl + ".";
throw new SwordDepositUnprocessableEntityException("XML parse failure, unable to publish using URL " + depositUrl + ".", re);
} else {
message = "Unable to publish using deposit URL " + depositUrl + ".";
}

throw new SwordDepositBadRequestException(message, re);
Expand Down

0 comments on commit ff98465

Please sign in to comment.