Search in sources :

Example 31 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class FilterPlugin method processPostQuery.

@Override
public QueryResponse processPostQuery(QueryResponse input) throws StopProcessingException {
    if (input.getRequest() == null || input.getRequest().getProperties() == null) {
        throw new StopProcessingException("Unable to filter contents of current message, no user Subject available.");
    }
    Subject subject = getSubject(input);
    List<Result> results = input.getResults();
    List<Result> newResults = new ArrayList<>(results.size());
    Metacard metacard;
    KeyValueCollectionPermission securityPermission = new KeyValueCollectionPermission(CollectionPermission.READ_ACTION);
    int filteredMetacards = 0;
    for (Result result : results) {
        metacard = result.getMetacard();
        Attribute attr = metacard.getAttribute(Metacard.SECURITY);
        if (!checkPermissions(attr, securityPermission, subject, CollectionPermission.READ_ACTION)) {
            for (FilterStrategy filterStrategy : filterStrategies.values()) {
                FilterResult filterResult = filterStrategy.process(input, metacard);
                if (filterResult.processed()) {
                    if (filterResult.metacard() != null) {
                        newResults.add(new ResultImpl(filterResult.metacard()));
                    }
                    break;
                //returned responses are ignored for queries
                }
            }
            filteredMetacards++;
        } else {
            newResults.add(result);
        }
    }
    if (filteredMetacards > 0) {
        SecurityLogger.audit("Filtered " + filteredMetacards + " metacards, returned " + newResults.size(), subject);
    }
    input.getResults().clear();
    input.getResults().addAll(newResults);
    newResults.clear();
    return input;
}
Also used : Metacard(ddf.catalog.data.Metacard) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Attribute(ddf.catalog.data.Attribute) ArrayList(java.util.ArrayList) FilterStrategy(ddf.catalog.security.FilterStrategy) ResultImpl(ddf.catalog.data.impl.ResultImpl) StopProcessingException(ddf.catalog.plugin.StopProcessingException) FilterResult(ddf.catalog.security.FilterResult) Subject(org.apache.shiro.subject.Subject) FilterResult(ddf.catalog.security.FilterResult) Result(ddf.catalog.data.Result)

Example 32 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class FilterPlugin method processPostDelete.

@Override
public DeleteResponse processPostDelete(DeleteResponse input) throws StopProcessingException {
    if (input.getRequest() == null || input.getRequest().getProperties() == null) {
        throw new StopProcessingException("Unable to filter contents of current message, no user Subject available.");
    }
    Subject subject = getSubject(input);
    List<Metacard> results = input.getDeletedMetacards();
    List<Metacard> newResults = new ArrayList<>(results.size());
    KeyValueCollectionPermission securityPermission = new KeyValueCollectionPermission(CollectionPermission.READ_ACTION);
    int filteredMetacards = 0;
    for (Metacard metacard : results) {
        Attribute attr = metacard.getAttribute(Metacard.SECURITY);
        if (!checkPermissions(attr, securityPermission, subject, CollectionPermission.READ_ACTION)) {
            for (FilterStrategy filterStrategy : filterStrategies.values()) {
                FilterResult filterResult = filterStrategy.process(input, metacard);
                if (filterResult.processed()) {
                    if (filterResult.metacard() != null) {
                        newResults.add(filterResult.metacard());
                    }
                    break;
                //returned responses are ignored for deletes
                }
            }
            filteredMetacards++;
        } else {
            newResults.add(metacard);
        }
    }
    if (filteredMetacards > 0) {
        SecurityLogger.audit("Filtered " + filteredMetacards + " metacards, returned " + newResults.size(), subject);
    }
    input.getDeletedMetacards().clear();
    input.getDeletedMetacards().addAll(newResults);
    newResults.clear();
    return input;
}
Also used : Metacard(ddf.catalog.data.Metacard) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Attribute(ddf.catalog.data.Attribute) ArrayList(java.util.ArrayList) FilterStrategy(ddf.catalog.security.FilterStrategy) StopProcessingException(ddf.catalog.plugin.StopProcessingException) FilterResult(ddf.catalog.security.FilterResult) Subject(org.apache.shiro.subject.Subject)

