Program: 51994
Project: 51994
Buggy source:
/** 
 * Create bitstream in item.
 * @param itemId Id of item in DSpace.
 * @param inputStream Data of bitstream in inputStream.
 * @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 Returns bitstream with status code OK(200). If id of item isinvalid , it returns status code NOT_FOUND(404). If user is not allowed to write to item, UNAUTHORIZED(401).
 * @throws WebApplicationException It is thrown by these exceptions: SQLException, when was problem with reading/writing from/to database. AuthorizeException, when was problem with authorization to item and add bitstream to item. IOException, when was problem with creating file or reading from inpustream. ContextException. When was problem with creating context of DSpace.
 */
@POST @Path("String_Node_Str") public Bitstream addItemBitstream(@PathParam("String_Node_Str") String itemId,InputStream inputStream,@QueryParam("String_Node_Str") String name,@QueryParam("String_Node_Str") String description,@QueryParam("String_Node_Str") String groupId,@QueryParam("String_Node_Str") Integer year,@QueryParam("String_Node_Str") Integer month,@QueryParam("String_Node_Str") Integer day,@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;
  Bitstream bitstream=null;
  try {
    context=createContext(getUser(headers));
    org.dspace.content.Item dspaceItem=findItem(context,itemId,org.dspace.core.Constants.WRITE);
    writeStats(dspaceItem,UsageEvent.Action.UPDATE,user_ip,user_agent,xforwardedfor,headers,request,context);
    log.trace("String_Node_Str");
    org.dspace.content.Bundle bundle=null;
    org.dspace.content.Bitstream dspaceBitstream=null;
    List<Bundle> bundles=itemService.getBundles(dspaceItem,org.dspace.core.Constants.CONTENT_BUNDLE_NAME);
    if (bundles != null && bundles.size() != 0) {
      bundle=bundles.get(0);
    }
    if (bundle == null) {
      log.trace("String_Node_Str");
      dspaceBitstream=itemService.createSingleBitstream(context,inputStream,dspaceItem);
    }
 else {
      log.trace("String_Node_Str");
      dspaceBitstream=bitstreamService.create(context,bundle,inputStream);
    }
    dspaceBitstream.setSource(context,"String_Node_Str");
    if (name != null) {
      if (BitstreamResource.getMimeType(name) == null) {
        dspaceBitstream.setFormat(context,bitstreamFormatService.findUnknown(context));
      }
 else {
        bitstreamService.setFormat(context,dspaceBitstream,bitstreamFormatService.findByMIMEType(context,BitstreamResource.getMimeType(name)));
      }
      dspaceBitstream.setName(context,name);
    }
    if (description != null) {
      dspaceBitstream.setDescription(context,description);
    }
    if (groupId != null) {
      List<BundleBitstream> bundleBitstreams=dspaceBitstream.getBundles();
      for (      Bundle dspaceBundle : bundles) {
        List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies=bundleService.getBitstreamPolicies(context,dspaceBundle);
        List<org.dspace.authorize.ResourcePolicy> policiesToRemove=new ArrayList<org.dspace.authorize.ResourcePolicy>();
        for (        org.dspace.authorize.ResourcePolicy policy : bitstreamsPolicies) {
          if (policy.getdSpaceObject().getID() == dspaceBitstream.getID()) {
            policiesToRemove.add(policy);
          }
        }
        for (        org.dspace.authorize.ResourcePolicy policy : policiesToRemove) {
          bitstreamsPolicies.remove(policy);
        }
        org.dspace.authorize.ResourcePolicy dspacePolicy=resourcePolicyService.create(context);
        dspacePolicy.setAction(org.dspace.core.Constants.READ);
        dspacePolicy.setGroup(groupService.findByIdOrLegacyId(context,groupId));
        dspacePolicy.setdSpaceObject(dspaceBitstream);
        if ((year != null) || (month != null) || (day != null)) {
          Date date=new Date();
          if (year != null) {
            date.setYear(year - 1900);
          }
          if (month != null) {
            date.setMonth(month - 1);
          }
          if (day != null) {
            date.setDate(day);
          }
          date.setHours(0);
          date.setMinutes(0);
          date.setSeconds(0);
          dspacePolicy.setStartDate(date);
        }
        resourcePolicyService.update(context,dspacePolicy);
        bitstreamService.updateLastModified(context,dspaceBitstream);
      }
    }
    dspaceBitstream=bitstreamService.find(context,dspaceBitstream.getID());
    bitstream=new Bitstream(dspaceBitstream,"String_Node_Str",context);
    context.complete();
  }
 catch (  SQLException e) {
    processException("String_Node_Str" + itemId + "String_Node_Str"+ e,context);
  }
catch (  AuthorizeException e) {
    processException("String_Node_Str" + itemId + "String_Node_Str"+ e,context);
  }
catch (  IOException e) {
    processException("String_Node_Str" + itemId + "String_Node_Str"+ e,context);
  }
catch (  ContextException e) {
    processException("String_Node_Str" + itemId + "String_Node_Str"+ e.getMessage(),context);
  }
 finally {
    processFinally(context);
  }
  log.info("String_Node_Str" + bitstream.getId() + "String_Node_Str"+ itemId+ "String_Node_Str");
  return bitstream;
}

