001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/com/corejsf/UploadRenderer.java $
003: * $Id: UploadRenderer.java 14907 2006-09-19 02:39:19Z daisyf@stanford.edu $
004: ***********************************************************************************
005: * Copyright (c) 2004 Sun Microsystems from the Java Series, Core Java ServerFaces
006: * source freely distributable.
007: * see http://www.sun.com/books/java_series.html
008: ***********************************************************************************
009: * Modifications Copyright (c) 2005
010: * The Regents of the University of Michigan, Trustees of Indiana University,
011: * Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
012: *
013: * Licensed under the Educational Community License Version 1.0 (the "License");
014: * By obtaining, using and/or copying this Original Work, you agree that you have read,
015: * understand, and will comply with the terms and conditions of the Educational Community License.
016: * You may obtain a copy of the License at:
017: *
018: * http://cvs.sakaiproject.org/licenses/license_1_0.html
019: *
020: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
021: * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
022: * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
023: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
024: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: *
026: **********************************************************************************/package com.corejsf;
027:
028: import java.io.File;
029: import java.io.IOException;
030: import java.util.Date;
031:
032: import javax.servlet.ServletContext;
033: import javax.faces.FacesException;
034: import javax.faces.component.EditableValueHolder;
035: import javax.faces.component.UIComponent;
036: import javax.faces.context.ExternalContext;
037: import javax.faces.context.FacesContext;
038: import javax.faces.context.ResponseWriter;
039: import javax.faces.el.ValueBinding;
040: import javax.faces.render.Renderer;
041: import javax.servlet.http.HttpServletRequest;
042:
043: import org.apache.commons.fileupload.FileItem;
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: public class UploadRenderer extends Renderer {
048:
049: private static Log log = LogFactory.getLog(UploadRenderer.class);
050: private static final String UPLOAD = ".upload";
051:
052: public UploadRenderer() {
053: }
054:
055: public void encodeBegin(FacesContext context, UIComponent component)
056: throws IOException {
057: if (!component.isRendered())
058: return;
059: ResponseWriter writer = context.getResponseWriter();
060: ExternalContext external = context.getExternalContext();
061: HttpServletRequest request = (HttpServletRequest) external
062: .getRequest();
063:
064: String clientId = component.getClientId(context);
065: log.debug("** encodeBegin, clientId =" + clientId);
066: encodeUploadField(writer, clientId, component);
067: }
068:
069: public void encodeUploadField(ResponseWriter writer,
070: String clientId, UIComponent component) throws IOException {
071: // write <input type=file> for browsing and upload
072: writer.startElement("input", component);
073: writer.writeAttribute("type", "file", "type");
074: writer.writeAttribute("name", clientId + UPLOAD, "clientId");
075: writer.writeAttribute("size", "50", null);
076: writer.endElement("input");
077: writer.flush();
078: }
079:
080: public void decode(FacesContext context, UIComponent component) {
081: log.debug("**** decode =");
082: boolean mediaIsValid = true;
083: ExternalContext external = context.getExternalContext();
084: HttpServletRequest request = (HttpServletRequest) external
085: .getRequest();
086: String clientId = component.getClientId(context);
087: FileItem item = (FileItem) request.getAttribute(clientId
088: + UPLOAD);
089: // check if file > maxSize allowed
090: log.debug("clientId =" + clientId);
091: log.debug("fileItem =" + item);
092: // if (item!=null) log.debug("***UploadRender: fileItem size ="+ item.getSize());
093: Long maxSize = (Long) ((ServletContext) external.getContext())
094: .getAttribute("FILEUPLOAD_SIZE_MAX");
095: if (item == null || item.getSize() / 1000 > maxSize.intValue()) {
096: ((ServletContext) external.getContext()).setAttribute(
097: "TEMP_FILEUPLOAD_SIZE", new Long(
098: item.getSize() / 1000));
099: mediaIsValid = false;
100: }
101:
102: Object target;
103: ValueBinding binding = component.getValueBinding("target");
104: if (binding != null)
105: target = binding.getValue(context);
106: else
107: target = component.getAttributes().get("target");
108:
109: String repositoryPath = (String) ((ServletContext) external
110: .getContext())
111: .getAttribute("FILEUPLOAD_REPOSITORY_PATH");
112: log.debug("****" + repositoryPath);
113: if (target != null) {
114: File dir = new File(repositoryPath + target.toString()); //directory where file would be saved
115: if (!dir.exists())
116: dir.mkdirs();
117: if (item != null && !("").equals(item.getName())) {
118: String fullname = item.getName();
119: fullname = fullname.replace('\\', '/'); // replace c:\fullname to c:/fullname
120: fullname = fullname
121: .substring(fullname.lastIndexOf("/") + 1);
122: int dot_index = fullname.lastIndexOf(".");
123: String filename = fullname.substring(0, dot_index)
124: + "_" + (new Date()).getTime()
125: + fullname.substring(dot_index);
126: File file = new File(dir.getPath() + "/" + filename);
127: log.debug("**1. filename=" + file.getPath());
128: try {
129: if (mediaIsValid)
130: item.write(file);
131: // change value so we can evoke the listener
132: ((EditableValueHolder) component)
133: .setSubmittedValue(file.getPath());
134: } catch (Exception ex) {
135: throw new FacesException(ex);
136: }
137: }
138: }
139: }
140: }
|