@Override
  public Object encode(SoapResponse objectToEncode, Map<HelperValues, String> additionalValues)
      throws EncodingException {
    AbstractServiceResponse bodyContent = objectToEncode.getBodyContent();

    /*
     * Special case: NoContent
     */
    if (bodyContent instanceof NoContentResponse) {
      return new NoContent();
    }

    EnvelopeDocument envDoc = EnvelopeDocument.Factory.newInstance();
    Envelope env = envDoc.addNewEnvelope();

    Body body = env.addNewBody();

    OwsExceptionReport exception = null;
    if (bodyContent != null) {
      try {
        body.set(encodeBody(bodyContent));
      } catch (OwsExceptionReport e) {
        exception = e;
        LOG.warn(exception.getMessage(), exception);
      }
    } else {
      exception = objectToEncode.getException();
    }

    if (exception != null) {
      body.set(encodeException(exception));
    }

    return envDoc;
  }
  @Test
  public void shouldCreateValidNotification() throws XmlException, IOException, OXFException {
    String sesUrl = "http://ses.host";
    String dialect = "http://my-funny/dialect";
    ParameterContainer parameters = new ParameterContainer();
    parameters.addParameterShell(SESRequestBuilder_00.NOTIFY_SES_URL, sesUrl);
    parameters.addParameterShell(SESRequestBuilder_00.NOTIFY_TOPIC_DIALECT, dialect);
    parameters.addParameterShell(SESRequestBuilder_00.NOTIFY_TOPIC, "<start>topic</start>");
    parameters.addParameterShell(SESRequestBuilder_00.NOTIFY_XML_MESSAGE, readMessage());

    SESRequestBuilder_00 request = new SESRequestBuilder_00();
    String asText = request.buildNotifyRequest(parameters);
    EnvelopeDocument envelope = EnvelopeDocument.Factory.parse(asText);

    XMLBeansParser.registerLaxValidationCase(SASamplingPointCase.getInstance());
    Collection<XmlError> errors = XMLBeansParser.validate(envelope);
    Assert.assertTrue("Notification is not valid: " + errors, errors.isEmpty());

    XmlCursor bodyCur = envelope.getEnvelope().getBody().newCursor();
    bodyCur.toFirstChild();
    Assert.assertTrue(bodyCur.getObject() instanceof Notify);
    XmlCursor tmpCur =
        ((Notify) bodyCur.getObject()).getNotificationMessageArray()[0].getMessage().newCursor();
    tmpCur.toFirstChild();
    Assert.assertTrue(tmpCur.getObject() instanceof ObservationType);

    Assert.assertTrue(
        ((Notify) bodyCur.getObject())
            .getNotificationMessageArray()[0]
            .getTopic()
            .getDialect()
            .trim()
            .equals(dialect));

    tmpCur = ((Notify) bodyCur.getObject()).getNotificationMessageArray()[0].getTopic().newCursor();

    Assert.assertTrue(tmpCur.getObject().xmlText().contains("<start>"));
  }
  @Test
  public void shouldCreateValidLevel1SubscribeRequest() throws OXFException, XmlException {
    String consumer = "http://*****:*****@xlink:href='urn:ogc:object:procedure:CITE:WeatherService:LGA'";

    ParameterContainer parameter = new ParameterContainer();
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_SES_URL, host);
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_CONSUMER_REFERENCE_ADDRESS, consumer);
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_FILTER_TOPIC, topicString);
    parameter.addParameterShell(
        ISESRequestBuilder.SUBSCRIBE_FILTER_MESSAGE_CONTENT_DIALECT, dialect);
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_FILTER_MESSAGE_CONTENT, xpath);

    SESRequestBuilder_00 request = new SESRequestBuilder_00();
    String asText = request.buildSubscribeRequest(parameter);
    EnvelopeDocument envelope = EnvelopeDocument.Factory.parse(asText);

    Collection<XmlError> errors = XMLBeansParser.validate(envelope);
    Assert.assertTrue("SubscribeRequest is not valid: " + errors, errors.isEmpty());

    Body body = envelope.getEnvelope().getBody();
    XmlCursor cur = body.newCursor();
    cur.toFirstChild();
    XmlObject subscribeRequestObject = cur.getObject();
    cur.dispose();

    if (!(subscribeRequestObject instanceof Subscribe))
      throw new IllegalStateException("Body does not contain a Subscribe request.");

    Subscribe subscribeRequest = (Subscribe) subscribeRequestObject;
    Assert.assertTrue(
        subscribeRequest
            .getConsumerReference()
            .getAddress()
            .getStringValue()
            .trim()
            .equals(consumer.trim()));

    cur = subscribeRequest.getFilter().newCursor();
    cur.toFirstChild();
    do {
      XmlObject filterElement = cur.getObject();
      if (filterElement instanceof QueryExpressionType) {
        QueryExpressionType qet = (QueryExpressionType) filterElement;
        Assert.assertTrue(qet.getDialect().trim().equals(dialect.trim()));
        XmlCursor tmpCur = qet.newCursor();
        Assert.assertTrue(tmpCur.toFirstContentToken() == TokenType.TEXT);
        Assert.assertTrue(tmpCur.getChars().trim().equals(xpath.trim()));
      } else if (filterElement instanceof TopicExpressionType) {
        TopicExpressionType tet = (TopicExpressionType) filterElement;
        Assert.assertTrue(
            tet.getDialect().trim().equals(SESRequestBuilder_00.DEFAULT_TOPIC_DIALECT));
        XmlCursor tmpCur = tet.newCursor();
        Assert.assertTrue(tmpCur.toFirstContentToken() == TokenType.TEXT);
        Assert.assertTrue(tmpCur.getChars().trim().equals(topicString.trim()));
      }
    } while (cur.toNextSibling());
  }