1 26 package org.snipsnap.net.admin; 27 28 import org.snipsnap.config.Configuration; 29 import org.snipsnap.config.InitializeDatabase; 30 import org.snipsnap.net.FileUploadServlet; 31 import org.snipsnap.net.filter.MultipartWrapper; 32 import org.snipsnap.snip.Snip; 33 import org.snipsnap.snip.SnipSpace; 34 import org.snipsnap.snip.SnipSpaceFactory; 35 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import java.io.File ; 39 import java.io.FileOutputStream ; 40 import java.io.IOException ; 41 import java.io.InputStream ; 42 import java.util.Map ; 43 44 public class SetupApplication implements SetupHandler { 45 private FileUploadServlet uploader = new FileUploadServlet(); 46 47 public String getName() { 48 return "application"; 49 } 50 51 public Map setup(HttpServletRequest request, HttpServletResponse response, Configuration config, Map errors) { 52 config.setName(request.getParameter(Configuration.APP_NAME)); 53 config.setTagline(request.getParameter(Configuration.APP_TAGLINE)); 54 if (request instanceof MultipartWrapper) { 55 try { 56 if (config.isConfigured()) { 57 SnipSpace space = SnipSpaceFactory.getInstance(); 58 Snip snip = space.load(Configuration.SNIPSNAP_CONFIG); 59 String logoName = uploader.uploadFile(request, snip); 60 if (logoName != null && !"".equals(logoName)) { 61 config.setLogo(logoName); 62 } 63 } else { 64 MultipartWrapper mpRequest = (MultipartWrapper) request; 65 String fileName = mpRequest.getFileName("file"); 66 if (fileName != null && !"".equals(fileName)) { 67 String logoFileName = uploader.getCanonicalFileName(mpRequest.getFileName("file")); 68 String logoFileType = mpRequest.getFileContentType("file"); 69 if (logoFileType.startsWith("image")) { 70 InputStream logoFileIs = mpRequest.getFileInputStream("file"); 71 File logoFile = File.createTempFile("SnipSnapLogo", null); 72 FileOutputStream imageOut = new FileOutputStream (logoFile); 73 byte buffer[] = new byte[8192]; 74 int len = 0; 75 while ((len = logoFileIs.read(buffer)) != -1) { 76 imageOut.write(buffer, 0, len); 77 } 78 imageOut.close(); 79 logoFileIs.close(); 80 config.setLogo(logoFileName); 81 config.set(InitializeDatabase.LOGO_FILE, logoFile.getPath()); 82 config.set(InitializeDatabase.LOGO_FILE_TYPE, logoFileType); 83 } else { 84 errors.put(Configuration.APP_LOGO, Configuration.APP_LOGO + ".type"); 85 } 86 } 87 88 } 89 } catch (IOException e) { 90 errors.put(Configuration.APP_LOGO, Configuration.APP_LOGO); 91 e.printStackTrace(); 92 } 93 } 94 95 if (!config.isConfigured()) { 96 String usage = request.getParameter("usage"); 97 if ("public".equals(usage)) { 98 config.setPermRegister("allow"); 99 config.setPermWeblogsPing("allow"); 100 } else if ("closed".equals(usage)) { 101 config.setPermRegister("deny"); 102 config.setPermWeblogsPing("deny"); 103 } else if ("intranet".equals(usage)) { 104 config.setPermWeblogsPing("deny"); 105 } 106 request.getSession().setAttribute(ConfigureServlet.ATT_USAGE, usage); 107 } 108 109 110 String name = config.getName(); 111 if (null == name || "".equals(name)) { 112 errors.put(Configuration.APP_NAME, Configuration.APP_NAME); 113 } 114 115 return errors; 116 } 117 } 118 | Popular Tags |