Search in sources :

Example 51 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class ContentResourceReader method retrieveResource.

@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> arguments) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    LOGGER.trace("ENTERING: retrieveResource");
    ResourceResponse response = null;
    if (resourceUri == null) {
        throw new ResourceNotFoundException("Unable to find resource - resource URI was null");
    }
    if (resourceUri.getScheme().equals(ContentItem.CONTENT_SCHEME)) {
        LOGGER.debug("Resource URI is content scheme");
        String contentId = resourceUri.getSchemeSpecificPart();
        if (contentId != null && !contentId.isEmpty()) {
            if (arguments != null && arguments.get(ContentItem.QUALIFIER_KEYWORD) instanceof String && StringUtils.isNotBlank((String) arguments.get(ContentItem.QUALIFIER_KEYWORD))) {
                try {
                    resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), (String) arguments.get(ContentItem.QUALIFIER_KEYWORD));
                } catch (URISyntaxException e) {
                    throw new ResourceNotFoundException("Unable to create with qualifier", e);
                }
            }
            ReadStorageRequest readRequest = new ReadStorageRequestImpl(resourceUri, arguments);
            try {
                ReadStorageResponse readResponse = storage.read(readRequest);
                ContentItem contentItem = readResponse.getContentItem();
                String fileName = contentItem.getFilename();
                LOGGER.debug("resource name: {}", fileName);
                InputStream is = contentItem.getInputStream();
                response = new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), contentItem.getMimeType(), fileName));
            } catch (StorageException e) {
                throw new ResourceNotFoundException(e);
            }
        }
    }
    LOGGER.trace("EXITING: retrieveResource");
    return response;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) URI(java.net.URI) ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) BufferedInputStream(java.io.BufferedInputStream) ReadStorageRequestImpl(ddf.catalog.content.operation.impl.ReadStorageRequestImpl) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem)

Example 52 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class TestResourceUsagePlugin method testPostResourceRemoteSourceFalseRemoteDestinationKey.

@Test
public void testPostResourceRemoteSourceFalseRemoteDestinationKey() throws Exception {
    plugin.setMonitorLocalSources(false);
    ResourceResponse originalResponse = getMockRemoteResourceResponseFalseRemoteDestinationKey(RESOURCE_SIZE, TEST_USER);
    ResourceResponse response = plugin.process(originalResponse);
    assertThat(response, is(notNullValue()));
    assertThat(response, is(originalResponse));
    verify(attributeStore, never()).getDataLimitByUser(anyString());
    verify(attributeStore, never()).getCurrentDataUsageByUser(anyString());
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Test(org.junit.Test)

Example 53 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class TestResourceUsagePlugin method testPostResourceRemoteSourceValidSize.

@Test
public void testPostResourceRemoteSourceValidSize() throws Exception {
    plugin.setMonitorLocalSources(false);
    ArgumentCaptor<String> usernameArg = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Long> dataUsage = ArgumentCaptor.forClass(Long.class);
    ResourceResponse originalResponse = getMockRemoteResourceResponse(RESOURCE_SIZE, TEST_USER);
    ResourceResponse response = plugin.process(originalResponse);
    assertThat(response, notNullValue());
    assertThat(response, is(originalResponse));
    verify(attributeStore).updateUserDataUsage(usernameArg.capture(), dataUsage.capture());
    assertThat(usernameArg.getValue(), is(TEST_USER));
    assertThat(dataUsage.getValue(), is(Long.valueOf(RESOURCE_SIZE)));
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Matchers.anyLong(org.mockito.Matchers.anyLong) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 54 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class TestResourceUsagePlugin method getMockRemoteResourceResponseFalseRemoteDestinationKey.

private ResourceResponse getMockRemoteResourceResponseFalseRemoteDestinationKey(String resourceSize, String expectedUsername) {
    setSubject(expectedUsername);
    ResourceResponse resourceResponse = getMockResourceResponse(resourceSize, expectedUsername);
    when(resourceResponse.getPropertyValue(Constants.REMOTE_DESTINATION_KEY)).thenReturn(false);
    when(resourceResponse.hasProperties()).thenReturn(true);
    return resourceResponse;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse)

Example 55 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class TestResourceUsagePlugin method getMockResourceResponse.

private ResourceResponse getMockResourceResponse(String resourceSize, String expectedUsername) {
    setSubject(expectedUsername);
    ResourceResponse resourceResponse = mock(ResourceResponse.class);
    Map<String, Serializable> responseProperties = new HashMap<>();
    responseProperties.put(SecurityConstants.SECURITY_SUBJECT, subject);
    responseProperties.put(Metacard.RESOURCE_SIZE, resourceSize);
    when(resourceResponse.getPropertyNames()).thenReturn(responseProperties.keySet());
    when(resourceResponse.getPropertyValue(SecurityConstants.SECURITY_SUBJECT)).thenReturn(subject);
    when(resourceResponse.getPropertyValue(Metacard.RESOURCE_SIZE)).thenReturn(resourceSize);
    return resourceResponse;
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString)

Aggregations

ResourceResponse (ddf.catalog.operation.ResourceResponse)94 Test (org.junit.Test)49 URI (java.net.URI)30 HashMap (java.util.HashMap)28 Serializable (java.io.Serializable)26 Resource (ddf.catalog.resource.Resource)25 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)23 ResourceRequest (ddf.catalog.operation.ResourceRequest)18 IOException (java.io.IOException)18 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)17 Metacard (ddf.catalog.data.Metacard)16 MimeType (javax.activation.MimeType)16 Response (javax.ws.rs.core.Response)15 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)14 URLResourceReader (ddf.catalog.resource.impl.URLResourceReader)14 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)10 InputStream (java.io.InputStream)9 Map (java.util.Map)9 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)8 StringWriter (java.io.StringWriter)8