}
private void writeResponse(HttpCommand command, SoapOperationBinding binding, Object result, CougarException error,
ExecutionContextWithTokens context, long bytesRead) {
MediaType mediaType = MediaType.TEXT_XML_TYPE;
ByteCountingOutputStream out = null;
long bytesWritten = 0;
boolean logAccess = true;
try {
command.getResponse().setContentType(mediaType.toString());
out = new ByteCountingOutputStream(command.getResponse().getOutputStream());
SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope envelope = factory.createSOAPEnvelope();
SOAPHeader header = factory.createSOAPHeader(envelope);
writeHeaders(factory, header, command, context);
SOAPBody body = factory.createSOAPBody(envelope);
writeError(factory, binding, body, error);
writeBody(factory, binding, body, result);
envelope.serialize(out);
bytesWritten = out.getCount();
} catch (Exception e) {
CougarException ce = handleResponseWritingIOException(e, result.getClass());
if (ce.getResponseCode() == ResponseCode.CantWriteToSocket) {
// Log in the access log what's happened and end it all.