Search in sources :

Example 1 with CompositeException

use of org.n52.janmayen.exception.CompositeException in project arctic-sea by 52North.

the class KvpBinding method toOwsExceptionReport.

private OwsExceptionReport toOwsExceptionReport(Throwable ex) {
    if (ex instanceof OwsExceptionReport) {
        return (OwsExceptionReport) ex;
    }
    Throwable cause = ex.getCause();
    if (cause instanceof OwsExceptionReport) {
        return (OwsExceptionReport) cause;
    }
    if (ex instanceof CompositeException) {
        return toOwsExceptionReport((CompositeException) ex);
    }
    if (cause instanceof CompositeException) {
        return toOwsExceptionReport((CompositeException) cause);
    }
    String location = null;
    if (ex instanceof LocationHintException) {
        location = ((LocationHintException) ex).getLocation().orElse(null);
    } else if (cause instanceof LocationHintException) {
        location = ((LocationHintException) cause).getLocation().orElse(null);
    }
    return new InvalidParameterValueException().withMessage(ex.getMessage()).causedBy(ex).at(location);
}
Also used : LocationHintException(org.n52.janmayen.exception.LocationHintException) InvalidParameterValueException(org.n52.shetland.ogc.ows.exception.InvalidParameterValueException) CompositeException(org.n52.janmayen.exception.CompositeException) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport)

Example 2 with CompositeException

use of org.n52.janmayen.exception.CompositeException in project arctic-sea by 52North.

the class XmlHelper method validateDocument.

/**
 * checks whether the XMLDocument is valid
 *
 * @param doc
 *            the document which should be checked
 *
 * @throws T
 *             * if the Document is not valid
 */
/*
     * TODO Replace this version with a method that uses LaxValidationCases and
     * provides means to access the errors after validating the document
     */
public static <X extends XmlObject, T extends Throwable> X validateDocument(X doc, Function<Throwable, T> supplier) throws T {
    // Create an XmlOptions instance and set the error listener.
    LinkedList<XmlError> validationErrors = new LinkedList<>();
    XmlOptions validationOptions = new XmlOptions().setErrorListener(validationErrors).setLoadLineNumbers(XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT);
    // Create Exception with error message if the xml document is invalid
    if (!doc.validate(validationOptions)) {
        String message;
        // getValidation error and throw service exception for the first
        // error
        Iterator<XmlError> iter = validationErrors.iterator();
        List<XmlError> errors = new LinkedList<>();
        while (iter.hasNext()) {
            XmlError error = iter.next();
            boolean shouldPass = false;
            if (error instanceof XmlValidationError) {
                for (LaxValidationCase lvc : LaxValidationCase.values()) {
                    if (lvc.shouldPass((XmlValidationError) error)) {
                        shouldPass = true;
                        LOGGER.debug("Lax validation case found for XML validation error: {}", error);
                        break;
                    }
                }
            }
            if (!shouldPass) {
                errors.add(error);
            }
        }
        CompositeException exceptions = new CompositeException();
        for (XmlError error : errors) {
            // get name of the missing or invalid parameter
            message = error.getMessage();
            if (message != null) {
                exceptions.add(new DecodingException(message, "[XmlBeans validation error:] %s", message));
            }
        }
        if (!errors.isEmpty()) {
            throw supplier.apply(exceptions);
        }
    }
    return doc;
}
Also used : XmlOptions(org.apache.xmlbeans.XmlOptions) CompositeException(org.n52.janmayen.exception.CompositeException) XmlError(org.apache.xmlbeans.XmlError) DecodingException(org.n52.svalbard.decode.exception.DecodingException) LinkedList(java.util.LinkedList) XmlValidationError(org.apache.xmlbeans.XmlValidationError)

Example 3 with CompositeException

use of org.n52.janmayen.exception.CompositeException in project arctic-sea by 52North.

the class SosDecoderv20 method parseInsertObservation.

