[DS-2903] Fix potential Nullpointer exceptions indicated by FindBugs

This commit is contained in:
KevinVdV
2015-11-20 10:03:13 +01:00
parent 440c26c861
commit 01c98d508f
18 changed files with 58 additions and 26 deletions

View File

@@ -1840,6 +1840,7 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
List<Collection> finalCollections = null;
if (theOwningCollection != null){
finalCollections = new ArrayList<>();
finalCollections.add(theOwningCollection);
finalCollections.addAll(collectionList);
}

View File

@@ -99,7 +99,11 @@ public class EPersonServiceImpl extends DSpaceObjectServiceImpl<EPerson> impleme
@Override
public List<EPerson> search(Context context, String query) throws SQLException {
if(StringUtils.isBlank(query)) query = null;
if(StringUtils.isBlank(query))
{
//If we don't have a query, just return everything.
return findAll(context, EPerson.EMAIL);
}
return search(context, query, -1, -1);
}
@@ -116,7 +120,10 @@ public class EPersonServiceImpl extends DSpaceObjectServiceImpl<EPerson> impleme
} catch(IllegalArgumentException e) {
MetadataField firstNameField = metadataFieldService.findByElement(context, "eperson", "firstname", null);
MetadataField lastNameField = metadataFieldService.findByElement(context, "eperson", "lastname", null);
if (StringUtils.isBlank(query)) query = null;
if (StringUtils.isBlank(query))
{
query = null;
}
return ePersonDAO.search(context, query, Arrays.asList(firstNameField, lastNameField), Arrays.asList(firstNameField, lastNameField), offset, limit);
}
}

View File

@@ -227,8 +227,12 @@ public class MetadataConverterPlugin implements ConverterPlugin
}
config.read(is, "file://" + mappingPath, FileUtils.guessLang(mappingPath));
try {
// Make sure that we have an input stream to avoid NullPointer
if(is != null)
{
is.close();
}
}
catch (IOException ex)
{
// nothing to do here.

View File

@@ -14,10 +14,7 @@ import gr.ekt.bte.core.Value;
import gr.ekt.bte.exceptions.MalformedSourceException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;
@@ -78,7 +75,7 @@ public abstract class NetworkSubmissionLookupDataLoader implements
: null;
String year = getSearchTerms().get("year") != null ? getSearchTerms()
.get("year").iterator().next()
: null;
: String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
int yearInt = Integer.parseInt(year);
results = search(null, title, authors, yearInt);
}

View File

@@ -361,7 +361,7 @@ public class XmlWorkflowServiceImpl implements XmlWorkflowService {
e.printStackTrace();
}
finally {
if((nextStep != null && nextActionConfig != null) || wfi.getItem().isArchived()){
if((nextStep != null && currentStep != null && nextActionConfig != null) || (wfi.getItem().isArchived() && currentStep != null)){
logWorkflowEvent(c, currentStep.getWorkflow().getID(), currentStep.getId(), currentActionConfig.getId(), wfi, user, nextStep, nextActionConfig);
}
}

View File

@@ -95,6 +95,12 @@ public class AssignOriginalSubmitterAction extends UserSelectionAction{
while(nextAction != null && !nextAction.requiresUI()){
nextAction = nextAction.getStep().getNextAction(nextAction);
}
if(nextAction == null)
{
//Should never occur, but just in case
log.error("Could not find next action for step with id: " + step.getId() + " to assign a submitter to. Aborting the action.");
throw new IllegalStateException();
}
createTaskForEPerson(c, wfi, step, nextAction, submitter);

View File

@@ -311,14 +311,16 @@ public class RequestItemServlet extends DSpaceServlet
{
Item item = requestItem.getItem();
String title = "";
String handle = "";
if (item != null) {
title = itemService.getMetadataFirstValue(item, "dc", "title", null, Item.ANY);
if (title == null) {
title = "";
}
handle = item.getHandle();
}
request.setAttribute("request-name", requestItem.getReqName());
request.setAttribute("handle", item.getHandle());
request.setAttribute("handle", handle);
request.setAttribute("title", title);
JSPManager.showJSP(request, response,

View File

@@ -217,11 +217,11 @@ public class JSPUploadStep extends JSPStep
size = bitstream.getSize();
url = request.getContextPath() + "/retrieve/" + bitstreamID
+ "/" + UIUtil.encodeBitstreamName(bitstreamName);
}
jsonResponse.addUploadFileStatus(bitstreamName, bitstreamID, size,
url, status);
response.getWriter().print(gson.toJson(jsonResponse));
response.flushBuffer();
}
return;
}

View File

@@ -143,11 +143,11 @@ public class JSPUploadWithEmbargoStep extends JSPUploadStep
size = bitstream.getSize();
url = request.getContextPath() + "/retrieve/" + bitstreamID
+ "/" + UIUtil.encodeBitstreamName(bitstreamName);
}
jsonResponse.addUploadFileStatus(bitstreamName, bitstreamID, size,
url, status);
response.getWriter().print(gson.toJson(jsonResponse));
response.flushBuffer();
}
return;
}