Example 33 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class FilterPlugin method processPreUpdate.

@Override
public UpdateRequest processPreUpdate(UpdateRequest input, Map<String, Metacard> metacards) throws StopProcessingException {
    KeyValueCollectionPermission securityPermission = new KeyValueCollectionPermission(CollectionPermission.UPDATE_ACTION);
    List<Map.Entry<Serializable, Metacard>> updates = input.getUpdates();
    Subject subject = getSubject(input);
    Subject systemSubject = getSystemSubject();
    List<String> unknownIds = new ArrayList<>();
    List<String> userNotPermittedIds = new ArrayList<>();
    List<String> systemNotPermittedIds = new ArrayList<>();
    for (Map.Entry<Serializable, Metacard> entry : updates) {
        Metacard newMetacard = entry.getValue();
        Attribute attr = newMetacard.getAttribute(Metacard.SECURITY);
        String id = null;
        if (entry.getKey() != null && !entry.getKey().equals("null")) {
            id = (String) entry.getKey();
        } else if (newMetacard.getId() != null && !newMetacard.getId().equals("null")) {
            id = newMetacard.getId();
        }
        Metacard oldMetacard = metacards.get(id);
        if (oldMetacard == null) {
            unknownIds.add(id);
        } else {
            Attribute oldAttr = oldMetacard.getAttribute(Metacard.SECURITY);
            if (!checkPermissions(attr, securityPermission, subject, CollectionPermission.UPDATE_ACTION) || !checkPermissions(oldAttr, securityPermission, subject, CollectionPermission.UPDATE_ACTION)) {
                userNotPermittedIds.add(newMetacard.getId());
            }
            if (!checkPermissions(attr, securityPermission, systemSubject, CollectionPermission.UPDATE_ACTION)) {
                systemNotPermittedIds.add(newMetacard.getId());
            }
        }
    }
    if (!unknownIds.isEmpty() || !userNotPermittedIds.isEmpty()) {
        throw new StopProcessingException("Update operation not permitted with bad data. Unknown metacards: [ " + listToString(unknownIds) + " ]. Not Permitted metacards: [ " + listToString(userNotPermittedIds) + " ]");
    }
    if (!systemNotPermittedIds.isEmpty()) {
        throw new StopProcessingException("Update operation not permitted for this system metacards: [ " + listToString(systemNotPermittedIds) + " ]");
    }
    return input;
}
Also used : KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Serializable(java.io.Serializable) Attribute(ddf.catalog.data.Attribute) ArrayList(java.util.ArrayList) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Subject(org.apache.shiro.subject.Subject) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 34 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class MetacardMarshallerImpl method marshal.

@Override
public String marshal(Metacard metacard, Map<String, Serializable> arguments) throws XmlPullParserException, IOException, CatalogTransformerException {
    PrintWriter writer = this.writerProvider.build(Metacard.class);
    if (arguments != null && arguments.get(OMIT_XML_DECL) != null) {
        Boolean omitXmlDec = Boolean.valueOf(String.valueOf(arguments.get(OMIT_XML_DECL)));
        if (omitXmlDec == null || !omitXmlDec) {
            writer.setRawValue(XML_DECL);
        }
    }
    writer.startNode("metacard");
    for (Map.Entry<String, String> nsRow : NAMESPACE_MAP.entrySet()) {
        writer.addAttribute(nsRow.getKey(), nsRow.getValue());
    }
    if (metacard.getId() != null) {
        writer.addAttribute(GML_PREFIX + ":id", metacard.getId());
    }
    writer.startNode("type");
    if (StringUtils.isBlank(metacard.getMetacardType().getName())) {
        writer.setValue(MetacardType.DEFAULT_METACARD_TYPE_NAME);
    } else {
        writer.setValue(metacard.getMetacardType().getName());
    }
    // type
    writer.endNode();
    if (StringUtils.isNotBlank(metacard.getSourceId())) {
        writer.startNode("source");
        writer.setValue(metacard.getSourceId());
        // source
        writer.endNode();
    }
    // if multi-threading, cannot abstract XmlPullParser creation to class member.
    // xmlPullParser used only for geometry
    XmlPullParser xmlPullParser = XppFactory.createDefaultParser();
    Set<AttributeDescriptor> attributeDescriptors = metacard.getMetacardType().getAttributeDescriptors();
    for (AttributeDescriptor attributeDescriptor : attributeDescriptors) {
        String attributeName = attributeDescriptor.getName();
        if (attributeName.equals("id")) {
            continue;
        }
        Attribute attribute = metacard.getAttribute(attributeName);
        if (attribute != null && attribute.getValue() != null) {
            AttributeType.AttributeFormat format = attributeDescriptor.getType().getAttributeFormat();
            writeAttributeToXml(writer, xmlPullParser, attribute, format);
        }
    }
    // metacard
    writer.endNode();
    return writer.makeString();
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeType(ddf.catalog.data.AttributeType) XmlPullParser(org.xmlpull.v1.XmlPullParser) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) PrintWriter(ddf.catalog.transformer.api.PrintWriter)

