Search in sources :

Example 26 with Data

use of org.geotoolkit.wps.xml.v200.Data in project geotoolkit by Geomatys.

the class WPS2Process method fillOutputs.

/**
 * Fill {@link ParameterValueGroup parameters} of the process using the WPS
 * {@link ExecuteResponse response}.
 *
 * @throws ProcessException if data conversion fails.
 */
private void fillOutputs(Object response) throws ProcessException {
    try {
        if (response == null) {
            // request the result from the server
            final GetResultRequest request = registry.getClient().createGetResult(jobId);
            request.setDebug(debug);
            request.setClientSecurity(security);
            response = request.getResponse();
        }
        if (response instanceof Result) {
            final Result result = (Result) response;
            for (DataOutput out : result.getOutput()) {
                fillOutputs(outputParameters, out);
            }
        } else if (response instanceof ExceptionResponse) {
            final ExceptionResponse report = (ExceptionResponse) response;
            throw new ProcessException("Exception when getting process result.", this, report.toException());
        }
    } catch (JAXBException ex) {
        Logger.getLogger(WPS2Process.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(WPS2Process.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : DataOutput(org.geotoolkit.wps.xml.v200.DataOutput) GetResultRequest(org.geotoolkit.wps.client.GetResultRequest) ProcessException(org.geotoolkit.process.ProcessException) DismissProcessException(org.geotoolkit.process.DismissProcessException) ExceptionResponse(org.geotoolkit.ows.xml.ExceptionResponse) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) Result(org.geotoolkit.wps.xml.v200.Result)

Example 27 with Data

use of org.geotoolkit.wps.xml.v200.Data in project geotoolkit by Geomatys.

the class WPS2ProcessDescriptor method toDescriptor.

/**
 * Convert Description to GeneralParameterDescriptor.
 *
 * @throws UnsupportedOperationException if data type could not be mapped
 */
private static GeneralParameterDescriptor toDescriptor(String processId, Description input) throws UnsupportedParameterException {
    final List<? extends Description> subInputs;
    final DataDescription dataDescType;
    final int min;
    final int max;
    if (input instanceof InputDescription) {
        final InputDescription id = (InputDescription) input;
        subInputs = id.getInput();
        dataDescType = id.getDataDescription();
        max = id.getMaxOccurs();
        min = id.getMinOccurs();
    } else if (input instanceof OutputDescription) {
        final OutputDescription od = (OutputDescription) input;
        subInputs = od.getOutput();
        dataDescType = od.getDataDescription();
        min = 1;
        max = 1;
    } else {
        throw new IllegalArgumentException("Unexpected description type " + input.getClass());
    }
    final String inputName = input.getIdentifier().getValue();
    final String title = input.getFirstTitle();
    final String remarks = input.getFirstAbstract();
    Map userObject = new HashMap();
    for (MetadataType meta : input.getMetadata()) {
        if (meta instanceof AdditionalParametersType) {
            AdditionalParametersType params = (AdditionalParametersType) meta;
            for (AdditionalParameter param : params.getAdditionalParameter()) {
                userObject.put(param.getName().getValue(), param.getValue());
            }
        }
    }
    if (dataDescType instanceof LiteralData) {
        final LiteralData cd = (LiteralData) dataDescType;
        for (LiteralDataDomain domain : cd.getLiteralDataDomain()) {
            final LiteralAdaptor adaptor = LiteralAdaptor.create(domain);
            if (adaptor == null)
                continue;
            String defaultValueValue = null;
            final ValueType defaultValue = domain.getDefaultValue();
            if (defaultValue != null)
                defaultValueValue = defaultValue.getValue();
            final Unit unit = getUnit(domain.getUOM());
            Object[] allowedValues = null;
            if (domain.getAllowedValues() != null && domain.getAllowedValues().getStringValues() != null) {
                allowedValues = new Object[domain.getAllowedValues().getStringValues().size()];
                int i = 0;
                for (String value : domain.getAllowedValues().getStringValues()) {
                    allowedValues[i] = adaptor.convert(value);
                    i++;
                }
            }
            try {
                userObject.put(DataAdaptor.USE_ADAPTOR, adaptor);
                return new ExtendedParameterDescriptor(inputName, title, remarks, min, max, adaptor.getValueClass(), adaptor.convert(defaultValueValue), allowedValues, userObject);
            } catch (UnconvertibleObjectException ex2) {
                throw new UnsupportedParameterException(processId, inputName, "Can't convert the default literal input value.", ex2);
            }
        }
        throw new UnsupportedParameterException(processId, inputName, "Unidentifiable literal input " + inputName);
    } else if (dataDescType instanceof ComplexData) {
        final ComplexData cdt = (ComplexData) dataDescType;
        // ensure default format is first in the list
        Collections.sort(cdt.getFormat(), (Format o1, Format o2) -> {
            boolean d1 = Boolean.TRUE.equals(o1.isDefault());
            boolean d2 = Boolean.TRUE.equals(o2.isDefault());
            if (d1 == d2)
                return 0;
            return d1 ? -1 : +1;
        });
        // find a complexe type adaptor
        DataAdaptor adaptor = null;
        for (Format format : cdt.getFormat()) {
            adaptor = ComplexAdaptor.getAdaptor(format);
            if (adaptor != null)
                break;
        }
        if (adaptor == null) {
            final StringBuilder sb = new StringBuilder();
            for (Format format : cdt.getFormat()) {
                if (sb.length() != 0)
                    sb.append(", ");
                sb.append(format.getMimeType()).append(' ');
                sb.append(format.getEncoding()).append(' ');
                sb.append(format.getSchema());
            }
            throw new UnsupportedParameterException(processId, inputName, "No compatible format found for parameter " + inputName + " formats : " + sb);
        }
        userObject.put(DataAdaptor.USE_ADAPTOR, adaptor);
        return new ExtendedParameterDescriptor(inputName, title, remarks, min, max, adaptor.getValueClass(), null, null, userObject);
    } else if (dataDescType instanceof BoundingBoxData) {
        final BboxAdaptor adaptor = BboxAdaptor.create((BoundingBoxData) dataDescType);
        userObject.put(DataAdaptor.USE_ADAPTOR, adaptor);
        return new ExtendedParameterDescriptor(inputName, title, remarks, min, max, Envelope.class, null, null, userObject);
    } else if (!subInputs.isEmpty()) {
        // sub group type
        final List<GeneralParameterDescriptor> params = new ArrayList<>();
        for (Description dt : subInputs) {
            params.add(toDescriptor(processId, dt));
        }
        return new ParameterBuilder().addName(inputName).addName(title).setRemarks(remarks).createGroup(params.toArray(new GeneralParameterDescriptor[0]));
    } else {
        throw new UnsupportedParameterException(processId, inputName, "Unidentifiable input " + inputName + " " + dataDescType);
    }
}
Also used : AdditionalParametersType(org.geotoolkit.ows.xml.v200.AdditionalParametersType) DataDescription(org.geotoolkit.wps.xml.v200.DataDescription) Description(org.geotoolkit.wps.xml.v200.Description) InputDescription(org.geotoolkit.wps.xml.v200.InputDescription) OutputDescription(org.geotoolkit.wps.xml.v200.OutputDescription) ProcessDescription(org.geotoolkit.wps.xml.v200.ProcessDescription) HashMap(java.util.HashMap) InternationalString(org.opengis.util.InternationalString) DefaultInternationalString(org.apache.sis.util.DefaultInternationalString) Unit(javax.measure.Unit) DataDescription(org.geotoolkit.wps.xml.v200.DataDescription) BboxAdaptor(org.geotoolkit.wps.adaptor.BboxAdaptor) OutputDescription(org.geotoolkit.wps.xml.v200.OutputDescription) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Format(org.geotoolkit.wps.xml.v200.Format) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) DataAdaptor(org.geotoolkit.wps.adaptor.DataAdaptor) BoundingBoxData(org.geotoolkit.wps.xml.v200.BoundingBoxData) InputDescription(org.geotoolkit.wps.xml.v200.InputDescription) ArrayList(java.util.ArrayList) List(java.util.List) ParameterBuilder(org.apache.sis.parameter.ParameterBuilder) LiteralDataDomain(org.geotoolkit.wps.xml.v200.LiteralDataDomain) LiteralAdaptor(org.geotoolkit.wps.adaptor.LiteralAdaptor) LiteralData(org.geotoolkit.wps.xml.v200.LiteralData) ValueType(org.geotoolkit.ows.xml.v200.ValueType) MetadataType(org.geotoolkit.ows.xml.v200.MetadataType) DomainMetadataType(org.geotoolkit.ows.xml.v200.DomainMetadataType) AdditionalParameter(org.geotoolkit.ows.xml.v200.AdditionalParameter) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with Data

use of org.geotoolkit.wps.xml.v200.Data in project geotoolkit by Geomatys.

the class WPSConvertersUtils method featureToParameterGroup.

/**
 * Convert a feature into a ParameterValueGroup.
 *
 * This function will try to convert objects if their types doesn't match between feature and parameter.
 * Then fill a ParameterValueGroup which contains data of the feature in parameter.
 *
 * @param version WPS version
 * @param toConvert The feature to transform.
 * @param toFill The descriptor of the ParameterValueGroup which will be created.
 */
public static void featureToParameterGroup(String version, Feature toConvert, ParameterValueGroup toFill) throws UnconvertibleObjectException {
    ArgumentChecks.ensureNonNull("feature", toConvert);
    ArgumentChecks.ensureNonNull("ParameterGroup", toFill);
    final WPSConverterRegistry registry = WPSConverterRegistry.getInstance();
    final Parameters toFill2 = Parameters.castOrWrap(toFill);
    for (final GeneralParameterDescriptor gpd : toFill.getDescriptor().descriptors()) {
        if (gpd instanceof ParameterDescriptor) {
            final Property prop = toConvert.getProperty(gpd.getName().getCode());
            if (prop == null && gpd.getMinimumOccurs() > 0) {
                throw new UnconvertibleObjectException("A mandatory attribute can't be found");
            }
            final ParameterDescriptor desc = (ParameterDescriptor) gpd;
            if (prop.getValue().getClass().isAssignableFrom(desc.getValueClass()) || desc.getValueClass().isAssignableFrom(prop.getValue().getClass())) {
                toFill2.getOrCreate(desc).setValue(prop.getValue());
            } else {
                if (prop.getValue().getClass().isAssignableFrom(URI.class)) {
                    Reference type = UriToReference(version, (URI) prop.getValue(), WPSIO.IOType.INPUT, null);
                    WPSObjectConverter converter = registry.getConverter(type.getClass(), desc.getValueClass());
                    toFill2.getOrCreate(desc).setValue(converter.convert(type, null));
                }
            }
        } else if (gpd instanceof ParameterDescriptorGroup) {
            final Collection<Feature> propCollection = (Collection<Feature>) toConvert.getPropertyValue(gpd.getName().getCode());
            int filledGroups = 0;
            for (Feature complex : propCollection) {
                ParameterValueGroup childGroup = toFill.addGroup(gpd.getName().getCode());
                featureToParameterGroup(version, complex, childGroup);
                filledGroups++;
            }
            if (filledGroups < gpd.getMinimumOccurs()) {
                throw new UnconvertibleObjectException("Not enough attributes have been found.");
            }
        } else {
            throw new UnconvertibleObjectException("Parameter type is not managed.");
        }
    }
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Parameters(org.apache.sis.parameter.Parameters) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Reference(org.geotoolkit.wps.xml.v200.Reference) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) Property(org.opengis.feature.Property) Feature(org.opengis.feature.Feature)

Example 29 with Data

use of org.geotoolkit.wps.xml.v200.Data in project geotoolkit by Geomatys.

the class WPSConvertersUtils method geojsonContentAsString.

/**
 * Extract the GeoJSON content of a complex and return it as a String.
 *
 * Pre-condition : the complex must have exactly one content element
 * Pre-condition : the content must be either of the type GeoJSONType, String
 * or Node
 *
 * @param objContent the complex to read
 * @return the complex content as a String
 */
public static String geojsonContentAsString(final Object objContent) {
    ArgumentChecks.ensureNonNull("Data", objContent);
    Object content = objContent;
    if (content instanceof Data) {
        List<Object> contents = ((Data) content).getContent();
        if (contents == null || contents.isEmpty()) {
            content = "";
        } else if (contents.size() > 1) {
            throw new UnconvertibleObjectException("We search for a single text content, but given data contains " + contents.size());
        } else {
            content = contents.get(0);
        }
    }
    // Data can contain a literal value, so we test it after
    if (content instanceof LiteralValue) {
        content = ((LiteralValue) content).getValue();
    } else if (content instanceof Node) {
        // Otherwise, data could contain a Dom node (rarely), so we also test it
        content = ((Node) content).getTextContent();
    }
    if (content instanceof String)
        // TODO: remove CDATA ?
        return (String) content;
    throw new UnconvertibleObjectException("Cannot extract text content from source " + objContent.getClass().getName());
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Node(org.w3c.dom.Node) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) Data(org.geotoolkit.wps.xml.v200.Data) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) LiteralValue(org.geotoolkit.wps.xml.v200.LiteralValue)

Example 30 with Data

use of org.geotoolkit.wps.xml.v200.Data in project geotoolkit by Geomatys.

the class AbstractComplexInputConverter method fromXml.

private static Stream<FeatureSet> fromXml(final Data source) {
    final XmlFeatureReader fcollReader;
    try {
        fcollReader = WPSIO.getFeatureReader(source.getSchema());
    } catch (MalformedURLException ex) {
        throw new UnconvertibleObjectException("Unable to reach the schema url.", ex);
    }
    List<Object> content = new ArrayList<>();
    // extract feature nodes from gml unmarshalled feature collection
    for (Object o : source.getContent()) {
        if (o instanceof JAXBElement) {
            o = ((JAXBElement) o).getValue();
        }
        if (o instanceof FeatureCollection) {
            FeatureCollection fc = (FeatureCollection) o;
            for (FeatureProperty fp : fc.getFeatureMember()) {
                if (fp.getUnknowFeature() != null) {
                    content.add(fp.getUnknowFeature());
                } else {
                    throw new UnconvertibleObjectException("Unabel to read feature from XML.");
                }
            }
        } else {
            content.add(o);
        }
    }
    final Stream<FeatureSet> result = content.stream().map(in -> {
        try {
            return fcollReader.read(in);
        } catch (XMLStreamException | IOException ex) {
            throw new UnconvertibleObjectException("Unable to read feature from nodes.", ex);
        }
    }).map(ReferenceToFeatureCollectionConverter::castOrWrap);
    return result.onClose(() -> fcollReader.dispose());
}
Also used : ReferenceToFeatureCollectionConverter(org.geotoolkit.wps.converters.inputs.references.ReferenceToFeatureCollectionConverter) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) JAXBElement(javax.xml.bind.JAXBElement) ArgumentChecks(org.apache.sis.util.ArgumentChecks) WPSConvertersUtils(org.geotoolkit.wps.converters.WPSConvertersUtils) IOException(java.io.IOException) FeatureProperty(org.geotoolkit.gml.xml.FeatureProperty) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) FeatureSet(org.apache.sis.storage.FeatureSet) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) FeatureCollection(org.geotoolkit.gml.xml.FeatureCollection) WPSDefaultConverter(org.geotoolkit.wps.converters.WPSDefaultConverter) Data(org.geotoolkit.wps.xml.v200.Data) DataStoreException(org.apache.sis.storage.DataStoreException) WPSIO(org.geotoolkit.wps.io.WPSIO) WPSMimeType(org.geotoolkit.wps.io.WPSMimeType) Path(java.nio.file.Path) XmlFeatureReader(org.geotoolkit.feature.xml.XmlFeatureReader) MalformedURLException(java.net.MalformedURLException) ReferenceToFeatureCollectionConverter(org.geotoolkit.wps.converters.inputs.references.ReferenceToFeatureCollectionConverter) XmlFeatureReader(org.geotoolkit.feature.xml.XmlFeatureReader) ArrayList(java.util.ArrayList) JAXBElement(javax.xml.bind.JAXBElement) FeatureProperty(org.geotoolkit.gml.xml.FeatureProperty) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) FeatureCollection(org.geotoolkit.gml.xml.FeatureCollection) FeatureSet(org.apache.sis.storage.FeatureSet)

Aggregations

Data (org.geotoolkit.wps.xml.v200.Data)42 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)33 IOException (java.io.IOException)25 Test (org.junit.Test)19 Reference (org.geotoolkit.wps.xml.v200.Reference)16 Path (java.nio.file.Path)15 DataInput (org.geotoolkit.wps.xml.v200.DataInput)15 Format (org.geotoolkit.wps.xml.v200.Format)14 DataStoreException (org.apache.sis.storage.DataStoreException)12 HashMap (java.util.HashMap)11 ComplexData (org.geotoolkit.wps.xml.v200.ComplexData)11 JAXBException (javax.xml.bind.JAXBException)9 ReferenceProxy (org.geotoolkit.wps.xml.ReferenceProxy)9 Data (com.microsoft.applicationinsights.smoketest.schemav2.Data)8 Envelope (com.microsoft.applicationinsights.smoketest.schemav2.Envelope)8 Feature (org.opengis.feature.Feature)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 LiteralValue (org.geotoolkit.wps.xml.v200.LiteralValue)7 Geometry (org.locationtech.jts.geom.Geometry)7 DataOutput (org.geotoolkit.wps.xml.v200.DataOutput)6