Creating a custom Java process extending AbstractAlgorithm
This tutorial is very similar to
Extend ing AbstractSelfDescribingAlgorithm. However, if you extend AbstractAlgorithm you are able to create the ProcessDescription file yourself.
Download the process development kit or the WPS extension skeleton project.
- Create a new package: org.n52.wps.demo
- Create a new class: AbstractAlgorithmExample
- Let the new class extend AbstractAlgorithm
Implement Process
Add the following fields to the class:
private final String inputID = "input";
private final String outputID = "output";
For AbstractSelfDescribingAlgorithm, you need to implement four methods:
This method lists all input datatypes for a given input data identifier.In our case, we only have one input identifier:
inputID. The return class must be a specific
binding.
We will simply use a LiteralStringBinding here:
if(id.equals(inputID)){
return LiteralStringBinding.class;
}
return null;
Method 2: public Class getOutputDataType(String id)
This method lists all output datatypes for a given output data identifier.In our case, we only have only one output identifier:
outputID. We will use the LiteralStringBinding again here:
if(id.equals(outputID)){
return LiteralStringBinding.class;
}
return null;
Method 3: public List<String> getErrors()
This method can be used to pass error descriptions to clients. For this example, we will not use it. Just keep the
return null;
statement.
This method holds the business logic. At first we check that all necessary inputdata is present.
if (inputData == null || !inputData.containsKey(inputID)) {
throw new RuntimeException("Error while allocating input parameters");
}
List<IData> dataList = inputData.get(inputID);
if (dataList == null || dataList.size() != 1) {
throw new RuntimeException("Error while allocating input parameters");
}
Next, we get the string binding:
IData stringInputData = dataList.get(0);
We will just return the string input. So the last step is to create the standard output hashmap which holds the name of the output identifier and an IData object. In our case we just pass the string input binding:
HashMap<String, IData> result = new HashMap<String, IData>();
result.put(outputID, stringInputData);
return result;
Save the source code and compile it. You may need to "Organize imports" to add all required classes. You can download the complete Java class
here.
Create the ProcessDescription
When our process is extending AbstractAlgorithm, the ProcessDescription is not generated automatically as with other algorithm base classes. So we have to create it ourselves. This gives us more control e.g. over the input and output formats our process should support. The following is a common header, where the namespaces and schemalocation is defined:
<?xml version="1.0" encoding="UTF-8"?>
<wps:ProcessDescriptions xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ows="http://www.opengis.net/ows/1.1"xsi:schemaLocation="http://www.opengis.net/wps/1.0.0
http://schemas.opengis.net/wps/1.0.0/wpsDescribeProcess_response.xsd" xml:lang="en-US" service="WPS" version="1.0.0">
Now we can define whether status messages and result storage are supported and also the process version:
<ProcessDescription statusSupported="true" storeSupported="true" wps:processVersion="1.1.0">
In the following section the inputs are defined:
<DataInputs>
<Input minOccurs="1" maxOccurs="1">
<ows:Identifier>input</ows:Identifier>
<ows:Title>input</ows:Title>
<LiteralData>
<ows:DataType ows:reference="xs:string"/>
<ows:AnyValue/>
</LiteralData>
</Input>
</DataInputs>
Note that the identifier must be the same as inputID in the Java class. The process outputs are defined as follows:
<ProcessOutputs>
<Output>
<ows:Identifier>output</ows:Identifier>
<ows:Title>output</ows:Title>
<LiteralOutput>
<ows:DataType ows:reference="xs:string"/>
</LiteralOutput>
</Output>
</ProcessOutputs>
In this case the identifier must be the same as outputID in the Java class. Now we just close the open tags:
</ProcessDescription>
</wps:ProcessDescriptions>
Save the Processdescription as AbstractAlgorithmExample.xml. You can download the complete Java class
here.
Deploy the custom process
See
here.
Execute the process
See
here.