use of org.n52.svalbard.encode.Encoder in project arctic-sea by 52North.
the class GetObservationResponseXmlStreamWriter method writeGetObservationResponseDoc.
private void writeGetObservationResponseDoc() throws XMLStreamException, EncodingException {
start(Sos2StreamingConstants.GET_OBSERVATION_RESPONSE);
namespace(W3CConstants.NS_XLINK_PREFIX, W3CConstants.NS_XLINK);
namespace(SosConstants.NS_SOS_PREFIX, Sos2Constants.NS_SOS_20);
GetObservationResponse response = getElement();
// get observation encoder
ObservationEncoder<XmlObject, OmObservation> encoder = findObservationEncoder(response.getResponseFormat());
// write schemaLocation
schemaLocation(getSchemaLocation(encoder));
EncodingContext ctx = getContext().with(XmlEncoderFlags.ENCODE_NAMESPACE, response.getResponseFormat()).with(XmlBeansEncodingFlags.DOCUMENT).with(StreamingEncoderFlags.EMBEDDED).without(XmlBeansEncodingFlags.PROPERTY_TYPE).without(XmlBeansEncodingFlags.TYPE);
try {
ObservationStream stream = response.getObservationCollection();
if (encoder.shouldObservationsWithSameXBeMerged()) {
stream = stream.merge();
}
while (stream.hasNext()) {
OmObservation o = stream.next();
if (o.getValue() instanceof ObservationStream) {
ObservationStream value = (ObservationStream) o.getValue();
if (encoder.supportsResultStreamingForMergedValues()) {
writeObservationData(ctx, o, encoder);
} else {
while (value.hasNext()) {
writeObservationData(ctx, value.next(), encoder);
}
}
} else {
writeObservationData(ctx, o, encoder);
}
}
} catch (OwsExceptionReport owse) {
throw new EncodingException(owse);
}
end(Sos2StreamingConstants.GET_OBSERVATION_RESPONSE);
}
use of org.n52.svalbard.encode.Encoder 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;
}
}
}
use of org.n52.svalbard.encode.Encoder in project arctic-sea by 52North.
the class SimpleBinding method encodeResponse.
protected Object encodeResponse(OwsServiceResponse response, MediaType contentType) throws OwsExceptionReport {
try {
OperationResponseEncoderKey key = new OperationResponseEncoderKey(new OwsOperationKey(response), contentType);
Encoder<Object, OwsServiceResponse> encoder = getEncoder(key);
if (encoder == null) {
throw new NoEncoderForKeyException(key);
}
return encoder.encode(response);
} catch (EncodingException ex) {
throw new NoApplicableCodeException().withMessage(ex.getMessage()).causedBy(ex);
}
}
use of org.n52.svalbard.encode.Encoder in project arctic-sea by 52North.
the class OwsExceptionReportEncoderTest method testExceptionWithoutCause.
@Test
public void testExceptionWithoutCause() throws JSONEncodingException {
final EncoderResponseUnsupportedException owse = new EncoderResponseUnsupportedException();
owse.setVersion("2.0.0");
final JsonNode json = enc.encodeJSON(owse);
assertThat(json, is(notNullValue()));
final String message = "The encoder response is not supported!";
e.checkThat(json, is(instanceOf(SchemaConstants.Common.EXCEPTION_REPORT)));
e.checkThat(json.path(VERSION).asText(), is(equalTo("2.0.0")));
e.checkThat(json.path(EXCEPTIONS), is(arrayOfLength(1)));
e.checkThat(json.path(EXCEPTIONS).path(0), isObject());
e.checkThat(json.path(EXCEPTIONS).path(0).path(LOCATOR), does(not(exist())));
e.checkThat(json.path(EXCEPTIONS).path(0).path(TEXT).asText(), is(equalTo(message)));
}
use of org.n52.svalbard.encode.Encoder in project arctic-sea by 52North.
the class AbstractWmlEncoderv20 method createMonitoringPoint.
/**
* Creates a WaterML 2.0 MonitoringPoint XML object from SOS feature object
*
* @param absFeature
* SOS feature
* @return WaterML 2.0 MonitoringPoint XML object
* @throws EncodingException
* If an error occurs
*/
protected XmlObject createMonitoringPoint(AbstractFeature absFeature) throws EncodingException {
if (absFeature instanceof AbstractSamplingFeature) {
AbstractSamplingFeature sampFeat = (AbstractSamplingFeature) absFeature;
StringBuilder builder = new StringBuilder();
builder.append("mp_");
builder.append(JavaHelper.generateID(absFeature.getIdentifierCodeWithAuthority().getValue()));
absFeature.setGmlId(builder.toString());
MonitoringPointDocument monitoringPointDoc = MonitoringPointDocument.Factory.newInstance(getXmlOptions());
if (sampFeat.isSetXml()) {
try {
XmlObject feature = XmlObject.Factory.parse(sampFeat.getXml());
if (XmlHelper.getNamespace(feature).equals(WaterMLConstants.NS_WML_20)) {
if (feature instanceof MonitoringPointDocument) {
monitoringPointDoc = (MonitoringPointDocument) feature;
} else if (feature instanceof MonitoringPointType) {
monitoringPointDoc.setSFSpatialSamplingFeature((MonitoringPointType) feature);
}
XmlHelper.updateGmlIDs(monitoringPointDoc.getDomNode(), absFeature.getGmlId(), null);
return monitoringPointDoc;
}
} catch (XmlException xmle) {
throw new EncodingException("Error while encoding GetFeatureOfInterest response, invalid samplingFeature description!", xmle);
}
}
MonitoringPointType mpt = monitoringPointDoc.addNewMonitoringPoint();
// set gml:id
mpt.setId(absFeature.getGmlId());
if (sampFeat.isSetIdentifier()) {
XmlObject xmlObject = encodeGML(sampFeat.getIdentifierCodeWithAuthority());
if (xmlObject != null) {
mpt.addNewIdentifier().set(xmlObject);
}
}
if (sampFeat.isSetName()) {
for (CodeType sosName : sampFeat.getName()) {
mpt.addNewName().set(encodeGML(sosName));
}
}
if (sampFeat.isSetDescription()) {
if (!mpt.isSetDescription()) {
mpt.addNewDescription();
}
mpt.getDescription().setStringValue(sampFeat.getDescription());
}
// TODO: CHECK
if (sampFeat.getSampledFeatures() != null && !sampFeat.getSampledFeatures().isEmpty()) {
if (sampFeat.getSampledFeatures().size() == 1) {
XmlObject encodeObjectToXml = encodeObjectToXml(GmlConstants.NS_GML_32, sampFeat.getSampledFeatures().get(0));
mpt.addNewSampledFeature().set(encodeObjectToXml);
} else {
FeatureCollection featureCollection = new FeatureCollection();
featureCollection.setGmlId("sampledFeatures_" + absFeature.getGmlId());
for (AbstractFeature sampledFeature : sampFeat.getSampledFeatures()) {
featureCollection.addMember(sampledFeature);
}
XmlObject encodeObjectToXml = encodeGML(featureCollection);
mpt.addNewSampledFeature().set(encodeObjectToXml);
}
} else {
mpt.addNewSampledFeature().setHref(GmlConstants.NIL_UNKNOWN);
}
if (sampFeat.isSetParameter()) {
addParameter(mpt, sampFeat);
}
// set position
ShapeType xbShape = mpt.addNewShape();
Encoder<XmlObject, Geometry> encoder = getEncoder(getEncoderKey(GmlConstants.NS_GML_32, sampFeat.getGeometry()));
if (encoder != null) {
XmlObject xmlObject = encoder.encode(sampFeat.getGeometry(), new EncodingContext().with(XmlBeansEncodingFlags.GMLID, absFeature.getGmlId()));
xbShape.addNewAbstractGeometry().set(xmlObject);
XmlHelper.substituteElement(xbShape.getAbstractGeometry(), xmlObject);
} else {
throw new EncodingException("Error while encoding geometry for feature, needed encoder is missing!");
}
if (absFeature instanceof WmlMonitoringPoint) {
addMonitoringPointValues(mpt, (WmlMonitoringPoint) absFeature);
}
sampFeat.wasEncoded();
return monitoringPointDoc;
}
throw new UnsupportedEncoderInputException(this, absFeature);
}
Aggregations