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