diff --git a/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/main/java/org/deegree/protocol/wps/client/process/ProcessExecution.java b/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/main/java/org/deegree/protocol/wps/client/process/ProcessExecution.java index d7a2109f7e1..2df97a15aa9 100644 --- a/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/main/java/org/deegree/protocol/wps/client/process/ProcessExecution.java +++ b/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/main/java/org/deegree/protocol/wps/client/process/ProcessExecution.java @@ -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; @@ -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(); } @@ -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" ); @@ -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; } } \ No newline at end of file diff --git a/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/main/java/org/deegree/protocol/wps/client/wps100/ExecuteResponse100Reader.java b/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/main/java/org/deegree/protocol/wps/client/wps100/ExecuteResponse100Reader.java index 518d1b20c36..74f979dde84 100644 --- a/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/main/java/org/deegree/protocol/wps/client/wps100/ExecuteResponse100Reader.java +++ b/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/main/java/org/deegree/protocol/wps/client/wps100/ExecuteResponse100Reader.java @@ -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; @@ -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; @@ -403,4 +407,48 @@ private ExecutionStatus parseStatus() XMLStreamUtils.nextElement( reader ); // 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; + } } \ No newline at end of file diff --git a/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/test/java/org/deegree/protocol/wps/client/WPSClientTest.java b/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/test/java/org/deegree/protocol/wps/client/WPSClientTest.java index 7f54c648051..5aefea25309 100644 --- a/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/test/java/org/deegree/protocol/wps/client/WPSClientTest.java +++ b/deegree-core/deegree-core-protocol/deegree-protocol-commons/src/test/java/org/deegree/protocol/wps/client/WPSClientTest.java @@ -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; @@ -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 ); }