@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;
  }
  private XmlObject encodeException(OwsExceptionReport exception) {
    OwsExceptionReport targetException;
    if (exception == null) {
      targetException = new NoApplicableCodeException().withMessage("Unexpected error");
    } else {
      targetException = exception;
    }

    FaultDocument faultDoc = FaultDocument.Factory.newInstance();
    Fault fault = faultDoc.addNewFault();
    Faultcode code = fault.addNewCode();
    code.setValue(
        new QName(
            EnvelopeDocument.type.getDocumentElementName().getNamespaceURI(),
            determineCauser(targetException.getStatus())));

    if (targetException instanceof SoapFault) {
      fault.addNewReason().addNewText().setStringValue(((SoapFault) targetException).getReason());
    }

    Detail detail = fault.addNewDetail();
    try {
      ExceptionEncoderKey excKey = new ExceptionEncoderKey(MediaTypes.APPLICATION_SOAP_XML);
      Encoder<XmlObject, OwsExceptionReport> encoder = this.encoderRepository.getEncoder(excKey);
      if (encoder == null) {
        encoder = new OwsExceptionReportEncoder();
      }
      XmlObject owsXml = encoder.encode(targetException);
      detail.set(owsXml);
    } catch (EncodingException ex) {
      LOG.warn("Error encoding OwsExceptionReport", ex);
    }

    return faultDoc;
  }