uoa-validator-api/src/main/java/eu/dnetlib/validatorapi/routes/OaiSetListRoute.java

65 lines
2.5 KiB
Java

package eu.dnetlib.validatorapi.routes;
import eu.dnetlib.validatorapi.utils.ListSetAggregationStrategy;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
@Component
public class OaiSetListRoute extends RouteBuilder {
/*private final String oaiEndpoint;*/
public String xml = new String();
/*public OaiSetListRoute(final String oaiEndpoint) {
this.oaiEndpoint = oaiEndpoint;
}*/
@Override
public void configure() throws Exception {
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);
}).log("${body}")
.split(xpath("//*[local-name()='set']")).log("${body}")
// .log("${body}")
.aggregate(constant(true), new ListSetAggregationStrategy())
.completionPredicate(exchangeProperty(Exchange.AGGREGATED_SIZE).isEqualTo(header("CamelSplitSize")))
.to("direct:processAggregatedSets")
.log("${body}");
from("direct:processAggregatedSets")
.process(exchange -> {
String aggregatedXml = exchange.getIn().getBody(ArrayList.class) + "";
JSONObject jsonObject = XML.toJSONObject(aggregatedXml);
String jsonString = jsonObject.toString();
exchange.getIn().setBody(jsonString);
}).to("seda:result");
}
}