KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > server > ManageServer


1 // $Id: ManageServer.java 9366 2005-06-06 14:41:58Z pvollenweider $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
//
14
// ManageServer
15
//
16
// 31.03.2001 AK added in jahia.
17
//
18

19 package org.jahia.admin.server;
20
21 import java.io.IOException JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.jahia.bin.Jahia;
29 import org.jahia.bin.JahiaAdministration;
30 import org.jahia.data.JahiaData;
31 import org.jahia.params.ParamBean;
32 import org.jahia.resourcebundle.JahiaResourceBundle;
33 import org.jahia.utils.DataValidator;
34 import org.jahia.utils.properties.PropertiesManager;
35
36
37 /**
38  * desc: This class is used by the administration to manage the
39  * server settings of a jahia portal, like the mail notification service (when
40  * jahia or a user generate error(s), or like the java server home disk path,
41  * mail server, etc.
42  *
43  * Copyright: Copyright (c) 2002
44  * Company: Jahia Ltd
45  *
46  * @author Alexandre Kraft
47  * @version 1.0
48  */

49 public class ManageServer
50 {
51     private static final String JavaDoc CLASS_NAME = JahiaAdministration.CLASS_NAME;
52     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
53
54     private static PropertiesManager properties;
55
56
57
58     /**
59      * Default constructor.
60      * @author Alexandre Kraft
61      *
62      * @param request Servlet request.
63      * @param response Servlet response.
64      * @param session Servlet session for the current user.
65      */

66     public ManageServer( HttpServletRequest JavaDoc request,
67                          HttpServletResponse JavaDoc response,
68                          HttpSession JavaDoc session )
69     throws Throwable JavaDoc
70     {
71         properties = new PropertiesManager( Jahia.getJahiaPropertiesFileName() );
72         userRequestDispatcher( request, response, session );
73     } // end constructor
74

75
76
77     /**
78      * This method is used like a dispatcher for user requests.
79      * @author Alexandre Kraft
80      *
81      * @param request Servlet request.
82      * @param response Servlet response.
83      * @param session Servlet session for the current user.
84      */

85     private void userRequestDispatcher( HttpServletRequest JavaDoc request,
86                                         HttpServletResponse JavaDoc response,
87                                         HttpSession JavaDoc session )
88     throws Throwable JavaDoc
89     {
90         String JavaDoc operation = request.getParameter("sub");
91
92         if(operation.equals("display")) {
93             displaySettings( request, response, session );
94         } else if(operation.equals("process")) {
95             processSettings( request, response, session );
96         }
97     } // userRequestDispatcher
98

99
100
101     /**
102      * Display the server settings page, using doRedirect().
103      * @author Alexandre Kraft
104      *
105      * @param request Servlet request.
106      * @param response Servlet response.
107      * @param session Servlet session for the current user.
108      */

109     private void displaySettings( HttpServletRequest JavaDoc request,
110                                   HttpServletResponse JavaDoc response,
111                                   HttpSession JavaDoc session )
112     throws IOException JavaDoc, ServletException JavaDoc
113     {
114         // retrieve previous form values...
115
String JavaDoc jahiaServerMailServer = (String JavaDoc) session.getAttribute(CLASS_NAME + "jahiaServerMailServer");
116         String JavaDoc jahiaServerMailAdministrator = (String JavaDoc) session.getAttribute(CLASS_NAME + "jahiaServerMailAdministrator");
117         String JavaDoc jahiaServerMailFrom = (String JavaDoc) session.getAttribute(CLASS_NAME + "jahiaServerMailFrom");
118         String JavaDoc jahiaServerNotificationLevel = (String JavaDoc) session.getAttribute(CLASS_NAME + "jahiaServerNotificationLevel");
119
120         // set default values (if necessary)...
121
if(jahiaServerMailServer == null) { jahiaServerMailServer = properties.getProperty("mail_server"); }
122         if(jahiaServerMailFrom == null) { jahiaServerMailFrom = properties.getProperty("mail_from"); }
123         if(jahiaServerMailAdministrator == null) { jahiaServerMailAdministrator = properties.getProperty("mail_administrator"); }
124         if(jahiaServerNotificationLevel == null) { jahiaServerNotificationLevel = properties.getProperty("mail_paranoia"); }
125
126         // set request attributes...
127
request.setAttribute("jahiaServerMailServer", jahiaServerMailServer);
128         request.setAttribute("jahiaServerMailAdministrator", jahiaServerMailAdministrator);
129         request.setAttribute("jahiaServerMailFrom", jahiaServerMailFrom);
130         request.setAttribute("jahiaServerNotificationLevel", jahiaServerNotificationLevel);
131
132         JahiaAdministration.doRedirect( request, response, session, JSP_PATH + "config_server.jsp" );
133     } // end displaySettings
134

135
136
137     /**
138      * Process and check the validity of the server settings page. If they are
139      * not valid, display the server settings page to the user.
140      * @author Alexandre Kraft
141      *
142      * @param request Servlet request.
143      * @param response Servlet response.
144      * @param session Servlet session for the current user.
145      */

146     private void processSettings( HttpServletRequest JavaDoc request,
147                                   HttpServletResponse JavaDoc response,
148                                   HttpSession JavaDoc session )
149     throws IOException JavaDoc, ServletException JavaDoc
150     {
151         boolean processError = true;
152
153         // get form values...
154
String JavaDoc jahiaServerMailServer = (String JavaDoc) request.getParameter("mailserver").trim();
155         String JavaDoc jahiaServerMailAdministrator = (String JavaDoc) request.getParameter("mailadministrator").trim();
156         String JavaDoc jahiaServerMailFrom = (String JavaDoc) request.getParameter("mailfrom").trim();
157         String JavaDoc jahiaServerNotificationLevel = (String JavaDoc) request.getParameter("mailparanoia").trim();
158         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
159         ParamBean jParams = null;
160         if (jData != null) {
161             jParams = jData.params();
162         }
163
164         // check form validity...
165
if(!jahiaServerNotificationLevel.equals("Disabled")) {
166             String JavaDoc jahiaDisplayMessage = "";
167             if(jahiaServerMailServer.length() == 0) {
168                 String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.mailServer_mustSet.label",
169                                                  jParams, jParams.getLocale());
170                 dspMsg += " " + JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.setLevleDisabled.label",
171                                                                                 jParams, jParams.getLocale());
172                 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
173             } else if(jahiaServerMailAdministrator.length() == 0) {
174               String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.mailAdmin_mustSet.label",
175                                                jParams, jParams.getLocale());
176               dspMsg += " " + JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.setLevleDisabled.label",
177                                                                               jParams, jParams.getLocale());
178               session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
179             } else if(jahiaServerMailFrom.length() == 0) {
180               String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.mailFrom_mustSet.label",
181                                                jParams, jParams.getLocale());
182               dspMsg += " " + JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.setLevleDisabled.label",
183                                                                               jParams, jParams.getLocale());
184               session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
185             } else if(!DataValidator.isValidEmail(jahiaServerMailAdministrator)) {
186               String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.enterValidEmailAdmin.label",
187                                                jParams, jParams.getLocale());
188                 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
189             } else if(!DataValidator.isValidEmail(jahiaServerMailFrom)) {
190               String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.enterValidEmailFrom.label",
191                                                jParams, jParams.getLocale());
192                 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
193             } else {
194                 processError = false;
195             }
196         } else {
197             processError = false;
198         }
199
200         // set session with new attributes...
201
session.setAttribute(CLASS_NAME + "jahiaServerMailServer", jahiaServerMailServer);
202         session.setAttribute(CLASS_NAME + "jahiaServerMailAdministrator", jahiaServerMailAdministrator);
203         session.setAttribute(CLASS_NAME + "jahiaServerMailFrom", jahiaServerMailFrom);
204         session.setAttribute(CLASS_NAME + "jahiaServerNotificationLevel", jahiaServerNotificationLevel);
205
206         if(processError) {
207             displaySettings( request, response, session );
208         } else {
209             storeSettings( request, response, session );
210         }
211     } // end processSettings
212

