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