Program: 51995
Project: 51995
Buggy source:
/** 
 * Delete bitstream from item bundle.
 * @param itemId Id of item in DSpace.
 * @param headers If you want to access to item under logged user into context. In headers must be set header "rest-dspace-token" with passed token from login method.
 * @param bitstreamId Id of bitstream, which will be deleted from bundle.
 * @return Return status code OK(200) if is all ok. NOT_FOUND(404) if itemor bitstream was not found. UNAUTHORIZED(401) if user is not allowed to delete bitstream.
 * @throws WebApplicationException It is thrown, when: Was problem with edditting database, SQLException. Or problem with authorization to item, bundle or bitstream, AuthorizeException. When was problem with deleting file IOException. Or problem with creating context of DSpace, ContextException.
 */
@DELETE @Path("String_Node_Str") public Response deleteItemBitstream(@PathParam("String_Node_Str") String itemId,@PathParam("String_Node_Str") String bitstreamId,@QueryParam("String_Node_Str") String user_ip,@QueryParam("String_Node_Str") String user_agent,@QueryParam("String_Node_Str") String xforwardedfor,@Context HttpHeaders headers,@Context HttpServletRequest request) throws WebApplicationException {
  log.info("String_Node_Str" + itemId + "String_Node_Str");
  org.dspace.core.Context context=null;
  try {
    context=createContext(getUser(headers));
    org.dspace.content.Item item=findItem(context,itemId,org.dspace.core.Constants.WRITE);
    org.dspace.content.Bitstream bitstream=bitstreamService.findByIdOrLegacyId(context,bitstreamId);
    if (bitstream == null) {
      context.abort();
      log.warn("String_Node_Str" + bitstreamId + "String_Node_Str");
      return Response.status(Status.NOT_FOUND).build();
    }
 else     if (!authorizeService.authorizeActionBoolean(context,bitstream,org.dspace.core.Constants.DELETE)) {
      context.abort();
      log.error("String_Node_Str" + getUser(headers).getEmail() + "String_Node_Str"+ bitstreamId+ "String_Node_Str");
      return Response.status(Status.UNAUTHORIZED).build();
    }
    writeStats(item,UsageEvent.Action.UPDATE,user_ip,user_agent,xforwardedfor,headers,request,context);
    writeStats(bitstream,UsageEvent.Action.REMOVE,user_ip,user_agent,xforwardedfor,headers,request,context);
    log.trace("String_Node_Str");
    Iterator<Bundle> bundleIterator=item.getBundles().iterator();
    while (bundleIterator.hasNext()) {
      Bundle bundle=bundleIterator.next();
      Iterator<BundleBitstream> bundleBitstreamIterator=bundle.getBitstreams().iterator();
      while (bundleBitstreamIterator.hasNext()) {
        BundleBitstream bundleBitstream=bundleBitstreamIterator.next();
        if (bundleBitstream.getBitstream().getID() == bitstream.getID()) {
          bundleService.removeBitstream(context,bundle,bitstream);
        }
      }
    }
    context.complete();
  }
 catch (  SQLException e) {
    processException("String_Node_Str" + bitstreamId + "String_Node_Str"+ e,context);
  }
catch (  AuthorizeException e) {
    processException("String_Node_Str" + bitstreamId + "String_Node_Str"+ e,context);
  }
catch (  IOException e) {
    processException("String_Node_Str" + bitstreamId + "String_Node_Str"+ e,context);
  }
catch (  ContextException e) {
    processException("String_Node_Str" + bitstreamId + "String_Node_Str"+ e.getMessage(),context);
  }
 finally {
    processFinally(context);
  }
  log.info("String_Node_Str" + bitstreamId + "String_Node_Str"+ itemId+ "String_Node_Str");
  return Response.status(Status.OK).build();
}

Fixed version (ground truth):
/** 
 * Delete bitstream from item bundle.
 * @param itemId Id of item in DSpace.
 * @param headers If you want to access to item under logged user into context. In headers must be set header "rest-dspace-token" with passed token from login method.
 * @param bitstreamId Id of bitstream, which will be deleted from bundle.
 * @return Return status code OK(200) if is all ok. NOT_FOUND(404) if itemor bitstream was not found. UNAUTHORIZED(401) if user is not allowed to delete bitstream.
 * @throws WebApplicationException It is thrown, when: Was problem with edditting database, SQLException. Or problem with authorization to item, bundle or bitstream, AuthorizeException. When was problem with deleting file IOException. Or problem with creating context of DSpace, ContextException.
 */
@DELETE @Path("String_Node_Str") public Response deleteItemBitstream(@PathParam("String_Node_Str") String itemId,@PathParam("String_Node_Str") String bitstreamId,@QueryParam("String_Node_Str") String user_ip,@QueryParam("String_Node_Str") String user_agent,@QueryParam("String_Node_Str") String xforwardedfor,@Context HttpHeaders headers,@Context HttpServletRequest request) throws WebApplicationException {
  log.info("String_Node_Str" + itemId + "String_Node_Str");
  org.dspace.core.Context context=null;
  try {
    context=createContext(getUser(headers));
    org.dspace.content.Item item=findItem(context,itemId,org.dspace.core.Constants.WRITE);
    org.dspace.content.Bitstream bitstream=bitstreamService.findByIdOrLegacyId(context,bitstreamId);
    if (bitstream == null) {
      context.abort();
      log.warn("String_Node_Str" + bitstreamId + "String_Node_Str");
      return Response.status(Status.NOT_FOUND).build();
    }
 else     if (!authorizeService.authorizeActionBoolean(context,bitstream,org.dspace.core.Constants.DELETE)) {
      context.abort();
      log.error("String_Node_Str" + getUser(headers).getEmail() + "String_Node_Str"+ bitstreamId+ "String_Node_Str");
      return Response.status(Status.UNAUTHORIZED).build();
    }
    writeStats(item,UsageEvent.Action.UPDATE,user_ip,user_agent,xforwardedfor,headers,request,context);
    writeStats(bitstream,UsageEvent.Action.REMOVE,user_ip,user_agent,xforwardedfor,headers,request,context);
    log.trace("String_Node_Str");
    Iterator<Bundle> bundleIterator=item.getBundles().iterator();
    while (bundleIterator.hasNext()) {
      Bundle bundle=bundleIterator.next();
      Iterator<BundleBitstream> bundleBitstreamIterator=bundle.getBitstreams().iterator();
      while (bundleBitstreamIterator.hasNext()) {
        BundleBitstream bundleBitstream=bundleBitstreamIterator.next();
        if (bundleBitstream.getBitstream().getID().equals(bitstream.getID())) {
          bundleBitstreamIterator.remove();
          bundleService.removeBitstream(context,bundle,bitstream);
        }
      }
    }
    context.complete();
  }
 catch (  SQLException e) {
    processException("String_Node_Str" + bitstreamId + "String_Node_Str"+ e,context);
  }
catch (  AuthorizeException e) {
    processException("String_Node_Str" + bitstreamId + "String_Node_Str"+ e,context);
  }
catch (  IOException e) {
    processException("String_Node_Str" + bitstreamId + "String_Node_Str"+ e,context);
  }
catch (  ContextException e) {
    processException("String_Node_Str" + bitstreamId + "String_Node_Str"+ e.getMessage(),context);
  }
 finally {
    processFinally(context);
  }
  log.info("String_Node_Str" + bitstreamId + "String_Node_Str"+ itemId+ "String_Node_Str");
  return Response.status(Status.OK).build();
}