Fixed version (ground truth):
/** 
 * Create bitstream in item.
 * @param itemId Id of item in DSpace.
 * @param inputStream Data of bitstream in inputStream.
 * @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 Returns bitstream with status code OK(200). If id of item isinvalid , it returns status code NOT_FOUND(404). If user is not allowed to write to item, UNAUTHORIZED(401).
 * @throws WebApplicationException It is thrown by these exceptions: SQLException, when was problem with reading/writing from/to database. AuthorizeException, when was problem with authorization to item and add bitstream to item. IOException, when was problem with creating file or reading from inpustream. ContextException. When was problem with creating context of DSpace.
 */
@POST @Path("String_Node_Str") public Bitstream addItemBitstream(@PathParam("String_Node_Str") String itemId,InputStream inputStream,@QueryParam("String_Node_Str") String name,@QueryParam("String_Node_Str") String description,@QueryParam("String_Node_Str") String groupId,@QueryParam("String_Node_Str") Integer year,@QueryParam("String_Node_Str") Integer month,@QueryParam("String_Node_Str") Integer day,@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;
  Bitstream bitstream=null;
  try {
    context=createContext(getUser(headers));
    org.dspace.content.Item dspaceItem=findItem(context,itemId,org.dspace.core.Constants.WRITE);
    writeStats(dspaceItem,UsageEvent.Action.UPDATE,user_ip,user_agent,xforwardedfor,headers,request,context);
    log.trace("String_Node_Str");
    org.dspace.content.Bundle bundle=null;
    org.dspace.content.Bitstream dspaceBitstream=null;
    List<Bundle> bundles=itemService.getBundles(dspaceItem,org.dspace.core.Constants.CONTENT_BUNDLE_NAME);
    if (bundles != null && bundles.size() != 0) {
      bundle=bundles.get(0);
    }
    if (bundle == null) {
      log.trace("String_Node_Str");
      dspaceBitstream=itemService.createSingleBitstream(context,inputStream,dspaceItem);
    }
 else {
      log.trace("String_Node_Str");
      dspaceBitstream=bitstreamService.create(context,bundle,inputStream);
    }
    dspaceBitstream.setSource(context,"String_Node_Str");
    if (name != null) {
      if (BitstreamResource.getMimeType(name) == null) {
        dspaceBitstream.setFormat(context,bitstreamFormatService.findUnknown(context));
      }
 else {
        bitstreamService.setFormat(context,dspaceBitstream,bitstreamFormatService.findByMIMEType(context,BitstreamResource.getMimeType(name)));
      }
      dspaceBitstream.setName(context,name);
    }
    if (description != null) {
      dspaceBitstream.setDescription(context,description);
    }
    if (groupId != null) {
      List<BundleBitstream> bundleBitstreams=dspaceBitstream.getBundles();
      for (      Bundle dspaceBundle : bundles) {
        List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies=bundleService.getBitstreamPolicies(context,dspaceBundle);
        List<org.dspace.authorize.ResourcePolicy> policiesToRemove=new ArrayList<org.dspace.authorize.ResourcePolicy>();
        for (        org.dspace.authorize.ResourcePolicy policy : bitstreamsPolicies) {
          if (policy.getdSpaceObject().getID().equals(dspaceBitstream.getID())) {
            policiesToRemove.add(policy);
          }
        }
        for (        org.dspace.authorize.ResourcePolicy policy : policiesToRemove) {
          bitstreamsPolicies.remove(policy);
        }
        org.dspace.authorize.ResourcePolicy dspacePolicy=resourcePolicyService.create(context);
        dspacePolicy.setAction(org.dspace.core.Constants.READ);
        dspacePolicy.setGroup(groupService.findByIdOrLegacyId(context,groupId));
        dspacePolicy.setdSpaceObject(dspaceBitstream);
        if ((year != null) || (month != null) || (day != null)) {
          Date date=new Date();
          if (year != null) {
            date.setYear(year - 1900);
          }
          if (month != null) {
            date.setMonth(month - 1);
          }
          if (day != null) {
            date.setDate(day);
          }
          date.setHours(0);
          date.setMinutes(0);
          date.setSeconds(0);
          dspacePolicy.setStartDate(date);
        }
        resourcePolicyService.update(context,dspacePolicy);
        bitstreamService.updateLastModified(context,dspaceBitstream);
      }
    }
    dspaceBitstream=bitstreamService.find(context,dspaceBitstream.getID());
    bitstream=new Bitstream(dspaceBitstream,"String_Node_Str",context);
    context.complete();
  }
 catch (  SQLException e) {
    processException("String_Node_Str" + itemId + "String_Node_Str"+ e,context);
  }
catch (  AuthorizeException e) {
    processException("String_Node_Str" + itemId + "String_Node_Str"+ e,context);
  }
catch (  IOException e) {
    processException("String_Node_Str" + itemId + "String_Node_Str"+ e,context);
  }
catch (  ContextException e) {
    processException("String_Node_Str" + itemId + "String_Node_Str"+ e.getMessage(),context);
  }
 finally {
    processFinally(context);
  }
  log.info("String_Node_Str" + bitstream.getId() + "String_Node_Str"+ itemId+ "String_Node_Str");
  return bitstream;
}