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 all 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,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;
Expand Down Expand Up @@ -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).
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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.");
Expand All @@ -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));
}

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

/**
* Remove the unnecessary parts from the output.
*/
private String updateOutput(String type, String output) {
try {
if (StringUtils.equals(type, BIBTEX_TYPE)) {
// Remove the XML header tag and the <bib:bibtex> 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());
milanmajchrak marked this conversation as resolved.
Show resolved Hide resolved
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();
}
}

/**
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