Example 35 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class TestXmlInputTransformer method testSimpleMetadata.

@Test
public void testSimpleMetadata() throws IOException, CatalogTransformerException, ParseException {
    Metacard metacard = xit.transform(new FileInputStream("src/test/resources/metacard1.xml"));
    LOGGER.info("Attributes: ");
    for (AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
        Attribute attribute = metacard.getAttribute(descriptor.getName());
        LOGGER.info("\t" + descriptor.getName() + ": " + ((attribute == null) ? attribute : attribute.getValue()));
    }
    LOGGER.info("ID: {}", metacard.getId());
    LOGGER.info("Type: {}", metacard.getMetacardType().getName());
    LOGGER.info("Source: {}", metacard.getSourceId());
    assertEquals("1234567890987654321", metacard.getId());
    assertEquals("ddf.metacard", metacard.getMetacardType().getName());
    assertEquals("foobar", metacard.getSourceId());
    // TODO use JTS to check for equality, not string comparison.
    assertEquals("POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10), (20 30, 35 35, 30 20, 20 30))", metacard.getAttribute(Metacard.GEOGRAPHY).getValue());
    assertEquals("Title!", metacard.getAttribute(Metacard.TITLE).getValue());
    assertArrayEquals(Base64.getDecoder().decode("AAABAAABAQEAAQAAAQEBAAEAAAEBAQABAAABAQEAAQAAAQEBAAEAAAEBAQABAAABAQE="), (byte[]) metacard.getAttribute(Metacard.THUMBNAIL).getValue());
    // TODO use XMLUnit to test equivalence
    assertThat(metacard.getAttribute(Metacard.METADATA).getValue().toString(), startsWith("<foo xmlns=\"http://foo.com\">"));
    assertEquals((new SimpleDateFormat("MMM d, yyyy HH:mm:ss.SSS z")).parse("Dec 27, 2012 16:31:01.641 MST"), metacard.getAttribute(Metacard.EXPIRATION).getValue());
    assertEquals(DESCRIPTION, metacard.getAttribute("description").getValue());
    assertEquals(POINT_OF_CONTACT, metacard.getAttribute("point-of-contact").getValue());
}
Also used : Metacard(ddf.catalog.data.Metacard) Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) SimpleDateFormat(java.text.SimpleDateFormat) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

Attribute (ddf.catalog.data.Attribute)103 Metacard (ddf.catalog.data.Metacard)39 Test (org.junit.Test)37 ArrayList (java.util.ArrayList)30 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)29 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)26 Serializable (java.io.Serializable)23 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)15 HashMap (java.util.HashMap)15 Result (ddf.catalog.data.Result)14 List (java.util.List)14 MetacardType (ddf.catalog.data.MetacardType)11 QueryResponse (ddf.catalog.operation.QueryResponse)11 Date (java.util.Date)11 Map (java.util.Map)11 HashSet (java.util.HashSet)10 Optional (java.util.Optional)8 Set (java.util.Set)8 Filter (org.opengis.filter.Filter)8 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)7