001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.servlets;
034:
035: import org.apache.commons.fileupload.FileItem;
036: import org.apache.commons.fileupload.FileItemFactory;
037: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
038: import org.apache.commons.fileupload.servlet.ServletFileUpload;
039: import org.apache.commons.fileupload.servlet.ServletRequestContext;
040:
041: import org.libresource.Libresource;
042:
043: import org.libresource.core.FileDataImpl;
044:
045: import org.libresource.kernel.KernelConstants;
046: import org.libresource.kernel.interfaces.KernelService;
047:
048: import org.libresource.web.WebImportExportLogger;
049: import org.libresource.web.config.Config;
050: import org.libresource.web.taglibs.JspFunctions;
051:
052: import org.libresource.xml.LibresourceImportExport;
053:
054: import java.io.File;
055: import java.io.FileOutputStream;
056: import java.io.InputStream;
057:
058: import java.net.URI;
059: import java.net.URL;
060:
061: import java.util.Iterator;
062: import java.util.List;
063:
064: import javax.servlet.http.HttpServletRequest;
065: import javax.servlet.http.HttpServletResponse;
066:
067: public class Import extends BaseServlet {
068: protected void process(HttpServletRequest request,
069: HttpServletResponse response) throws Exception {
070: if (!ServletFileUpload
071: .isMultipartContent(new ServletRequestContext(request))
072: && (request.getParameter("defaultBootstrap") == null)) {
073: if (request.getParameter("uri") != null) {
074: request
075: .setAttribute("uri", request
076: .getParameter("uri"));
077: }
078:
079: request.getRequestDispatcher("/pages/import.jsp").forward(
080: request, response);
081:
082: return;
083: }
084:
085: // Common vars
086: boolean createProfile = false;
087: FileDataImpl fileData = null;
088: String uri = null;
089: String fileName = "defaultBootstrap.xml";
090: long size = 0;
091:
092: // Case of custom bootstrap
093: if (ServletFileUpload
094: .isMultipartContent(new ServletRequestContext(request))) {
095: // Create a new file upload handler
096: FileItemFactory factory = new DiskFileItemFactory();
097: ServletFileUpload upload = new ServletFileUpload(factory);
098: List items = upload.parseRequest(request);
099:
100: // Process the uploaded items
101: Iterator iter = items.iterator();
102:
103: while (iter.hasNext()) {
104: FileItem item = (FileItem) iter.next();
105:
106: if (item.isFormField()) {
107: if (item.getFieldName().equals("uri")) {
108: uri = item.getString();
109: }
110:
111: if (item.getFieldName().equals("createProfile")) {
112: createProfile = item.getString().equals("on");
113: }
114: } else {
115: if (item.getFieldName().equals("file")) {
116: if (item.getSize() > 0) {
117: File temp = File.createTempFile("file",
118: "import", Config.getInstance(null)
119: .getLsWebBuffer());
120: item.write(temp);
121: fileData = new FileDataImpl(temp);
122: fileName = item.getName();
123: size = item.getSize();
124: }
125: }
126: }
127: }
128: } else if (request.getParameter("defaultBootstrap") != null) {
129: // Case of default bootstrap
130: uri = "";
131: createProfile = true;
132:
133: // Build file data
134: File temp = File.createTempFile("file", "import", Config
135: .getInstance(null).getLsWebBuffer());
136: InputStream is = new URL(org.libresource.Libresource
137: .getLibresourceConfiguration("kernel.serverUrl")
138: + "/static/download/Bootstrap.xml")
139: .openConnection().getInputStream();
140: FileOutputStream os = new FileOutputStream(temp);
141: int readCount = -1;
142: byte[] buffer = new byte[1024 * 1024 * 2];
143: size = 0;
144:
145: while ((readCount = is.read(buffer)) > 0) {
146: os.write(buffer, 0, readCount);
147: size += readCount;
148: }
149:
150: os.close();
151: fileData = new FileDataImpl(temp);
152: }
153:
154: try {
155: response.setContentType("text/html");
156:
157: URI connectedUser = ((KernelService) Libresource
158: .getService(KernelConstants.SERVICE))
159: .getConnectedResource();
160: response
161: .getWriter()
162: .println(
163: "<html><head><base href=\"http://"
164: + request.getServerName()
165: + ":"
166: + request.getServerPort()
167: + request.getContextPath()
168: + "/\"/><link rel=\"stylesheet\" type=\"text/css\" href=\""
169: + JspFunctions.getSkin(
170: connectedUser).getCss()
171: + "\" media=\"screen\"/></head><body><h1 class=\"heading-1\">Importing "
172: + fileName + " (" + size
173: + " o.) at " + uri + " (you are "
174: + connectedUser + ") </h1><ul>");
175:
176: System.out.println("IMPORT, createProfile = "
177: + createProfile);
178: new LibresourceImportExport(createProfile).importXML(
179: new URI(uri), fileData.getInputStream(),
180: new WebImportExportLogger(response));
181:
182: response
183: .getWriter()
184: .println(
185: "</ul><div><a href=\""
186: + ((new URI(uri).getPath().length() > 0) ? new URI(
187: uri).getPath().substring(1)
188: : "")
189: + "?action=indexation\">Go to "
190: + uri + "</a></div></body></html>");
191:
192: for (int i = 0; i < 100; i++)
193: response.getWriter().println(
194: " ");
195: } catch (Exception e) {
196: response.getWriter().println("<pre class=\"error\">");
197:
198: // e.printStackTrace(new PrintWriter(response.getWriter()));
199: StringBuffer buffer = new StringBuffer();
200: buffer.append(e.getMessage() + "<br>");
201:
202: Throwable cause = null;
203:
204: while ((cause = e.getCause()) != null) {
205: buffer.append("<i>Caused by :</i> "
206: + cause.getMessage() + "<br>");
207: e = (Exception) cause;
208: }
209:
210: response.getWriter().println(buffer.toString());
211: response.getWriter().println("</pre>");
212: response
213: .getWriter()
214: .println(
215: "<a href=\"/\">Return to Platform Home</a></body></html>");
216: }
217:
218: response.getWriter().flush();
219: response.flushBuffer();
220: response.getWriter().close();
221: }
222: }
|