Skip to content
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

Fixed all mistakes in the Item View page #551

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -77,6 +79,9 @@
@RestController
@RequestMapping("/api/core/refbox")
public class ClarinRefBoxController {

private final String XML_FILE_START = "<?xml version='1.0' encoding='UTF-8'?>";
private final String BIBTEX_TAG = "bib:bibtex";
private final Logger log = org.apache.logging.log4j.LogManager.getLogger(ClarinRefBoxController.class);

@Autowired
Expand Down Expand Up @@ -228,7 +233,7 @@ public ResponseEntity getCitationText(@RequestParam(name = "type") String type,
XmlOutputContext xmlOutContext = XmlOutputContext.emptyContext(output);
xmlOutContext.getWriter().writeStartDocument();

//Try to obtain just the metadata, if that fails return "normal" response
// Try to obtain just the metadata, if that fails return "normal" response
try {
oaipmh.getInfo().getGetRecord().getRecord().getMetadata().write(xmlOutContext);
} catch (Exception e) {
Expand All @@ -246,8 +251,10 @@ public ResponseEntity getCitationText(@RequestParam(name = "type") String type,
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Unexpected error while writing the output. For more information visit the log files.");
} catch (XOAIManagerResolverException e) {
throw new ServletException("OAI 2.0 wasn't correctly initialized," +
" please check the log for previous errors", e);
String errMessage = "OAI 2.0 wasn't correctly initialized, please check the log for previous errors. " +
"Error message: " + e.getMessage();
log.error(errMessage);
throw new ServletException(errMessage);
} catch (OAIException e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Unexpected error. For more information visit the log files.");
Expand All @@ -263,8 +270,11 @@ public ResponseEntity getCitationText(@RequestParam(name = "type") String type,
HttpStatus.valueOf(HttpServletResponse.SC_NO_CONTENT));
}

// Update the output string and remove the unwanted parts.
String outputString = updateOutput(output.toString());

// Wrap the String output to the class for better parsing in the FE
OaiMetadataWrapper oaiMetadataWrapper = new OaiMetadataWrapper(output.toString());
OaiMetadataWrapper oaiMetadataWrapper = new OaiMetadataWrapper(outputString);
return new ResponseEntity<>(oaiMetadataWrapper, HttpStatus.valueOf(SC_OK));
}

Expand Down Expand Up @@ -331,6 +341,53 @@ private List<ClarinFeaturedServiceLink> mapFeaturedServiceLinks(List<MetadataVal
return featuredServiceLinkList;
}

/**
* Remove the unnecessary parts from the output. It is removing the XML header and the <bib:bibtex> tag.
*/
private String updateOutput(String output) {
String outputString = "";
// Remove <?xml version="1.0" encoding="UTF-8" ?> from the output.
if (output != null && output.startsWith(XML_FILE_START)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do it more flexible? what if someone adds there a whitspace or similar

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

// Remove XML_FILE_START from the start of the string.
outputString = output.substring(XML_FILE_START.length());
}

// If bibtex, remove the <bib:bibtex> in the start and the end of the string.
outputString = this.removeBibtexTag(outputString);
return outputString;
}

/**
* Remove the <bib:bibtex> tag from the string.
*/
private String removeBibtexTag(String xml) {
String outputString = xml;
String openingTagPattern = String.format("<%s[^>]*>", BIBTEX_TAG);
String closingTagPattern = ">";
String endBibtexTagPattern = String.format("</%s>", BIBTEX_TAG);

Pattern openingTagRegex = Pattern.compile(openingTagPattern);
Pattern closingTagRegex = Pattern.compile(closingTagPattern);

Matcher openingTagMatcher = openingTagRegex.matcher(outputString);
Matcher closingTagMatcher = closingTagRegex.matcher(outputString);

if (openingTagMatcher.find() && closingTagMatcher.find()) {
int openingTagStart = openingTagMatcher.start();
int closingTagEnd = closingTagMatcher.end();

// Remove the <bib:bibtex> tag from the start of the string.
outputString = outputString.substring(0, openingTagStart) + outputString.substring(closingTagEnd);
// Remove the </bib:bibtex> tag from the end of the string.
outputString = outputString.replace(endBibtexTagPattern, "");
// Remove empty spaces from the start and the end of the string.
return outputString.trim();
} else {
// Tags not found or mismatched
return xml;
}
}

}

/**
Expand Down
5 changes: 5 additions & 0 deletions dspace/config/clarin-dspace.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,8 @@ s3.upload.by.parts.enabled = true

### The build version is stored in the specific file ###
build.version.file.path = ${dspace.dir}/config/VERSION_D.txt


#### Item View ####
# Show handle and doi as identifiers - show only DOI if it exists instead of handle by default
item-page.show-handle-and-doi = false
MajoBerger marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions dspace/config/modules/rest.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ rest.properties.exposed = themed.by.company.name
rest.properties.exposed = identifier.doi.resolver
rest.properties.exposed = spring.servlet.multipart.max-file-size
rest.properties.exposed = authentication-shibboleth.show.idp-attributes
rest.properties.exposed = item-page.show-handle-and-doi

# TUL
rest.properties.exposed = dspace.ui.url
Expand Down
Loading