use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.
the class ResourceMetacardTransformer method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
LOGGER.trace("Entering resource ResourceMetacardTransformer.transform");
if (!isValid(metacard)) {
throw new CatalogTransformerException("Could not transform metacard to a resource because the metacard is not valid.");
}
if (StringUtils.isNotEmpty(metacard.getResourceSize())) {
arguments.put(Metacard.RESOURCE_SIZE, metacard.getResourceSize());
}
String id = metacard.getId();
LOGGER.debug("executing resource request with id '{}'", id);
final ResourceRequest resourceRequest = new ResourceRequestById(id, arguments);
ResourceResponse resourceResponse = null;
String sourceName = metacard.getSourceId();
if (StringUtils.isBlank(sourceName)) {
sourceName = catalogFramework.getId();
}
String resourceUriAscii = "";
if (metacard.getResourceURI() != null) {
resourceUriAscii = metacard.getResourceURI().toASCIIString();
}
try {
resourceResponse = catalogFramework.getResource(resourceRequest, sourceName);
} catch (IOException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
} catch (ResourceNotFoundException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
} catch (ResourceNotSupportedException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
}
if (resourceResponse == null) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii));
}
Resource transformedContent = resourceResponse.getResource();
MimeType mimeType = transformedContent.getMimeType();
if (mimeType == null) {
try {
mimeType = new MimeType(DEFAULT_MIME_TYPE_STR);
// There is no method to set the MIME type, so in order to set it to our default
// one, we need to create a new object.
transformedContent = new ResourceImpl(transformedContent.getInputStream(), mimeType, transformedContent.getName());
} catch (MimeTypeParseException e) {
throw new CatalogTransformerException("Could not create default mime type upon null mimeType, for default mime type '" + DEFAULT_MIME_TYPE_STR + "'.", e);
}
}
LOGGER.debug("Found mime type: '{}' for product of metacard with id: '{}'.\nGetting associated resource from input stream. \n", mimeType, id);
LOGGER.trace("Exiting resource transform for metacard id: '{}'", id);
return transformedContent;
}
use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.
the class ResourceImplTest method testInvalidMimeType.
@Test
public void testInvalidMimeType() {
InputStream is;
try {
is = new FileInputStream(content);
ResourceImpl ri = new ResourceImpl(is, "123467890", null);
assertEquals(null, ri.getMimeType());
} catch (IOException e) {
LOGGER.error("IO Failure", e);
new Failure(null, e);
}
}
use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.
the class ResourceImplTest method testResourceNameNotSet.
@Test
public void testResourceNameNotSet() {
InputStream is;
try {
is = new FileInputStream(content);
ResourceImpl ri = new ResourceImpl(is, mimeType, null);
assertEquals(null, ri.getName());
} catch (IOException e) {
LOGGER.error("IO Failure", e);
new Failure(null, e);
}
}
use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.
the class ResourceImplTest method testResourceImplConstructors.
@Test
public void testResourceImplConstructors() {
InputStream is;
try {
is = new FileInputStream(content);
ResourceImpl ri = new ResourceImpl(is, TEST_NAME);
ri.setSize(content.length());
assertEquals(is, ri.getInputStream());
assertEquals(null, ri.getMimeType());
assertEquals(content.length(), ri.getSize());
assertNotNull(ri.toString());
ri = new ResourceImpl(is, mimeType, TEST_NAME);
ri.setSize(content.length());
assertEquals(is, ri.getInputStream());
assertEquals(mimeType.toString(), ri.getMimeType().toString());
assertEquals(content.length(), ri.getSize());
assertEquals(mimeType.toString(), ri.getMimeTypeValue());
assertNotNull(ri.toString());
ri = new ResourceImpl(is, mimeType.toString(), TEST_NAME);
ri.setSize(content.length());
assertEquals(is, ri.getInputStream());
assertEquals(mimeType.toString(), ri.getMimeType().toString());
assertEquals(content.length(), ri.getSize());
assertNotNull(ri.toString());
} catch (IOException e) {
LOGGER.error("IO Failure", e);
new Failure(null, e);
}
}
use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.
the class ResourceImplTest method testResourceImplSizeNotSet.
@Test
public void testResourceImplSizeNotSet() {
InputStream is;
try {
is = new FileInputStream(content);
ResourceImpl ri = new ResourceImpl(is, mimeType, TEST_NAME);
assertEquals(-1, ri.getSize());
} catch (IOException e) {
LOGGER.error("IO Failure", e);
new Failure(null, e);
}
}
Aggregations