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;
}
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;
}
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;
}
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();
}
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());
}
Aggregations