001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.servlet;
034:
035: import com.flexive.shared.content.FxContent;
036: import com.flexive.shared.value.BinaryDescriptor;
037: import com.flexive.shared.value.FxBinary;
038: import com.flexive.war.beans.admin.content.ContentEditorBean;
039: import org.apache.commons.fileupload.FileItem;
040: import org.apache.commons.fileupload.FileItemFactory;
041: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
042: import org.apache.commons.fileupload.servlet.ServletFileUpload;
043:
044: import javax.servlet.*;
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.http.HttpServletResponse;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.io.PrintWriter;
050: import java.util.List;
051:
052: public class CeFileUpload implements Servlet {
053:
054: private ServletConfig servletConfig = null;
055:
056: public void init(ServletConfig servletConfig)
057: throws ServletException {
058: this .servletConfig = servletConfig;
059: }
060:
061: public ServletConfig getServletConfig() {
062: return servletConfig;
063: }
064:
065: public void service(ServletRequest servletRequest,
066: ServletResponse servletResponse) throws ServletException,
067: IOException {
068: String renderContent = null;
069: try {
070:
071: final HttpServletRequest request = (HttpServletRequest) servletRequest;
072: final ContentEditorBean ceb = ContentEditorBean
073: .getSingleton().getInstance(request);
074:
075: if (ceb == null) {
076: renderContent = "No Content Editor Bean is active";
077: } else {
078:
079: // Create a factory for disk-based file items
080: FileItemFactory factory = new DiskFileItemFactory();
081:
082: // Create a new file upload handler
083: ServletFileUpload upload = new ServletFileUpload(
084: factory);
085:
086: // Parse the request
087: List /* FileItem */items = upload
088: .parseRequest(request);
089:
090: BinaryDescriptor binary = null;
091:
092: String xpath = null;
093: for (Object item1 : items) {
094: FileItem item = (FileItem) item1;
095: if (item.isFormField()) {
096: if (item.getFieldName().equalsIgnoreCase(
097: "result")) {
098: renderContent = item.getString()
099: .replaceAll("\\\\n", "\\\n");
100: } else if (item.getFieldName()
101: .equalsIgnoreCase("xpath")) {
102: xpath = item.getString();
103: }
104: } else {
105: InputStream uploadedStream = null;
106: try {
107: uploadedStream = item.getInputStream();
108: String name = item.getName();
109: if (name.indexOf('\\') > 0)
110: name = name.substring(name
111: .lastIndexOf('\\') + 1);
112: binary = new BinaryDescriptor(name, item
113: .getSize(), uploadedStream);
114: } finally {
115: if (uploadedStream != null)
116: uploadedStream.close();
117: }
118: }
119: // System.out.println("Item: " + item.getName());
120: }
121:
122: FxContent co = ceb.getContent();
123: FxBinary binProperty = new FxBinary(binary);
124: co.setValue(xpath, binProperty);
125: ceb.getContentEngine().prepareSave(co);
126: }
127: } catch (Throwable t) {
128: System.err.println(t.getMessage());
129: t.printStackTrace();
130: renderContent = t.getMessage();
131: }
132:
133: // Render the result
134: PrintWriter w = servletResponse.getWriter();
135: if (renderContent == null) {
136: renderContent = "No content";
137: }
138: w.print(renderContent);
139: w.close();
140: servletResponse.setContentType("text/html");
141: servletResponse.setContentLength(renderContent.length());
142: ((HttpServletResponse) servletResponse)
143: .setStatus(HttpServletResponse.SC_OK);
144: }
145:
146: public String getServletInfo() {
147: return this .getClass().getName();
148: }
149:
150: public void destroy() {
151: // nothing to do
152: }
153: }
|