Commit 9091cd98 by Tuomas Riihimäki

Make a utility for fetching the contents of an uploaded file

This was a copy-paste snippet in multiple places, where findbugs reported
error on ignoring the result of read() function.
Making this an utility we can make sure that it works as expected ( and clean up code)
1 parent 1538f1a2
Pipeline #65 passed
in 0 seconds
...@@ -41,8 +41,9 @@ public class GitRepositoryState { ...@@ -41,8 +41,9 @@ public class GitRepositoryState {
private GitRepositoryState() { private GitRepositoryState() {
logger.info("Initializing git status"); logger.info("Initializing git status");
this.properties = new Properties(); this.properties = new Properties();
InputStream resource = null;
try { try {
InputStream resource = getClass().getResourceAsStream("/git.properties"); resource = getClass().getResourceAsStream("/git.properties");
if (resource == null) { if (resource == null) {
logger.warn("Resource not found!"); logger.warn("Resource not found!");
...@@ -53,6 +54,14 @@ public class GitRepositoryState { ...@@ -53,6 +54,14 @@ public class GitRepositoryState {
logger.info("Git keys:", this.properties.keys()); logger.info("Git keys:", this.properties.keys());
} catch (IOException e) { } catch (IOException e) {
logger.warn("Error initializing git proerties", e); logger.warn("Error initializing git proerties", e);
} finally {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
// Error clsoing resource in finally.. Eat the error.
}
}
} }
} }
......
...@@ -32,6 +32,7 @@ import javax.faces.event.ActionEvent; ...@@ -32,6 +32,7 @@ import javax.faces.event.ActionEvent;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.inject.Named; import javax.inject.Named;
import fi.codecrew.moya.web.helpers.FileUploadUtils;
import org.primefaces.model.DefaultStreamedContent; import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent; import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile; import org.primefaces.model.UploadedFile;
...@@ -130,15 +131,10 @@ public class MapManageView extends GenericCDIView { ...@@ -130,15 +131,10 @@ public class MapManageView extends GenericCDIView {
public String submitBg() { public String submitBg() {
byte[] bytes = bgFile.getContents(); byte[] bytes = FileUploadUtils.getFileContents(bgFile);
if (bytes == null && bgFile.getSize() > 0) { if (bytes == null || bytes.length == 0) {
bytes = new byte[(int) bgFile.getSize()]; super.addFaceMessage("mapManage.errorUploadingBackground");
try { return null;
bgFile.getInputstream().read(bytes);
} catch (IOException e) {
super.addFaceMessage("mapManage.errorUploadingBackground");
return null;
}
} }
map.setMapData(bytes); map.setMapData(bytes);
map.setMimeType(bgFile.getContentType()); map.setMimeType(bgFile.getContentType());
......
...@@ -29,6 +29,7 @@ import javax.faces.model.ListDataModel; ...@@ -29,6 +29,7 @@ import javax.faces.model.ListDataModel;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import fi.codecrew.moya.web.helpers.FileUploadUtils;
import org.primefaces.model.UploadedFile; import org.primefaces.model.UploadedFile;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -131,20 +132,9 @@ public class EventPropertyView extends GenericCDIView { ...@@ -131,20 +132,9 @@ public class EventPropertyView extends GenericCDIView {
public String savePrivateProperty() { public String savePrivateProperty() {
logger.info("Saving property {}, eventorg {}, key {}", new Object[]{privateProperty.getEvent(), privateProperty.getEventorg(), privateProperty.getKey()}); logger.info("Saving property {}, eventorg {}, key {}", new Object[]{privateProperty.getEvent(), privateProperty.getEventorg(), privateProperty.getKey()});
if (privateProperty.getKey().isData() && file != null) { if (privateProperty.getKey().isData() && file != null) {
byte[] contents = null; byte[] contents = FileUploadUtils.getFileContents(file);
if (file.getContents() != null) {
contents = file.getContents();
} else {
contents = new byte[(int) file.getSize()];
try {
file.getInputstream().read(contents);
} catch (IOException e) {
logger.warn("Error reading file from stream", e);
contents = null;
}
}
if (contents == null) { if (contents == null || contents.length == 0) {
super.addFaceMessage("eventProperty.errorUploadingFile"); super.addFaceMessage("eventProperty.errorUploadingFile");
return null; return null;
} }
...@@ -187,20 +177,9 @@ public class EventPropertyView extends GenericCDIView { ...@@ -187,20 +177,9 @@ public class EventPropertyView extends GenericCDIView {
logger.debug("Is data: {} File {}", property.getKey().isData(), file); logger.debug("Is data: {} File {}", property.getKey().isData(), file);
if (property.getKey().isData() && file != null) { if (property.getKey().isData() && file != null) {
logger.info("saving data values type: {}, length {}", file.getContentType(), file.getSize()); logger.info("saving data values type: {}, length {}", file.getContentType(), file.getSize());
byte[] contents = null; byte[] contents = FileUploadUtils.getFileContents(file);
if (file.getContents() != null) {
contents = file.getContents();
} else {
contents = new byte[(int) file.getSize()];
try {
file.getInputstream().read(contents);
} catch (IOException e) {
logger.warn("Error reading file from stream", e);
contents = null;
}
}
if (contents == null) { if (contents == null || contents.length == 0) {
super.addFaceMessage("eventProperty.errorUploadingFile"); super.addFaceMessage("eventProperty.errorUploadingFile");
return null; return null;
} }
......
...@@ -27,6 +27,7 @@ import javax.enterprise.context.ConversationScoped; ...@@ -27,6 +27,7 @@ import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import fi.codecrew.moya.web.helpers.FileUploadUtils;
import org.primefaces.model.UploadedFile; import org.primefaces.model.UploadedFile;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -95,20 +96,9 @@ public class CardView extends GenericCDIView { ...@@ -95,20 +96,9 @@ public class CardView extends GenericCDIView {
try { try {
logger.info("Templateimage: {}", getTemplateImage()); logger.info("Templateimage: {}", getTemplateImage());
byte[] contents = null; byte[] contents = null;
UploadedFile file = getTemplateImage(); contents = FileUploadUtils.getFileContents(getTemplateImage());
if (file.getContents() != null) {
contents = file.getContents();
} else {
contents = new byte[(int) file.getSize()];
try {
file.getInputstream().read(contents);
} catch (IOException e) {
logger.warn("Error reading file from stream", e);
contents = null;
}
}
if (contents == null) { if (contents == null || contents.length == 0) {
super.addFaceMessage("eventProperty.errorUploadingFile"); super.addFaceMessage("eventProperty.errorUploadingFile");
return null; return null;
} }
......
...@@ -27,6 +27,7 @@ import javax.ejb.EJB; ...@@ -27,6 +27,7 @@ import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped; import javax.enterprise.context.ConversationScoped;
import javax.inject.Named; import javax.inject.Named;
import fi.codecrew.moya.web.helpers.FileUploadUtils;
import org.primefaces.model.UploadedFile; import org.primefaces.model.UploadedFile;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -82,19 +83,10 @@ public class ImportView extends GenericCDIView { ...@@ -82,19 +83,10 @@ public class ImportView extends GenericCDIView {
{ {
ArrayList<ImportWrapper> ret = new ArrayList<ImportWrapper>(); ArrayList<ImportWrapper> ret = new ArrayList<ImportWrapper>();
byte[] bytes = file.getContents(); byte[] bytes = FileUploadUtils.getFileContents(file);
if (bytes == null && file.getSize() > 0)
{
bytes = new byte[(int) file.getSize()];
try {
file.getInputstream().read(bytes);
} catch (IOException e) {
super.addFaceMessage("import.erroruploading");
return null;
}
}
if (bytes == null) { if (bytes == null || bytes.length == 0) {
super.addFaceMessage("import.erroruploading");
return null; return null;
} }
......
...@@ -26,6 +26,7 @@ import javax.enterprise.inject.Produces; ...@@ -26,6 +26,7 @@ import javax.enterprise.inject.Produces;
import javax.faces.model.ListDataModel; import javax.faces.model.ListDataModel;
import javax.inject.Named; import javax.inject.Named;
import fi.codecrew.moya.web.helpers.FileUploadUtils;
import org.primefaces.event.RateEvent; import org.primefaces.event.RateEvent;
import org.primefaces.model.UploadedFile; import org.primefaces.model.UploadedFile;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -169,23 +170,9 @@ public class CompoView extends GenericCDIView { ...@@ -169,23 +170,9 @@ public class CompoView extends GenericCDIView {
return null; return null;
} }
byte[] contents = null; byte[] contents = FileUploadUtils.getFileContents(file);
if (file.getContents() != null) {
contents = file.getContents();
logger.info("Got file contents from .confents()");
} else {
contents = new byte[(int) file.getSize()];
logger.info("Read {} bytes from stream in file {}", file.getSize(), file.getFileName());
try {
file.getInputstream().read(contents);
} catch (IOException e) {
logger.warn("Error reading file from stream", e);
contents = null;
}
}
if (contents == null) if (contents == null || contents.length == 0) {
{
super.addFaceMessage("compo.fileuploadFailed"); super.addFaceMessage("compo.fileuploadFailed");
return null; return null;
} }
......
/* /*
* Copyright Codecrew Ry * Copyright Codecrew Ry
* *
* All rights reserved. * All rights reserved.
* *
* This license applies to any software containing a notice placed by the * This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software. * copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software. * This license covers modification, distribution and use of the Software.
* *
* Any distribution and use in source and binary forms, with or without * Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the * modification is not permitted without explicit written permission from the
* copyright owner. * copyright owner.
* *
* A non-exclusive royalty-free right is granted to the copyright owner of the * A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in * Software to use, modify and distribute all modifications to the Software in
* future versions of the Software. * future versions of the Software.
* *
*/ */
package fi.codecrew.moya.web.cdiview.voting; package fi.codecrew.moya.web.cdiview.voting;
import java.io.IOException; import java.io.IOException;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Named; import javax.inject.Named;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import org.primefaces.model.UploadedFile; import fi.codecrew.moya.web.helpers.FileUploadUtils;
import org.slf4j.Logger; import org.primefaces.model.UploadedFile;
import org.slf4j.LoggerFactory; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.VotingBeanLocal;
import fi.codecrew.moya.model.CompoEntry; import fi.codecrew.moya.beans.VotingBeanLocal;
import fi.codecrew.moya.model.CompoEntryFile; import fi.codecrew.moya.model.CompoEntry;
import fi.codecrew.moya.web.cdiview.GenericCDIView; import fi.codecrew.moya.model.CompoEntryFile;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
@Named
@RequestScoped @Named
public class VotingCompoAddEntryView extends GenericCDIView { @RequestScoped
public class VotingCompoAddEntryView extends GenericCDIView {
private static final long serialVersionUID = -93065881159476197L;
private static final long serialVersionUID = -93065881159476197L;
@EJB
VotingBeanLocal votingBean; @EJB
VotingBeanLocal votingBean;
@NotNull
private String name; @NotNull
private String notes; private String name;
private String screenMessage; private String notes;
private String screenMessage;
@NotNull
private UploadedFile uploadedFile; @NotNull
private UploadedFile uploadedFile;
private Integer compoId;
private String compoName; private Integer compoId;
private String compoName;
private static final Logger logger = LoggerFactory.getLogger(VotingCompoAddEntryView.class);
private static final Logger logger = LoggerFactory.getLogger(VotingCompoAddEntryView.class);
public Integer getCompoId() {
return compoId; public Integer getCompoId() {
} return compoId;
}
public void setCompoId(Integer compoId) {
this.compoId = compoId; public void setCompoId(Integer compoId) {
} this.compoId = compoId;
}
public UploadedFile getUploadedFile() {
return uploadedFile; public UploadedFile getUploadedFile() {
} return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile; public void setUploadedFile(UploadedFile uploadedFile) {
} this.uploadedFile = uploadedFile;
}
public String getScreenMessage() {
return screenMessage; public String getScreenMessage() {
} return screenMessage;
}
public void setScreenMessage(String screenMessage) {
this.screenMessage = screenMessage; public void setScreenMessage(String screenMessage) {
} this.screenMessage = screenMessage;
}
public String getNotes() {
return notes; public String getNotes() {
} return notes;
}
public void setNotes(String notes) {
this.notes = notes; public void setNotes(String notes) {
} this.notes = notes;
}
public String getName() {
return name; public String getName() {
} return name;
}
public void setName(String name) {
this.name = name; public void setName(String name) {
} this.name = name;
}
public String getCompoName() {
return compoName; public String getCompoName() {
} return compoName;
}
public void setCompoName(String compoName) {
this.compoName = compoName; public void setCompoName(String compoName) {
} this.compoName = compoName;
}
public String send() {
public String send() {
UploadedFile file = getUploadedFile();
UploadedFile file = getUploadedFile();
if (file == null)
{ if (file == null) {
super.addFaceMessage("compo.fileuploadFailed"); super.addFaceMessage("compo.fileuploadFailed");
return null; return null;
} }
byte[] contents = null; byte[] contents = FileUploadUtils.getFileContents(file);
if (file.getContents() != null) {
contents = file.getContents(); if (contents == null || contents.length == 0) {
} else { super.addFaceMessage("compo.fileuploadFailed");
contents = new byte[(int) file.getSize()]; return null;
try { }
file.getInputstream().read(contents);
} catch (IOException e) { CompoEntry compoEntry = new CompoEntry();
logger.warn("Error reading file from stream", e); compoEntry.setTitle(name);
contents = null; compoEntry.setNotes(notes);
} compoEntry.setScreenMessage(screenMessage);
} compoEntry.setCompo(votingBean.getCompoById(compoId));
CompoEntryFile cef = new CompoEntryFile(compoEntry);
if (contents == null) cef.setFileData(contents);
{ cef.setFileName(uploadedFile.getFileName());
super.addFaceMessage("compo.fileuploadFailed"); votingBean.addEntry(compoEntry, cef);
return null; return null;
} }
CompoEntry compoEntry = new CompoEntry(); public void initView() {
compoEntry.setTitle(name); compoName = votingBean.getCompoById(compoId).getName();
compoEntry.setNotes(notes); }
compoEntry.setScreenMessage(screenMessage); }
compoEntry.setCompo(votingBean.getCompoById(compoId));
CompoEntryFile cef = new CompoEntryFile(compoEntry);
cef.setFileData(contents);
cef.setFileName(uploadedFile.getFileName());
votingBean.addEntry(compoEntry, cef);
return null;
}
public void initView() {
compoName = votingBean.getCompoById(compoId).getName();
}
}
package fi.codecrew.moya.web.helpers;
import org.apache.commons.io.IOUtils;
import org.primefaces.model.UploadedFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
public class FileUploadUtils {
private static final Logger logger = LoggerFactory.getLogger(FileUploadUtils.class);
public static byte[] getFileContents(UploadedFile file) {
if (file.getContents() != null) {
return file.getContents();
} else {
InputStream istream = null;
try {
istream = file.getInputstream();
return IOUtils.toByteArray(istream);
} catch (IOException e) {
logger.warn("Error reading file from stream", e);
} finally {
if (istream != null) {
try {
istream.close();
} catch (IOException e) {
logger.warn("Error closing UploadFile inputstream");
}
}
}
}
return null;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!