From 1f7d4310189108503355829f6a883909addb0eb1 Mon Sep 17 00:00:00 2001 From: milanmajchrak <90026355+milanmajchrak@users.noreply.github.com> Date: Thu, 14 Mar 2024 13:45:32 +0100 Subject: [PATCH] Fixed all mistakes in the Item View page (#551) * Remove the xml info and bibtex tag from the citation refbox * Show handle and DOI in the Item View following the new cfg property * Made updating xml string more generic * Updated the method name for the getting text content from the bibtex xml. --- .../app/rest/ClarinRefBoxController.java | 74 ++++++++++++++++++- dspace/config/clarin-dspace.cfg | 5 ++ dspace/config/modules/rest.cfg | 1 + 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinRefBoxController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinRefBoxController.java index 8c8490f1d53e..710a96648152 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinRefBoxController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinRefBoxController.java @@ -21,10 +21,15 @@ 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; import javax.ws.rs.NotFoundException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLStreamException; import com.hp.hpl.jena.rdf.model.Model; @@ -67,6 +72,9 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.SAXException; /** * A Controller for fetching the data for the ref-box in the Item View (FE). @@ -77,6 +85,9 @@ @RestController @RequestMapping("/api/core/refbox") public class ClarinRefBoxController { + + private final static String BIBTEX_TYPE = "bibtex"; + private final Logger log = org.apache.logging.log4j.LogManager.getLogger(ClarinRefBoxController.class); @Autowired @@ -228,7 +239,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) { @@ -246,8 +257,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."); @@ -263,8 +276,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(type, 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(StringUtils.defaultIfEmpty(outputString, "")); return new ResponseEntity<>(oaiMetadataWrapper, HttpStatus.valueOf(SC_OK)); } @@ -331,6 +347,56 @@ private List mapFeaturedServiceLinks(List tag from the string. + return getXmlTextContent(output); + } else { + // Remove the XML header tag from the string. + return removeXmlHeaderTag(output); + } + } catch (Exception e) { + log.error("Cannot update the xml string for citation because of: " + e.getMessage()); + return null; + } + } + + /** + * Remove the XML header tag from the string. + * + * @param xml + * @return + */ + private String removeXmlHeaderTag(String xml) { + String xmlHeaderPattern = "<\\?xml[^>]*\\?>"; + Pattern xmlHeaderRegex = Pattern.compile(xmlHeaderPattern); + Matcher xmlHeaderMatcher = xmlHeaderRegex.matcher(xml); + if (xmlHeaderMatcher.find()) { + return xml.replaceFirst(xmlHeaderPattern, ""); + } else { + return xml; + } + } + + /** + * Get the text content from the xml string. + */ + private String getXmlTextContent(String xml) throws ParserConfigurationException, IOException, SAXException { + // Parse the XML string + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(new org.xml.sax.InputSource(new java.io.StringReader(xml))); + + // Get the root element + Node root = document.getDocumentElement(); + + // Get the text content of the root element + return root.getTextContent().trim(); + } } /** diff --git a/dspace/config/clarin-dspace.cfg b/dspace/config/clarin-dspace.cfg index 7f47d9896cd5..a13a684a4118 100644 --- a/dspace/config/clarin-dspace.cfg +++ b/dspace/config/clarin-dspace.cfg @@ -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 \ No newline at end of file diff --git a/dspace/config/modules/rest.cfg b/dspace/config/modules/rest.cfg index d17c6dc6bf2c..32dcc0fb1e67 100644 --- a/dspace/config/modules/rest.cfg +++ b/dspace/config/modules/rest.cfg @@ -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