213
214
215     /**
216      * Store new settings for the server page and redirect to the final page.
217      * @author Alexandre Kraft
218      *
219      * @param request Servlet request.
220      * @param response Servlet response.
221      * @param session Servlet session for the current user.
222      */

223     private void storeSettings( HttpServletRequest JavaDoc request,
224                                 HttpServletResponse JavaDoc response,
225                                 HttpSession JavaDoc session )
226     throws IOException JavaDoc, ServletException JavaDoc
227     {
228         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
229         ParamBean jParams = null;
230         if (jData != null) {
231             jParams = jData.params();
232         }
233         // set new values in the properties manager...
234
properties.setProperty("mail_server", (String JavaDoc) session.getAttribute(CLASS_NAME + "jahiaServerMailServer"));
235         properties.setProperty("mail_administrator", (String JavaDoc) session.getAttribute(CLASS_NAME + "jahiaServerMailAdministrator"));
236         properties.setProperty("mail_from", (String JavaDoc) session.getAttribute(CLASS_NAME + "jahiaServerMailFrom"));
237         properties.setProperty("mail_paranoia", (String JavaDoc) session.getAttribute(CLASS_NAME + "jahiaServerNotificationLevel"));
238
239         // write in the jahia properties file...
240
properties.storeProperties();
241
242         String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.restartJahiaAfterChange.label",
243                                          jParams, jParams.getLocale());
244         request.setAttribute ("processMessage", dspMsg);
245         session.setAttribute (CLASS_NAME + "jahiaDisplayMessage",
246             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
247                 jParams, jParams.getLocale()));
248         JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "finish.jsp");
249     } // end storeSettings
250

251
252 } // end ManageServer
Popular Tags