use of org.jdom2.filter.Filter in project mycore by MyCoRe-Org.
the class MCRRestAPIObjectsHelper method listDerivates.
/**
* returns a list of derivate objects
* @param info - the injected Jersey URIInfo object
*
* @param mcrObjID - the MyCoRe Object ID
* @param format - the output format ('xml'|'json')
* @param sort - the sort criteria
*
* @return a Jersey response object
* @throws MCRRestAPIException
*
* @see org.mycore.restapi.v1.MCRRestAPIObjects#listDerivates(UriInfo, HttpServletRequest, String, String, String)
*/
public static Response listDerivates(UriInfo info, HttpServletRequest request, String mcrObjID, String format, String sort) throws MCRRestAPIException {
List<MCRRestAPIError> errors = new ArrayList<>();
MCRRestAPISortObject sortObj = null;
try {
sortObj = createSortObject(sort);
} catch (MCRRestAPIException rae) {
errors.addAll(rae.getErrors());
}
if (format.equals(MCRRestAPIObjects.FORMAT_JSON) || format.equals(MCRRestAPIObjects.FORMAT_XML)) {
// ok
} else {
errors.add(new MCRRestAPIError(MCRRestAPIError.CODE_WRONG_PARAMETER, "The Parameter format is wrong.", "Allowed values for format are 'json' or 'xml'."));
}
if (errors.size() > 0) {
throw new MCRRestAPIException(Status.BAD_REQUEST, errors);
}
// Parameters are checked - continue to retrieve data
List<MCRObjectIDDate> objIdDates = retrieveMCRObject(mcrObjID).getStructure().getDerivates().stream().map(MCRMetaLinkID::getXLinkHrefID).filter(MCRMetadataManager::exists).map(id -> new MCRObjectIDDate() {
long lastModified;
{
try {
lastModified = MCRXMLMetadataManager.instance().getLastModified(id);
} catch (IOException e) {
lastModified = 0;
LOGGER.error("Exception while getting last modified of {}", id, e);
}
}
@Override
public String getId() {
return id.toString();
}
@Override
public Date getLastModified() {
return new Date(lastModified);
}
}).sorted(new MCRRestAPISortObjectComparator(sortObj)::compare).collect(Collectors.toList());
String authHeader = MCRJSONWebTokenUtil.createJWTAuthorizationHeader(MCRJSONWebTokenUtil.retrieveAuthenticationToken(request));
// output as XML
if (MCRRestAPIObjects.FORMAT_XML.equals(format)) {
Element eDerObjects = new Element("derobjects");
Document docOut = new Document(eDerObjects);
eDerObjects.setAttribute("numFound", Integer.toString(objIdDates.size()));
for (MCRObjectIDDate oid : objIdDates) {
Element eDerObject = new Element("derobject");
eDerObject.setAttribute("ID", oid.getId());
MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(oid.getId()));
String mcrID = der.getDerivate().getMetaLink().getXLinkHref();
eDerObject.setAttribute("metadata", mcrID);
if (der.getLabel() != null) {
eDerObject.setAttribute("label", der.getLabel());
}
eDerObject.setAttribute("lastModified", SDF_UTC.format(oid.getLastModified()));
eDerObject.setAttribute("href", info.getAbsolutePathBuilder().path(oid.getId()).build().toString());
eDerObjects.addContent(eDerObject);
}
try {
StringWriter sw = new StringWriter();
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
xout.output(docOut, sw);
return Response.ok(sw.toString()).type("application/xml; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
} catch (IOException e) {
throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, GENERAL_ERROR_MSG, e.getMessage()));
}
}
// output as JSON
if (MCRRestAPIObjects.FORMAT_JSON.equals(format)) {
StringWriter sw = new StringWriter();
try {
JsonWriter writer = new JsonWriter(sw);
writer.setIndent(" ");
writer.beginObject();
writer.name("numFound").value(objIdDates.size());
writer.name("mycoreobjects");
writer.beginArray();
for (MCRObjectIDDate oid : objIdDates) {
writer.beginObject();
writer.name("ID").value(oid.getId());
MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(oid.getId()));
String mcrID = der.getDerivate().getMetaLink().getXLinkHref();
writer.name("metadata").value(mcrID);
if (der.getLabel() != null) {
writer.name("label").value(der.getLabel());
}
writer.name("lastModified").value(SDF_UTC.format(oid.getLastModified()));
writer.name("href").value(info.getAbsolutePathBuilder().path(oid.getId()).build().toString());
writer.endObject();
}
writer.endArray();
writer.endObject();
writer.close();
return Response.ok(sw.toString()).type("application/json; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
} catch (IOException e) {
throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, GENERAL_ERROR_MSG, e.getMessage()));
}
}
throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unexepected program flow termination.", "Please contact a developer!"));
}
use of org.jdom2.filter.Filter in project mycore by MyCoRe-Org.
the class MCRRestAPIClassifications method writeXML.
/**
* Output xml
* @param eRoot - the root element
* @param lang - the language which should be filtered or null for no filter
* @return a string representation of the XML
* @throws IOException
*/
private static String writeXML(Element eRoot, String lang) throws IOException {
StringWriter sw = new StringWriter();
if (lang != null) {
// <label xml:lang="en" text="part" />
XPathExpression<Element> xpE = XPathFactory.instance().compile("//label[@xml:lang!='" + lang + "']", Filters.element(), null, Namespace.XML_NAMESPACE);
for (Element e : xpE.evaluate(eRoot)) {
e.getParentElement().removeContent(e);
}
}
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
Document docOut = new Document(eRoot.detach());
xout.output(docOut, sw);
return sw.toString();
}
use of org.jdom2.filter.Filter in project qpp-conversion-tool by CMSgov.
the class ValidationApiAcceptance method verifyDetail.
private void verifyDetail(Detail detail) {
String xPath = detail.getPath();
Filter filter = xPath.contains("@") ? Filters.attribute() : Filters.element();
try {
Object found = evaluateXpath(detail.getPath(), filter);
if (filter.equals(Filters.attribute())) {
Attribute attribute = (Attribute) found;
assertThat(attribute.getIntValue()).isEqualTo(CANNED_VALUE);
assertThat(detail.getMessage()).startsWith(ValidationServiceImpl.SV_LABEL);
} else {
assertThat(found).isNotNull();
}
} catch (XmlException | DataConversionException ex) {
fail("This xpath could not be found: " + detail.getPath(), ex);
}
}
Aggregations