Search in sources :

Example 26 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext in project arctic-sea by 52North.

the class SweCommonEncoderv101 method encode.

@Override
public XmlObject encode(Object element, EncodingContext context) throws EncodingException {
    XmlObject encodedObject = null;
    if (element instanceof SweAbstractSimpleType) {
        encodedObject = createSimpleType((SweAbstractSimpleType<?>) element, context);
    // }
    // if (element instanceof SweBoolean) {
    // encodedObject = createBoolean((SweBoolean) element);
    // } else if (element instanceof SweCategory) {
    // encodedObject = createCategory((SweCategory) element);
    // } else if (element instanceof SweCount) {
    // encodedObject = createCount((SweCount) element);
    // } else if (element instanceof SweObservableProperty) {
    // encodedObject = createObservableProperty((SweObservableProperty)
    // element);
    // } else if (element instanceof SweQuantity) {
    // encodedObject = createQuantity((SweQuantity) element);
    // } else if (element instanceof SweQuantityRange) {
    // encodedObject = createQuantityRange((SweQuantityRange) element);
    // } else if (element instanceof SweText) {
    // encodedObject = createText((SweText) element);
    // } else if (element instanceof SweTime) {
    // encodedObject = createTime((SweTime) element);
    // } else if (element instanceof SweTimeRange) {
    // encodedObject = createTimeRange((SweTimeRange) element);
    } else if (element instanceof SweCoordinate) {
        encodedObject = createCoordinate((SweCoordinate<?>) element);
    } else if (element instanceof SweDataArray) {
        encodedObject = createDataArray((SweDataArray) element);
    } else if (element instanceof SweDataRecord) {
        DataRecordType drt = createDataRecord((SweDataRecord) element);
        if (context.has(XmlBeansEncodingFlags.DOCUMENT)) {
            DataRecordDocument drd = DataRecordDocument.Factory.newInstance(getXmlOptions());
            drd.setDataRecord(drt);
            encodedObject = drd;
        } else {
            encodedObject = drt;
        }
    } else if (element instanceof SweEnvelope) {
        encodedObject = createEnvelope((SweEnvelope) element);
    } else if (element instanceof SweSimpleDataRecord) {
        encodedObject = createSimpleDataRecord((SweSimpleDataRecord) element);
    } else if (element instanceof TimePeriod) {
        encodedObject = createTimeGeometricPrimitivePropertyType((TimePeriod) element);
    } else {
        throw new UnsupportedEncoderInputException(this, element);
    }
    XmlHelper.validateDocument(encodedObject, EncodingException::new);
    return encodedObject;
}
Also used : DataRecordType(net.opengis.swe.x101.DataRecordType) SimpleDataRecordType(net.opengis.swe.x101.SimpleDataRecordType) SweEnvelope(org.n52.shetland.ogc.swe.SweEnvelope) SweSimpleDataRecord(org.n52.shetland.ogc.swe.SweSimpleDataRecord) SweAbstractSimpleType(org.n52.shetland.ogc.swe.simpleType.SweAbstractSimpleType) SweDataRecord(org.n52.shetland.ogc.swe.SweDataRecord) EncodingException(org.n52.svalbard.encode.exception.EncodingException) NotYetSupportedEncodingException(org.n52.svalbard.encode.exception.NotYetSupportedEncodingException) TimePeriod(org.n52.shetland.ogc.gml.time.TimePeriod) XmlObject(org.apache.xmlbeans.XmlObject) SweDataArray(org.n52.shetland.ogc.swe.SweDataArray) DataRecordDocument(net.opengis.swe.x101.DataRecordDocument) SweCoordinate(org.n52.shetland.ogc.swe.SweCoordinate) UnsupportedEncoderInputException(org.n52.svalbard.encode.exception.UnsupportedEncoderInputException)

Example 27 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext in project arctic-sea by 52North.

the class Soap11Encoder method encode.

@Override
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
public SOAPMessage encode(SoapResponse soapResponse, EncodingContext additionalValues) throws EncodingException {
    if (soapResponse == null) {
        throw new UnsupportedEncoderInputException(this, soapResponse);
    }
    String soapVersion = soapResponse.getSoapVersion();
    SOAPMessage soapResponseMessage;
    String action = null;
    try {
        soapResponseMessage = SoapHelper.getSoapMessageForProtocol(soapVersion);
        if (soapResponse.getSoapFault() != null) {
            createSOAPFault(soapResponseMessage.getSOAPBody().addFault(), soapResponse.getSoapFault());
        } else {
            if (soapResponse.getException() != null) {
                action = createSOAPFaultFromExceptionResponse(soapResponseMessage.getSOAPBody().addFault(), soapResponse.getException());
                addSchemaLocationForExceptionToSOAPMessage(soapResponseMessage);
            } else {
                action = createSOAPBody(soapResponseMessage, soapResponse, soapResponse.getSoapAction());
            }
        }
        if (soapResponse.getHeader() != null) {
            List<SoapHeader> headers = soapResponse.getHeader();
            for (SoapHeader header : headers) {
                if (WsaConstants.NS_WSA.equals(header.getNamespace()) && header instanceof WsaActionHeader) {
                    ((WsaHeader) header).setValue(action);
                }
                Encoder<Map<QName, String>, SoapHeader> encoder = getEncoder(CodingHelper.getEncoderKey(header.getNamespace(), header));
                if (encoder != null) {
                    Map<QName, String> headerElements = encoder.encode(header);
                    for (Entry<QName, String> entry : headerElements.entrySet()) {
                        QName qName = entry.getKey();
                        soapResponseMessage.getSOAPHeader().addChildElement(qName).setTextContent(headerElements.get(qName));
                    }
                }
            }
        } else {
            soapResponseMessage.getSOAPHeader().detachNode();
        }
        soapResponseMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, String.valueOf(true));
        return soapResponseMessage;
    } catch (SOAPException soape) {
        throw new EncodingException("Error while encoding SOAPMessage!", soape);
    }
}
Also used : EncodingException(org.n52.svalbard.encode.exception.EncodingException) QName(javax.xml.namespace.QName) SOAPMessage(javax.xml.soap.SOAPMessage) WsaHeader(org.n52.shetland.w3c.wsa.WsaHeader) WsaActionHeader(org.n52.shetland.w3c.wsa.WsaActionHeader) UnsupportedEncoderInputException(org.n52.svalbard.encode.exception.UnsupportedEncoderInputException) SOAPException(javax.xml.soap.SOAPException) SoapHeader(org.n52.shetland.w3c.soap.SoapHeader) Map(java.util.Map) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 28 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext in project arctic-sea by 52North.

