1 5 package org.exoplatform.faces.core.component; 6 7 import java.util.*; 8 9 import javax.faces.context.FacesContext; 10 import javax.portlet.PortletRequest; 11 import javax.servlet.http.HttpServletRequest ; 12 13 import org.apache.commons.fileupload.FileUpload; 14 import org.apache.commons.fileupload.DiskFileUpload; 15 import org.apache.commons.fileupload.FileItem; 16 import org.exoplatform.faces.FacesConstants; 17 import org.exoplatform.faces.core.event.ExoActionEvent; 18 25 public class UIFileUpload extends UIExoCommand { 26 public static final String UPLOAD_ACTION = "upload"; 27 public static final String CANCEL_ACTION = "cancel"; 28 29 private int numberOfField_; 30 private String userAction_; 31 private HashMap userInputs_; 32 private String parentUri_; 33 private boolean showCancel_ = true ; 34 private boolean showHeader_ = true ; 35 36 public UIFileUpload() { 37 setRendererType("FileUploadRenderer"); 38 setId("UIFileUpload"); 39 numberOfField_ = 3; 40 userInputs_ = new HashMap(); 41 } 42 43 public void decode(FacesContext context) { 44 Map paramMap = context.getExternalContext().getRequestParameterMap() ; 45 String uicomponent = (String ) paramMap.get(UICOMPONENT) ; 46 if (!getId().equals(uicomponent)) return ; 47 PortletRequest request = (PortletRequest) context.getExternalContext().getRequest(); 48 HttpServletRequest httpRequest = (HttpServletRequest ) request; 49 userInputs_.clear(); 50 try { 51 if (FileUpload.isMultipartContent(httpRequest)) { 52 DiskFileUpload upload = new DiskFileUpload(); 53 List items = upload.parseRequest(httpRequest); 54 Iterator iter = items.iterator(); 55 while (iter.hasNext()) { 56 FileItem item = (FileItem) iter.next(); 57 if (!item.isFormField()) { 58 String fileField = item.getFieldName(); 59 String fileName = item.getName(); 60 byte[] buf = item.get(); 61 if (buf != null && buf.length > 0) { 62 userInputs_.put(fileField, new UserInput(null, fileName, buf)); 63 } 64 } 65 } 66 iter = items.iterator(); 67 while (iter.hasNext()) { 68 FileItem item = (FileItem) iter.next(); 69 if (item.isFormField()) { 70 String name = item.getFieldName(); 71 if (FacesConstants.ACTION.equals(name)) { 72 userAction_ = item.getString(); 73 } else { 74 String value = item.getString(); 75 int index = name.lastIndexOf("-"); 76 String number = name.substring(index + 1, name.length()); 77 String fileField = "file-" + number; 78 UserInput input = (UserInput) userInputs_.get(fileField); 79 if (input != null) { 80 input.setName(value); 81 } 82 } 83 } 84 } 85 } else { 86 userAction_ = "cancel"; 87 } 88 broadcast(new ExoActionEvent(this, userAction_)); 89 } catch (Exception ex) { 90 ex.printStackTrace(); 91 } 92 } 93 94 public int getNumberOfField() { return numberOfField_; } 95 public void setNumberOfField(int numberOfField) { numberOfField_ = numberOfField;} 96 97 public String getUserAction() { return userAction_; } 98 99 public String getParentUri() { return parentUri_; } 100 101 public void setParentUri(String uri) { parentUri_ = uri; } 102 103 public Collection getUserInputs() { return userInputs_.values(); } 104 105 public boolean isShowCancel() { return showCancel_ ; } 106 public void setShowCancel(boolean b) { showCancel_ = b ; } 107 108 public boolean isShowHeader() { return showHeader_ ; } 109 public void setShowHeader(boolean b) { showHeader_ = b ; } 110 111 public class UserInput { 112 String fileName_; 113 String name_; 114 byte[] buf_; 115 116 public UserInput(String name, String fileName, byte[] buf) { 117 name_ = name; 118 fileName_ = fileName; 119 buf_ = buf; 120 121 } 122 123 public String getName() { return name_; } 124 125 public void setName(String name) { name_ = name; } 126 127 public String getFileName() { return fileName_; } 128 129 public void setFileName(String name) { fileName_ = name; } 130 131 public byte[] getStream() { return buf_; } 132 133 public void setStream(byte[] buf) { buf_ = buf; } 134 } 135 } 136 | Popular Tags |