Search in sources :

Example 11 with ResourceResponseImpl

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;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl)

Example 12 with ResourceResponseImpl

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);
}
Also used : Serializable(java.io.Serializable) FilterPlugin(ddf.catalog.security.filter.plugin.FilterPlugin) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) ResultImpl(ddf.catalog.data.impl.ResultImpl) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) CollectionPermission(ddf.security.permission.CollectionPermission) Permission(org.apache.shiro.authz.Permission) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) Subject(ddf.security.Subject) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Metacard(ddf.catalog.data.Metacard) DeleteResponseImpl(ddf.catalog.operation.impl.DeleteResponseImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) ResourceRequest(ddf.catalog.operation.ResourceRequest) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) DeleteRequest(ddf.catalog.operation.DeleteRequest) Principal(java.security.Principal) Before(org.junit.Before)

Example 13 with ResourceResponseImpl

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);
}
Also used : Serializable(java.io.Serializable) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) Resource(ddf.catalog.resource.Resource) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) GetRecordByIdRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) BufferedInputStream(java.io.BufferedInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 14 with ResourceResponseImpl

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;
}
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

ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)14 Resource (ddf.catalog.resource.Resource)8 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)7 ResourceResponse (ddf.catalog.operation.ResourceResponse)6 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)5 IOException (java.io.IOException)5 BufferedInputStream (java.io.BufferedInputStream)4 InputStream (java.io.InputStream)4 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)3 CacheKey (ddf.catalog.cache.impl.CacheKey)2 Metacard (ddf.catalog.data.Metacard)2 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)2 Subject (ddf.security.Subject)2 File (java.io.File)2 URI (java.net.URI)2 MimeType (javax.activation.MimeType)2 Response (javax.ws.rs.core.Response)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 CountingOutputStream (com.google.common.io.CountingOutputStream)1