use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class ExportCommand method writeResultToZip.
private void writeResultToZip(/*Mutable,IO*/
ZipOutputStream zipOutputStream, Result result) {
String id = result.getMetacard().getId();
ZipEntry zipEntry = new ZipEntry(Paths.get("metacards", id.substring(0, 3), id, "metacard", id + ".xml").toString());
try {
BinaryContent binaryMetacard = transformer.transform(result.getMetacard(), Collections.emptyMap());
try (InputStream metacardStream = binaryMetacard.getInputStream()) {
zipOutputStream.putNextEntry(zipEntry);
IOUtils.copy(metacardStream, zipOutputStream);
}
} catch (CatalogTransformerException | IOException e) {
LOGGER.warn("Could not transform metacard. Metacard will not be added to zip [{}]", result.getMetacard().getId());
console.printf("%sCould not transform metacard. Metacard will not be included in export. %s - %s%s%n", Ansi.ansi().fg(Ansi.Color.RED).toString(), result.getMetacard().getId(), result.getMetacard().getTitle(), Ansi.ansi().reset().toString());
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class IngestCommand method processIncludeContent.
private void processIncludeContent(ArrayBlockingQueue<Metacard> metacardQueue) {
File inputFile = new File(filePath);
Map<String, Serializable> arguments = new HashMap<>();
arguments.put(DumpCommand.FILE_PATH, inputFile.getParent() + File.separator);
arguments.put(FILE_NAME, inputFile.getName());
ByteSource byteSource = com.google.common.io.Files.asByteSource(inputFile);
Optional<InputCollectionTransformer> zipDecompression = getZipDecompression();
if (zipDecompression.isPresent()) {
try (InputStream inputStream = byteSource.openBufferedStream()) {
List<Metacard> metacardList = zipDecompression.get().transform(inputStream, arguments).stream().filter(Objects::nonNull).collect(Collectors.toList());
if (!metacardList.isEmpty()) {
metacardFileMapping = generateFileMap(new File(inputFile.getParent(), CONTENT_PATH));
fileCount.set(metacardList.size());
for (Metacard metacard : metacardList) {
putMetacardOnQueue(metacardQueue, metacard);
}
}
} catch (IOException | CatalogTransformerException e) {
LOGGER.info("Unable to transform zip file into metacard list.", e);
INGEST_LOGGER.warn("Unable to transform zip file into metacard list.", e);
}
} else {
LOGGER.info("No Zip Transformer found. Unable to transform zip file into metacard list.");
INGEST_LOGGER.warn("No Zip Transformer found. Unable to transform zip file into metacard list.");
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class DumpCommand method exportMetacard.
private void exportMetacard(File dumpLocation, Metacard metacard, AtomicLong resultCount) throws IOException {
if (SERIALIZED_OBJECT_ID.matches(transformerId)) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(getOutputFile(dumpLocation, metacard)))) {
oos.writeObject(new MetacardImpl(metacard));
oos.flush();
resultCount.incrementAndGet();
}
} else {
BinaryContent binaryContent;
if (metacard != null) {
for (MetacardTransformer transformer : transformers) {
try {
binaryContent = transformer.transform(metacard, new HashMap<>());
if (binaryContent != null) {
try (FileOutputStream fos = new FileOutputStream(getOutputFile(dumpLocation, metacard))) {
fos.write(binaryContent.getByteArray());
fos.flush();
}
resultCount.incrementAndGet();
break;
}
} catch (CatalogTransformerException e) {
LOGGER.info("One or more metacards failed to transform. Enable debug log for more details.");
}
}
}
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class DumpCommand method createZip.
private void createZip(CatalogFacade catalog, QueryRequest queryRequest, File outputFile, AtomicLong resultCount) throws CatalogTransformerException {
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
// write the metacards to the zip
ResultIterable.resultIterable(catalog::query, queryRequest).stream().map(Result::getMetacard).forEach(metacard -> {
writeMetacardToZip(zipOutputStream, metacard);
if (hasLocalResources(metacard)) {
// write the resources to the zip
Map<String, Resource> resourceMap = getAllMetacardContent(metacard);
resourceMap.forEach((filename, resource) -> writeResourceToZip(zipOutputStream, filename, resource));
}
resultCount.incrementAndGet();
});
} catch (IOException e) {
throw new CatalogTransformerException(String.format("Error occurred when initializing/closing ZipOutputStream with path %s.", outputFile.getAbsoluteFile()), e);
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class Query method getMetacardForId.
/**
* @param searchPhrase The search phrase used to query for the metacard.
* @param proxyTicket The CAS proxy ticket that will be used by the STS to get a SAML assertion.
* @return
*/
private String getMetacardForId(String searchPhrase, String proxyTicket) {
Filter filter = filterBuilder.attribute(Metacard.ANY_TEXT).is().like().text(searchPhrase);
LOGGER.info("Query filter: {}", filter.toString());
String queryError = "Unable to perform query " + filter.toString() + ".";
QueryRequest request = new QueryRequestImpl(new QueryImpl(filter), true);
StringBuilder responseString = new StringBuilder();
try {
Subject subject = securityManager.getSubject(new CasAuthenticationToken(proxyTicket));
LOGGER.info("Adding {} property with value {} to request", SecurityConstants.SECURITY_SUBJECT, subject);
request.getProperties().put(SecurityConstants.SECURITY_SUBJECT, subject);
} catch (SecurityServiceException se) {
LOGGER.error("Could not retrieve subject from securitymanager.", se);
return queryError;
}
try {
LOGGER.debug("About to query the catalog framework with query {}", filter.toString());
QueryResponse queryResponse = catalogFramework.query(request, null);
LOGGER.debug("Got query response from catalog framework for query {}", filter.toString());
List<Result> results = queryResponse.getResults();
if (results != null) {
String message = "The query for " + filter.toString() + " returned " + results.size() + " results.";
responseString.append(message);
LOGGER.debug(message);
for (Result curResult : results) {
Metacard metacard = curResult.getMetacard();
LOGGER.debug("Transforming the metacard with id [{}] to xml.", metacard.getId());
BinaryContent content = catalogFramework.transform(metacard, "xml", null);
StringWriter writer = new StringWriter();
IOUtils.copy(content.getInputStream(), writer, "UTF8");
LOGGER.debug("Formatting xml for metacard with id [{}].", metacard.getId());
responseString.append(format(writer.toString()));
}
} else {
String message = "The query for " + filter.toString() + " returned a null result.";
responseString.append(message);
LOGGER.warn(message);
}
} catch (SourceUnavailableException e) {
LOGGER.error(queryError, e);
} catch (UnsupportedQueryException e) {
LOGGER.error(queryError, e);
} catch (FederationException e) {
LOGGER.error(queryError, e);
} catch (CatalogTransformerException e) {
LOGGER.error(queryError, e);
} catch (IOException e) {
LOGGER.error(queryError, e);
}
return responseString.toString();
}
Aggregations