View File

@@ -10,13 +10,14 @@ package org.dspace.xoai.services.api;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.Item;
import org.dspace.xoai.services.api.context.ContextService;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
public interface CollectionsService {
List<UUID> getAllSubCollections(UUID communityId) throws SQLException;
List<UUID> getAllSubCollections(ContextService contextService, UUID communityId) throws SQLException;
List<Community> flatParentCommunities(Collection collection) throws SQLException;
List<Community> flatParentCommunities(Community community) throws SQLException;
List<Community> flatParentCommunities(Item item) throws SQLException;

View File

@@ -25,12 +25,11 @@ import org.dspace.content.service.CommunityService;
public class DSpaceCollectionsService implements CollectionsService {
private ContextService contextService;
private static final CommunityService communityService
= ContentServiceFactory.getInstance().getCommunityService();
@Override
public List<UUID> getAllSubCollections(UUID communityId)
public List<UUID> getAllSubCollections(ContextService contextService, UUID communityId)
throws SQLException
{
Queue<Community> comqueue = new LinkedList<>();

View File

@@ -919,7 +919,6 @@ public class ItemsResource extends Resource
// Must used own style.
if ((metadata.length < 2) || (metadata.length > 3))
{
context.abort();
log.error("Finding failed, bad metadata key.");
throw new WebApplicationException(Response.Status.NOT_FOUND);
}

View File

@@ -258,6 +258,9 @@ public class FlowAuthorizationUtils {
.authorizeManageItemPolicy(context, (Item) (bitstreamService.getParentObject(context, (Bitstream) policyParent)));
break;
}
default:
//If we can't find a parent the policy will receive a NULL dspace object, this is not something we want.
throw new IllegalArgumentException("Invalid DSpaceObject type provided");
}
policy = resourcePolicyService.create(context);
policy.setdSpaceObject(policyParent);

View File

@@ -132,7 +132,7 @@ public class EditItemMetadataForm extends AbstractDSpaceTransformer {
// Metadata editing is the only type of editing available for a template item.
boolean editingTemplateItem = false;
String templateCollectionID = parameters.getParameter("templateCollectionID", null);
Collection templateCollection = templateCollectionID != null ? null : collectionService.find(context, UUID.fromString(templateCollectionID));
Collection templateCollection = templateCollectionID == null ? null : collectionService.find(context, UUID.fromString(templateCollectionID));
if (templateCollection != null)
{
Item templateItem = templateCollection.getTemplateItem();
@@ -144,7 +144,7 @@ public class EditItemMetadataForm extends AbstractDSpaceTransformer {
// DIVISION: main
Division main = body.addInteractiveDivision("edit-item-status", contextPath+"/admin/item", Division.METHOD_POST,"primary administrative item");
if (editingTemplateItem)
if (templateCollection != null && editingTemplateItem)
{
main.setHead(T_template_head.parameterize(templateCollection.getName()));
}

View File

@@ -273,9 +273,16 @@ public class ConfigurableBrowse extends AbstractDSpaceTransformer implements
HttpServletResponse response = (HttpServletResponse)objectModel
.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
BrowseInfo info = getBrowseInfo();
if(info == null)
{
HttpServletResponse response = (HttpServletResponse)objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
String type = info.getBrowseIndex().getName();

View File

@@ -81,7 +81,7 @@ public class ItemRequestResponseAction extends AbstractAction
String title;
Item item = requestItem.getItem();
String titleDC = item.getName();
if (titleDC != null || titleDC.length() > 0)
if (titleDC != null && titleDC.length() > 0)
title=titleDC;
else
title="untitled";

View File

@@ -124,7 +124,7 @@ public class ItemRequestResponseTrueForm extends AbstractDSpaceTransformer imple
String title;
Item item = requestItem.getItem();
String titleDC = item.getName();
if (titleDC != null || titleDC.length() > 0)
if (titleDC != null && titleDC.length() > 0)
title = titleDC;
else
title = "untitled";

View File

@@ -142,6 +142,12 @@ public class HandleResolverReader extends AbstractReader implements Recyclable {
log.error("SQLException: ", e);
return;
}
if(jsonString == null)
{
//No handle found bad request
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
try {
ObjectModelHelper.getResponse(objectModel).setHeader("Content-Type", CONTENTTYPE);