Show UploadPortlet.java syntax highlighted
package fi.hip.gb.portlet.disk;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.portlet.PortletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import fi.hip.gb.disk.FileManager;
import fi.hip.gb.disk.conf.Config;
public class UploadPortlet extends GenericPortlet {
private static String NEW_JSP;
private static String STATUS_JSP;
private static String UP_OP = "upload";
public static final String STATUS_BEAN = UploadPortlet.class.getName() + ".status_bean";
private static final Log log = LogFactory.getLog(UploadPortlet.class);
public void init() throws PortletException {
super.init();
initEnv();
}
public void init(PortletConfig config) throws PortletException {
super.init(config);
initEnv();
}
private void initEnv() throws PortletException {
NEW_JSP = getInitParameter("NEW_JSP");
STATUS_JSP = getInitParameter("STATUS_JSP");
}
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
FileManager fm = (FileManager)request.getPortletSession().getAttribute(STATUS_BEAN);
response.setContentType("text/html");
log.info("view " + request.getParameter("op"));
if(fm == null || fm.getCurrentFile() == null) {
getPortletContext().getRequestDispatcher(NEW_JSP).include(request, response);
} else {
getPortletContext().getRequestDispatcher(STATUS_JSP).include(request, response);
}
}
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
FileManager fm = new FileManager();
request.getPortletSession().setAttribute(STATUS_BEAN, fm);
log.info("action " + request.getParameter("op"));
if(request.getParameter("op") != null) {
if(UP_OP.equals(request.getParameter("op"))) {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
PortletFileUpload upload = new PortletFileUpload(factory);
List fileItems = upload.parseRequest(request);
Iterator itr = fileItems.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if(item.getName() == null)
continue;
//fm.setCurrentFile(item.getName());
log.info("putting file: " + item.getName() + " " + item.getSize() + " bytes");
File outFile = new File(Config.getTempDir() +"/" + item.getName());
/*
if(outFile.exists() && outFile.length() == item.getSize() ){
fm.stopOp(outFile, new IOException("File already found"));
log.info("not putting, already exists: " + outFile.getAbsolutePath());
continue;
}*/
InputStream input = item.getInputStream();
FileOutputStream fileStream = new FileOutputStream(outFile);
byte[] bt = new byte[10000];
int cnt = input.read(bt);
while(cnt!=-1){
fileStream.write(bt,0,cnt);
cnt = input.read(bt);
}
input.close();
fileStream.close();
// add the file to the GB-DISK in background thread
fm.put(outFile.getName(), outFile);
fm.run();
}
} catch(Exception e) {
log.error("Upload failed " + e);
throw new PortletException(e);
}
} else {
request.getPortletSession().removeAttribute(STATUS_BEAN);
}
//response.setRenderParameter("op", "list");
//response.setRenderParameter("statusMessage","<div class=\"portlet-msg-success\">Job submission succeed</div>");
//response.setRenderParameter("statusMessage","<div class=\"portlet-msg-error\">Job submission failed</div>");
}
}
}
See more files for this project here