KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > util > NukeResource


1  /*
2  -- GeiNuke --
3 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
4  http://www.hostingjava.it/-geinuke/
5
6 This file is part of GeiNuke.
7
8     GeiNuke is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     GeiNuke is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with GeiNuke; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */

22 package com.geinuke.util;
23
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.Cookie JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.commons.fileupload.FileItem;
36 import org.springframework.mail.SimpleMailMessage;
37 import org.springframework.mail.javamail.JavaMailSenderImpl;
38 import org.springframework.mail.javamail.MimeMessageHelper;
39
40 import com.geinuke.bizlogic.UploadException;
41 import com.geinuke.common.NukeRoleI;
42 import com.geinuke.servlet.GeiServlet;
43 import com.geinuke.vo.RoleVO;
44
45
46 public class NukeResource {
47     public static Hashtable JavaDoc cache=null;
48     public static Hashtable JavaDoc locales=null;
49     public static ArrayList JavaDoc categoryTypes=null;
50     static{
51         cache=new Hashtable JavaDoc();
52         
53         locales=new Hashtable JavaDoc();
54         locales.put("Italiano",Locale.ITALY);
55         locales.put("English",Locale.ENGLISH);
56         /*
57         locales.put("Spain",new Locale("es","ES"));
58         locales.put("France",Locale.FRANCE);
59         */

60         categoryTypes=new ArrayList JavaDoc();
61         categoryTypes.add("NEWS");
62         categoryTypes.add("FORUM");
63         categoryTypes.add("BLOG");
64     }
65     public static ArrayList JavaDoc getResources(ServletContext JavaDoc sc, String JavaDoc initPath) {
66         ArrayList JavaDoc resList = null;
67         String JavaDoc path = sc.getRealPath(initPath);
68         File JavaDoc f = new File JavaDoc(path);
69         String JavaDoc strs[] = f.list();
70         resList = new ArrayList JavaDoc(Arrays.asList(strs));
71         return resList;
72     }
73     
74     
75     public static ArrayList JavaDoc getDirectories(ServletContext JavaDoc sc, String JavaDoc initPath) {
76         ArrayList JavaDoc resList = null;
77         String JavaDoc path = sc.getRealPath(initPath);
78         File JavaDoc f = new File JavaDoc(path);
79         String JavaDoc strs[] = f.list();
80         resList = new ArrayList JavaDoc();
81         for(int i=0;i<strs.length;i++){
82             File JavaDoc dir=new File JavaDoc(path,strs[i]);
83             if(dir.isDirectory())
84                 resList.add(strs[i]);
85         }
86         return resList;
87     }
88     
89     public static ArrayList JavaDoc getDirectories( String JavaDoc initPath) {
90         ArrayList JavaDoc resList = null;
91         
92         File JavaDoc f = new File JavaDoc(initPath);
93         String JavaDoc strs[] = f.list();
94         resList = new ArrayList JavaDoc();
95         for(int i=0;i<strs.length;i++){
96             File JavaDoc dir=new File JavaDoc(initPath,strs[i]);
97             if(dir.isDirectory())
98                 resList.add(strs[i]);
99         }
100         return resList;
101     }
102     
103     public static ArrayList JavaDoc getFiles(ServletContext JavaDoc sc, String JavaDoc webPath) {
104         ArrayList JavaDoc resList = null;
105         String JavaDoc path = sc.getRealPath(webPath);
106         
107         return getFiles(path);
108     }
109     
110     public static ArrayList JavaDoc getThemesList() {
111         ArrayList JavaDoc list=null;
112         String JavaDoc path=GeiServlet.getNukePath();
113         path=path+"/WEB-INF/templates/themes/";
114         list=NukeResource.getDirectories(path);
115         return list;
116     }
117     
118     public static ArrayList JavaDoc getFiles(String JavaDoc path) {
119         ArrayList JavaDoc resList = null;
120         
121         File JavaDoc f = new File JavaDoc(path);
122         String JavaDoc strs[] = f.list();
123         resList = new ArrayList JavaDoc();
124         for(int i=0;i<strs.length;i++){
125             File JavaDoc dir=new File JavaDoc(path,strs[i]);
126             if(!dir.isDirectory())
127                 resList.add(strs[i]);
128         }
129         return resList;
130     }
131     
132     public static boolean existsFile(String JavaDoc filePath){
133         boolean flag=false;
134         File JavaDoc f=new File JavaDoc(filePath);
135         flag=f.isFile() && f.exists();
136         return flag;
137     }
138     public static Locale JavaDoc getLocale(HttpServletRequest JavaDoc request,HttpServletResponse JavaDoc response){
139         Locale JavaDoc locale = null;
140         //locale=new Locale(Locale.)
141
try{
142         Cookie JavaDoc[] cookies = request.getCookies();
143         boolean flag=false;
144         if (cookies != null)
145             for (int i = 0; i < cookies.length && !flag; i++) {
146                 if (cookies[i].getName().equals("LANGUAGE")) {
147                     locale = new Locale JavaDoc(cookies[i].getValue());
148
149                     Cookie JavaDoc cookie = new Cookie JavaDoc("LANGUAGE", locale
150                             .getLanguage());
151                     cookie.setMaxAge(-1);
152                     if(response!=null)
153                         response.addCookie(cookie);
154                     flag=true;
155                 }
156             }
157         String JavaDoc newL=request.getParameter("chLang");
158         if(newL!=null)
159             try{
160                 locale=(Locale JavaDoc)locales.get(newL);
161                 Cookie JavaDoc cookie = new Cookie JavaDoc("LANGUAGE", locale.getLanguage());
162                 cookie.setMaxAge(-1);
163                 response.addCookie(cookie);
164             }catch(Exception JavaDoc e){
165                 e.printStackTrace();
166             }
167         if (locale == null)
168             locale = request.getLocale();
169         if (locale == null)
170             locale = Locale.getDefault();
171         if(NukeResource.cache.get("Locale")!=null)
172             locale=(Locale JavaDoc)NukeResource.cache.get("Locale");
173         }catch(Throwable JavaDoc t){
174             t.printStackTrace();
175         }
176         return locale;
177     }
178     
179     public static int getRolePositionByRoleName(NukeRoleI r,ArrayList JavaDoc roles){
180         int pos=-1;
181         RoleVO aux=null;
182         for(int i=0;i<roles.size()&& pos<0 ;i++){
183             aux=(RoleVO)roles.get(i);
184             if(aux.getRoleName().equals(r.getRoleName()))
185                 pos=i;
186         }
187         return pos;
188     }
189     
190     public static int getRolePositionByRoleLevel(int level,ArrayList JavaDoc roles){
191         int pos=-1;
192         RoleVO aux=null;
193         for(int i=0;i<roles.size()&& pos<0 ;i++){
194             aux=(RoleVO)roles.get(i);
195             if(aux.getLevela()>=level)
196                 pos=i;
197         }
198         return pos;
199     }
200     
201     protected static String JavaDoc filterPath4Upload(String JavaDoc fileN){
202         
203         //questa patch perke explorer mette come nome del file
204
//uploadato anke il path locale (ke mer<beep>da)
205
String JavaDoc res=null;
206         if(fileN.indexOf("/")>=0){
207             res=fileN.substring( fileN.lastIndexOf("/")+1 );
208         }else if(fileN.indexOf("\\")>=0){
209             res=fileN.substring( fileN.lastIndexOf("\\")+1 );
210         }else{
211             res=fileN;
212         }
213         return res;
214     }
215     
216     protected static boolean filterExts(String JavaDoc fileName, ArrayList JavaDoc exts){
217         boolean flag=false;
218         String JavaDoc ext=null;
219         //System.out.println("CDCDCD "+fileName);
220
for(int i=0;i<exts.size() && ! flag;i++){
221             ext=(String JavaDoc)exts.get(i);
222             //System.out.println("CDCDCD1 "+ext);
223
if(fileName.toLowerCase().endsWith(ext.toLowerCase()))
224                 flag=true;
225             if(ext.equals("*"))
226                 flag=true;
227         }
228         if(fileName.toLowerCase().endsWith("jsp"))
229             flag=false;
230             //System.out.println("CDCDCD2 "+flag);
231
return flag;
232     }
233     
234     
235     public static String JavaDoc getUploadProperty(MagicUpload upload,HttpServletRequest JavaDoc request,String JavaDoc name){
236         String JavaDoc res=null;
237         try{
238             
239             // Set upload parameters
240
upload.setSizeThreshold(2000000);
241             upload.setSizeMax(2000000);
242             
243             upload.parseRequest(request);
244             
245             boolean flag=false;
246             for(int i=0;i<upload.getParams().size() && !flag;i++){
247                 FileItem item = (FileItem)upload.getParams().get(i);
248                 if (item.isFormField()) {
249                     if(item.getFieldName().equals(name) && ! item.getString().trim().equals(""))
250                         res=item.getString().trim();
251                     flag=true;
252                 }
253             }
254         }catch(Exception JavaDoc e){
255             res=null;
256         }
257         //System.out.println("CSCSCSCS "+res);
258
return res;
259     }
260     
261     public static String JavaDoc upload(MagicUpload upload,HttpServletRequest JavaDoc request,String JavaDoc localPath,ArrayList JavaDoc exts)
262         throws UploadException{
263         
264         String JavaDoc fN=null;
265         String JavaDoc fileName=null;
266         boolean uploaded=false;
267         try{
268             
269     
270             // Set upload parameters
271
upload.setSizeThreshold(2000000);
272             upload.setSizeMax(2000000);
273             //modificare questa var per definire un path di default
274
//String defaultRepository="/home/dsk/sleetest/tomcat/webapps/udb/profile";
275
//String defaultRepository="/root/jakarta-tomcat-4.0.4/webapps/stubUDB/profile";
276
upload.setRepositoryPath(localPath);
277             //Parse the request
278
upload.parseRequest(request);
279             //request.
280

281             
282             for(int i=0;i<upload.getParams().size();i++){
283                 FileItem item = (FileItem) upload.getParams().get(i);
284                 
285                 if (!item.isFormField()) {
286                     fileName=NukeResource.filterPath4Upload( item.getName() );
287                     
288                     if(item.getSize()>0 && NukeResource.filterExts(fileName,exts)){
289                         
290                         File JavaDoc uploadedFile = new File JavaDoc(upload.getRepositoryPath() +"/"+ fileName );
291                         
292                         item.write(uploadedFile);
293                         fN=fileName;
294                         uploaded=true;
295                         
296                     }else
297                         fN=null;
298                 }
299             }
300             if(!uploaded)
301                 throw new UploadException("Error uploading "+fN);
302             
303         }catch(Exception JavaDoc e){
304             //System.out.println("ECCCCEZZZIONE "+e);
305
fN=null;
306             throw new UploadException("Generic Error uploading "+e);
307         }
308         return fN;
309     }
310     
311     
312     
313     
314 }
Popular Tags