1 22 package com.geinuke.util; 23 24 import java.io.File ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Hashtable ; 28 import java.util.Locale ; 29 30 import javax.servlet.ServletContext ; 31 import javax.servlet.http.Cookie ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 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 cache=null; 48 public static Hashtable locales=null; 49 public static ArrayList categoryTypes=null; 50 static{ 51 cache=new Hashtable (); 52 53 locales=new Hashtable (); 54 locales.put("Italiano",Locale.ITALY); 55 locales.put("English",Locale.ENGLISH); 56 60 categoryTypes=new ArrayList (); 61 categoryTypes.add("NEWS"); 62 categoryTypes.add("FORUM"); 63 categoryTypes.add("BLOG"); 64 } 65 public static ArrayList getResources(ServletContext sc, String initPath) { 66 ArrayList resList = null; 67 String path = sc.getRealPath(initPath); 68 File f = new File (path); 69 String strs[] = f.list(); 70 resList = new ArrayList (Arrays.asList(strs)); 71 return resList; 72 } 73 74 75 public static ArrayList getDirectories(ServletContext sc, String initPath) { 76 ArrayList resList = null; 77 String path = sc.getRealPath(initPath); 78 File f = new File (path); 79 String strs[] = f.list(); 80 resList = new ArrayList (); 81 for(int i=0;i<strs.length;i++){ 82 File dir=new File (path,strs[i]); 83 if(dir.isDirectory()) 84 resList.add(strs[i]); 85 } 86 return resList; 87 } 88 89 public static ArrayList getDirectories( String initPath) { 90 ArrayList resList = null; 91 92 File f = new File (initPath); 93 String strs[] = f.list(); 94 resList = new ArrayList (); 95 for(int i=0;i<strs.length;i++){ 96 File dir=new File (initPath,strs[i]); 97 if(dir.isDirectory()) 98 resList.add(strs[i]); 99 } 100 return resList; 101 } 102 103 public static ArrayList getFiles(ServletContext sc, String webPath) { 104 ArrayList resList = null; 105 String path = sc.getRealPath(webPath); 106 107 return getFiles(path); 108 } 109 110 public static ArrayList getThemesList() { 111 ArrayList list=null; 112 String path=GeiServlet.getNukePath(); 113 path=path+"/WEB-INF/templates/themes/"; 114 list=NukeResource.getDirectories(path); 115 return list; 116 } 117 118 public static ArrayList getFiles(String path) { 119 ArrayList resList = null; 120 121 File f = new File (path); 122 String strs[] = f.list(); 123 resList = new ArrayList (); 124 for(int i=0;i<strs.length;i++){ 125 File dir=new File (path,strs[i]); 126 if(!dir.isDirectory()) 127 resList.add(strs[i]); 128 } 129 return resList; 130 } 131 132 public static boolean existsFile(String filePath){ 133 boolean flag=false; 134 File f=new File (filePath); 135 flag=f.isFile() && f.exists(); 136 return flag; 137 } 138 public static Locale getLocale(HttpServletRequest request,HttpServletResponse response){ 139 Locale locale = null; 140 try{ 142 Cookie [] 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 (cookies[i].getValue()); 148 149 Cookie cookie = new Cookie ("LANGUAGE", locale 150 .getLanguage()); 151 cookie.setMaxAge(-1); 152 if(response!=null) 153 response.addCookie(cookie); 154 flag=true; 155 } 156 } 157 String newL=request.getParameter("chLang"); 158 if(newL!=null) 159 try{ 160 locale=(Locale )locales.get(newL); 161 Cookie cookie = new Cookie ("LANGUAGE", locale.getLanguage()); 162 cookie.setMaxAge(-1); 163 response.addCookie(cookie); 164 }catch(Exception 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 )NukeResource.cache.get("Locale"); 173 }catch(Throwable t){ 174 t.printStackTrace(); 175 } 176 return locale; 177 } 178 179 public static int getRolePositionByRoleName(NukeRoleI r,ArrayList 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 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 filterPath4Upload(String fileN){ 202 203 String 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 fileName, ArrayList exts){ 217 boolean flag=false; 218 String ext=null; 219 for(int i=0;i<exts.size() && ! flag;i++){ 221 ext=(String )exts.get(i); 222 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 return flag; 232 } 233 234 235 public static String getUploadProperty(MagicUpload upload,HttpServletRequest request,String name){ 236 String res=null; 237 try{ 238 239 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 e){ 255 res=null; 256 } 257 return res; 259 } 260 261 public static String upload(MagicUpload upload,HttpServletRequest request,String localPath,ArrayList exts) 262 throws UploadException{ 263 264 String fN=null; 265 String fileName=null; 266 boolean uploaded=false; 267 try{ 268 269 270 upload.setSizeThreshold(2000000); 272 upload.setSizeMax(2000000); 273 upload.setRepositoryPath(localPath); 277 upload.parseRequest(request); 279 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 uploadedFile = new File (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 e){ 304 fN=null; 306 throw new UploadException("Generic Error uploading "+e); 307 } 308 return fN; 309 } 310 311 312 313 314 } | Popular Tags |