added dynamic endpoint route for ListSets. Need to aggregate and serve JSON
This commit is contained in:
parent
ce4d729185
commit
e61e252306
48
pom.xml
48
pom.xml
|
@ -89,7 +89,55 @@
|
||||||
<artifactId>camel-spring-boot-starter</artifactId>
|
<artifactId>camel-spring-boot-starter</artifactId>
|
||||||
<version>${apache.camel.version}</version>
|
<version>${apache.camel.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel.springboot</groupId>
|
||||||
|
<artifactId>camel-jackson-starter</artifactId>
|
||||||
|
<version>${apache.camel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-jackson</artifactId>
|
||||||
|
<version>${apache.camel.version}</version> <!-- Use the appropriate version -->
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-jacksonxml</artifactId>
|
||||||
|
<version>${apache.camel.version}</version>
|
||||||
|
<!-- use the same version as your Camel core version -->
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-core</artifactId>
|
||||||
|
<version>2.15.2</version> <!-- Use the appropriate version -->
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.15.2</version> <!-- Use the appropriate version -->
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
|
<artifactId>jackson-dataformat-xml</artifactId>
|
||||||
|
<version>2.15.2</version> <!-- Use the appropriate version -->
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel.springboot</groupId>
|
||||||
|
<artifactId>camel-servlet-starter</artifactId>
|
||||||
|
<version>${apache.camel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-http</artifactId>
|
||||||
|
<version>${apache.camel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
|
<artifactId>jackson-dataformat-xml</artifactId>
|
||||||
|
<version>2.15.2</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -13,6 +13,7 @@ import eu.dnetlib.validatorapi.repositories.ValidationResultRepository;
|
||||||
import eu.dnetlib.validatorapi.routes.OaiPmhRoute;
|
import eu.dnetlib.validatorapi.routes.OaiPmhRoute;
|
||||||
import eu.dnetlib.validatorapi.routes.OaiSetListRoute;
|
import eu.dnetlib.validatorapi.routes.OaiSetListRoute;
|
||||||
import org.apache.camel.CamelContext;
|
import org.apache.camel.CamelContext;
|
||||||
|
import org.apache.camel.ProducerTemplate;
|
||||||
import org.apache.camel.builder.RouteBuilder;
|
import org.apache.camel.builder.RouteBuilder;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
@ -233,14 +234,29 @@ public class ValidationController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = {"/getSets"}, method = RequestMethod.GET)
|
@RequestMapping(value = {"/getSets"}, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
public void getSets(@RequestParam(name = "baseUrl", defaultValue = "http://repositorium.sdum.uminho.pt/oai/request") String baseURL //not in use now
|
public String getSets(@RequestParam(name = "baseUrl", defaultValue = "http://repositorium.sdum.uminho.pt/oai/request") String baseURL //not in use now
|
||||||
) throws Exception {
|
) throws Exception {
|
||||||
|
|
||||||
|
RouteBuilder listSetsBuilder = new OaiSetListRoute();
|
||||||
|
ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
|
||||||
|
String response = producerTemplate.requestBodyAndHeader("direct:getResponse", null, "endpoint", "oaipmh://"+ baseURL + "?verb=ListSets", String.class);
|
||||||
|
return response;
|
||||||
|
|
||||||
|
/*
|
||||||
|
System.out.println(camelContext.getRoutes());
|
||||||
RouteBuilder listSetsBuilder = new OaiSetListRoute("oaipmh://"+ baseURL + "?verb=ListSets");
|
RouteBuilder listSetsBuilder = new OaiSetListRoute("oaipmh://"+ baseURL + "?verb=ListSets");
|
||||||
|
|
||||||
|
if(camelContext.getRoutes().size()<1)
|
||||||
camelContext.addRoutes(listSetsBuilder);
|
camelContext.addRoutes(listSetsBuilder);
|
||||||
camelContext.getRouteController().startRoute("oaiSetListRoute");
|
System.out.println(camelContext.getRoutes());
|
||||||
camelContext.getRouteController().stopRoute("oaiSetListRoute");
|
/*camelContext.getRouteController().startRoute("oaiSetListRoute");
|
||||||
System.out.println(((OaiSetListRoute) listSetsBuilder).xml);
|
camelContext.getRouteController().stopRoute("oaiSetListRoute");*/
|
||||||
|
/* ProducerTemplate template = camelContext.createProducerTemplate();
|
||||||
|
String response = template.requestBody("direct:getResponse", null, String.class);
|
||||||
|
return response;
|
||||||
|
*/
|
||||||
|
//System.out.println(((OaiSetListRoute) listSetsBuilder).xml);
|
||||||
}
|
}
|
||||||
@RequestMapping(value = {"/demo"}, produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
|
@RequestMapping(value = {"/demo"}, produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
|
||||||
public String demo() {
|
public String demo() {
|
||||||
|
|
|
@ -1,24 +1,80 @@
|
||||||
package eu.dnetlib.validatorapi.routes;
|
package eu.dnetlib.validatorapi.routes;
|
||||||
|
|
||||||
|
import eu.dnetlib.validatorapi.utils.ListSetAggregationStrategy;
|
||||||
|
import org.apache.camel.Exchange;
|
||||||
import org.apache.camel.builder.RouteBuilder;
|
import org.apache.camel.builder.RouteBuilder;
|
||||||
|
import org.apache.camel.model.dataformat.JacksonXMLDataFormat;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class OaiSetListRoute extends RouteBuilder {
|
public class OaiSetListRoute extends RouteBuilder {
|
||||||
|
|
||||||
private final String oaiEndpoint;
|
/*private final String oaiEndpoint;*/
|
||||||
|
|
||||||
public String xml = new String();
|
public String xml = new String();
|
||||||
public OaiSetListRoute(final String oaiEndpoint) {
|
|
||||||
|
/*public OaiSetListRoute(final String oaiEndpoint) {
|
||||||
this.oaiEndpoint = oaiEndpoint;
|
this.oaiEndpoint = oaiEndpoint;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configure() throws Exception {
|
public void configure() throws Exception {
|
||||||
/* from(oaiEndpoint).process(exchange -> {
|
JacksonXMLDataFormat xmlJsonFormat = new JacksonXMLDataFormat();
|
||||||
// System.out.println(exchange.getIn().getBody(String.class));
|
|
||||||
xml+=exchange.getIn().getBody(String.class)+"\n\n Katerina \n\n";
|
|
||||||
});*/
|
|
||||||
|
|
||||||
from(oaiEndpoint)
|
|
||||||
|
from("direct:getResponse")
|
||||||
|
.process(exchange -> {
|
||||||
|
// Get the endpoint URL from the request parameter
|
||||||
|
String endpoint = exchange.getIn().getHeader("endpoint", String.class);
|
||||||
|
|
||||||
|
// Set the dynamic endpoint URL in the exchange header
|
||||||
|
exchange.getIn().setHeader("dynamicEndpoint", endpoint);
|
||||||
|
})
|
||||||
|
.recipientList(header("dynamicEndpoint"));
|
||||||
|
//.to("direct:processSets");
|
||||||
|
|
||||||
|
from("direct:processSets")
|
||||||
|
.process(exchange -> {
|
||||||
|
// Access the individual response from the ArrayList
|
||||||
|
ArrayList<?> responseList = exchange.getIn().getBody(ArrayList.class);
|
||||||
|
InputStream responseStream = new ByteArrayInputStream(responseList.get(0).toString().getBytes());
|
||||||
|
|
||||||
|
// Set the converted InputStream as the new body
|
||||||
|
exchange.getIn().setBody(responseStream);
|
||||||
|
})
|
||||||
|
.split(xpath("//set")).log("${body}")
|
||||||
|
.aggregate(constant(true), new ListSetAggregationStrategy())
|
||||||
|
.completionPredicate(exchangeProperty(Exchange.AGGREGATED_SIZE).isEqualTo(header("CamelSplitSize")))
|
||||||
|
.to("direct:processAggregatedSets").log("Aggregated: ${body}");
|
||||||
|
|
||||||
|
/*
|
||||||
|
.aggregate(header("dynamicEndpoint"), new ListSetAggregationStrategy())
|
||||||
|
.completionPredicate(exchangeProperty(Exchange.AGGREGATED_SIZE).isEqualTo(header("CamelSplitSize")))
|
||||||
|
.marshal().json(JsonLibrary.Jackson)
|
||||||
|
.convertBodyTo(String.class)
|
||||||
|
.log("Aggregated Response: ${body}");*/
|
||||||
|
}
|
||||||
|
|
||||||
|
/*from("direct:getResponse")
|
||||||
|
.to("oaipmh:http://repositorium.sdum.uminho.pt/oai/request?verb=ListSets")
|
||||||
|
.process(exchange -> {
|
||||||
|
// Process the API response
|
||||||
|
String apiResponse = exchange.getIn().getBody(String.class);
|
||||||
|
|
||||||
|
// Create a new response
|
||||||
|
String newResponse = "New Response: I made it!";
|
||||||
|
|
||||||
|
// Set the new response in the exchange body
|
||||||
|
exchange.getIn().setBody(newResponse);
|
||||||
|
});*/
|
||||||
|
|
||||||
|
|
||||||
|
/* from(oaiEndpoint)
|
||||||
.convertBodyTo(String.class) // Convert response to String
|
.convertBodyTo(String.class) // Convert response to String
|
||||||
.marshal().json() // Convert String to JSON
|
.marshal().json() // Convert String to JSON
|
||||||
.to("direct:serveResponse")
|
.to("direct:serveResponse")
|
||||||
|
@ -31,5 +87,5 @@ public class OaiSetListRoute extends RouteBuilder {
|
||||||
.setBody(simple("${body}")) // Set the response body
|
.setBody(simple("${body}")) // Set the response body
|
||||||
.log("${body}")
|
.log("${body}")
|
||||||
.to("mock:result"); // Replace 'mock:result' with the appropriate endpoint to serve the response
|
.to("mock:result"); // Replace 'mock:result' with the appropriate endpoint to serve the response
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
package eu.dnetlib.validatorapi.utils;
|
||||||
|
|
||||||
|
import org.apache.camel.AggregationStrategy;
|
||||||
|
import org.apache.camel.Exchange;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
import org.xml.sax.InputSource;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import javax.xml.transform.TransformerException;
|
||||||
|
import javax.xml.xpath.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
|
|
||||||
|
public class ListSetAggregationStrategy implements AggregationStrategy {
|
||||||
|
|
||||||
|
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
|
||||||
|
if (oldExchange == null) {
|
||||||
|
return newExchange;
|
||||||
|
}
|
||||||
|
|
||||||
|
String oldBody = null;
|
||||||
|
try {
|
||||||
|
oldBody = exctractSets(oldExchange.getIn().getBody(String.class));
|
||||||
|
String newBody = exctractSets(newExchange.getIn().getBody(String.class));
|
||||||
|
oldExchange.getIn().setBody(oldBody + newBody);
|
||||||
|
|
||||||
|
} catch (ParserConfigurationException e) {
|
||||||
|
System.out.println(e);
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(e);
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (SAXException e) {
|
||||||
|
System.out.println(e);
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (TransformerException e) {
|
||||||
|
System.out.println(e);
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (XPathExpressionException e) {
|
||||||
|
System.out.println(e);
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return oldExchange;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String exctractSets(String xml) throws ParserConfigurationException, IOException, SAXException, TransformerException, XPathExpressionException {
|
||||||
|
// Create an XPathFactory
|
||||||
|
XPathFactory xfactory = XPathFactory.newInstance();
|
||||||
|
|
||||||
|
// Create an XPath object
|
||||||
|
XPath xpath = xfactory.newXPath();
|
||||||
|
|
||||||
|
// Compile the XPath expression to select the set elements
|
||||||
|
XPathExpression expression = xpath.compile("//set");
|
||||||
|
|
||||||
|
// Create an InputSource from the XML string
|
||||||
|
InputSource inputSource = new InputSource(new StringReader(xml));
|
||||||
|
|
||||||
|
// Parse the inputSource and create a Document object
|
||||||
|
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource);
|
||||||
|
|
||||||
|
// Evaluate the XPath expression on the document
|
||||||
|
NodeList setElements = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
|
||||||
|
|
||||||
|
// Process the extracted set elements
|
||||||
|
StringBuilder extractedElements = new StringBuilder();
|
||||||
|
for (int i = 0; i < setElements.getLength(); i++) {
|
||||||
|
extractedElements.append(getElementAsString(setElements.item(i)));
|
||||||
|
}
|
||||||
|
// System.out.println(extractedElements.toString());
|
||||||
|
return extractedElements.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method to convert a Node to String
|
||||||
|
private static String getElementAsString(Node node) {
|
||||||
|
try {
|
||||||
|
javax.xml.transform.TransformerFactory tf = javax.xml.transform.TransformerFactory.newInstance();
|
||||||
|
javax.xml.transform.Transformer transformer = tf.newTransformer();
|
||||||
|
transformer.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
|
java.io.StringWriter writer = new java.io.StringWriter();
|
||||||
|
transformer.transform(new javax.xml.transform.dom.DOMSource(node), new javax.xml.transform.stream.StreamResult(writer));
|
||||||
|
return writer.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue