Program: 3940
Project: 3940
Buggy source:
/** 
 * Executes this instance.
 * @return the autodiscover response
 * @throws ServiceLocalException the service local exception
 * @throws Exception             the exception
 */
protected AutodiscoverResponse internalExecute() throws ServiceLocalException, Exception {
  this.validate();
  HttpWebRequest request=null;
  try {
    request=this.service.prepareHttpWebRequestForUrl(this.url);
    this.service.traceHttpRequestHeaders(TraceFlags.AutodiscoverRequestHttpHeaders,request);
    boolean needSignature=this.getService().getCredentials() != null && this.getService().getCredentials().isNeedSignature();
    boolean needTrace=this.getService().isTraceEnabledFor(TraceFlags.AutodiscoverRequest);
    OutputStream urlOutStream=request.getOutputStream();
    ByteArrayOutputStream memoryStream=new ByteArrayOutputStream();
    EwsServiceXmlWriter writer=new EwsServiceXmlWriter(this.getService(),memoryStream);
    writer.setRequireWSSecurityUtilityNamespace(needSignature);
    this.writeSoapRequest(this.url,writer);
    if (needSignature) {
      this.service.getCredentials().sign(memoryStream);
    }
    if (needTrace) {
      memoryStream.flush();
      this.service.traceXml(TraceFlags.AutodiscoverRequest,memoryStream);
    }
    memoryStream.writeTo(urlOutStream);
    urlOutStream.flush();
    urlOutStream.close();
    memoryStream.close();
    request.executeRequest();
    request.getResponseCode();
    if (AutodiscoverRequest.isRedirectionResponse(request)) {
      AutodiscoverResponse response=this.createRedirectionResponse(request);
      if (response != null) {
        return response;
      }
 else {
        throw new ServiceRemoteException(Strings.InvalidRedirectionResponseReturned);
      }
    }
    memoryStream=new ByteArrayOutputStream();
    InputStream serviceResponseStream=request.getInputStream();
    while (true) {
      int data=serviceResponseStream.read();
      if (-1 == data) {
        break;
      }
 else {
        memoryStream.write(data);
      }
    }
    memoryStream.flush();
    serviceResponseStream.close();
    if (this.service.isTraceEnabled()) {
      this.service.traceResponse(request,memoryStream);
    }
    ByteArrayInputStream memoryStreamIn=new ByteArrayInputStream(memoryStream.toByteArray());
    EwsXmlReader ewsXmlReader=new EwsXmlReader(memoryStreamIn);
    ewsXmlReader.read();
    if (ewsXmlReader.getNodeType().getNodeType() == XmlNodeType.START_DOCUMENT) {
      ewsXmlReader.readStartElement(XmlNamespace.Soap,XmlElementNames.SOAPEnvelopeElementName);
    }
 else     if ((ewsXmlReader.getNodeType().getNodeType() != XmlNodeType.START_ELEMENT) || (!ewsXmlReader.getLocalName().equals(XmlElementNames.SOAPEnvelopeElementName)) || (!ewsXmlReader.getNamespaceUri().equals(EwsUtilities.getNamespaceUri(XmlNamespace.Soap)))) {
      throw new ServiceXmlDeserializationException(Strings.InvalidAutodiscoverServiceResponse);
    }
    this.readSoapHeaders(ewsXmlReader);
    AutodiscoverResponse response=this.readSoapBody(ewsXmlReader);
    ewsXmlReader.readEndElement(XmlNamespace.Soap,XmlElementNames.SOAPEnvelopeElementName);
    if (response.getErrorCode() == AutodiscoverErrorCode.NoError) {
      return response;
    }
 else {
      throw new AutodiscoverResponseException(response.getErrorCode(),response.getErrorMessage());
    }
  }
 catch (  XMLStreamException ex) {
    this.service.traceMessage(TraceFlags.AutodiscoverConfiguration,String.format("String_Node_Str",ex.getMessage()));
    throw new ServiceRequestException(String.format(Strings.ServiceRequestFailed,ex.getMessage()),ex);
  }
catch (  IOException ex) {
    this.service.traceMessage(TraceFlags.AutodiscoverConfiguration,String.format("String_Node_Str",ex.getMessage()));
    throw new ServiceRequestException(String.format(Strings.ServiceRequestFailed,ex.getMessage()),ex);
  }
catch (  Exception ex) {
    if (null != request && request.getResponseCode() == 7) {
      if (AutodiscoverRequest.isRedirectionResponse(request)) {
        this.service.processHttpResponseHeaders(TraceFlags.AutodiscoverResponseHttpHeaders,request);
        AutodiscoverResponse response=this.createRedirectionResponse(request);
        if (response != null) {
          return response;
        }
      }
 else {
        this.processWebException(ex,request);
      }
    }
    throw new ServiceRequestException(String.format(Strings.ServiceRequestFailed,ex.getMessage()),ex);
  }
 finally {
    try {
      if (request != null) {
        request.close();
      }
    }
 catch (    Exception e) {
    }
  }
}

Fixed version (ground truth):
/** 
 * Executes this instance.
 * @return the autodiscover response
 * @throws ServiceLocalException the service local exception
 * @throws Exception             the exception
 */
protected AutodiscoverResponse internalExecute() throws ServiceLocalException, Exception {
  this.validate();
  HttpWebRequest request=null;
  try {
    request=this.service.prepareHttpWebRequestForUrl(this.url);
    this.service.traceHttpRequestHeaders(TraceFlags.AutodiscoverRequestHttpHeaders,request);
    boolean needSignature=this.getService().getCredentials() != null && this.getService().getCredentials().isNeedSignature();
    boolean needTrace=this.getService().isTraceEnabledFor(TraceFlags.AutodiscoverRequest);
    OutputStream urlOutStream=request.getOutputStream();
    ByteArrayOutputStream memoryStream=new ByteArrayOutputStream();
    EwsServiceXmlWriter writer=new EwsServiceXmlWriter(this.getService(),memoryStream);
    writer.setRequireWSSecurityUtilityNamespace(needSignature);
    this.writeSoapRequest(this.url,writer);
    if (needSignature) {
      this.service.getCredentials().sign(memoryStream);
    }
    if (needTrace) {
      memoryStream.flush();
      this.service.traceXml(TraceFlags.AutodiscoverRequest,memoryStream);
    }
    memoryStream.writeTo(urlOutStream);
    urlOutStream.flush();
    urlOutStream.close();
    memoryStream.close();
    request.executeRequest();
    request.getResponseCode();
    if (AutodiscoverRequest.isRedirectionResponse(request)) {
      AutodiscoverResponse response=this.createRedirectionResponse(request);
      if (response != null) {
        return response;
      }
 else {
        throw new ServiceRemoteException("String_Node_Str");
      }
    }
    memoryStream=new ByteArrayOutputStream();
    InputStream serviceResponseStream=request.getInputStream();
    while (true) {
      int data=serviceResponseStream.read();
      if (-1 == data) {
        break;
      }
 else {
        memoryStream.write(data);
      }
    }
    memoryStream.flush();
    serviceResponseStream.close();
    if (this.service.isTraceEnabled()) {
      this.service.traceResponse(request,memoryStream);
    }
    ByteArrayInputStream memoryStreamIn=new ByteArrayInputStream(memoryStream.toByteArray());
    EwsXmlReader ewsXmlReader=new EwsXmlReader(memoryStreamIn);
    ewsXmlReader.read();
    if (ewsXmlReader.getNodeType().getNodeType() == XmlNodeType.START_DOCUMENT) {
      ewsXmlReader.readStartElement(XmlNamespace.Soap,XmlElementNames.SOAPEnvelopeElementName);
    }
 else     if ((ewsXmlReader.getNodeType().getNodeType() != XmlNodeType.START_ELEMENT) || (!ewsXmlReader.getLocalName().equals(XmlElementNames.SOAPEnvelopeElementName)) || (!ewsXmlReader.getNamespaceUri().equals(EwsUtilities.getNamespaceUri(XmlNamespace.Soap)))) {
      throw new ServiceXmlDeserializationException("String_Node_Str");
    }
    this.readSoapHeaders(ewsXmlReader);
    AutodiscoverResponse response=this.readSoapBody(ewsXmlReader);
    ewsXmlReader.readEndElement(XmlNamespace.Soap,XmlElementNames.SOAPEnvelopeElementName);
    if (response.getErrorCode() == AutodiscoverErrorCode.NoError) {
      return response;
    }
 else {
      throw new AutodiscoverResponseException(response.getErrorCode(),response.getErrorMessage());
    }
  }
 catch (  XMLStreamException ex) {
    this.service.traceMessage(TraceFlags.AutodiscoverConfiguration,String.format("String_Node_Str",ex.getMessage()));
    throw new ServiceRequestException(String.format("String_Node_Str",ex.getMessage()),ex);
  }
catch (  IOException ex) {
    this.service.traceMessage(TraceFlags.AutodiscoverConfiguration,String.format("String_Node_Str",ex.getMessage()));
    throw new ServiceRequestException(String.format("String_Node_Str",ex.getMessage()),ex);
  }
catch (  Exception ex) {
    if (null != request && request.getResponseCode() == 7) {
      if (AutodiscoverRequest.isRedirectionResponse(request)) {
        this.service.processHttpResponseHeaders(TraceFlags.AutodiscoverResponseHttpHeaders,request);
        AutodiscoverResponse response=this.createRedirectionResponse(request);
        if (response != null) {
          return response;
        }
      }
 else {
        this.processWebException(ex,request);
      }
    }
    throw new ServiceRequestException(String.format("String_Node_Str",ex.getMessage()),ex);
  }
 finally {
    try {
      if (request != null) {
        request.close();
      }
    }
 catch (    Exception e) {
    }
  }
}