Index: lams_central/src/java/org/lamsfoundation/lams/web/LAMSConnectorServlet.java
===================================================================
diff -u -r8179037dc958585c054547eb7de9be14c6aed233 -rf256bc13ec7422b5803fee9364138983e1004a00
--- lams_central/src/java/org/lamsfoundation/lams/web/LAMSConnectorServlet.java (.../LAMSConnectorServlet.java) (revision 8179037dc958585c054547eb7de9be14c6aed233)
+++ lams_central/src/java/org/lamsfoundation/lams/web/LAMSConnectorServlet.java (.../LAMSConnectorServlet.java) (revision f256bc13ec7422b5803fee9364138983e1004a00)
@@ -1,41 +1,26 @@
/*
- * FCKeditor - The text editor for internet
- * Copyright (C) 2003-2005 Frederico Caldeira Knabben
- *
- * Licensed under the terms of the GNU Lesser General Public License:
- * http://www.opensource.org/licenses/lgpl-license.php
- *
- * For further information visit:
- * http://www.fckeditor.net/
- *
- * File Name: ConnectorServlet.java
- * Java Connector for Resource Manager class.
- *
- * Version: 2.3
- * Modified: 2005-08-11 16:29:00
- *
- * File Authors:
- * Simone Chiaretta (simo@users.sourceforge.net)
- */
-
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
+For licensing, see LICENSE.html or http://ckeditor.com/license
+*/
+
package org.lamsfoundation.lams.web;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
+import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -44,420 +29,447 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
-import org.jfree.util.Log;
+import org.apache.commons.fileupload.DiskFileUpload;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.commons.io.IOUtils;
+import org.apache.log4j.Logger;
import org.lamsfoundation.lams.authoring.web.AuthoringConstants;
import org.lamsfoundation.lams.usermanagement.dto.UserDTO;
-import org.lamsfoundation.lams.util.Base64StringToImageUtil;
import org.lamsfoundation.lams.util.Configuration;
import org.lamsfoundation.lams.util.ConfigurationKeys;
import org.lamsfoundation.lams.util.FileUtil;
import org.lamsfoundation.lams.web.session.SessionManager;
import org.lamsfoundation.lams.web.util.AttributeNames;
-
-import org.apache.commons.fileupload.DiskFileUpload;
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileUploadException;
-import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* Servlet to upload and browse files.
- *
- * This servlet accepts 4 commands used to retrieve and create files and folders from a server directory.
- * The allowed commands are:
+ *
+ * This servlet accepts 4 commands used to retrieve and create files and folders from a server directory. The allowed
+ * commands are:
*
* - GetFolders: Retrive the list of directory under the current folder
*
- GetFoldersAndFiles: Retrive the list of files and directory under the current folder
*
- CreateFolder: Create a new directory under the current folder
*
- FileUpload: Send a new file to the server (must be sent with a POST)
*
- *
- * This servlet has been modified for LAMS to support the lams_www/secure/[design folder] format. The design folder is
- * a folder with a unique numeric name. Whenever a new design is created (using "New" in the client), a new design
- * folder name is assigned. The [design folder] is passed in as DesignFolder and the CurrentFolder is the folder within
- * the DesignFolder (but not the type folder). This support the multi-level folders that are available in the File browser
+ *
+ * This servlet has been modified for LAMS to support the lams_www/secure/[design folder] format. The design folder is a
+ * folder with a unique numeric name. Whenever a new design is created (using "New" in the client), a new design folder
+ * name is assigned. The [design folder] is passed in as DesignFolder and the CurrentFolder is the folder within the
+ * DesignFolder (but not the type folder). This support the multi-level folders that are available in the File browser
* but is different to the LAMSUploadServlet.
*
- * @author Simone Chiaretta (simo@users.sourceforge.net)
+ * @author Simone Chiaretta (simo@users.sourceforge.net)
* @author Mitchell Seaton
*
* @web:servlet name="Connector" load-on-startup = "1"
- * @web.servlet-init-param name = "baseDir"
- * value = "secure"
- * @web.servlet-init-param name = "debug"
- * value = "true"
- *
+ * @web.servlet-init-param name = "baseDir" value = "secure"
+ * @web.servlet-init-param name = "debug" value = "true"
+ *
* @web:servlet-mapping url-pattern="/ckeditor/filemanager/browser/default/connectors/jsp/connector"
-
*/
public class LAMSConnectorServlet extends HttpServlet {
-
- private static String baseDir;
- private String realBaseDir;
- private String lamsContextPath;
- private static boolean debug=false;
-
- /**
- * Initialize the servlet.
- * Retrieve from the servlet configuration the "baseDir" which is the root of the file repository:
- * If not specified the value of "/UserFiles/" will be used.
- *
- */
- public void init() throws ServletException {
- baseDir=getInitParameter("baseDir");
- debug=(new Boolean(getInitParameter("debug"))).booleanValue();
-
- if(baseDir==null)
- baseDir="secure";
-
- getConfigKeyValues();
-
- File baseFile=new File(realBaseDir);
- if(!baseFile.exists()){
- baseFile.mkdir();
- }
+
+ private static final Logger log = Logger.getLogger(LAMSConnectorServlet.class);
+
+ private static String baseDir;
+ private static boolean debug = false;
+
+ private String realBaseDir;
+ private String lamsContextPath;
+
+ /**
+ * Initialize the servlet.
+ * Retrieve from the servlet configuration the "baseDir" which is the root of the file repository:
+ * If not specified the value of "/UserFiles/" will be used.
+ *
+ */
+ @Override
+ public void init() throws ServletException {
+ LAMSConnectorServlet.baseDir = getInitParameter("baseDir");
+ debug = (new Boolean(getInitParameter("debug"))).booleanValue() && log.isDebugEnabled();
+
+ if (LAMSConnectorServlet.baseDir == null) {
+ LAMSConnectorServlet.baseDir = "secure";
}
-
- /**
- * Manage the Get requests (GetFolders, GetFoldersAndFiles, CreateFolder).
- *
- * The servlet accepts commands sent in the following format:
- * connector?Command=CommandName&Type=ResourceType&CurrentFolder=FolderPath
- * It execute the command and then return the results to the client in XML format.
- *
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- if (debug) System.out.println("--- BEGIN DOGET ---");
- // get realBaseDir and lamsContextPath at request time from config values in memory
- getConfigKeyValues();
-
- response.setContentType("text/xml; charset=UTF-8");
- response.setHeader("Cache-Control","no-cache");
- PrintWriter out = response.getWriter();
-
- String commandStr=request.getParameter("Command");
- String typeStr=request.getParameter("Type");
- String currentFolderStr=request.getParameter("CurrentFolder");
- String designFolder=request.getParameter("DesignFolder");
-
- // create content directory if non-existant
- String currentDirPath=realBaseDir + designFolder + typeStr +"/" + currentFolderStr;
- String validCurrentDirPath = currentDirPath.replace('/', File.separatorChar);
-
- String currentWebPath= lamsContextPath + AuthoringConstants.LAMS_WWW_FOLDER + FileUtil.LAMS_WWW_SECURE_DIR
- + designFolder + typeStr +"/" + currentFolderStr;
-
- File currentDir=new File(validCurrentDirPath);
- if(!currentDir.exists()){
- currentDir.mkdirs();
- }
-
- Document document=null;
- try {
- DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- document=builder.newDocument();
- } catch (ParserConfigurationException pce) {
- pce.printStackTrace();
- }
-
- Node root=CreateCommonXml(document,commandStr,typeStr,currentFolderStr,currentWebPath);
-
- if (debug) System.out.println("Command = " + commandStr);
-
- if(commandStr.equals("GetFolders")) {
- getFolders(currentDir,root,document);
- }
- else if (commandStr.equals("GetFoldersAndFiles")) {
- getFolders(currentDir,root,document);
- getFiles(currentDir,root,document);
- }
- else if (commandStr.equals("CreateFolder")) {
- String newFolderStr=request.getParameter("NewFolderName");
- File newFolder=new File(currentDir,newFolderStr);
- String retValue="110";
-
- if(newFolder.exists()){
- retValue="101";
- }
- else {
- try {
- boolean dirCreated = newFolder.mkdir();
- if(dirCreated)
- retValue="0";
- else
- retValue="102";
- }catch(SecurityException sex) {
- retValue="103";
- }
-
- }
- setCreateFolderResponse(retValue,root,document);
- } else if (commandStr.equals("DeleteFile")) {
- String fileName=request.getParameter("fileName");
- File fileToDelete = new File(currentDir,fileName);
+ getConfigKeyValues();
- if(fileToDelete.isFile()) fileToDelete.delete();
-
- } else if(commandStr.equals("DeleteFolder")) {
- String folderName=request.getParameter("folderName");
- File folderToDelete = new File(currentDir,folderName);
+ File baseFile = new File(realBaseDir);
+ if (!baseFile.exists()) {
+ baseFile.mkdir();
+ }
+ }
- if(folderToDelete.isDirectory()) folderToDelete.delete();
- }
-
- document.getDocumentElement().normalize();
- try {
- TransformerFactory tFactory = TransformerFactory.newInstance();
- Transformer transformer = tFactory.newTransformer();
-
- DOMSource source = new DOMSource(document);
+ /**
+ * Manage the Get requests (GetFolders, GetFoldersAndFiles, CreateFolder).
+ *
+ * The servlet accepts commands sent in the following format:
+ * connector?Command=CommandName&Type=ResourceType&CurrentFolder=FolderPath
+ *
+ * It execute the command and then return the results to the client in XML format.
+ *
+ */
+ @Override
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- StreamResult result = new StreamResult(out);
- transformer.transform(source, result);
-
- if (debug) {
- StreamResult dbgResult = new StreamResult(System.out);
- transformer.transform(source, dbgResult);
- System.out.println("");
- System.out.println("--- END DOGET ---");
- }
-
-
- } catch (Exception ex) {
- ex.printStackTrace();
- }
-
-
- out.flush();
- out.close();
+ if (LAMSConnectorServlet.debug) {
+ log.debug("Browse started");
}
-
- /**
- * Manage the Post requests (FileUpload).
- *
- * The servlet accepts commands sent in the following format:
- * connector?Command=FileUpload&Type=ResourceType&CurrentFolder=FolderPath
- * It store the file (renaming it in case a file with the same name exists) and then return an HTML file
- * with a javascript command in it.
- *
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ // get realBaseDir and lamsContextPath at request time from config values in memory
+ getConfigKeyValues();
- if (debug) System.out.println("--- BEGIN DOPOST ---");
+ String commandStr = request.getParameter("Command");
+ String typeStr = request.getParameter("Type");
+ String currentFolderStr = request.getParameter("CurrentFolder");
+ String designFolder = request.getParameter("DesignFolder");
- response.setContentType("text/html; charset=UTF-8");
- response.setHeader("Cache-Control","no-cache");
- PrintWriter out = response.getWriter();
-
- String commandStr=request.getParameter("Command");
- String typeStr=request.getParameter("Type");
- String currentFolderStr=request.getParameter("CurrentFolder");
- String designFolder=request.getParameter("DesignFolder");
-
- String currentDirPath=realBaseDir + designFolder + typeStr +"/" + currentFolderStr;
- String validCurrentDirPath = currentDirPath.replace("/", File.separator);
-
- if (debug) System.out.println(currentDirPath);
-
- String retVal="0";
- String newName="";
-
+ // create content directory if non-existant
+ String currentDirPath = realBaseDir + designFolder + typeStr + "/" + currentFolderStr;
+ String validCurrentDirPath = currentDirPath.replace('/', File.separatorChar);
+
+ String currentWebPath = lamsContextPath + AuthoringConstants.LAMS_WWW_FOLDER + FileUtil.LAMS_WWW_SECURE_DIR
+ + designFolder + typeStr + "/" + currentFolderStr;
+
+ File currentDir = new File(validCurrentDirPath);
+ if (!currentDir.exists()) {
+ currentDir.mkdirs();
+ }
+
+ Document document = null;
+ try {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ document = builder.newDocument();
+ } catch (ParserConfigurationException e) {
+ log.error(e);
+ }
+
+ Node root = CreateCommonXml(document, commandStr, typeStr, currentFolderStr, currentWebPath);
+
+ if (LAMSConnectorServlet.debug) {
+ log.debug("Command = " + commandStr);
+ }
+
+ if (commandStr.equals("GetFolders")) {
+ getFolders(currentDir, root, document);
+ } else if (commandStr.equals("GetFoldersAndFiles")) {
+ getFolders(currentDir, root, document);
+ getFiles(currentDir, root, document);
+ } else if (commandStr.equals("CreateFolder")) {
+ String newFolderStr = request.getParameter("NewFolderName");
+ File newFolder = new File(currentDir, newFolderStr);
+ String retValue = "110";
+
+ if (newFolder.exists()) {
+ retValue = "101";
+ } else {
try {
- if(commandStr.equals("PaintUpload"))
- newName = createNewPaint(validCurrentDirPath, request);
- else if(!commandStr.equals("FileUpload") || !isEnabled(currentFolderStr))
- throw new Exception("Illegal command.");
- else
- newName = createNewFile(validCurrentDirPath, request);
-
- retVal="201";
- } catch(Exception ex) {
- if(debug) System.out.println(ex.getMessage());
- retVal="203";
+ boolean dirCreated = newFolder.mkdir();
+ if (dirCreated) {
+ retValue = "0";
+ } else {
+ retValue = "102";
+ }
+ } catch (SecurityException sex) {
+ retValue = "103";
}
-
- if(!commandStr.equals("PaintUpload")) {
- out.println("");
- out.flush();
- out.close();
- } else {
- // send back URL to new Paint file
- String currentWebPath= lamsContextPath + AuthoringConstants.LAMS_WWW_FOLDER + FileUtil.LAMS_WWW_SECURE_DIR
- + designFolder + typeStr +"/" + currentFolderStr;
-
- out.println( currentWebPath + newName);
- }
-
- if (debug) System.out.println("--- END DOPOST ---");
-
+
+ }
+ setCreateFolderResponse(retValue, root, document);
+ } else if (commandStr.equals("DeleteFile")) {
+ String fileName = request.getParameter("fileName");
+ File fileToDelete = new File(currentDir, fileName);
+
+ if (fileToDelete.isFile()) {
+ fileToDelete.delete();
+ }
+
+ } else if (commandStr.equals("DeleteFolder")) {
+ String folderName = request.getParameter("folderName");
+ File folderToDelete = new File(currentDir, folderName);
+
+ if (folderToDelete.isDirectory()) {
+ folderToDelete.delete();
+ }
}
-
- private String createNewFile(String validCurrentDirPath, HttpServletRequest request) throws FileUploadException, IOException, Exception {
- if (debug) System.out.println("--- BEGIN FILE SAVE ---");
- String newName = "";
- DiskFileUpload upload = new DiskFileUpload();
- List items = upload.parseRequest(request);
-
- Map fields=new HashMap();
-
- Iterator iter = items.iterator();
- while (iter.hasNext()) {
- FileItem item = (FileItem) iter.next();
- if (item.isFormField())
- fields.put(item.getFieldName(),item.getString());
- else
- fields.put(item.getFieldName(),item);
- }
-
- FileItem uplFile = (FileItem)fields.get("NewFile");
- String fileNameLong = uplFile.getName();
- fileNameLong = fileNameLong.replace('\\','/');
- String[] pathParts = fileNameLong.split("/");
- String fileName = pathParts[pathParts.length-1];
-
- String nameWithoutExt = getNameWithoutExtension(fileName);
- String ext = getExtension(fileName);
-
- File pathToSave = new File(validCurrentDirPath, fileName);
-
- int counter=1;
- while(pathToSave.exists()){
- fileName = nameWithoutExt+"_"+counter+"."+ext;
- pathToSave = new File(validCurrentDirPath, fileName);
- counter++;
- }
-
- uplFile.write(pathToSave);
-
- if (debug) System.out.println("--- END FILE SAVE ---");
-
- return fileName;
+
+ document.getDocumentElement().normalize();
+
+ try {
+ TransformerFactory tFactory = TransformerFactory.newInstance();
+ Transformer transformer = tFactory.newTransformer();
+
+ DOMSource source = new DOMSource(document);
+
+ response.setContentType("text/xml; charset=UTF-8");
+ response.setHeader("Cache-Control", "no-cache");
+ PrintWriter out = response.getWriter();
+
+ StreamResult result = new StreamResult(out);
+ transformer.transform(source, result);
+
+ out.flush();
+ out.close();
+
+ if (LAMSConnectorServlet.debug) {
+ StreamResult dbgResult = new StreamResult(System.out);
+ transformer.transform(source, dbgResult);
+ log.debug("Browse finished");
+ }
+ } catch (Exception e) {
+ log.error(e);
}
+ }
- private String createNewPaint(String validCurrentDirPath, HttpServletRequest request) throws IOException {
- if (debug) System.out.println("--- BEGIN PAINT SAVE ---");
-
- String extension = "png";
- String nameWithoutExt = getUserID() + "_" + String.valueOf(new Date().getTime());
- String fileName = nameWithoutExt + "." + extension;
-
- File dir = new File(validCurrentDirPath);
- File pathToSave = new File(validCurrentDirPath, fileName);
-
- int counter=1;
- while(pathToSave.exists()){
- fileName = nameWithoutExt+"_"+counter+"."+extension;
- pathToSave = new File(validCurrentDirPath, fileName);
- counter++;
- }
+ /**
+ * Manage the Post requests (FileUpload).
+ *
+ * The servlet accepts commands sent in the following format:
+ * connector?Command=FileUpload&Type=ResourceType&CurrentFolder=FolderPath
+ *
+ * It store the file (renaming it in case a file with the same name exists) and then return an HTML file with a
+ * javascript command in it.
+ *
+ */
+ @Override
+ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- if(!dir.exists()) dir.mkdirs();
-
- InputStream is = request.getInputStream();
- FileOutputStream fileos = new FileOutputStream(pathToSave);
-
- byte[] bs = IOUtils.toByteArray(is);
-
- fileos.write(bs);
-
- fileos.flush();
- fileos.close();
-
- if (debug) System.out.println("--- END PAINT SAVE ---");
-
- return fileName;
+ if (LAMSConnectorServlet.debug) {
+ log.debug("Upload started");
}
-
- private void setCreateFolderResponse(String retValue,Node root,Document doc) {
- Element myEl=doc.createElement("Error");
- myEl.setAttribute("number",retValue);
- root.appendChild(myEl);
+
+ String commandStr = request.getParameter("Command");
+ String typeStr = request.getParameter("Type");
+ String currentFolderStr = request.getParameter("CurrentFolder");
+ String designFolder = request.getParameter("DesignFolder");
+
+ String currentDirPath = realBaseDir + designFolder + typeStr + "/" + currentFolderStr;
+ String validCurrentDirPath = currentDirPath.replace("/", File.separator);
+
+ if (LAMSConnectorServlet.debug) {
+ log.debug(currentDirPath);
}
-
- private void getFolders(File dir,Node root,Document doc) {
- Element folders=doc.createElement("Folders");
- root.appendChild(folders);
- File[] fileList=dir.listFiles();
- for(int i=0;i");
+ out.println("window.parent.frames['frmUpload'].OnUploadCompleted(" + retVal + ",'" + newName + "');");
+ out.println("");
+
+ } else {
+ // send back URL to new Paint file
+ String currentWebPath = lamsContextPath + AuthoringConstants.LAMS_WWW_FOLDER + FileUtil.LAMS_WWW_SECURE_DIR
+ + designFolder + typeStr + "/" + currentFolderStr;
+ out.println(currentWebPath + newName);
}
-
- /*
- * This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF bug #991489
- */
- private static String getNameWithoutExtension(String fileName) {
- return fileName.substring(0, fileName.lastIndexOf("."));
- }
-
- /*
- * This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF bug #991489
- */
- private String getExtension(String fileName) {
- return fileName.substring(fileName.lastIndexOf(".")+1);
+
+ out.flush();
+ out.close();
+
+ if (LAMSConnectorServlet.debug) {
+ log.debug("Upload finished");
}
-
- private boolean isEnabled(String contentID) {
- if(contentID.equals("/-1/"))
- return false;
- else
- return true;
+ }
+
+ private String createNewFile(String validCurrentDirPath, HttpServletRequest request, StringBuilder retVal)
+ throws FileUploadException, IOException, Exception {
+ if (LAMSConnectorServlet.debug) {
+ log.debug("File save started");
}
-
- private void getConfigKeyValues() {
- realBaseDir = Configuration.get(ConfigurationKeys.LAMS_EAR_DIR) + File.separator + FileUtil.LAMS_WWW_DIR + File.separator + baseDir;
- lamsContextPath = "/" + Configuration.get(ConfigurationKeys.SERVER_URL_CONTEXT_PATH) +"/";
- }
-
- private String getUserID() {
- HttpSession ss = SessionManager.getSession();
- UserDTO user = (UserDTO) ss.getAttribute(AttributeNames.USER);
- return user != null ? user.getUserID().toString() : "";
+ String newName = "";
+ DiskFileUpload upload = new DiskFileUpload();
+ List items = upload.parseRequest(request);
+
+ Map fields = new HashMap();
+
+ Iterator iter = items.iterator();
+ while (iter.hasNext()) {
+ FileItem item = (FileItem) iter.next();
+ if (item.isFormField()) {
+ fields.put(item.getFieldName(), item.getString());
+ } else {
+ fields.put(item.getFieldName(), item);
+ }
}
-}
+ FileItem uplFile = (FileItem) fields.get("NewFile");
+ String fileNameLong = uplFile.getName();
+ fileNameLong = fileNameLong.replace('\\', '/');
+ String[] pathParts = fileNameLong.split("/");
+ String fileName = pathParts[pathParts.length - 1];
+ String nameWithoutExt = LAMSConnectorServlet.getNameWithoutExtension(fileName);
+ String ext = getExtension(fileName);
+
+ File pathToSave = new File(validCurrentDirPath, fileName);
+
+ int counter = 1;
+ while (pathToSave.exists()) {
+ fileName = nameWithoutExt + "_" + counter + "." + ext;
+ pathToSave = new File(validCurrentDirPath, fileName);
+ counter++;
+ }
+
+ uplFile.write(pathToSave);
+
+ if (counter > 1) {
+ retVal.append("201");
+ } else {
+ retVal.append("0");
+ }
+
+ if (LAMSConnectorServlet.debug) {
+ log.debug("File save finished");
+ }
+
+ return fileName;
+ }
+
+ private String createNewPaint(String validCurrentDirPath, HttpServletRequest request, StringBuilder retVal)
+ throws IOException {
+ if (LAMSConnectorServlet.debug) {
+ log.debug("Paint save started");
+ }
+
+ String extension = "png";
+ String nameWithoutExt = getUserID() + "_" + String.valueOf(new Date().getTime());
+ String fileName = nameWithoutExt + "." + extension;
+
+ File dir = new File(validCurrentDirPath);
+ File pathToSave = new File(validCurrentDirPath, fileName);
+
+ int counter = 1;
+ while (pathToSave.exists()) {
+ fileName = nameWithoutExt + "_" + counter + "." + extension;
+ pathToSave = new File(validCurrentDirPath, fileName);
+ counter++;
+ }
+
+ if (!dir.exists()) {
+ dir.mkdirs();
+ }
+
+ InputStream is = request.getInputStream();
+ FileOutputStream fileos = new FileOutputStream(pathToSave);
+
+ byte[] bs = IOUtils.toByteArray(is);
+
+ fileos.write(bs);
+
+ fileos.flush();
+ fileos.close();
+
+ if (counter > 1) {
+ retVal.append("201");
+ } else {
+ retVal.append("0");
+ }
+
+ if (LAMSConnectorServlet.debug) {
+ log.debug("Paint save finished");
+ }
+
+ return fileName;
+ }
+
+ private void setCreateFolderResponse(String retValue, Node root, Document doc) {
+ Element myEl = doc.createElement("Error");
+ myEl.setAttribute("number", retValue);
+ root.appendChild(myEl);
+ }
+
+ private void getFolders(File dir, Node root, Document doc) {
+ Element folders = doc.createElement("Folders");
+ root.appendChild(folders);
+ File[] fileList = dir.listFiles();
+ for (int i = 0; i < fileList.length; ++i) {
+ if (fileList[i].isDirectory()) {
+ Element myEl = doc.createElement("Folder");
+ myEl.setAttribute("name", fileList[i].getName());
+ folders.appendChild(myEl);
+ }
+ }
+ }
+
+ private void getFiles(File dir, Node root, Document doc) {
+ Element files = doc.createElement("Files");
+ root.appendChild(files);
+ File[] fileList = dir.listFiles();
+ for (int i = 0; i < fileList.length; ++i) {
+ if (fileList[i].isFile()) {
+ Element myEl = doc.createElement("File");
+ myEl.setAttribute("name", fileList[i].getName());
+ myEl.setAttribute("size", "" + fileList[i].length() / 1024);
+ files.appendChild(myEl);
+ }
+ }
+ }
+
+ private Node CreateCommonXml(Document doc, String commandStr, String typeStr, String currentPath, String currentUrl) {
+
+ Element root = doc.createElement("Connector");
+ doc.appendChild(root);
+ root.setAttribute("command", commandStr);
+ root.setAttribute("resourceType", typeStr);
+
+ Element myEl = doc.createElement("CurrentFolder");
+ myEl.setAttribute("path", currentPath);
+ myEl.setAttribute("url", currentUrl);
+ root.appendChild(myEl);
+
+ return root;
+
+ }
+
+ /*
+ * This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF bug #991489
+ */
+ private static String getNameWithoutExtension(String fileName) {
+ return fileName.substring(0, fileName.lastIndexOf("."));
+ }
+
+ /*
+ * This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF bug #991489
+ */
+ private String getExtension(String fileName) {
+ return fileName.substring(fileName.lastIndexOf(".") + 1);
+ }
+
+ private void getConfigKeyValues() {
+ realBaseDir = Configuration.get(ConfigurationKeys.LAMS_EAR_DIR) + File.separator + FileUtil.LAMS_WWW_DIR
+ + File.separator + LAMSConnectorServlet.baseDir;
+ lamsContextPath = "/" + Configuration.get(ConfigurationKeys.SERVER_URL_CONTEXT_PATH) + "/";
+ }
+
+ private String getUserID() {
+ HttpSession ss = SessionManager.getSession();
+ UserDTO user = (UserDTO) ss.getAttribute(AttributeNames.USER);
+ return user != null ? user.getUserID().toString() : "";
+ }
+}
\ No newline at end of file