92 lines
3.7 KiB
Java
92 lines
3.7 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.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 {
|
|
|
|
/*private final String oaiEndpoint;*/
|
|
|
|
public String xml = new String();
|
|
|
|
/*public OaiSetListRoute(final String oaiEndpoint) {
|
|
this.oaiEndpoint = oaiEndpoint;
|
|
}*/
|
|
|
|
|
|
@Override
|
|
public void configure() throws Exception {
|
|
JacksonXMLDataFormat xmlJsonFormat = new JacksonXMLDataFormat();
|
|
|
|
|
|
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
|
|
.marshal().json() // Convert String to JSON
|
|
.to("direct:serveResponse")
|
|
.log("JSON Response: ${body}");
|
|
|
|
// Serve the JSON response
|
|
from("direct:serveResponse")
|
|
.setHeader("Content-Type", constant("application/json")) // Set the response content type
|
|
.setHeader("CamelHttpResponseCode", constant(200)) // Set the HTTP response code
|
|
.setBody(simple("${body}")) // Set the response body
|
|
.log("${body}")
|
|
.to("mock:result"); // Replace 'mock:result' with the appropriate endpoint to serve the response
|
|
}*/
|
|
}
|