use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class DocumentSerialization method consumeHeader.
public static InputStream consumeHeader(byte[] data) throws InvalidDocumentHeader {
if (null == data || 3 > data.length) {
QueryException qe = new QueryException(DatawaveErrorCode.DATA_INVALID_ERROR, MessageFormat.format("Length: {0}", (null != data ? data.length : null)));
throw new InvalidDocumentHeader(qe);
}
ByteArrayInputStream bais = new ByteArrayInputStream(data);
int magic = readUShort(bais);
if (DOC_MAGIC != magic) {
NotFoundQueryException qe = new NotFoundQueryException(DatawaveErrorCode.EXPECTED_HEADER_NOT_FOUND);
throw new InvalidDocumentHeader(qe);
}
int compression = readUByte(bais);
if (NONE == compression) {
return new ByteArrayInputStream(data, 3, data.length - 3);
} else if (GZIP == compression) {
ByteArrayInputStream bytes = new ByteArrayInputStream(data, 3, data.length - 3);
return new InflaterInputStream(bytes, new Inflater(), 1024);
} else {
BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.UNKNOWN_COMPRESSION_SCHEME, MessageFormat.format("{0}", compression));
throw new InvalidDocumentHeader(qe);
}
}
use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class KeyToDocumentData method apply.
@Override
public Entry<DocumentData, Document> apply(Entry<Key, Document> from) {
// We want to ensure that we have a non-empty colqual
if (null == from || null == from.getKey() || null == from.getValue())
return null;
Range keyRange = getKeyRange(from);
try {
source.seek(keyRange, columnFamilies, inclusive);
if (log.isDebugEnabled())
log.debug(source.hasTop() + " Key range is " + keyRange);
// Assign only once for
final List<Entry<Key, Value>> attrs;
// efficiency
final Set<Key> docKeys = new HashSet<>();
if (source.hasTop()) {
attrs = this.collectDocumentAttributes(from.getKey(), docKeys, keyRange);
this.appendHierarchyFields(attrs, keyRange, from.getKey());
} else {
attrs = Collections.emptyList();
}
return Maps.immutableEntry(new DocumentData(from.getKey(), docKeys, attrs, false), from.getValue());
} catch (IOException e) {
log.error("Unable to collection document attributes for evaluation: " + keyRange, e);
QueryException qe = new QueryException(DatawaveErrorCode.DOCUMENT_EVALUATION_ERROR, e);
throw new DatawaveFatalQueryException(qe);
}
}
use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class IndexOnlyKeyToDocumentData method next.
@Override
public Entry<DocumentData, Document> next() {
final Entry<Key, Value> next;
try {
next = this.seekNext(false);
} catch (IOException e) {
QueryException qe = new QueryException(DatawaveErrorCode.SEEK_NEXT_ELEMENT_ERROR, e);
throw new DatawaveFatalQueryException(qe);
}
final Entry<DocumentData, Document> entry;
if (null != next) {
final List<Entry<Key, Value>> keyValues = new LinkedList<>();
keyValues.add(next);
Key docKey = getDocKey(next.getKey());
final DocumentData documentData = new DocumentData(this.iteratorDocumentKey, Collections.singleton(docKey), keyValues, true);
entry = Maps.immutableEntry(documentData, this.iteratorDocument);
} else if (next == ITERATOR_COMPLETE_KEY) {
QueryException qe = new QueryException(DatawaveErrorCode.FETCH_NEXT_ELEMENT_ERROR, MessageFormat.format("Fieldname: {0}, Range: {1}", this.fieldName, this.parent));
throw (NoSuchElementException) (new NoSuchElementException().initCause(qe));
} else {
entry = null;
}
return entry;
}
use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class MutableMetadataUUIDHandler method process.
@Override
public void process(Connector con, ModificationRequestBase request, Map<String, Set<String>> mutableFieldList, Set<Authorizations> userAuths, String user) throws BadRequestException, AccumuloException, AccumuloSecurityException, TableNotFoundException, ExecutionException {
VoidResponse response = new VoidResponse();
ArrayList<Exception> exceptions = new ArrayList<>();
MetadataHelper mHelper = getMetadataHelper(con);
// Receive DefaultUUIDModificationRequest
DefaultUUIDModificationRequest uuidModReq = DefaultUUIDModificationRequest.class.cast(request);
List<ModificationEvent> events = uuidModReq.getEvents();
for (ModificationEvent event : events) {
List<ModificationOperationImpl> operations = event.getOperations();
for (ModificationOperationImpl operation : operations) {
ResetValues();
OPERATIONMODE mode = operation.getOperationMode();
String columnVisibility = operation.getColumnVisibility();
String oldColumnVisibility = operation.getOldColumnVisibility();
String eventUser = event.getUser();
// check whether this is a security-marking exempt field. Meaning we can pull the marking if not specified
boolean securityMarkingExempt = false;
fieldName = operation.getFieldName();
for (String s : this.getSecurityMarkingExemptFields()) {
if (fieldName.toUpperCase().equals(s)) {
securityMarkingExempt = true;
}
}
// if they are updating, assume the old values should be the same as current
if (OPERATIONMODE.UPDATE.equals(mode) && oldColumnVisibility == null) {
oldColumnVisibility = columnVisibility;
}
try {
if (mHelper.getIndexOnlyFields(mutableFieldList.keySet()).contains(event.getIdType().toUpperCase())) {
throw new IllegalStateException("Cannot perform modification because " + event.getIdType() + " is index only. Please search " + "with a different uuidType to identify the event you wish to modify.");
}
// perform the lookupUUID
EventBase<?, ? extends FieldBase<?>> idEvent = findMatchingEventUuid(event.getId(), event.getIdType(), userAuths, operation);
// extract contents from lookupUUID necessary for modification
List<? extends FieldBase<?>> fields = idEvent.getFields();
if (operation.getOldFieldValue() != null)
oldFieldValue = operation.getOldFieldValue();
if (fields != null) {
// there may be multiple values for a single field
for (FieldBase<?> f : fields) {
if (f.getName().equals(fieldName)) {
fieldCount++;
// if they are doing a replace, we need all the current values, store them
if (operation.getOperationMode().equals(OPERATIONMODE.REPLACE)) {
if (log != null)
log.trace("Adding " + f.getValueString() + ",delete to replaceMap");
replaceMap.put(f.getValueString(), OPERATIONMODE.DELETE);
}
// user sent an oldValue and we found that value or no oldValue
if ((oldFieldValue != null && f.getValueString().equals(oldFieldValue)) || oldFieldValue == null) {
fieldValue = f.getValueString();
if (columnVisibility == null && securityMarkingExempt) {
fieldColumnVisibility = f.getColumnVisibility();
}
}
} else // only if the input didn't supply a security marking AND it is an exempt field
if (f.getName().equalsIgnoreCase(event.getIdType()) && fieldCount < 1 && columnVisibility == null && securityMarkingExempt) {
if (log != null)
log.trace("Using visibility of " + f.getName() + " and setting to " + f.getColumnVisibility());
fieldColumnVisibility = f.getColumnVisibility();
}
}
List<DefaultModificationRequest> modificationRequests = new ArrayList<>();
if (OPERATIONMODE.INSERT.equals(mode) || OPERATIONMODE.UPDATE.equals(mode) || OPERATIONMODE.DELETE.equals(mode)) {
modificationRequests.add(createModificationRequest(idEvent, operation, columnVisibility, oldColumnVisibility, securityMarkingExempt));
} else if (OPERATIONMODE.REPLACE.equals(mode)) {
if (log != null)
log.trace("Adding " + operation.getFieldValue() + ",insert to replaceMap");
replaceMap.put(operation.getFieldValue(), OPERATIONMODE.INSERT);
// create a modification request of delete for each current value and an insert for the new value
for (String s : replaceMap.keySet()) {
ModificationOperation replaceOperation = operation.clone();
replaceOperation.setOperationMode(replaceMap.get(s));
replaceOperation.setFieldValue(s);
oldFieldValue = s;
modificationRequests.add(createModificationRequest(idEvent, replaceOperation, columnVisibility, oldColumnVisibility, securityMarkingExempt));
}
}
if (log != null)
log.trace("modificationRequests= " + modificationRequests);
for (DefaultModificationRequest modReq : modificationRequests) {
try {
if (fieldCount > 1 && (oldFieldValue == null && modReq.getMode() != MODE.INSERT) && !mode.equals(OPERATIONMODE.REPLACE)) {
throw new IllegalStateException("Unable to perform modification. More than one value exists for " + modReq.getFieldName() + ". Please specify the current value you wish to modify in the oldFieldValue field.");
} else if (fieldCount < 1 && modReq.getMode() != MODE.INSERT) {
throw new IllegalStateException("Unable to perform modification. No values exist for " + modReq.getFieldName() + ".");
} else if (columnVisibility == null && !securityMarkingExempt) {
throw new IllegalStateException("Must provide columnVisibility");
} else // submit DefaultModificationRequest
{
log.info("eventUser = " + eventUser + ", event.getUser() = " + event.getUser());
if (log != null)
log.trace("Submitting request to MutableMetadataHandler from MutableMetadataUUIDHandler: " + modReq);
// make sure user isn't null or empty
if (eventUser == null || eventUser.equals("")) {
if (log != null)
log.trace("No user provided for event. Using caller: " + user);
super.process(con, modReq, mutableFieldList, userAuths, user);
} else {
super.process(con, modReq, mutableFieldList, userAuths, event.getUser());
}
}
}// log exceptions that occur for each modification request. Let as many requests work as possible before returning
catch (Exception e) {
if (log != null)
log.error("Modification error", e);
exceptions.add(new Exception(event.getId() + ": " + e.getMessage() + "\n" + modReq));
}
}
modificationRequests.clear();
} else {
throw new IllegalStateException("No event matched " + event.getId());
}
} catch (Exception e) {
if (log != null)
log.error("Modification error", e);
exceptions.add(new Exception(event.getId() + ": " + e.getMessage() + "\n"));
}
}
}
// If any errors occurred, return them in the response to the user
if (!exceptions.isEmpty()) {
for (Exception e : exceptions) {
QueryException qe = new QueryException(DatawaveErrorCode.MODIFICATION_ERROR, e);
response.addException(qe.getBottomQueryException());
}
QueryException e = new QueryException(DatawaveErrorCode.MODIFICATION_ERROR);
throw new BadRequestException(e, response);
}
}
use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class FileSortedSet method last.
@Override
public E last() {
boolean gotLast = false;
E last = null;
if (persisted) {
try (SortedSetInputStream<E> stream = getBoundedFileHandler().getInputStream(getStart(), getEnd())) {
last = stream.readObject();
E next = stream.readObject();
while (next != null) {
last = next;
next = stream.readObject();
}
gotLast = true;
} catch (Exception e) {
throw new IllegalStateException("Unable to get last from file", e);
}
} else if (!set.isEmpty()) {
last = set.last();
gotLast = true;
}
if (!gotLast) {
QueryException qe = new QueryException(DatawaveErrorCode.FETCH_LAST_ELEMENT_ERROR);
throw (NoSuchElementException) (new NoSuchElementException().initCause(qe));
} else {
return last;
}
}
Aggregations