Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method to create WPS client ExecutionResponse from status location URL #760

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,13 @@
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.deegree.commons.ows.exception.OWSException;
import org.deegree.commons.tom.ows.CodeType;
import org.deegree.commons.xml.stax.XMLStreamUtils;
import org.deegree.protocol.ows.exception.OWSExceptionReader;
import org.deegree.protocol.ows.exception.OWSExceptionReport;
import org.deegree.protocol.wps.WPSConstants;
import org.deegree.protocol.wps.WPSConstants.ExecutionState;
import org.deegree.protocol.wps.client.WPSClient;
import org.deegree.protocol.wps.client.output.type.OutputType;
Expand Down Expand Up @@ -226,16 +220,8 @@ public ExecutionState getState()
if ( statusLocation == null ) {
throw new RuntimeException( "Cannot update status. No statusLocation provided." );
}
LOG.debug( "Polling response document from status location: " + statusLocation );
XMLInputFactory inFactory = XMLInputFactory.newInstance();
InputStream is = statusLocation.openStream();
XMLStreamReader xmlReader = inFactory.createXMLStreamReader( is );
XMLStreamUtils.nextElement( xmlReader );
if ( OWSExceptionReader.isExceptionReport( xmlReader.getName() ) ) {
throw OWSExceptionReader.parseExceptionReport( xmlReader );
}
ExecuteResponse100Reader reader = new ExecuteResponse100Reader( xmlReader );
lastResponse = reader.parse100();

lastResponse = ExecuteResponse100Reader.createExecutionResponseFromURL( statusLocation );
}
return lastResponse.getStatus().getState();
}
Expand Down Expand Up @@ -327,7 +313,6 @@ private ExecutionResponse sendExecute( boolean async )
XMLOutputFactory outFactory = XMLOutputFactory.newInstance();

OutputStream os = conn.getOutputStream();
XMLInputFactory inFactory = XMLInputFactory.newInstance();

if ( LOG.isDebugEnabled() ) {
File logFile = File.createTempFile( "wpsclient", "request.xml" );
Expand Down Expand Up @@ -370,20 +355,9 @@ private ExecutionResponse sendExecute( boolean async )
}

// String outputContent = conn.getContentType();
// TODO determine XML reader encoding based on mime type
XMLStreamReader reader = inFactory.createXMLStreamReader( responseStream );
XMLStreamUtils.nextElement( reader );
if ( OWSExceptionReader.isExceptionReport( reader.getName() ) ) {
throw OWSExceptionReader.parseExceptionReport( reader );
}
if ( new QName( WPSConstants.WPS_100_NS, "ExecuteResponse" ).equals( reader.getName() ) ) {
ExecuteResponse100Reader responseReader = new ExecuteResponse100Reader( reader );
lastResponse = responseReader.parse100();
reader.close();

} else {
throw new RuntimeException( "Unexpected Execute response: root element is '" + reader.getName() + "'" );
}
lastResponse = ExecuteResponse100Reader.createExecutionResponseFromStream( responseStream );

return lastResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
Expand All @@ -59,6 +62,7 @@
import org.deegree.commons.xml.stax.XMLStreamUtils;
import org.deegree.protocol.ows.exception.OWSExceptionReader;
import org.deegree.protocol.ows.exception.OWSExceptionReport;
import org.deegree.protocol.wps.WPSConstants;
import org.deegree.protocol.wps.WPSConstants.ExecutionState;
import org.deegree.protocol.wps.client.output.BBoxOutput;
import org.deegree.protocol.wps.client.output.ComplexOutput;
Expand Down Expand Up @@ -403,4 +407,48 @@ private ExecutionStatus parseStatus()
XMLStreamUtils.nextElement( reader ); // </Status>
return new ExecutionStatus( state, statusMsg, percent, creationTime, exceptionReport );
}

/**
* (Convenience) method to read an ExecutionResponse object for a process from a URL.
* @param url The status location URL of the process.
* @return The response object describing the execution of the process.
* @throws OWSExceptionReport
* @throws IOException
* @throws XMLStreamException
*/
public static ExecutionResponse createExecutionResponseFromURL( URL url )
throws OWSExceptionReport, IOException, XMLStreamException {
LOG.debug( "Polling response document from status location: " + url );
InputStream is = url.openStream();
return createExecutionResponseFromStream( is );
}

/**
* (Convenience) method to read an ExecutionResponse object for a process from an input stream.
* @param is The input stream to read from.
* @return The response object describing the execution of the process.
* @throws OWSExceptionReport
* @throws IOException
* @throws XMLStreamException
*/
public static ExecutionResponse createExecutionResponseFromStream( InputStream is )
throws OWSExceptionReport, IOException, XMLStreamException {
// TODO determine XML reader encoding based on mime type
XMLInputFactory inFactory = XMLInputFactory.newInstance();
XMLStreamReader xmlReader = inFactory.createXMLStreamReader( is );
XMLStreamUtils.nextElement( xmlReader );
if ( OWSExceptionReader.isExceptionReport( xmlReader.getName() ) ) {
throw OWSExceptionReader.parseExceptionReport( xmlReader );
}

if ( !new QName( WPSConstants.WPS_100_NS, "ExecuteResponse" ).equals( xmlReader.getName() ) ) {
throw new RuntimeException( "Unexpected Execute response: root element is '" + xmlReader.getName() + "'" );
}

ExecuteResponse100Reader reader = new ExecuteResponse100Reader( xmlReader );
ExecutionResponse response = reader.parse100();
xmlReader.close();

return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
import org.deegree.protocol.wps.client.process.ProcessExecution;
import org.deegree.protocol.wps.client.process.RawProcessExecution;
import org.deegree.protocol.wps.client.process.execute.ExecutionOutputs;
import org.deegree.protocol.wps.client.process.execute.ExecutionResponse;
import org.deegree.protocol.wps.client.wps100.ExecuteResponse100Reader;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
Expand Down Expand Up @@ -536,8 +538,21 @@ public void testExecuteAsync()
Assert.assertNotSame( ExecutionState.SUCCEEDED, execution.getState() );
Assert.assertNotSame( ExecutionState.FAILED, execution.getState() );

URL url = execution.getStatusLocation();
Integer progress = new Integer(0);

while ( ( execution.getState() ) != ExecutionState.SUCCEEDED ) {
System.out.println( execution.getPercentCompleted() );

Assert.assertNotNull( execution.getPercentCompleted() );
Assert.assertTrue( progress <= execution.getPercentCompleted() );
Assert.assertTrue( execution.getPercentCompleted() <= 100 );
progress = execution.getPercentCompleted();

ExecutionResponse response = ExecuteResponse100Reader.createExecutionResponseFromURL( url );
Assert.assertNotNull( response );
Assert.assertTrue( progress <= response.getStatus().getPercentCompleted() );

Thread.sleep( 500 );
}

Expand Down