001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /*
019: * RollerPropertiesAction.java
020: *
021: * Created on April 21, 2005, 2:48 PM
022: */
023:
024: package org.apache.roller.ui.admin.struts.actions;
025:
026: import java.io.IOException;
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: import javax.servlet.ServletException;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.apache.struts.action.ActionError;
037: import org.apache.struts.action.ActionErrors;
038: import org.apache.struts.action.ActionForm;
039: import org.apache.struts.action.ActionForward;
040: import org.apache.struts.action.ActionMapping;
041: import org.apache.struts.action.ActionMessage;
042: import org.apache.struts.action.ActionMessages;
043: import org.apache.struts.actions.DispatchAction;
044: import org.apache.roller.RollerException;
045: import org.apache.roller.RollerPermissionsException;
046: import org.apache.roller.business.PropertiesManager;
047: import org.apache.roller.business.Roller;
048: import org.apache.roller.business.RollerFactory;
049: import org.apache.roller.pojos.RollerPropertyData;
050: import org.apache.roller.ui.core.BasePageModel;
051: import org.apache.roller.ui.core.RollerRequest;
052: import org.apache.roller.ui.core.RollerSession;
053:
054: /**
055: * Struts Action class which handles requests to the Admin Properties page.
056: *
057: * @author Allen Gilliland
058: *
059: * @struts.action path="/roller-ui/admin/rollerConfig"
060: * scope="request" parameter="method"
061: *
062: * @struts.action-forward name="rollerProperties.page"
063: * path=".rollerProperties"
064: */
065: public class RollerPropertiesAction extends DispatchAction {
066:
067: private static Log mLogger = LogFactory.getFactory().getInstance(
068: RollerPropertiesAction.class);
069:
070: public ActionForward unspecified(ActionMapping mapping,
071: ActionForm actionForm, HttpServletRequest request,
072: HttpServletResponse response) throws IOException,
073: ServletException {
074:
075: // make "edit" our default action
076: return this .edit(mapping, actionForm, request, response);
077: }
078:
079: public ActionForward edit(ActionMapping mapping,
080: ActionForm actionForm, HttpServletRequest request,
081: HttpServletResponse response) throws IOException,
082: ServletException {
083:
084: mLogger.debug("Handling edit request");
085:
086: ActionForward forward = mapping
087: .findForward("rollerProperties.page");
088: try {
089: BasePageModel pageModel = new BasePageModel(
090: "configForm.title", request, response, mapping);
091: request.setAttribute("model", pageModel);
092: RollerRequest rreq = RollerRequest
093: .getRollerRequest(request);
094: RollerSession rollerSession = RollerSession
095: .getRollerSession(request);
096: if (rollerSession.isGlobalAdminUser()) {
097:
098: // just grab our properties map and put it in the request
099: Roller mRoller = RollerFactory.getRoller();
100: PropertiesManager propsManager = mRoller
101: .getPropertiesManager();
102: Map props = propsManager.getProperties();
103: request.setAttribute("RollerProps", props);
104:
105: } else {
106: forward = mapping.findForward("access-denied");
107: }
108: } catch (Exception e) {
109: mLogger.error("ERROR in action", e);
110: throw new ServletException(e);
111: }
112: return forward;
113: }
114:
115: public ActionForward update(ActionMapping mapping,
116: ActionForm actionForm, HttpServletRequest request,
117: HttpServletResponse response) throws IOException,
118: ServletException {
119:
120: mLogger.debug("Handling update request");
121:
122: ActionForward forward = mapping
123: .findForward("rollerProperties.page");
124: ActionErrors errors = new ActionErrors();
125: try {
126: RollerRequest rreq = RollerRequest
127: .getRollerRequest(request);
128: RollerSession rollerSession = RollerSession
129: .getRollerSession(request);
130: BasePageModel pageModel = new BasePageModel(
131: "configForm.title", request, response, mapping);
132: request.setAttribute("model", pageModel);
133: if (rollerSession.isGlobalAdminUser()) {
134:
135: // just grab our properties map and put it in the request
136: Roller mRoller = RollerFactory.getRoller();
137: PropertiesManager propsManager = mRoller
138: .getPropertiesManager();
139: Map props = propsManager.getProperties();
140: request.setAttribute("RollerProps", props);
141:
142: // only set values for properties that are already defined
143: String propName = null;
144: RollerPropertyData updProp = null;
145: String incomingProp = null;
146: Iterator propsIT = props.keySet().iterator();
147: while (propsIT.hasNext()) {
148: propName = (String) propsIT.next();
149: updProp = (RollerPropertyData) props.get(propName);
150: incomingProp = request.getParameter(updProp
151: .getName());
152:
153: mLogger.debug("Checking property [" + propName
154: + "]");
155:
156: // some special treatment for booleans
157: // this is a bit hacky since we are assuming that any prop
158: // with a value of "true" or "false" is meant to be a boolean
159: // it may not always be the case, but we should be okay for now
160: if (updProp.getValue() != null // null check needed w/Oracle
161: && (updProp.getValue().equals("true") || updProp
162: .getValue().equals("false"))) {
163:
164: if (incomingProp == null
165: || !incomingProp.equals("on"))
166: incomingProp = "false";
167: else
168: incomingProp = "true";
169: }
170:
171: // only work on props that were submitted with the request
172: if (incomingProp != null) {
173: mLogger.debug("Setting new value for ["
174: + propName + "]");
175:
176: // NOTE: the old way had some locale sensitive way to do this??
177: updProp.setValue(incomingProp.trim());
178: }
179: }
180:
181: // save it
182: propsManager.saveProperties(props);
183: RollerFactory.getRoller().flush();
184:
185: // this operation causes OutOfMemory exceptions on sites with
186: // lots of referers so i am disabling it until it's
187: // not as dangerous -- Allen G
188: //mRoller.getRefererManager().applyRefererFilters();
189:
190: ActionMessages uiMessages = new ActionMessages();
191: uiMessages.add(null, new ActionMessage(
192: "weblogEdit.changesSaved"));
193: saveMessages(request, uiMessages);
194:
195: } else {
196: forward = mapping.findForward("access-denied");
197: }
198:
199: } catch (RollerPermissionsException e) {
200: errors.add(null, new ActionError(
201: "error.permissions.deniedSave"));
202: saveErrors(request, errors);
203: forward = mapping.findForward("access-denied");
204:
205: } catch (RollerException e) {
206: mLogger.error(e);
207: errors.add(ActionErrors.GLOBAL_ERROR,
208: new ActionError("error.update.rollerConfig", e
209: .getClass().getName()));
210: saveErrors(request, errors);
211: }
212:
213: return forward;
214: }
215:
216: }
|