KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > module > CofaxToolsExtUpload


1 /*
2  * CofaxToolsExtUpload is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/module/CofaxToolsExtUpload.java,v 1.4.2.1 2006/12/11 16:31:50 fxrobin Exp $
21  */

22
23 package org.cofax.module;
24
25 import org.cofax.*;
26 import org.cofax.cms.*;
27
28 import javax.servlet.http.*;
29 import java.io.*;
30 import java.util.*;
31
32 /**
33  * CofaxToolsExtUpload This module allows to upload files on the server
34  *
35  * @author - Badr Chentouf
36  *
37  */

38
39 public class CofaxToolsExtUpload extends CofaxToolsExtModule {
40
41     /*
42      * must be done before launch the database must me initialized with the
43      * following script
44      *
45      * INSERT INTO tblmodules (moduleName, moduleClass, moduleMode,
46      * moduleModeInit, enabled) VALUES ('Téléchargement de fichiers',
47      * 'org.cofax.module.CofaxToolsExtUpload', 'imageupload_',
48      * 'imageupload_edit_imageupload', 1);
49      *
50      * insert into tblpermmodes (modes,type) select
51      * 'imageupload_edit_imageupload' as ua, groupTypeID from tblpermgrouptype ;
52      *
53      * insert into tblpermmodes (modes,type) select
54      * 'imageupload_upload_imageupload' as ua, groupTypeID from tblpermgrouptype ;
55      *
56      * insert into tblpermmodes (modes,type) select
57      * 'imageupload_view_imageupload' as ua, groupTypeID from tblpermgrouptype ;
58      *
59      * insert into tblpermmodes (modes,type) select
60      * 'imageupload_delete_imageupload' as ua, groupTypeID from tblpermgrouptype ;
61      *
62      */

63
64     /**
65      * navigate throught the different modes for polls
66      *
67      */

68     public String JavaDoc navigate(DataStore db, CofaxPage page, HttpServletRequest req, HttpServletResponse res, HttpSession session) {
69         String JavaDoc mode = (String JavaDoc) req.getParameter("mode");
70
71         String JavaDoc contentType = (String JavaDoc) req.getHeader("content-type");
72         if (contentType != null) {
73             if (contentType.indexOf("multipart/form-data") > -1) {
74                 mode = (String JavaDoc) req.getAttribute("mode");
75             }
76         }
77
78         HashMap ht = new HashMap();
79         CofaxToolsUser user = (CofaxToolsUser) (session.getAttribute("user"));
80         String JavaDoc workingPubName = (String JavaDoc) user.workingPubName;
81
82         // newFilePath = [staticFolder]/[workingPubName]/images/[username]/
83
String JavaDoc newFilePath = CofaxToolsServlet.staticFolder;
84         if (!(newFilePath.endsWith(File.separator)))
85             newFilePath += File.separator;
86         newFilePath += user.workingPubName + File.separator;
87         // FX :
88
newFilePath += CofaxToolsServlet.userImageFolder + File.separator;
89         newFilePath += (String JavaDoc) user.userInfoHash.get("USERNAME") + File.separator;
90
91         CofaxToolsUtil.log("Path to store : " + newFilePath);
92
93         page.putGlossaryValue("system:message", getI18NMessage("message_welcome"));
94
95         page.putGlossaryValue("system:highLightTab", "admin");
96         page.putGlossaryValue("request:pubName", workingPubName);
97
98         if (mode.equals("imageupload_edit_imageupload")) {
99             CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "module/editImageupload.jsp", req, res, session); // FX :
100
// added
101
// templatePath
102
return "";
103         }
104
105         CofaxToolsUtil.log("Entering mode : " + mode);
106
107         if (mode.equals("imageupload_upload_imageupload")) {
108             CofaxToolsUtil.log("uploading done ");
109             String JavaDoc errorMessage = "";
110             boolean uploaded = false;
111             String JavaDoc uploadfile = (String JavaDoc) req.getAttribute("uploadfile");
112             CofaxToolsUtil.log("upload file : " + uploadfile);
113             if (uploadfile == null)
114                 uploadfile = "";
115             if (uploadfile.length() >= 5) {
116                 String JavaDoc fileName = uploadfile; // FX : init with the right
117
// value
118
if (uploadfile.lastIndexOf("/") > -1) {
119                     fileName = uploadfile.substring(uploadfile.lastIndexOf("/") + 1);
120                 }
121                 if (uploadfile.lastIndexOf("\\") > -1) {
122                     fileName = uploadfile.substring(uploadfile.lastIndexOf("\\") + 1);
123                 }
124                 // replace all specific caracters
125
fileName = CofaxToolsUtil.replace(fileName, " ", "_");
126                 fileName = CofaxToolsUtil.replace(fileName, "é", "e");
127                 fileName = CofaxToolsUtil.replace(fileName, "è", "e");
128                 fileName = CofaxToolsUtil.replace(fileName, "à", "a");
129                 fileName = CofaxToolsUtil.replace(fileName, "ù", "u");
130                 fileName = CofaxToolsUtil.replace(fileName, "î", "i");
131                 fileName = CofaxToolsUtil.replace(fileName, "û", "u");
132                 fileName = CofaxToolsUtil.replace(fileName, "'", "_");
133
134                 File createPath = new File(newFilePath);
135
136                 CofaxToolsUtil.log("Direcotory to store : " + createPath);
137                 CofaxToolsUtil.log("Directory exists ? " + createPath.exists());
138
139                 if (!createPath.exists()) {
140                     CofaxToolsUtil.log("CofaxToolsExtUpload: creating directory " + newFilePath);
141                     boolean createdDir = false;
142                     try {
143                         createdDir = createPath.mkdirs();
144                     } catch (Exception JavaDoc e) {
145                         CofaxToolsUtil.log("CofaxToolsExtUpload creating " + newFilePath + "threw an exception: " + e);
146                         errorMessage += "<br>ERROR creating " + newFilePath + "threw an exception: " + e;
147                     }
148                     if (!createdDir) {
149                         CofaxToolsUtil.log("CofaxToolsExtUpload ERROR creating dir:" + newFilePath);
150                         errorMessage += "<br>ERROR creating dir:" + newFilePath;
151                     }
152                 }
153                 String JavaDoc orgFilePath = CofaxToolsServlet.fileTransferFolder;
154                 if (!(orgFilePath.endsWith(File.separator)))
155                     orgFilePath += File.separator;
156                 orgFilePath += CofaxToolsFTP.stripPath(uploadfile);
157                 File orgFile = new File(orgFilePath);
158
159                 File newFile = new File(newFilePath + File.separator + fileName);
160
161                 CofaxToolsUtil.log("orgFile : " + orgFile);
162                 CofaxToolsUtil.log("newFile : " + newFile);
163
164                 if (newFile.exists()) {
165                     newFile.delete();
166                 }
167                 try {
168                     uploaded = orgFile.renameTo(newFile);
169                     CofaxToolsUtil.log("Renaming OK");
170                 } catch (Exception JavaDoc e) {
171                     CofaxToolsUtil.log("CofaxToolsExtUpload: failed remaning fileName." + e.toString());
172                     errorMessage += "<br>ERROR failed remaning fileName." + e.toString();
173                 }
174                 page.putGlossaryValue("system:errorMessage", errorMessage);
175                 page.putGlossaryValue("system:uploaded", uploaded + "");
176             }
177             CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "module/editImageupload_step2.jsp", req, res, session); // FX :
178
// added
179
// templatePath
180
return "";
181         }
182
183         if (mode.equals("imageupload_view_imageupload")) {
184             page.putGlossaryValue("request:localImportDir", newFilePath);
185             page.putGlossaryValue("request:userName", (String JavaDoc) user.userInfoHash.get("USERNAME"));
186
187             CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "module/editImageupload_step3.jsp", req, res, session); // FX :
188
// added
189
// templatePath
190
return "";
191         }
192
193         if (mode.equals("imageupload_delete_imageupload")) {
194             File localDir = new File(newFilePath);
195             String JavaDoc[] localFileList = localDir.list();
196             if ((localFileList != null) && (localFileList.length != 0)) {
197                 for (int i = 0; i < localFileList.length; i++) {
198                     String JavaDoc filename = localFileList[i];
199                     String JavaDoc delete = (String JavaDoc) req.getParameter("" + i);
200                     if ((delete != null) && (delete.equals("on"))) {
201                         File newFile = new File(newFilePath + File.separator + filename);
202                         if (newFile.exists()) {
203                             newFile.delete();
204                         }
205                     }
206                 }
207             }
208             page.putGlossaryValue("request:localImportDir", newFilePath);
209             page.putGlossaryValue("request:userName", (String JavaDoc) user.userInfoHash.get("USERNAME"));
210             CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "module/editImageupload_step3.jsp", req, res, session); // FX :
211
// added
212
// templatePath
213
return "";
214         }
215
216         return "";
217     }
218
219     public String JavaDoc formatString(String JavaDoc input) {
220         String JavaDoc retVal = CofaxToolsUtil.replace(input, "'", "''");
221         retVal = CofaxToolsUtil.replace(retVal, "\\", "\\\\");
222         return retVal;
223     }
224
225     public String JavaDoc getI18NMessage(String JavaDoc message) {
226         ResourceBundle messages;
227         String JavaDoc returnMessage = "";
228         Locale lcl = CofaxToolsServlet.lcl;
229         try {
230             messages = ResourceBundle.getBundle("org.cofax.module.upload", lcl);
231             returnMessage = messages.getString(message);
232         } catch (Exception JavaDoc e) {
233             CofaxToolsUtil.log("CofaxToolsExtUpload : getI18NMessage : error while reading " + message);
234         }
235         return (returnMessage);
236     }
237
238 }
239
Popular Tags