the class Soap12Encoder method encode.

@Override
public void encode(Object element, OutputStream outputStream, EncodingContext ctx) throws EncodingException {
    if (element instanceof SoapResponse) {
        try {
            EncodingContext context = ctx.with(EncoderFlags.ENCODER_REPOSITORY, getEncoderRepository()).with(XmlEncoderFlags.XML_OPTIONS, (Supplier<XmlOptions>) this::getXmlOptions);
            new Soap12XmlStreamWriter(context, outputStream, (SoapResponse) element).write();
        } catch (XMLStreamException ex) {
            throw new EncodingException(ex);
        }
    } else {
        try {
            encode(element, ctx).save(outputStream, getXmlOptions());
        } catch (IOException ioe) {
            throw new EncodingException("Error while writing element to stream!", ioe);
        }
    }
}
Also used : Soap12XmlStreamWriter(org.n52.svalbard.write.Soap12XmlStreamWriter) SoapResponse(org.n52.shetland.w3c.soap.SoapResponse) XMLStreamException(javax.xml.stream.XMLStreamException) EncodingException(org.n52.svalbard.encode.exception.EncodingException) Supplier(java.util.function.Supplier) IOException(java.io.IOException)

Example 29 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext 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);
}
Also used : EncodingContext(org.n52.svalbard.encode.EncodingContext) ObservationStream(org.n52.shetland.ogc.om.ObservationStream) EncodingException(org.n52.svalbard.encode.exception.EncodingException) GetObservationResponse(org.n52.shetland.ogc.sos.response.GetObservationResponse) OmObservation(org.n52.shetland.ogc.om.OmObservation) XmlObject(org.apache.xmlbeans.XmlObject) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport)

Example 30 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext 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);
}
Also used : MonitoringPointType(net.opengis.waterml.x20.MonitoringPointType) MonitoringPointDocument(net.opengis.waterml.x20.MonitoringPointDocument) AbstractSamplingFeature(org.n52.shetland.ogc.om.features.samplingFeatures.AbstractSamplingFeature) EncodingException(org.n52.svalbard.encode.exception.EncodingException) ShapeType(net.opengis.samplingSpatial.x20.ShapeType) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature) WmlMonitoringPoint(org.n52.shetland.ogc.om.series.wml.WmlMonitoringPoint) UnsupportedEncoderInputException(org.n52.svalbard.encode.exception.UnsupportedEncoderInputException) Geometry(org.locationtech.jts.geom.Geometry) FeatureCollection(org.n52.shetland.ogc.om.features.FeatureCollection) XmlException(org.apache.xmlbeans.XmlException) CodeType(org.n52.shetland.ogc.gml.CodeType) XmlObject(org.apache.xmlbeans.XmlObject)

Aggregations

EncodingException (org.n52.svalbard.encode.exception.EncodingException)26 XmlObject (org.apache.xmlbeans.XmlObject)24 UnsupportedEncoderInputException (org.n52.svalbard.encode.exception.UnsupportedEncoderInputException)15 XmlException (org.apache.xmlbeans.XmlException)9 XMLStreamException (javax.xml.stream.XMLStreamException)6 OmObservation (org.n52.shetland.ogc.om.OmObservation)6 Supplier (java.util.function.Supplier)5 Time (org.n52.shetland.ogc.gml.time.Time)5 OwsExceptionReport (org.n52.shetland.ogc.ows.exception.OwsExceptionReport)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 AbstractCRSType (net.opengis.gml.x32.AbstractCRSType)4 CodeType (net.opengis.gml.x32.CodeType)4 FeaturePropertyType (net.opengis.gml.x32.FeaturePropertyType)4 DateTime (org.joda.time.DateTime)4 AbstractFeature (org.n52.shetland.ogc.gml.AbstractFeature)4 TimePeriod (org.n52.shetland.ogc.gml.time.TimePeriod)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 AbstractGeometryType (net.opengis.gml.x32.AbstractGeometryType)3 GeometryPropertyType (net.opengis.gml.x32.GeometryPropertyType)3