Search in sources :

Example 1 with QueryException

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);
    }
}
Also used : InvalidDocumentHeader(datawave.query.exceptions.InvalidDocumentHeader) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) ByteArrayInputStream(java.io.ByteArrayInputStream) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException)

Example 2 with QueryException

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);
    }
}
Also used : DocumentData(datawave.query.iterator.aggregation.DocumentData) Entry(java.util.Map.Entry) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) QueryException(datawave.webservice.query.exception.QueryException) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) IOException(java.io.IOException) Range(org.apache.accumulo.core.data.Range) Key(org.apache.accumulo.core.data.Key) PartialKey(org.apache.accumulo.core.data.PartialKey) HashSet(java.util.HashSet)

Example 3 with QueryException

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;
}
Also used : IOException(java.io.IOException) Document(datawave.query.attributes.Document) LinkedList(java.util.LinkedList) DocumentData(datawave.query.iterator.aggregation.DocumentData) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) QueryException(datawave.webservice.query.exception.QueryException) Entry(java.util.Map.Entry) Value(org.apache.accumulo.core.data.Value) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) Key(org.apache.accumulo.core.data.Key) PartialKey(org.apache.accumulo.core.data.PartialKey) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with QueryException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) BadRequestException(datawave.webservice.common.exception.BadRequestException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) QueryException(datawave.webservice.query.exception.QueryException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) OPERATIONMODE(datawave.webservice.modification.ModificationOperation.OPERATIONMODE) MetadataHelper(datawave.query.util.MetadataHelper) QueryException(datawave.webservice.query.exception.QueryException) VoidResponse(datawave.webservice.result.VoidResponse) BadRequestException(datawave.webservice.common.exception.BadRequestException)

Example 5 with QueryException

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;
    }
}
Also used : QueryException(datawave.webservice.query.exception.QueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) NoSuchElementException(java.util.NoSuchElementException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

QueryException (datawave.webservice.query.exception.QueryException)131 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)63 IOException (java.io.IOException)62 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)57 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)51 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)47 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)45 Produces (javax.ws.rs.Produces)44 NoResultsException (datawave.webservice.common.exception.NoResultsException)40 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)39 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)36 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)36 Interceptors (javax.interceptor.Interceptors)36 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)34 GZIP (org.jboss.resteasy.annotations.GZIP)34 Principal (java.security.Principal)32 WebApplicationException (javax.ws.rs.WebApplicationException)31 BadRequestException (datawave.webservice.common.exception.BadRequestException)29 Path (javax.ws.rs.Path)28 Timed (com.codahale.metrics.annotation.Timed)26