KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > admin > struts > actions > RollerPropertiesAction


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /*
19  * RollerPropertiesAction.java
20  *
21  * Created on April 21, 2005, 2:48 PM
22  */

23
24 package org.apache.roller.ui.admin.struts.actions;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.struts.action.ActionError;
37 import org.apache.struts.action.ActionErrors;
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionForward;
40 import org.apache.struts.action.ActionMapping;
41 import org.apache.struts.action.ActionMessage;
42 import org.apache.struts.action.ActionMessages;
43 import org.apache.struts.actions.DispatchAction;
44 import org.apache.roller.RollerException;
45 import org.apache.roller.RollerPermissionsException;
46 import org.apache.roller.model.PropertiesManager;
47 import org.apache.roller.model.Roller;
48 import org.apache.roller.model.RollerFactory;
49 import org.apache.roller.pojos.RollerPropertyData;
50 import org.apache.roller.ui.core.BasePageModel;
51 import org.apache.roller.ui.core.RollerRequest;
52 import org.apache.roller.ui.core.RollerSession;
53
54
55
56 /**
57  * Struts Action class which handles requests to the Admin Properties page.
58  *
59  * @author Allen Gilliland
60  *
61  * @struts.action path="/roller-ui/admin/rollerConfig"
62  * scope="request" parameter="method"
63  *
64  * @struts.action-forward name="rollerProperties.page"
65  * path=".rollerProperties"
66  */

67 public class RollerPropertiesAction extends DispatchAction {
68     
69     private static Log mLogger =
70             LogFactory.getFactory().getInstance(RollerPropertiesAction.class);
71     
72     
73     public ActionForward unspecified(
74             ActionMapping mapping,
75             ActionForm actionForm,
76             HttpServletRequest JavaDoc request,
77             HttpServletResponse JavaDoc response)
78             throws IOException JavaDoc, ServletException JavaDoc {
79         
80         // make "edit" our default action
81
return this.edit(mapping, actionForm, request, response);
82     }
83     
84     
85     public ActionForward edit(
86             ActionMapping mapping,
87             ActionForm actionForm,
88             HttpServletRequest JavaDoc request,
89             HttpServletResponse JavaDoc response)
90             throws IOException JavaDoc, ServletException JavaDoc {
91         
92         mLogger.debug("Handling edit request");
93         
94         ActionForward forward = mapping.findForward("rollerProperties.page");
95         try {
96             BasePageModel pageModel = new BasePageModel(
97                     "configForm.title", request, response, mapping);
98             request.setAttribute("model",pageModel);
99             RollerRequest rreq = RollerRequest.getRollerRequest(request);
100             RollerSession rollerSession = RollerSession.getRollerSession(request);
101             if (rollerSession.isGlobalAdminUser() ) {
102                 
103                 // just grab our properties map and put it in the request
104
Roller mRoller = RollerFactory.getRoller();
105                 PropertiesManager propsManager = mRoller.getPropertiesManager();
106                 Map JavaDoc props = propsManager.getProperties();
107                 request.setAttribute("RollerProps", props);
108                 
109             } else {
110                 forward = mapping.findForward("access-denied");
111             }
112         } catch (Exception JavaDoc e) {
113             mLogger.error("ERROR in action",e);
114             throw new ServletException JavaDoc(e);
115         }
116         return forward;
117     }
118     
119     
120     public ActionForward update(
121             ActionMapping mapping,
122             ActionForm actionForm,
123             HttpServletRequest JavaDoc request,
124             HttpServletResponse JavaDoc response)
125             throws IOException JavaDoc, ServletException JavaDoc {
126         
127         mLogger.debug("Handling update request");
128         
129         ActionForward forward = mapping.findForward("rollerProperties.page");
130         ActionErrors errors = new ActionErrors();
131         try {
132             RollerRequest rreq = RollerRequest.getRollerRequest(request);
133             RollerSession rollerSession = RollerSession.getRollerSession(request);
134             BasePageModel pageModel = new BasePageModel(
135                     "configForm.title", request, response, mapping);
136             request.setAttribute("model",pageModel);
137             if (rollerSession.isGlobalAdminUser()) {
138             
139                 // just grab our properties map and put it in the request
140
Roller mRoller = RollerFactory.getRoller();
141                 PropertiesManager propsManager = mRoller.getPropertiesManager();
142                 Map JavaDoc props = propsManager.getProperties();
143                 request.setAttribute("RollerProps", props);
144                 
145                 // only set values for properties that are already defined
146
String JavaDoc propName = null;
147                 RollerPropertyData updProp = null;
148                 String JavaDoc incomingProp = null;
149                 Iterator JavaDoc propsIT = props.keySet().iterator();
150                 while(propsIT.hasNext()) {
151                     propName = (String JavaDoc) propsIT.next();
152                     updProp = (RollerPropertyData) props.get(propName);
153                     incomingProp = request.getParameter(updProp.getName());
154                     
155                     mLogger.debug("Checking property ["+propName+"]");
156                     
157                     // some special treatment for booleans
158
// this is a bit hacky since we are assuming that any prop
159
// with a value of "true" or "false" is meant to be a boolean
160
// it may not always be the case, but we should be okay for now
161
if( updProp.getValue() != null // null check needed w/Oracle
162
&& (updProp.getValue().equals("true") || updProp.getValue().equals("false"))) {
163                         
164                         if(incomingProp == null || !incomingProp.equals("on"))
165                             incomingProp = "false";
166                         else
167                             incomingProp = "true";
168                     }
169                     
170                     // only work on props that were submitted with the request
171
if(incomingProp != null) {
172                         mLogger.debug("Setting new value for ["+propName+"]");
173                         
174                         // NOTE: the old way had some locale sensitive way to do this??
175
updProp.setValue(incomingProp.trim());
176                     }
177                 }
178                 
179                 // save it
180
propsManager.saveProperties(props);
181                 RollerFactory.getRoller().flush();
182                 
183                 // this operation causes OutOfMemory exceptions on sites with
184
// lots of referers so i am disabling it until it's
185
// not as dangerous -- Allen G
186
//mRoller.getRefererManager().applyRefererFilters();
187

188                 ActionMessages uiMessages = new ActionMessages();
189                 uiMessages.add(null, new ActionMessage("weblogEdit.changesSaved"));
190                 saveMessages(request, uiMessages);
191                 
192             } else {
193                 forward = mapping.findForward("access-denied");
194             }
195             
196         } catch (RollerPermissionsException e) {
197             errors.add(null, new ActionError("error.permissions.deniedSave"));
198             saveErrors(request, errors);
199             forward = mapping.findForward("access-denied");
200             
201         } catch (RollerException e) {
202             mLogger.error(e);
203             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
204                     "error.update.rollerConfig",e.getClass().getName()));
205             saveErrors(request,errors);
206         }
207         
208         return forward;
209     }
210     
211 }
212
Popular Tags