KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > admin > AdminAction


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: 17/01/2004 / 19:34:01
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.admin;
44
45
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.InputStream JavaDoc;
48 import java.io.OutputStream JavaDoc;
49 import java.net.URL JavaDoc;
50 import java.net.URLConnection JavaDoc;
51
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53
54 import net.jforum.ActionServletRequest;
55 import net.jforum.Command;
56 import net.jforum.JForumExecutionContext;
57 import net.jforum.SessionFacade;
58 import net.jforum.dao.DataAccessDriver;
59 import net.jforum.dao.ForumDAO;
60 import net.jforum.entities.UserSession;
61 import net.jforum.repository.ModulesRepository;
62 import net.jforum.repository.SecurityRepository;
63 import net.jforum.security.PermissionControl;
64 import net.jforum.security.SecurityConstants;
65 import net.jforum.util.preferences.ConfigKeys;
66 import net.jforum.util.preferences.SystemGlobals;
67 import net.jforum.util.preferences.TemplateKeys;
68 import freemarker.template.SimpleHash;
69 import freemarker.template.Template;
70
71 /**
72  * @author Rafael Steil
73  * @version $Id: AdminAction.java,v 1.13 2006/01/29 15:07:13 rafaelsteil Exp $
74  */

75 public class AdminAction extends Command {
76
77     /**
78      * @see net.jforum.Command#list()
79      */

80     public void list() throws Exception JavaDoc
81     {
82         this.login();
83     }
84     
85     public void login() throws Exception JavaDoc
86     {
87         String JavaDoc logged = (String JavaDoc)SessionFacade.getAttribute("logged");
88         UserSession us = SessionFacade.getUserSession();
89         
90         PermissionControl pc = SecurityRepository.get(us.getUserId());
91         
92         if (logged == null || logged.toString().equals("0")
93                 || pc == null || !pc.canAccess(SecurityConstants.PERM_ADMINISTRATION)) {
94             String JavaDoc returnPath = this.request.getContextPath() + "/admBase/login"
95                 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION);
96
97             JForumExecutionContext.setRedirect(this.request.getContextPath()
98                 + "/jforum"
99                 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)
100                 + "?module=user&action=login&returnPath="
101                 + returnPath);
102         }
103         else {
104             this.setTemplateName(TemplateKeys.ADMIN_INDEX);
105         }
106     }
107     
108     public void menu() throws Exception JavaDoc
109     {
110         this.setTemplateName(TemplateKeys.ADMIN_MENU);
111     }
112     
113     public void main() throws Exception JavaDoc
114     {
115         this.setTemplateName(TemplateKeys.ADMIN_MAIN);
116         
117         // Checks if the install module is still active
118
this.context.put("installModuleExists", ModulesRepository.getModuleClass("install") != null);
119         this.context.put("sessions", SessionFacade.getAllSessions());
120         
121         ForumDAO dao = DataAccessDriver.getInstance().newForumDAO();
122         this.context.put("stats", dao.getBoardStatus());
123         
124         this.checkBoardVersion();
125     }
126     
127     private void checkBoardVersion()
128     {
129         String JavaDoc data = this.readVersionFromSocket();
130         
131         if (data == null || data.trim().length() == 0) {
132             return;
133         }
134         
135         int index = data.indexOf('\n');
136         
137         String JavaDoc version = data.substring(0, index).trim();
138         String JavaDoc notes = data.substring(index + 1, data.length());
139         
140         this.matchVersion(version);
141         this.context.put("notes", notes);
142     }
143     
144     private void matchVersion(String JavaDoc latest)
145     {
146         String JavaDoc current = SystemGlobals.getValue(ConfigKeys.VERSION);
147         
148         String JavaDoc[] currentParts = current.split("\\.");
149         String JavaDoc[] latestParts = latest.split("\\.");
150         
151         if (Integer.parseInt(latestParts[2]) > Integer.parseInt(currentParts[2]) // Revision
152
|| Integer.parseInt(latestParts[1]) > Integer.parseInt(currentParts[1]) // Minor
153
|| Integer.parseInt(latestParts[0]) > Integer.parseInt(currentParts[0])) { // Major
154
this.context.put("upToDate", false);
155         }
156         else {
157             this.context.put("upToDate", true);
158         }
159         
160         this.context.put("latestVersion", latest);
161         this.context.put("currentVersion", current);
162     }
163     
164     private String JavaDoc readVersionFromSocket()
165     {
166         InputStream JavaDoc is = null;
167         OutputStream JavaDoc os = null;
168         
169         String JavaDoc data = null;
170         
171         try {
172             URL JavaDoc url = new URL JavaDoc(SystemGlobals.getValue(ConfigKeys.JFORUM_VERSION_URL));
173             URLConnection JavaDoc conn = url.openConnection();
174             
175             is = conn.getInputStream();
176             os = new ByteArrayOutputStream JavaDoc();
177             
178             int available = is.available();
179             
180             while (available > 0) {
181                 byte[] b = new byte[available];
182                 is.read(b);
183                 os.write(b);
184                 
185                 available = is.available();
186             }
187             
188             data = os.toString();
189         }
190         catch (Exception JavaDoc e) {
191             e.printStackTrace();
192         }
193         finally {
194             if (is != null) {
195                 try { is.close(); os.close(); } catch (Exception JavaDoc e) {}
196             }
197         }
198         
199         return data;
200     }
201     
202     public boolean checkAdmin()
203     {
204         int userId = SessionFacade.getUserSession().getUserId();
205         
206         if (SecurityRepository.get(userId).canAccess(SecurityConstants.PERM_ADMINISTRATION)) {
207             return true;
208         }
209         
210         JForumExecutionContext.setRedirect(JForumExecutionContext.getRequest().getContextPath()
211             + "/admBase/login"
212             + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
213         
214         super.ignoreAction();
215
216         return false;
217     }
218
219     public Template process(ActionServletRequest request,
220             HttpServletResponse JavaDoc response,
221             SimpleHash context) throws Exception JavaDoc
222     {
223         return super.process(request, response, context);
224     }
225 }
226
Popular Tags