/**
  * Returns the current state of the execution.
  *
  * @return state of the execution, or <code>null</code> if the execution has not been started yet
  * @throws OWSExceptionReport if the server replied with an exception
  * @throws IOException if a communication/network problem occured
  * @throws XMLStreamException
  */
 public ExecutionState getState() throws OWSExceptionReport, IOException, XMLStreamException {
   if (lastResponse == null) {
     return null;
   }
   if (lastResponse.getStatus().getState() != ExecutionState.SUCCEEDED
       && lastResponse.getStatus().getState() != ExecutionState.FAILED) {
     URL statusLocation = lastResponse.getStatusLocation();
     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();
   }
   return lastResponse.getStatus().getState();
 }
  private ExecutionResponse sendExecute(boolean async)
      throws OWSExceptionReport, XMLStreamException, IOException {

    responseFormat = new ResponseFormat(false, async, false, async, outputDefs);

    // TODO what if server only supports Get?
    URL url = client.getExecuteURL(true);

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    // TODO does this need configurability?
    conn.setRequestProperty("Content-Type", "application/xml");

    XMLOutputFactory outFactory = XMLOutputFactory.newInstance();

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

    if (LOG.isDebugEnabled()) {
      File logFile = File.createTempFile("wpsclient", "request.xml");
      XMLStreamWriter logWriter =
          outFactory.createXMLStreamWriter(new FileOutputStream(logFile), "UTF-8");
      ExecuteRequest100Writer executer = new ExecuteRequest100Writer(logWriter);
      executer.write100(process.getId(), inputs, responseFormat);
      logWriter.close();
      LOG.debug("WPS request can be found at " + logFile);

      InputStream is = new FileInputStream(logFile);
      byte[] buffer = new byte[1024];
      int read = 0;
      while ((read = is.read(buffer)) != -1) {
        os.write(buffer, 0, read);
      }
      is.close();
      os.close();
    } else {
      XMLStreamWriter writer = outFactory.createXMLStreamWriter(os, "UTF-8");
      ExecuteRequest100Writer executer = new ExecuteRequest100Writer(writer);
      executer.write100(process.getId(), inputs, responseFormat);
      writer.close();
    }

    InputStream responseStream = conn.getInputStream();

    if (LOG.isDebugEnabled()) {
      File logFile = File.createTempFile("wpsclient", "response");
      OutputStream logStream = new FileOutputStream(logFile);

      byte[] buffer = new byte[1024];
      int read = 0;
      while ((read = responseStream.read(buffer)) != -1) {
        logStream.write(buffer, 0, read);
      }
      logStream.close();

      responseStream = new FileInputStream(logFile);
      LOG.debug("WPS response can be found at " + logFile);
    }

    // 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() + "'");
    }
    return lastResponse;
  }