001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Brice Ruzand
022: *
023: * --------------------------------------------------------------------------
024: * $Id$
025: * --------------------------------------------------------------------------
026: */
027: package emb.sample.servlet;
028:
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.net.URL;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import javax.emb.GenericMediaFormat;
036: import javax.emb.MediaEntityLocal;
037: import javax.emb.MediaFormat;
038: import javax.emb.MediaFormatRegistry;
039: import javax.servlet.ServletException;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042:
043: import org.apache.commons.fileupload.DiskFileUpload;
044: import org.apache.commons.fileupload.FileItem;
045:
046: import emb.sample.session.MediaSampleSessionLocal;
047:
048: /**
049: * Generate an HTML web page to present the media
050: *
051: * @author Brice Ruzand
052: */
053: public class UploadSampleMedia extends BaseSampleServlet {
054:
055: /**
056: * serialVersionUID
057: */
058: private static final long serialVersionUID = 3762250859580634161L;
059:
060: /**
061: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
062: * javax.servlet.http.HttpServletResponse)
063: * @inheritDoc
064: */
065: public void doGet(HttpServletRequest request,
066: HttpServletResponse response) throws ServletException,
067: IOException {
068: try {
069:
070: response.setContentType("text/html;charset=ISO-8859-1");
071:
072: StringPrinter fromatSupported = new StringPrinter();
073: fromatSupported.println("<ul>");
074: for (Iterator i = MediaFormatRegistry.SINGLETON
075: .getFileExtensions(); i.hasNext();) {
076: String fileExt = (String) i.next();
077: MediaFormat format = MediaFormatRegistry.SINGLETON
078: .lookup(fileExt);
079: if (!(format == null || format instanceof GenericMediaFormat)) {
080: fromatSupported.println("<li>"
081: + fileExt
082: + " : "
083: + MediaFormatRegistry.SINGLETON
084: .lookup(fileExt) + "</li>");
085: }
086: }
087: fromatSupported.println("</ul>");
088:
089: request.setAttribute("fromatSupported", fromatSupported);
090: getServletConfig().getServletContext()
091: .getRequestDispatcher("/jsps/uploadForm.jsp")
092: .include(request, response);
093:
094: } catch (Throwable e) {
095: exceptionHandler(e, getClass(), request, response);
096: }
097: }
098:
099: /**
100: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
101: * javax.servlet.http.HttpServletResponse)
102: * @inheritDoc
103: */
104: public void doPost(HttpServletRequest request,
105: HttpServletResponse response) throws ServletException,
106: IOException {
107: try {
108:
109: String mediaDesc = "";
110: String mediaName = "";
111: String mediaUrl = "";
112: InputStream mediaInputStream = null;
113:
114: // Create a new file upload handler
115: DiskFileUpload upload = new DiskFileUpload();
116:
117: // Parse the request
118: List /* FileItem */items = upload.parseRequest(request);
119:
120: // Process the uploaded items
121: Iterator iter = items.iterator();
122: while (iter.hasNext()) {
123: FileItem item = (FileItem) iter.next();
124: if (item.isFormField()) {
125: if (item.getFieldName().equals("desc")) {
126: mediaDesc = item.getString();
127: }
128: if (item.getFieldName().equals("url")) {
129: mediaUrl = item.getString();
130: }
131: } else {
132: if (item.getFieldName().equals("file")) {
133: mediaInputStream = item.getInputStream();
134: mediaName = item.getName();
135: }
136: }
137: }
138:
139: MediaSampleSessionLocal session = getSession(request);
140:
141: MediaEntityLocal meb = null;
142: if (mediaUrl != null && mediaUrl.length() > 0) {
143: // meb = createNewSample(mediaUrl, mediaDesc);
144: meb = session.createMediaEntity(new URL(mediaUrl),
145: mediaDesc, MediaSampleSessionLocal.USER_ADDED);
146: } else if (mediaInputStream != null && mediaName != null
147: && mediaName.length() > 0) {
148: meb = session.createMediaEntity(mediaInputStream,
149: mediaName, mediaDesc,
150: MediaSampleSessionLocal.USER_ADDED, null);
151: }
152:
153: if (meb != null) {
154:
155: // forward onto the retrieve servlet
156: request.setAttribute("identity", meb.getPrimaryKey());
157: this .getServletContext().getRequestDispatcher(
158: RETRIEVE_SERVLET).forward(request, response);
159:
160: } else {
161:
162: StringPrinter workspaceContent = new StringPrinter();
163: workspaceContent
164: .println("<h2>Missing information to add media</h2>");
165: workspaceContent
166: .println("There were some missing information to add media, please retry.");
167: request.setAttribute("workspaceContent",
168: workspaceContent);
169: this .getServletContext().getRequestDispatcher(
170: TEMPLATE_JSP).forward(request, response);
171:
172: }
173:
174: } catch (Throwable e) {
175: exceptionHandler(e, getClass(), request, response);
176: }
177: }
178:
179: }
|