KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > core > component > UIFileUpload


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

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 JavaDoc;
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 /**
19  * Wed, Dec 22, 2003 @ 23:14
20  *
21  * @author: Tuan Nguyen
22  * @email: tuan08@users.sourceforge.net
23  * @version: $Id: UIFileUpload.java,v 1.5 2004/06/22 21:46:04 tuan08 Exp $
24  */

25 public class UIFileUpload extends UIExoCommand {
26     public static final String JavaDoc UPLOAD_ACTION = "upload";
27     public static final String JavaDoc CANCEL_ACTION = "cancel";
28
29   private int numberOfField_;
30   private String JavaDoc userAction_;
31   private HashMap userInputs_;
32   private String JavaDoc 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 JavaDoc uicomponent = (String JavaDoc) paramMap.get(UICOMPONENT) ;
46     if (!getId().equals(uicomponent)) return ;
47     PortletRequest request = (PortletRequest) context.getExternalContext().getRequest();
48     HttpServletRequest JavaDoc httpRequest = (HttpServletRequest JavaDoc) 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 JavaDoc fileField = item.getFieldName();
59             String JavaDoc 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 JavaDoc name = item.getFieldName();
71             if (FacesConstants.ACTION.equals(name)) {
72               userAction_ = item.getString();
73             } else {
74               String JavaDoc value = item.getString();
75               int index = name.lastIndexOf("-");
76               String JavaDoc number = name.substring(index + 1, name.length());
77               String JavaDoc 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 JavaDoc 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 JavaDoc getUserAction() { return userAction_; }
98
99   public String JavaDoc getParentUri() { return parentUri_; }
100
101   public void setParentUri(String JavaDoc 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 JavaDoc fileName_;
113     String JavaDoc name_;
114     byte[] buf_;
115
116     public UserInput(String JavaDoc name, String JavaDoc fileName, byte[] buf) {
117       name_ = name;
118       fileName_ = fileName;
119       buf_ = buf;
120
121     }
122
123     public String JavaDoc getName() { return name_; }
124
125     public void setName(String JavaDoc name) { name_ = name; }
126
127     public String JavaDoc getFileName() { return fileName_; }
128
129     public void setFileName(String JavaDoc name) { fileName_ = name; }
130
131     public byte[] getStream() { return buf_; }
132
133     public void setStream(byte[] buf) { buf_ = buf; }
134   }
135 }
136
Popular Tags