KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > admin > SetupApplication


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

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 JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import java.io.File JavaDoc;
39 import java.io.FileOutputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.util.Map JavaDoc;
43
44 public class SetupApplication implements SetupHandler {
45   private FileUploadServlet uploader = new FileUploadServlet();
46
47   public String JavaDoc getName() {
48     return "application";
49   }
50
51   public Map JavaDoc setup(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Configuration config, Map JavaDoc 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 JavaDoc 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 JavaDoc fileName = mpRequest.getFileName("file");
66           if (fileName != null && !"".equals(fileName)) {
67             String JavaDoc logoFileName = uploader.getCanonicalFileName(mpRequest.getFileName("file"));
68             String JavaDoc logoFileType = mpRequest.getFileContentType("file");
69             if (logoFileType.startsWith("image")) {
70               InputStream JavaDoc logoFileIs = mpRequest.getFileInputStream("file");
71               File JavaDoc logoFile = File.createTempFile("SnipSnapLogo", null);
72               FileOutputStream JavaDoc imageOut = new FileOutputStream JavaDoc(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 JavaDoc e) {
90         errors.put(Configuration.APP_LOGO, Configuration.APP_LOGO);
91         e.printStackTrace();
92       }
93     }
94
95     if (!config.isConfigured()) {
96       String JavaDoc 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 JavaDoc 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