private OwsServiceRequest parseInsertObservation(final InsertObservationDocument insertObservationDoc) throws DecodingException {
    // set namespace for default XML type (e.g. xs:string, xs:integer,
    // xs:boolean, ...)
    // Fix for problem with XmlBeans: namespace is not set in child elements
    // when defined in root of request (SOAP)
    final XmlCursor cursor = insertObservationDoc.newCursor();
    if (cursor.toFirstChild() && cursor.namespaceForPrefix(W3CConstants.NS_XS_PREFIX) == null) {
        cursor.prefixForNamespace(W3CConstants.NS_XS);
    }
    cursor.dispose();
    final InsertObservationRequest insertObservationRequest = new InsertObservationRequest();
    final InsertObservationType insertObservationType = insertObservationDoc.getInsertObservation();
    insertObservationRequest.setService(insertObservationType.getService());
    insertObservationRequest.setVersion(insertObservationType.getVersion());
    if (insertObservationDoc.getInsertObservation().getOfferingArray() != null) {
        insertObservationRequest.setOfferings(Arrays.asList(insertObservationType.getOfferingArray()));
    }
    insertObservationRequest.setExtensions(parseExtensibleRequest(insertObservationType));
    if (insertObservationType.getObservationArray() != null) {
        final int length = insertObservationType.getObservationArray().length;
        final Map<String, Time> phenomenonTimes = new HashMap<>(length);
        final Map<String, TimeInstant> resultTimes = new HashMap<>(length);
        final Map<String, AbstractFeature> features = new HashMap<>(length);
        CompositeException exceptions = new CompositeException();
        for (final Observation observation : insertObservationType.getObservationArray()) {
            final Object decodedObject = decodeXmlElement(observation.getOMObservation());
            if (decodedObject instanceof OmObservation) {
                final OmObservation sosObservation = (OmObservation) decodedObject;
                checkAndAddPhenomenonTime(sosObservation.getPhenomenonTime(), phenomenonTimes);
                checkAndAddResultTime(sosObservation.getResultTime(), resultTimes);
                checkAndAddFeatures(sosObservation.getObservationConstellation().getFeatureOfInterest(), features);
                insertObservationRequest.addObservation(sosObservation);
            } else {
                exceptions.add(new DecodingException(Sos2Constants.InsertObservationParams.observation, "The requested observation type (%s) is not supported by this server!", observation.getOMObservation().getDomNode().getNodeName()));
            }
        }
        checkReferencedElements(insertObservationRequest.getObservations(), phenomenonTimes, resultTimes, features);
        try {
            exceptions.throwIfNotEmpty();
        } catch (CompositeException ex) {
            throw new DecodingException(ex, Sos2Constants.InsertObservationParams.observation);
        }
    } else {
        // TODO MissingMandatoryParameterException?
        throw new DecodingException(Sos2Constants.InsertObservationParams.observation, "The request does not contain an observation");
    }
    return insertObservationRequest;
}
Also used : HashMap(java.util.HashMap) CompositeException(org.n52.janmayen.exception.CompositeException) OmObservation(org.n52.shetland.ogc.om.OmObservation) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature) Time(org.n52.shetland.ogc.gml.time.Time) DecodingException(org.n52.svalbard.decode.exception.DecodingException) XmlString(org.apache.xmlbeans.XmlString) InsertObservationType(net.opengis.sos.x20.InsertObservationType) XmlCursor(org.apache.xmlbeans.XmlCursor) InsertObservationRequest(org.n52.shetland.ogc.sos.request.InsertObservationRequest) OmObservation(org.n52.shetland.ogc.om.OmObservation) Observation(net.opengis.sos.x20.InsertObservationType.Observation) OwsServiceCommunicationObject(org.n52.shetland.ogc.ows.service.OwsServiceCommunicationObject) XmlObject(org.apache.xmlbeans.XmlObject) TimeInstant(org.n52.shetland.ogc.gml.time.TimeInstant)

Example 4 with CompositeException

use of org.n52.janmayen.exception.CompositeException in project arctic-sea by 52North.

the class JSONDecoder method decodeJsonToObjectList.

protected <T> List<T> decodeJsonToObjectList(JsonNode node, Class<T> type) throws DecodingException {
    Decoder<T, JsonNode> decoder = getDecoder(type);
    if (node.isArray()) {
        CompositeException exceptions = new CompositeException();
        List<T> list = Streams.stream(node).filter(JsonNode::isObject).map(exceptions.wrapFunction(decoder::decode)).filter(Optional::isPresent).map(Optional::get).collect(toList());
        exceptions.throwIfNotEmpty(DecodingException::new);
        return list;
    } else if (node.isObject()) {
        return Collections.singletonList(decoder.decode(node));
    } else {
        return Collections.emptyList();
    }
}
Also used : Optional(java.util.Optional) CompositeException(org.n52.janmayen.exception.CompositeException) JsonNode(com.fasterxml.jackson.databind.JsonNode) DecodingException(org.n52.svalbard.decode.exception.DecodingException)

Example 5 with CompositeException

use of org.n52.janmayen.exception.CompositeException in project arctic-sea by 52North.

the class JSONEncoder method encodeListChecked.

protected <T, X extends Exception> void encodeListChecked(ObjectNode json, String name, Collection<T> collection, ThrowingFunction<T, JsonNode, X> encoder) throws CompositeException {
    if (!collection.isEmpty()) {
        CompositeException exceptions = new CompositeException();
        json.set(name, collection.stream().map(exceptions.wrapFunction(encoder)).map(o -> o.orElseGet(nodeFactory()::nullNode)).collect(toJsonArray()));
        if (!exceptions.isEmpty()) {
            throw exceptions;
        }
    }
}
Also used : CompositeException(org.n52.janmayen.exception.CompositeException)

Aggregations

CompositeException (org.n52.janmayen.exception.CompositeException)6 DecodingException (org.n52.svalbard.decode.exception.DecodingException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Optional (java.util.Optional)1 InsertObservationType (net.opengis.sos.x20.InsertObservationType)1 Observation (net.opengis.sos.x20.InsertObservationType.Observation)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlError (org.apache.xmlbeans.XmlError)1 XmlObject (org.apache.xmlbeans.XmlObject)1 XmlOptions (org.apache.xmlbeans.XmlOptions)1 XmlString (org.apache.xmlbeans.XmlString)1 XmlValidationError (org.apache.xmlbeans.XmlValidationError)1 LocationHintException (org.n52.janmayen.exception.LocationHintException)1 AbstractFeature (org.n52.shetland.ogc.gml.AbstractFeature)1 Time (org.n52.shetland.ogc.gml.time.Time)1 TimeInstant (org.n52.shetland.ogc.gml.time.TimeInstant)1 OmObservation (org.n52.shetland.ogc.om.OmObservation)1 InvalidParameterValueException (org.n52.shetland.ogc.ows.exception.InvalidParameterValueException)1