use of org.n52.svalbard.decode.exception.DecodingException in project arctic-sea by 52North.
the class JSONBinding method parseRequest.
private OwsServiceRequest parseRequest(HttpServletRequest request) throws OwsExceptionReport {
try {
JsonNode json = Json.loadReader(request.getReader());
if (LOG.isDebugEnabled()) {
LOG.debug("JSON-REQUEST: {}", Json.print(json));
}
OperationDecoderKey key = new OperationDecoderKey(json.path(SERVICE).textValue(), json.path(VERSION).textValue(), json.path(REQUEST).textValue(), MediaTypes.APPLICATION_JSON);
Decoder<OwsServiceRequest, JsonNode> decoder = getDecoder(key);
if (decoder == null) {
NoDecoderForKeyException cause = new NoDecoderForKeyException(key);
throw new NoApplicableCodeException().withMessage(cause.getMessage()).causedBy(cause);
}
OwsServiceRequest sosRequest;
try {
sosRequest = decoder.decode(json);
} catch (OwsDecodingException ex) {
throw ex.getCause();
} catch (DecodingException ex) {
throw new NoApplicableCodeException().withMessage(ex.getMessage()).causedBy(ex);
}
sosRequest.setRequestContext(getRequestContext(request));
return sosRequest;
} catch (IOException ioe) {
throw new NoApplicableCodeException().causedBy(ioe).withMessage("Error while reading request! Message: %s", ioe.getMessage());
}
}
use of org.n52.svalbard.decode.exception.DecodingException in project arctic-sea by 52North.
the class KvpBinding method parseRequest.
protected OwsServiceRequest parseRequest(HttpServletRequest req) throws OwsExceptionReport {
if (req.getParameterMap() == null || req.getParameterMap().isEmpty()) {
throw new MissingRequestParameterException();
}
Map<String, String> parameters = Streams.stream(req.getParameterNames()).collect(toMap(name -> name.replace("amp;", "").toLowerCase(Locale.ROOT), req::getParameter));
String service = parameters.get(RequestParams.service.name());
String version = parameters.get(RequestParams.version.name());
String operation = parameters.get(RequestParams.request.name());
if (Strings.isNullOrEmpty(service)) {
throw new MissingServiceParameterException();
}
if (!isServiceSupported(service)) {
throw new InvalidServiceParameterException(service);
}
if (Strings.isNullOrEmpty(operation)) {
throw new MissingRequestParameterException();
}
if (version != null && !isVersionSupported(service, version)) {
throw new VersionNotSupportedException();
}
Decoder<OwsServiceRequest, Map<String, String>> decoder = getDecoder(new OperationDecoderKey(service, version, operation, MediaTypes.APPLICATION_KVP));
if (decoder == null) {
throw new OperationNotSupportedException(operation);
}
OwsServiceRequest request;
try {
request = decoder.decode(parameters);
} catch (OwsDecodingException ex) {
throw ex.getCause();
} catch (DecodingException ex) {
throw toOwsExceptionReport(ex);
}
if (this.includeOriginalRequest) {
request.setOriginalRequest(String.join("?", req.getRequestURL(), req.getQueryString()));
}
return request;
}
use of org.n52.svalbard.decode.exception.DecodingException in project arctic-sea by 52North.
the class AbstractXmlBinding method decode.
protected T decode(HttpServletRequest request) throws OwsExceptionReport {
String characterEncoding = getCharacterEncoding(request);
String xmlString = xmlToString(request, characterEncoding);
LOGGER.debug("XML-REQUEST: {}", xmlString);
DecoderKey key = getDecoderKey(xmlString, characterEncoding);
LOGGER.trace("Found decoder key: {}", key);
Decoder<T, String> decoder = getDecoder(key);
if (decoder == null) {
// if this a GetCapabilities request, then the service is not supported
String opOrType = null;
Optional<String> service = Optional.empty();
if (key instanceof XmlNamespaceOperationDecoderKey) {
XmlNamespaceOperationDecoderKey xmlNamespaceKey = (XmlNamespaceOperationDecoderKey) key;
opOrType = xmlNamespaceKey.getType();
} else if (key instanceof XmlStringOperationDecoderKey) {
XmlStringOperationDecoderKey xmlStringKey = (XmlStringOperationDecoderKey) key;
opOrType = xmlStringKey.getOperation();
service = Optional.of(xmlStringKey.getService());
}
if (OWSConstants.Operations.GetCapabilities.toString().equalsIgnoreCase(opOrType)) {
if (service.isPresent()) {
throw new InvalidParameterValueException(OWSConstants.GetCapabilitiesParams.service, service.get()).withMessage("The service '%s' is not supported.", service);
} else {
throw new MissingParameterValueException(OWSConstants.GetCapabilitiesParams.service).withMessage("The parameter '%s' is missing.", OWSConstants.GetCapabilitiesParams.service);
}
} else {
throw new InvalidParameterValueException().withMessage("No decoder found for incoming message " + "based on derived decoder key: %s\nMessage: %s", key, xmlString);
}
} else {
LOGGER.trace("Using decoder: {}", decoder);
}
try {
return decoder.decode(xmlString);
} catch (OwsDecodingException ex) {
throw ex.getCause();
} catch (DecodingException ex) {
throw new NoApplicableCodeException().withMessage(ex.getMessage()).causedBy(ex);
}
}
use of org.n52.svalbard.decode.exception.DecodingException in project arctic-sea by 52North.
the class OmObservationTest method should_have_SpatialFilteringProfileParameter.
@Test
public final void should_have_SpatialFilteringProfileParameter() throws OwsExceptionReport, DecodingException {
OmObservation omObservation = new OmObservation();
NamedValue<Geometry> namedValue = new NamedValue<>();
namedValue.setName(new ReferenceType(OmConstants.PARAM_NAME_SAMPLING_GEOMETRY));
GeometryFactory fac = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), 4326);
namedValue.setValue(new GeometryValue(fac.createPoint(new Coordinate(34.5, 76.4))));
// test no parameter is set
assertFalse(omObservation.isSetParameter());
assertFalse(omObservation.isSetSpatialFilteringProfileParameter());
omObservation.addParameter(namedValue);
// test with set SpatialFilteringProfile parameter
assertTrue(omObservation.isSetParameter());
assertTrue(omObservation.isSetSpatialFilteringProfileParameter());
assertThat(omObservation.getSpatialFilteringProfileParameter(), is(equalTo(namedValue)));
}
use of org.n52.svalbard.decode.exception.DecodingException 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;
}
Aggregations