use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class CatalogComponentTest method testTransformMetacardNoProducerInputTransformerRegistered.
@Test
public void testTransformMetacardNoProducerInputTransformerRegistered() throws Exception {
LOGGER.debug("Running testTransformMetacard()");
// Mock the MimeTypeToTransformerMapper and register it in the OSGi
// Registry (PojoSR)
MimeTypeToTransformerMapper matchingService = mock(MimeTypeToTransformerMapper.class);
// bundleContext.registerService(
// MimeTypeToTransformerMapper.class.getName(), matchingService, null );
catalogComponent.setMimeTypeToTransformerMapper(matchingService);
// Mock the MimeTypeToTransformerMapper returning empty list of
// InputTransformers
List list = new ArrayList<InputTransformer>();
when(matchingService.findMatches(eq(InputTransformer.class), isA(MimeType.class))).thenReturn(list);
// Send in sample XML as InputStream to InputTransformer
InputStream input = IOUtils.toInputStream(xmlInput);
// Get the InputTransformer registered with the ID associated with the
// <from> node in the Camel route
InputTransformer transformer = getTransformer("text/xml", "identity");
assertNotNull("InputTransformer for text/xml;id=identity not found", transformer);
// Attempt to transform the XML input into a Metacard
try {
transformer.transform(input);
fail("Should have thrown a CatalogTransformerException");
} catch (CatalogTransformerException e) {
assertEquals("Did not find an InputTransformer for MIME Type [text/xml] and id [xml]", e.getMessage());
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class OpenSearchEndpoint method executeQuery.
/**
* Executes the OpenSearchQuery and formulates the response
*
* @param format - of the results in the response
* @param query - the query to execute
* @param ui -the ui information to use to format the results
* @return the response on the query
*/
private Response executeQuery(String format, OpenSearchQuery query, UriInfo ui, Map<String, Serializable> properties) {
Response response = null;
String queryFormat = format;
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
List<String> subscriptionList = queryParams.get(Constants.SUBSCRIPTION_KEY);
LOGGER.trace("Attempting to execute query: {}", query);
try {
Map<String, Serializable> arguments = new HashMap<>();
String organization = framework.getOrganization();
String url = ui.getRequestUri().toString();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("organization: {}", organization);
LOGGER.trace("url: {}", url);
}
arguments.put("organization", organization);
arguments.put("url", url);
// interval
if (CollectionUtils.isNotEmpty(subscriptionList)) {
String subscription = subscriptionList.get(0);
LOGGER.trace("Subscription: {}", subscription);
arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
if (CollectionUtils.isNotEmpty(intervalList)) {
arguments.put(UPDATE_QUERY_INTERVAL, intervalList.get(0));
}
}
if (StringUtils.isEmpty(queryFormat)) {
queryFormat = DEFAULT_FORMAT;
}
if (query.getFilter() != null) {
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), properties);
QueryResponse queryResponse;
LOGGER.trace("Sending query");
queryResponse = framework.query(queryRequest);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponse, queryFormat, arguments);
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
} else {
// No query was specified
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), null);
// Create a dummy QueryResponse with zero results
QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, new ArrayList<>(), 0);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponseQueue, queryFormat, arguments);
if (null != content) {
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
}
}
} catch (UnsupportedQueryException ce) {
LOGGER.debug("Unsupported query", ce);
response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
} catch (CatalogTransformerException e) {
LOGGER.debug("Error transforming response", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
} catch (FederationException e) {
LOGGER.debug("Error executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
} catch (SourceUnavailableException e) {
LOGGER.debug("Error executing query because the underlying source was unavailable.", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query because the underlying source was unavailable.")).build();
} catch (RuntimeException e) {
// Account for any runtime exceptions and send back a server error
// this prevents full stacktraces returning to the client
// this allows for a graceful server error to be returned
LOGGER.debug("RuntimeException on executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
}
return response;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class MetacardFactory method generateMetacard.
Metacard generateMetacard(String mimeTypeRaw, String id, String fileName, Path tmpContentPath) throws MetacardCreationException, MimeTypeParseException {
Metacard generatedMetacard = null;
MimeType mimeType = new MimeType(mimeTypeRaw);
List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
List<String> stackTraceList = new ArrayList<>();
LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
for (InputTransformer candidate : listOfCandidates) {
try (InputStream transformerStream = com.google.common.io.Files.asByteSource(tmpContentPath.toFile()).openStream()) {
generatedMetacard = candidate.transform(transformerStream);
} catch (RuntimeException | CatalogTransformerException | IOException e) {
List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
stackTraceList.add(String.format("Transformer [%s] could not create metacard.", candidate));
stackTraceList.addAll(stackTraces);
LOGGER.debug("Transformer [{}] could not create metacard.", candidate, e);
}
if (generatedMetacard != null) {
break;
}
}
if (generatedMetacard == null) {
throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeTypeRaw, StringUtils.join(stackTraceList, "\n")));
}
if (id != null) {
generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
} else {
generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, uuidGenerator.generateUuid()));
}
if (StringUtils.isBlank(generatedMetacard.getTitle())) {
generatedMetacard.setAttribute(new AttributeImpl(Metacard.TITLE, fileName));
}
return generatedMetacard;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class CatalogComponent method createEndpoint.
/*
* (non-Javadoc)
*
* @see org.apache.camel.impl.DefaultComponent#createEndpoint(java.lang.String,
* java.lang.String, java.util.Map)
*/
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws CatalogTransformerException {
LOGGER.trace("ENTERING: createEndpoint");
LOGGER.debug("CatalogEndpoint: uri = {}, remaining = {}, parameters = {}", uri, remaining, parameters);
String transformerId = getAndRemoveParameter(parameters, ID_PARAMETER, String.class);
String mimeType = getAndRemoveParameter(parameters, MIME_TYPE_PARAMETER, String.class);
LOGGER.debug("transformerId = {}", transformerId);
Endpoint endpoint = new CatalogEndpoint(uri, this, transformerId, mimeType, remaining, catalogFramework, mimeTypeMapper);
try {
setProperties(endpoint, parameters);
} catch (Exception e) {
throw new CatalogTransformerException("Failed to create transformer endpoint", e);
}
LOGGER.trace("EXITING: createEndpoint");
return endpoint;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class CatalogComponentTest method testTransformMetacardNoMimeTypeToTransformerMapperRegistered.
@Test
public void testTransformMetacardNoMimeTypeToTransformerMapperRegistered() throws Exception {
LOGGER.debug("Running testTransformMetacard_NoMimeTypeToTransformerMapperRegistered()");
catalogComponent.setMimeTypeToTransformerMapper(null);
// Mock a XML InputTransformer and register it in the OSGi Registry
// (PojoSR)
InputTransformer mockTransformer = getMockInputTransformer();
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(MimeTypeToTransformerMapper.ID_KEY, "xml");
props.put(MimeTypeToTransformerMapper.MIME_TYPE_KEY, "text/xml");
bundleContext.registerService(InputTransformer.class.getName(), mockTransformer, props);
// Send in sample XML as InputStream to InputTransformer
InputStream input = IOUtils.toInputStream(xmlInput);
// Get the InputTransformer registered with the ID associated with the
// <from> node in the Camel route
InputTransformer transformer = getTransformer("text/xml", "identity");
assertNotNull("InputTransformer for text/xml;id=identity not found", transformer);
// Attempt to transform the XML input into a Metacard
try {
transformer.transform(input);
fail("Should have thrown a CatalogTransformerException");
} catch (CatalogTransformerException e) {
assertEquals("Did not find a MimeTypeToTransformerMapper service", e.getMessage());
}
}
Aggregations