Search in sources :

Example 86 with ResourceResponse

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

the class OpenSearchSourceTest method testRetrieveResource.

/**
 * Basic retrieve product case. Tests the url sent to the connection is correct.
 */
@Test
public void testRetrieveResource() throws Exception {
    // given
    ResourceReader mockReader = mock(ResourceReader.class);
    when(response.getEntity()).thenReturn(getBinaryData());
    when(mockReader.retrieveResource(any(URI.class), any(Map.class))).thenReturn(new ResourceResponseImpl(new ResourceImpl(getBinaryData(), "")));
    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList("application/octet-stream"));
    when(response.getHeaders()).thenReturn(headers);
    source.setLocalQueryOnly(true);
    source.setResourceReader(mockReader);
    Map<String, Serializable> requestProperties = new HashMap<>();
    requestProperties.put(ID_ATTRIBUTE_NAME, SAMPLE_ID);
    // when
    ResourceResponse response = source.retrieveResource(null, requestProperties);
    // then
    assertThat(response.getResource().getByteArray().length, is(3));
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 87 with ResourceResponse

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

the class OpenSearchSourceTest method testRetrieveResourceBasicAuth.

/**
 * Retrieve Product case using Basic Authentication. Test that the properties map passed to the
 * resource reader includes a username and password.
 */
@Test
public void testRetrieveResourceBasicAuth() throws Exception {
    ResourceReader mockReader = mock(ResourceReader.class);
    when(response.getEntity()).thenReturn(getBinaryData());
    when(mockReader.retrieveResource(any(URI.class), argThat(allOf(hasEntry("username", (Serializable) "user"), hasEntry("password", (Serializable) "secret"))))).thenReturn(new ResourceResponseImpl(new ResourceImpl(getBinaryData(), "")));
    when(encryptionService.decryptValue("secret")).thenReturn("secret");
    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList("application/octet-stream"));
    when(response.getHeaders()).thenReturn(headers);
    source.setLocalQueryOnly(true);
    source.setResourceReader(mockReader);
    source.setUsername("user");
    source.setPassword("secret");
    Map<String, Serializable> requestProperties = new HashMap<>();
    requestProperties.put(ID_ATTRIBUTE_NAME, SAMPLE_ID);
    ResourceResponse response = source.retrieveResource(null, requestProperties);
    assertThat(response.getResource().getByteArray().length, is(3));
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Serializable(java.io.Serializable) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) Test(org.junit.Test)

Example 88 with ResourceResponse

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

the class CatalogPolicyTest method testPostResources.

@Test
public void testPostResources() throws StopProcessingException {
    ResourceResponse resourceResponse = mock(ResourceResponse.class);
    PolicyResponse response = policyPlugin.processPostResource(resourceResponse, mock(Metacard.class));
    assertThat(response.itemPolicy().size(), equalTo(0));
    assertThat(response.operationPolicy().size(), equalTo(0));
}
Also used : Metacard(ddf.catalog.data.Metacard) ResourceResponse(ddf.catalog.operation.ResourceResponse) PolicyResponse(ddf.catalog.plugin.PolicyResponse) Test(org.junit.Test)

Example 89 with ResourceResponse

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

the class CswEndpointTest method setUpMocksForProductRetrieval.

private void setUpMocksForProductRetrieval(boolean includeMimeType) throws ResourceNotFoundException, IOException, ResourceNotSupportedException {
    ResourceResponse resourceResponse = mock(ResourceResponse.class);
    Resource resource = mock(Resource.class);
    if (includeMimeType) {
        MimeType mimeType = mock(MimeType.class);
        when(resource.getMimeType()).thenReturn(mimeType);
    }
    when(resourceResponse.getResource()).thenReturn(resource);
    when(catalogFramework.getLocalResource(any(ResourceRequest.class))).thenReturn(resourceResponse);
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Resource(ddf.catalog.resource.Resource) ResourceRequest(ddf.catalog.operation.ResourceRequest) MimeType(javax.activation.MimeType)

Example 90 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse 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
 */
@Override
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;
}
Also used : ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) MimeType(javax.activation.MimeType)

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