hello ,
I'am a java develloper and we decide to use fck editor in a project. But we wanted to modify the connector servlet to be able to specify a directory that is outside of the application for uploaded content. The access to this content we configure a virtual directory on the server.
The aim was to separate source off the application from the content.
To realize that we add initparams in the servlet...
<servlet>
<servlet-name>Connector</servlet-name>
<servlet-class> com.fredck.FCKeditor.connector.ConnectorServlet
</servlet-class>
<init-param>
<param-name>baseDir</param-name>
<param-value>/repository/</param-value>
</init-param>
<init-param>
<param-name>extDir</param-name>
<param-value>
C:/APP_EDITOR_REPOSITORY
</param-value>
</init-param>
<init-param>
<param-name>extContext</param-name>
<param-value> /library-app-content
</param-value>
</init-param>
</servlet>
i can give the code of the servlet :
package com.fredck.FCKeditor.connector;
import org.apache.commons.fileupload.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
* Servlet to upload and browse files.<br>
*
* This servlet accepts 4 commands used to retrieve and create files and folders from a server directory.
* The allowed commands are:
* <ul>
* <li>GetFolders: Retrive the list of directory under the current folder
* <li>GetFoldersAndFiles: Retrive the list of files and directory under the current folder
* <li>CreateFolder: Create a new directory under the current folder
* <li>FileUpload: Send a new file to the server (must be sent with a POST)
* </ul>
*
* @author Simone Chiaretta (simo@users.sourceforge.net)
*/
public class ConnectorServlet extends HttpServlet {
private static String webappBaseDir;
private static String externalBaseDir;
private static String externalContextPath;
private static boolean isDirExternal = false;
private static boolean debug = false;
private static ServletContext servletContext;
/**
* Initialize the servlet.<br>
* Retrieve from the servlet configuration the "baseDir" which is the root of the file repository:<br>
* If not specified the value of "/UserFiles/" will be used.
*
*/
public void init(ServletConfig servConfig) throws ServletException {
debug = (new Boolean(servConfig.getInitParameter("debug"))).booleanValue();
if (debug) {
System.out.println("--- BEGIN INIT ---");
}
servletContext = servConfig.getServletContext();
String realBaseDir = "";
webappBaseDir = servConfig.getInitParameter("baseDir");
if (webappBaseDir == null) {
webappBaseDir = "/UserFiles/";
}
if (servConfig.getInitParameter("extDir") == null) {
isDirExternal = false;
realBaseDir = servletContext.getRealPath(webappBaseDir);
} else {
isDirExternal = true;
externalBaseDir = servConfig.getInitParameter("extDir");
realBaseDir = externalBaseDir + webappBaseDir;
externalContextPath = servConfig.getInitParameter("extContext");
}
File baseFile = new File(realBaseDir);
if (!baseFile.exists()) {
baseFile.mkdir();
}
if (debug) {
System.out.println("webappBaseDir : "+webappBaseDir);
System.out.println("isDirExternal : "+isDirExternal);
System.out.println("externalBaseDir : "+externalBaseDir);
System.out.println("externalContextPath : "+externalContextPath);
System.out.println("--- END INIT ---");
}
}
/**
* Manage the Get requests (GetFolders, GetFoldersAndFiles, CreateFolder).<br>
*
* The servlet accepts commands sent in the following format:<br>
* connector?Command=CommandName&Type=ResourceType&CurrentFolder=FolderPath<br><br>
* 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 ---");
}
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");
//current path is the path after the contextPath
String currentPath = webappBaseDir + typeStr + currentFolderStr;
File currentDir = new File(getCurrentDirPath(currentPath));
if (!currentDir.exists()) {
currentDir.mkdir();
}
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, addContextPath(currentPath, request));
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);
}
document.getDocumentElement().normalize();
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
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();
}
/**
* Manage the Post requests (FileUpload).<br>
*
* The servlet accepts commands sent in the following format:<br>
* connector?Command=FileUpload&Type=ResourceType&CurrentFolder=FolderPath<br><br>
* 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 {
if (debug) {
System.out.println("--- BEGIN DOPOST ---");
}
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");
//current path is the path after the contextPath
String currentPath = webappBaseDir + typeStr + currentFolderStr;
System.out.println(getCurrentDirPath(currentPath));
String retVal = "0";
String newName = "";
if (!commandStr.equals("FileUpload")) {
retVal = "203";
} else {
DiskFileUpload upload = new DiskFileUpload();
try {
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(getCurrentDirPath(currentPath),
fileName);
int counter = 1;
while (pathToSave.exists()) {
newName = nameWithoutExt + "(" + counter + ")" + "." + ext;
retVal = "201";
pathToSave = new File(getCurrentDirPath(currentPath),
newName);
counter++;
}
uplFile.write(pathToSave);
} catch (Exception ex) {
retVal = "203";
}
}
out.println("<script type=\"text/javascript\">");
out.println("window.parent.frames['frmUpload'].OnUploadCompleted(" +
retVal + ",'" + newName + "');");
out.println("</script>");
out.flush();
out.close();
if (debug) {
System.out.println("--- END DOPOST ---");
}
}
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);
}
/**
* this methos give the directory to where the upload is done
*/
private String getCurrentDirPath(String currentPath) {
String currentDirPath;
if (isDirExternal) {
currentDirPath = externalBaseDir + currentPath;
} else {
currentDirPath = servletContext.getRealPath(currentPath);
}
return currentDirPath;
}
/**
* this create the link with the correct contextPath
*/
private String addContextPath(String currentPath, HttpServletRequest request) {
String path;
if (isDirExternal) {
path = externalContextPath + currentPath;
} else {
path =request.getContextPath() + currentPath;
}
return path;
}
}
by the way the package name begin by upper case this does not follow java coding convention..
we also devellop a servlet and a manager to permit upload and save on the server so if you are interrested we can provide it
benjamin
Thu, 04/28/2005 - 02:26
#1