use of ddf.catalog.operation.impl.ResourceResponseImpl in project ddf by codice.
the class ResourceOperations method putPropertiesInResponse.
private ResourceResponse putPropertiesInResponse(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {
if (resourceResponse != null) {
// must add the request properties into response properties in case the source forgot to
Map<String, Serializable> properties = new HashMap<>(resourceResponse.getProperties());
resourceRequest.getProperties().forEach(properties::putIfAbsent);
resourceResponse = new ResourceResponseImpl(resourceResponse.getRequest(), properties, resourceResponse.getResource());
}
return resourceResponse;
}
use of ddf.catalog.operation.impl.ResourceResponseImpl in project ddf by codice.
the class FilterPluginTest method setup.
@Before
public void setup() {
AuthorizingRealm realm = mock(AuthorizingRealm.class);
when(realm.getName()).thenReturn("mockRealm");
when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).then(makeDecision());
Collection<org.apache.shiro.realm.Realm> realms = new ArrayList<>();
realms.add(realm);
DefaultSecurityManager manager = new DefaultSecurityManager();
manager.setRealms(realms);
SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {
@Override
public String getName() {
return "testuser";
}
}, realm.getName());
Subject systemSubject = new MockSubject(manager, principalCollection);
plugin = new FilterPlugin() {
@Override
protected Subject getSystemSubject() {
return systemSubject;
}
};
QueryRequestImpl request = getSampleRequest();
Map<String, Serializable> properties = new HashMap<>();
Subject subject = new MockSubject(manager, principalCollection);
properties.put(SecurityConstants.SECURITY_SUBJECT, subject);
request.setProperties(properties);
incomingResponse = new QueryResponseImpl(request);
ResourceRequest resourceRequest = mock(ResourceRequest.class);
when(resourceRequest.getProperties()).thenReturn(properties);
resourceResponse = new ResourceResponseImpl(resourceRequest, mock(Resource.class));
resourceResponse.setProperties(properties);
DeleteRequest deleteRequest = mock(DeleteRequest.class);
when(deleteRequest.getProperties()).thenReturn(properties);
List<Metacard> deletedMetacards = new ArrayList<>();
deletedMetacards.add(getExactRolesMetacard());
deleteResponse = new DeleteResponseImpl(deleteRequest, properties, deletedMetacards);
List<Metacard> badDeletedMetacards = new ArrayList<>();
badDeletedMetacards.add(getMoreRolesMetacard());
badDeleteResponse = new DeleteResponseImpl(deleteRequest, properties, badDeletedMetacards);
createRequest = new CreateRequestImpl(getExactRolesMetacard());
createRequest.setProperties(properties);
badCreateRequest = new CreateRequestImpl(getMoreRolesMetacard());
badCreateRequest.setProperties(properties);
updateRequest = new UpdateRequestImpl(getExactRolesMetacard().getId(), getExactRolesMetacard());
updateRequest.setProperties(properties);
ResultImpl result1 = new ResultImpl(getMoreRolesMetacard());
ResultImpl result2 = new ResultImpl(getMissingRolesMetacard());
ResultImpl result3 = new ResultImpl(getExactRolesMetacard());
ResultImpl result4 = new ResultImpl(getNoRolesMetacard());
ResultImpl result5 = new ResultImpl(getNoSecurityAttributeMetacard());
incomingResponse.addResult(result1, false);
incomingResponse.addResult(result2, false);
incomingResponse.addResult(result3, false);
incomingResponse.addResult(result4, false);
incomingResponse.addResult(result5, true);
}
use of ddf.catalog.operation.impl.ResourceResponseImpl in project ddf by codice.
the class AbstractCswSource method retrieveResource.
@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> requestProperties) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
if (canRetrieveResourceById()) {
// If no resource reader was found, retrieve the product through a GetRecordById request
Serializable serializableId = null;
if (requestProperties != null) {
serializableId = requestProperties.get(Core.ID);
}
if (serializableId == null) {
throw new ResourceNotFoundException("Unable to retrieve resource because no metacard ID was found.");
}
String metacardId = serializableId.toString();
LOGGER.debug("Retrieving resource for ID : {}", metacardId);
Csw csw = factory.getClientForSubject((Subject) requestProperties.get(SecurityConstants.SECURITY_SUBJECT));
GetRecordByIdRequest getRecordByIdRequest = new GetRecordByIdRequest();
getRecordByIdRequest.setService(CswConstants.CSW);
getRecordByIdRequest.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
getRecordByIdRequest.setOutputFormat(MediaType.APPLICATION_OCTET_STREAM);
getRecordByIdRequest.setId(metacardId);
String rangeValue = "";
long requestedBytesToSkip = 0;
if (requestProperties.containsKey(CswConstants.BYTES_TO_SKIP)) {
requestedBytesToSkip = (Long) requestProperties.get(CswConstants.BYTES_TO_SKIP);
rangeValue = String.format("%s%s-", CswConstants.BYTES_EQUAL, requestProperties.get(CswConstants.BYTES_TO_SKIP).toString());
LOGGER.debug("Range: {}", rangeValue);
}
CswRecordCollection recordCollection;
try {
recordCollection = csw.getRecordById(getRecordByIdRequest, rangeValue);
Resource resource = recordCollection.getResource();
if (resource != null) {
long responseBytesSkipped = 0L;
if (recordCollection.getResourceProperties().get(BYTES_SKIPPED) != null) {
responseBytesSkipped = (Long) recordCollection.getResourceProperties().get(BYTES_SKIPPED);
}
alignStream(resource.getInputStream(), requestedBytesToSkip, responseBytesSkipped);
return new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(resource.getInputStream()), resource.getMimeTypeValue(), FilenameUtils.getName(resource.getName())));
}
} catch (CswException | IOException e) {
throw new ResourceNotFoundException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, metacardId), e);
}
}
LOGGER.debug("Retrieving resource at : {}", resourceUri);
return resourceReader.retrieveResource(resourceUri, requestProperties);
}
use of ddf.catalog.operation.impl.ResourceResponseImpl in project ddf by codice.
the class OgcUrlResourceReader method retrieveResource.
/**
* Retrieves a {@link ddf.catalog.resource.Resource} based on a {@link URI} and provided
* arguments. A connection is made to the {@link URI} to obtain the
* {@link ddf.catalog.resource.Resource}'s {@link InputStream} and build a
* {@link ResourceResponse} from that. The {@link ddf.catalog.resource.Resource}'s name gets set
* to the {@link URI} passed in. Calls {@link URLResourceReader}, if the mime-type is
* "text/html" it will inject a simple script to redirect to the resourceURI instead of
* attempting to download it.
*
* @param resourceURI
* A {@link URI} that defines what {@link Resource} to retrieve and how to do it.
* @param properties
* Any additional arguments that should be passed to the
* {@link ddf.catalog.resource.ResourceReader}.
* @return A {@link ResourceResponse} containing the retrieved {@link Resource}.
* @throws ResourceNotSupportedException
*/
public ResourceResponse retrieveResource(URI resourceURI, Map<String, Serializable> properties) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
LOGGER.debug("Calling URLResourceReader.retrieveResource()");
ResourceResponse response = urlResourceReader.retrieveResource(resourceURI, properties);
Resource resource = response.getResource();
MimeType mimeType = resource.getMimeType();
LOGGER.debug("mimeType: {}", mimeType);
if (mimeType != null) {
String mimeTypeStr = mimeType.toString();
String detectedMimeType = "";
if (UNKNOWN_MIME_TYPES.contains(mimeTypeStr)) {
detectedMimeType = tika.detect(resourceURI.toURL());
}
if (StringUtils.contains(detectedMimeType, MediaType.TEXT_HTML) || StringUtils.contains(mimeTypeStr, MediaType.TEXT_HTML)) {
LOGGER.debug("Detected \"text\\html\". Building redirect script");
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("<html><script type=\"text/javascript\">window.location.replace(\"");
strBuilder.append(resourceURI);
strBuilder.append("\");</script></html>");
return new ResourceResponseImpl(new ResourceImpl(new ByteArrayInputStream(strBuilder.toString().getBytes(StandardCharsets.UTF_8)), detectedMimeType, resource.getName()));
}
}
return response;
}
Aggregations