uri-resolver/src/main/java/org/gcube/datatransfer/resolver/gis/util/GetResponseRecordFilter.java

111 lines
3.4 KiB
Java

/**
*
*/
package org.gcube.datatransfer.resolver.gis.util;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* The Class GetResponseRecordFilter.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Jun 16, 2016
*/
public class GetResponseRecordFilter {
public static Logger logger = LoggerFactory.getLogger(GetResponseRecordFilter.class);
/**
* Delete summary record.
*
* @param doc
* the doc
* @param identifier
* the identifier
*/
private static void deleteSummaryRecord(Document doc, String identifier) {
// <csw:SummaryRecord> list
NodeList nodes = doc.getElementsByTagName("csw:SummaryRecord");
logger.trace("SummaryRecord are: " + nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
Element summaryRecord = (Element) nodes.item(i);
// <dc:identifier>
Element id = (Element) summaryRecord.getElementsByTagName("dc:identifier").item(0);
String idValue = id.getTextContent();
logger.trace("Summary dc:identifier is: " + idValue);
if (idValue.equals(identifier)) {
summaryRecord.getParentNode().removeChild(summaryRecord);
logger.trace("Removed child " + idValue);
}
}
}
/**
* Removes the summary ids by list ids.
*
* @param getRecordsResponse the get records response
* @param idsToRemove the ids to remove
* @return the input stream
* @throws IOException Signals that an I/O exception has occurred.
*/
public static InputStream removeSummaryIdsByListIds(InputStream getRecordsResponse, List<String> idsToRemove) throws IOException {
try {
// logger.trace("getRecordsResponse is: "+IOUtils.toString(getRecordsResponse));
BufferedInputStream bis = new BufferedInputStream(getRecordsResponse);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(bis);
for (String identifier : idsToRemove) {
deleteSummaryRecord(doc, identifier);
}
return documentToInputStream(doc);
}
catch (Exception e) {
logger.error("An error occurred during removing IDS by List: ", e);
return getRecordsResponse;
}
}
/**
* Document to input stream.
*
* @param xml the xml
* @return the input stream
* @throws Exception the exception
*/
private static final InputStream documentToInputStream(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
StreamResult outputTarget = new StreamResult(outputStream);
tf.transform(new DOMSource(xml), outputTarget);
return new ByteArrayInputStream(outputStream.toByteArray());
// return out.toString();
}
}