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: package org.apache.roller.ui.authoring.struts.actions;
020:
021: import java.io.IOException;
022: import java.util.Date;
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.struts.action.ActionForm;
029: import org.apache.struts.action.ActionForward;
030: import org.apache.struts.action.ActionMapping;
031: import org.apache.struts.action.ActionMessage;
032: import org.apache.struts.action.ActionMessages;
033: import org.apache.struts.actions.DispatchAction;
034: import org.apache.roller.RollerException;
035: import org.apache.roller.business.search.IndexManager;
036: import org.apache.roller.business.RollerFactory;
037: import org.apache.roller.business.UserManager;
038: import org.apache.roller.pojos.WebsiteData;
039: import org.apache.roller.ui.core.BasePageModel;
040: import org.apache.roller.ui.core.RollerRequest;
041: import org.apache.roller.ui.core.RollerSession;
042: import org.apache.roller.util.cache.CacheManager;
043:
044: /**
045: * Allows user to perform Website maintenence operations such as flushing
046: * the website page cache or re-indexing the website search index.
047: *
048: * @struts.action path="/roller-ui/authoring/maintenance" name="maintenanceForm"
049: * scope="request" parameter="method"
050: *
051: * @struts.action-forward name="maintenance.page" path=".Maintenance"
052: */
053: public class MaintenanceAction extends DispatchAction {
054:
055: private static Log mLogger = LogFactory
056: .getLog(MaintenanceAction.class);
057:
058: /* (non-Javadoc)
059: * @see org.apache.struts.actions.DispatchAction#unspecified(
060: * org.apache.struts.action.ActionMapping,
061: * org.apache.struts.action.ActionForm,
062: * javax.servlet.http.HttpServletRequest,
063: * javax.servlet.http.HttpServletResponse)
064: */
065: protected ActionForward unspecified(ActionMapping mapping,
066: ActionForm actionForm, HttpServletRequest request,
067: HttpServletResponse response) throws ServletException {
068:
069: request.setAttribute("model", new BasePageModel(
070: "maintenance.title", request, response, mapping));
071: return mapping.findForward("maintenance.page");
072: }
073:
074: /**
075: * Respond to user's request to rebuild search index.
076: */
077: public ActionForward index(ActionMapping mapping,
078: ActionForm actionForm, HttpServletRequest request,
079: HttpServletResponse response) throws IOException,
080: ServletException {
081:
082: try {
083: RollerRequest rreq = RollerRequest
084: .getRollerRequest(request);
085: WebsiteData website = rreq.getWebsite();
086: RollerSession rses = RollerSession
087: .getRollerSession(request);
088:
089: if (rses.isUserAuthorizedToAdmin(website)) {
090: IndexManager manager = RollerFactory.getRoller()
091: .getIndexManager();
092: manager.rebuildWebsiteIndex(website);
093:
094: ActionMessages messages = new ActionMessages();
095: messages.add(null, new ActionMessage(
096: "maintenance.message.indexed"));
097: saveMessages(request, messages);
098: }
099:
100: request.setAttribute("model", new BasePageModel(
101: "maintenance.title", request, response, mapping));
102:
103: } catch (RollerException re) {
104: mLogger.error("Unexpected exception", re.getRootCause());
105: throw new ServletException(re);
106: } catch (Exception e) {
107: mLogger.error("Unexpected exception", e);
108: throw new ServletException(e);
109: }
110:
111: return mapping.findForward("maintenance.page");
112: }
113:
114: /**
115: * Respond to request to flush a user's page cache.
116: */
117: public ActionForward flushCache(ActionMapping mapping,
118: ActionForm actionForm, HttpServletRequest request,
119: HttpServletResponse response) throws IOException,
120: ServletException {
121:
122: try {
123: RollerRequest rreq = RollerRequest
124: .getRollerRequest(request);
125: WebsiteData website = rreq.getWebsite();
126: RollerSession rses = RollerSession
127: .getRollerSession(request);
128:
129: if (rses.isUserAuthorizedToAdmin(website)) {
130:
131: // some caches are based on weblog last-modified, so update it
132: website.setLastModified(new Date());
133:
134: try {
135: UserManager umgr = RollerFactory.getRoller()
136: .getUserManager();
137: umgr.saveWebsite(website);
138: RollerFactory.getRoller().flush();
139: } catch (RollerException ex) {
140: mLogger.error("Error saving website", ex);
141: }
142:
143: // also notify cache manager
144: CacheManager.invalidate(website);
145:
146: ActionMessages messages = new ActionMessages();
147: messages.add(null, new ActionMessage(
148: "maintenance.message.flushed"));
149: saveMessages(request, messages);
150:
151: }
152:
153: request.setAttribute("model", new BasePageModel(
154: "maintenance.title", request, response, mapping));
155:
156: } catch (Exception e) {
157: throw new ServletException(e);
158: }
159:
160: return mapping.findForward("maintenance.page");
161: }
162:
163: }
|