001: package org.contineo.web.upload;
002:
003: import com.icesoft.faces.async.render.RenderManager;
004: import com.icesoft.faces.async.render.Renderable;
005: import com.icesoft.faces.component.inputfile.InputFile;
006: import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
007: import com.icesoft.faces.webapp.xmlhttp.RenderingException;
008:
009: import org.apache.commons.io.FileUtils;
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import java.io.File;
014: import java.io.FileInputStream;
015: import java.io.IOException;
016: import java.io.InputStream;
017:
018: import java.util.EventObject;
019:
020: import javax.faces.event.ActionEvent;
021:
022: /**
023: * <p>
024: * The InputFileBean class is the backing bean for the inputfile showcase
025: * demonstration. It is used to store the state of the uploaded file.
026: * </p>
027: *
028: * @author Marco Meschieri
029: * @version $Id: InputFileBean.java,v 1.4 2007/07/16 06:35:17 marco Exp $
030: * @since 3.0
031: */
032: public class InputFileBean implements Renderable {
033: protected static Log log = LogFactory.getLog(InputFileBean.class);
034: private int percent = -1;
035: private String language;
036: private File file = null;
037: private String description;
038: private String versionType;
039: private PersistentFacesState state;
040: private RenderManager renderManager;
041: private String fileName = "";
042: private String contentType = "";
043:
044: public InputFileBean() {
045: state = PersistentFacesState.getInstance();
046: }
047:
048: /**
049: * Sets the Render Manager.
050: *
051: * @param renderManager
052: */
053: public void setRenderManager(RenderManager renderManager) {
054: this .renderManager = renderManager;
055: }
056:
057: /**
058: * Gets RenderManager, just try to satisfy WAS
059: *
060: * @return RenderManager null
061: */
062: public RenderManager getRenderManager() {
063: return null;
064: }
065:
066: /**
067: * Get the PersistentFacesState.
068: *
069: * @return state the PersistantFacesState
070: */
071: public PersistentFacesState getState() {
072: return state;
073: }
074:
075: /**
076: * Handles rendering exceptions for the progress bar.
077: *
078: * @param renderingException the exception that occured
079: */
080: public void renderingException(RenderingException renderingException) {
081: renderingException.printStackTrace();
082: }
083:
084: public void setPercent(int percent) {
085: this .percent = percent;
086: }
087:
088: public int getPercent() {
089: return percent;
090: }
091:
092: public void setFile(File file) {
093: try {
094: // If another file was uploaded, first delete the old file
095: if ((this .file != null) && this .file.exists()) {
096: FileUtils.forceDelete(this .file);
097: }
098: } catch (IOException e) {
099: log.error("Unable to delete temp file " + this .file);
100: }
101:
102: this .file = file;
103: }
104:
105: public File getFile() {
106: return file;
107: }
108:
109: public void action(ActionEvent event) {
110: InputFile inputFile = (InputFile) event.getSource();
111: fileName = inputFile.getFileInfo().getFileName();
112: contentType = inputFile.getFileInfo().getContentType();
113: this .percent = inputFile.getFileInfo().getPercent();
114:
115: if (inputFile.getStatus() == InputFile.SAVED) {
116: setFile(inputFile.getFile());
117: }
118:
119: if (inputFile.getStatus() == InputFile.INVALID) {
120: inputFile.getFileInfo().getException().printStackTrace();
121: }
122:
123: if (inputFile.getStatus() == InputFile.SIZE_LIMIT_EXCEEDED) {
124: inputFile.getFileInfo().getException().printStackTrace();
125: }
126:
127: if (inputFile.getStatus() == InputFile.UNKNOWN_SIZE) {
128: inputFile.getFileInfo().getException().printStackTrace();
129: }
130: }
131:
132: public void progress(EventObject event) {
133: InputFile file = (InputFile) event.getSource();
134: this .percent = file.getFileInfo().getPercent();
135:
136: if (renderManager != null) {
137: renderManager.requestRender(this );
138: }
139:
140: try {
141: if (state != null) {
142: state.render();
143: }
144: } catch (RenderingException e) {
145: }
146: }
147:
148: public void setFileName(String fileName) {
149: this .fileName = fileName;
150: }
151:
152: public String getFileName() {
153: return fileName;
154: }
155:
156: public void setContentType(String contentType) {
157: this .contentType = contentType;
158: }
159:
160: public String getContentType() {
161: return contentType;
162: }
163:
164: public boolean isReady() {
165: return percent == 100;
166: }
167:
168: public void deleteFile() {
169: if ((file != null) && file.exists()) {
170: try {
171: FileUtils.forceDelete(file);
172: } catch (IOException e) {
173: log.error("Unable to delete temp file "
174: + file.getPath());
175: }
176: }
177:
178: percent = 0;
179: }
180:
181: public String getLanguage() {
182: return language;
183: }
184:
185: public void setLanguage(String language) {
186: this .language = language;
187: }
188:
189: /**
190: * Reads the uploaded file and stores it's content into a string bufferO
191: *
192: * @throws IOException
193: */
194: public StringBuffer getContent() throws IOException {
195: byte[] buffer = new byte[1024];
196: int read = 0;
197: StringBuffer content = new StringBuffer();
198: InputStream in = new FileInputStream(getFile());
199:
200: while ((read = in.read(buffer, 0, 1024)) >= 0) {
201: content.append(new String(buffer, 0, read));
202: }
203:
204: return content;
205: }
206:
207: public String getDescription() {
208: return description;
209: }
210:
211: public void setDescription(String description) {
212: this .description = description;
213: }
214:
215: public void reset() {
216: deleteFile();
217: this .description = null;
218: this .file = null;
219: this .language = null;
220: this .fileName = null;
221: }
222:
223: public String getVersionType() {
224: return versionType;
225: }
226:
227: public void setVersionType(String versionType) {
228: this.versionType = versionType;
229: }
230: }
|