Program: 52041
Project: 52041
Buggy source:
/** 
 * Update bitstream metadata. It replace everything on targeted bitstream. It can throws WebApplicationException caused by two exceptions: SQLException, if there was problem with database. AuthorizeException if there was problem with authorization to edit bitstream metadata.
 * @param bitstreamId Id of bistream, wich will be updated.
 * @param bitstream Bitstream with will be placed. It muset have filled user creditials.
 * @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.
 * @return Return response codes: OK(200), NOT_FOUND(404) if bitstream doesnot exist and UNAUTHORIZED(401) if user is not allowed to write to bitstream.
 * @throws WebApplicationException It can be thrown by: Error in reading from database. Or creating context or with authorization to bitstream.
 */
@PUT @Path("String_Node_Str") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) public Response updateBitstream(@PathParam("String_Node_Str") Integer bitstreamId,Bitstream bitstream,@QueryParam("String_Node_Str") String user_ip,@QueryParam("String_Node_Str") String user_agent,@QueryParam("String_Node_Str") String xforwarderfor,@Context HttpHeaders headers,@Context HttpServletRequest request) throws WebApplicationException {
  log.info("String_Node_Str" + bitstreamId + "String_Node_Str");
  org.dspace.core.Context context=null;
  try {
    context=createContext(getUser(headers));
    org.dspace.content.Bitstream dspaceBitstream=findBitstream(context,bitstreamId,org.dspace.core.Constants.WRITE);
    writeStats(dspaceBitstream,UsageEvent.Action.UPDATE,user_ip,user_agent,xforwarderfor,headers,request);
    log.trace("String_Node_Str");
    dspaceBitstream.setDescription(bitstream.getDescription());
    if (getMimeType(bitstream.getName()) == null) {
      dspaceBitstream.setFormat(BitstreamFormat.findUnknown(context));
    }
 else {
      dspaceBitstream.setFormat(BitstreamFormat.findByMIMEType(context,getMimeType(bitstream.getName())));
    }
    dspaceBitstream.setName(bitstream.getName());
    Integer sequenceId=bitstream.getSequenceId();
    if (sequenceId != null && sequenceId.intValue() != -1) {
      dspaceBitstream.setSequenceID(sequenceId);
    }
    dspaceBitstream.update();
    if (bitstream.getPolicies() != null) {
      Bundle[] bundles=dspaceBitstream.getBundles();
      ResourcePolicy[] policies=bitstream.getPolicies();
      for (      Bundle bundle : bundles) {
        List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies=bundle.getBitstreamPolicies();
        List<org.dspace.authorize.ResourcePolicy> policiesToRemove=new ArrayList<org.dspace.authorize.ResourcePolicy>();
        for (        org.dspace.authorize.ResourcePolicy policy : bitstreamsPolicies) {
          if (policy.getResourceID() == dspaceBitstream.getID()) {
            policiesToRemove.add(policy);
          }
        }
        for (        org.dspace.authorize.ResourcePolicy policy : policiesToRemove) {
          bitstreamsPolicies.remove(policy);
        }
        for (        ResourcePolicy policy : policies) {
          org.dspace.authorize.ResourcePolicy dspacePolicy=org.dspace.authorize.ResourcePolicy.create(context);
          dspacePolicy.setAction(policy.getActionInt());
          dspacePolicy.setGroup(Group.find(context,policy.getGroupId()));
          dspacePolicy.setResourceID(dspaceBitstream.getID());
          dspacePolicy.setResource(dspaceBitstream);
          dspacePolicy.setResourceType(org.dspace.core.Constants.BITSTREAM);
          dspacePolicy.setStartDate(policy.getStartDate());
          dspacePolicy.setEndDate(policy.getEndDate());
          dspacePolicy.setRpDescription(policy.getRpDescription());
          dspacePolicy.setRpName(policy.getRpName());
          dspacePolicy.update();
          bitstreamsPolicies.add(dspacePolicy);
        }
        bundle.replaceAllBitstreamPolicies(bitstreamsPolicies);
        bundle.update();
      }
    }
    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 (  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");
  return Response.ok().build();
}

Fixed version (ground truth):
/** 
 * Update bitstream metadata. It replace everything on targeted bitstream. It can throws WebApplicationException caused by two exceptions: SQLException, if there was problem with database. AuthorizeException if there was problem with authorization to edit bitstream metadata.
 * @param bitstreamId Id of bistream, wich will be updated.
 * @param bitstream Bitstream with will be placed. It muset have filled user creditials.
 * @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.
 * @return Return response codes: OK(200), NOT_FOUND(404) if bitstream doesnot exist and UNAUTHORIZED(401) if user is not allowed to write to bitstream.
 * @throws WebApplicationException It can be thrown by: Error in reading from database. Or creating context or with authorization to bitstream.
 */
@PUT @Path("String_Node_Str") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) public Response updateBitstream(@PathParam("String_Node_Str") Integer bitstreamId,Bitstream bitstream,@QueryParam("String_Node_Str") String user_ip,@QueryParam("String_Node_Str") String user_agent,@QueryParam("String_Node_Str") String xforwarderfor,@Context HttpHeaders headers,@Context HttpServletRequest request) throws WebApplicationException {
  log.info("String_Node_Str" + bitstreamId + "String_Node_Str");
  org.dspace.core.Context context=null;
  try {
    context=createContext(getUser(headers));
    org.dspace.content.Bitstream dspaceBitstream=findBitstream(context,bitstreamId,org.dspace.core.Constants.WRITE);
    writeStats(dspaceBitstream,UsageEvent.Action.UPDATE,user_ip,user_agent,xforwarderfor,headers,request,context);
    log.trace("String_Node_Str");
    dspaceBitstream.setDescription(bitstream.getDescription());
    if (getMimeType(bitstream.getName()) == null) {
      dspaceBitstream.setFormat(BitstreamFormat.findUnknown(context));
    }
 else {
      dspaceBitstream.setFormat(BitstreamFormat.findByMIMEType(context,getMimeType(bitstream.getName())));
    }
    dspaceBitstream.setName(bitstream.getName());
    Integer sequenceId=bitstream.getSequenceId();
    if (sequenceId != null && sequenceId.intValue() != -1) {
      dspaceBitstream.setSequenceID(sequenceId);
    }
    dspaceBitstream.update();
    if (bitstream.getPolicies() != null) {
      Bundle[] bundles=dspaceBitstream.getBundles();
      ResourcePolicy[] policies=bitstream.getPolicies();
      for (      Bundle bundle : bundles) {
        List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies=bundle.getBitstreamPolicies();
        List<org.dspace.authorize.ResourcePolicy> policiesToRemove=new ArrayList<org.dspace.authorize.ResourcePolicy>();
        for (        org.dspace.authorize.ResourcePolicy policy : bitstreamsPolicies) {
          if (policy.getResourceID() == dspaceBitstream.getID()) {
            policiesToRemove.add(policy);
          }
        }
        for (        org.dspace.authorize.ResourcePolicy policy : policiesToRemove) {
          bitstreamsPolicies.remove(policy);
        }
        for (        ResourcePolicy policy : policies) {
          org.dspace.authorize.ResourcePolicy dspacePolicy=org.dspace.authorize.ResourcePolicy.create(context);
          dspacePolicy.setAction(policy.getActionInt());
          dspacePolicy.setGroup(Group.find(context,policy.getGroupId()));
          dspacePolicy.setResourceID(dspaceBitstream.getID());
          dspacePolicy.setResource(dspaceBitstream);
          dspacePolicy.setResourceType(org.dspace.core.Constants.BITSTREAM);
          dspacePolicy.setStartDate(policy.getStartDate());
          dspacePolicy.setEndDate(policy.getEndDate());
          dspacePolicy.setRpDescription(policy.getRpDescription());
          dspacePolicy.setRpName(policy.getRpName());
          dspacePolicy.update();
          bitstreamsPolicies.add(dspacePolicy);
        }
        bundle.replaceAllBitstreamPolicies(bitstreamsPolicies);
        bundle.update();
      }
    }
    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 (  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");
  return Response.ok().build();
}