use of datawave.security.authorization.DatawavePrincipal in project datawave by NationalSecurityAgency.
the class AtomServiceBean method getFeed.
/**
* @param category
* collection name
* @param lastKey
* last key returned, page will begin with the next key
* @param pagesize
* size of the page
* @return Atom Feed document for a collection
*/
@GET
@GZIP
@Produces("application/atom+xml")
@Path("/{category}")
public Feed getFeed(@Required("category") @PathParam("category") String category, @QueryParam("l") String lastKey, @QueryParam("pagesize") @DefaultValue("30") int pagesize) {
// Feed must contain
// one atom:author
// exactly one atom:id
// SHOULD contain one atom:link element with a rel
// attribute value of "self". This is the preferred URI for
// retrieving Atom Feed Documents representing this Atom feed.
// contain exactly one atom:title element.
// contain exactly one atom:updated element.
Principal p = ctx.getCallerPrincipal();
Set<Authorizations> auths = new HashSet<>();
if (p instanceof DatawavePrincipal) {
DatawavePrincipal dp = (DatawavePrincipal) p;
for (Collection<String> cbAuths : dp.getAuthorizations()) auths.add(new Authorizations(cbAuths.toArray(new String[cbAuths.size()])));
}
Feed result;
Connector connection = null;
Date maxDate = new Date(0);
try {
Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
connection = connectionFactory.getConnection(poolName, Priority.NORMAL, trackingMap);
result = abdera.newFeed();
result.addAuthor(clustername);
result.setTitle(category);
Key nextLastKey = null;
int count = 0;
try (Scanner scanner = ScannerHelper.createScanner(connection, tableName, auths)) {
if (null != lastKey) {
Key lastSeenKey = deserializeKey(lastKey);
scanner.setRange(new Range(lastSeenKey, false, new Key(category + "\1"), false));
} else {
scanner.setRange(new Range(category, true, category + "\1", false));
}
for (Map.Entry<Key, Value> entry : scanner) {
AtomKeyValueParser atom = AtomKeyValueParser.parse(entry.getKey(), entry.getValue());
if (atom.getUpdated().after(maxDate)) {
maxDate = atom.getUpdated();
}
nextLastKey = entry.getKey();
Entry e = atom.toEntry(abdera, this.host, this.port);
result.addEntry(e);
count++;
if (count >= pagesize)
break;
}
}
String thisLastKey = "";
if (null != nextLastKey)
thisLastKey = serializeKey(nextLastKey);
String id = MessageFormat.format(COLLECTION_LINK_FORMAT, this.host, this.port, category);
result.setId(id);
// need a link that contains the offset of null and current pagesize
result.addLink(id + "?pagesize=" + pagesize, "first");
// need a link that contains the next offset and current pagesize
result.addLink(id + "?pagesize=" + pagesize + "&l=" + thisLastKey, "next");
result.setUpdated(maxDate);
if (count == 0)
throw new NoResultsException(null);
else
return result;
} catch (WebApplicationException web) {
throw web;
} catch (Exception e) {
VoidResponse response = new VoidResponse();
QueryException qe = new QueryException(DatawaveErrorCode.FEED_GET_ERROR, e, MessageFormat.format("collection: {0}", category));
log.error(qe);
response.addException(qe.getBottomQueryException());
throw new DatawaveWebApplicationException(qe, response);
} finally {
if (null != connection) {
try {
connectionFactory.returnConnection(connection);
} catch (Exception e) {
log.error("Error returning connection to factory", e);
}
}
}
}
use of datawave.security.authorization.DatawavePrincipal in project datawave by NationalSecurityAgency.
the class TestCardinalityWithQuery method setup.
@Before
public void setup() throws Exception {
System.setProperty(NpeUtils.NPE_OU_PROPERTY, "iamnotaperson");
temporaryFolder = tempDir.newFolder().toPath();
logic = new ShardQueryLogic();
logic.setMarkingFunctions(new MarkingFunctions.Default());
logic.setResponseObjectFactory(new DefaultResponseObjectFactory());
logic.setMetadataHelperFactory(new MetadataHelperFactory());
logic.setDateIndexHelperFactory(new DateIndexHelperFactory());
QueryTestTableHelper.configureLogicToScanTables(logic);
loadData();
SubjectIssuerDNPair dn = SubjectIssuerDNPair.of("userDn", "issuerDn");
DatawaveUser user = new DatawaveUser(dn, UserType.USER, Sets.newHashSet(auths.toString().split(",")), null, null, -1L);
datawavePrincipal = new DatawavePrincipal(Collections.singleton(user));
}
use of datawave.security.authorization.DatawavePrincipal in project datawave by NationalSecurityAgency.
the class AuthorizationsUtil method buildUserAuthorizationString.
public static String buildUserAuthorizationString(Principal principal) {
String auths = "";
if (principal != null && (principal instanceof DatawavePrincipal)) {
DatawavePrincipal datawavePrincipal = (DatawavePrincipal) principal;
auths = new Authorizations(datawavePrincipal.getPrimaryUser().getAuths().toArray(new String[0])).toString();
}
return auths;
}
Aggregations