KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > edit > EditModule


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12 package com.openedit.modules.edit;
13 import java.net.URLEncoder JavaDoc;
14
15 import javax.servlet.ServletException JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 import com.openedit.OpenEditException;
21 import com.openedit.WebPageRequest;
22 import com.openedit.modules.BaseModule;
23 import com.openedit.page.Page;
24 import com.openedit.users.User;
25 /**
26  * This module provides the page editing functionality, and several actions to support it.
27  *
28  * @author Eric Galluzzo
29  */

30 public class EditModule extends BaseModule
31 {
32     protected static final String JavaDoc WARNING_PAGE_PATH = "/openedit/editors/lock-warning.html";
33     private static Log log = LogFactory.getLog(EditModule.class);
34     protected EditLockRegistry fieldEditLockRegistry;
35     protected String JavaDoc fieldEditLockWarningPath = WARNING_PAGE_PATH;
36     /**
37      * Returns the edit lock registry used when claiming and releasing locks.
38      *
39      * @return EditLockRegistry
40      */

41     public EditLockRegistry getEditLockRegistry()
42     {
43         if (fieldEditLockRegistry == null)
44         {
45             fieldEditLockRegistry = new EditLockRegistry();
46         }
47         return fieldEditLockRegistry;
48     }
49     /**
50      * This command claims the edit lock for a path, which should be given via the
51      * <code>editPath</code> request parameter, unless the <code>doNotCheckLock</code> request
52      * parameter is set to <code>true</code>, in which case nothing will be done. If the edit lock is
53      * already claimed, the user will be redirected to a warning page.
54      * @throws ServletException
55      * @throws OpenEditException
56      *
57      * @author Eric Galluzzo
58      */

59     public void claimEditLock( WebPageRequest inReq ) throws OpenEditException, ServletException JavaDoc
60     {
61         String JavaDoc doNotCheckLockStr = inReq.getRequestParameter("doNotCheckLock");
62
63         if ((doNotCheckLockStr == null) || !doNotCheckLockStr.equals("true"))
64         {
65             User user = inReq.getUser();
66
67             if (user == null)
68             {
69                 inReq.forward("/openedit/authentication/logon.html");
70                 inReq.getRequest().setAttribute(
71                     "oe-exception", "You must log in as an editor in order to edit pages.");
72
73                 //throw new WSPException( "Cannot edit a page without logging in" );
74
return;
75             }
76
77             //String editPath = getPath(inParameters, "editPath");
78
String JavaDoc editPath = inReq.getRequiredParameter( "editPath" );
79             editPath = normalizePath( editPath );
80             User oldUser = getEditLockRegistry().getLockOwner(editPath);
81
82             if (!getEditLockRegistry().canLock(editPath, user))
83             {
84                 String JavaDoc redirectURL = getEditLockWarningPath(); // FIXME: Externalize this.
85
redirectURL += ("?editPath='" + editPath +"'&origURL=" + URLEncoder.encode(inReq.getPathUrl()) );
86                     
87                 if (oldUser != null)
88                 {
89                     redirectURL += ("&oldUsername=" + oldUser.getUserName());
90                 }
91
92                 try
93                 {
94                     inReq.redirect( redirectURL );
95                 }
96                 catch (Exception JavaDoc e)
97                 {
98                     throw new OpenEditException(e);
99                 }
100             }
101             else
102             {
103                 getEditLockRegistry().lockPath(editPath, user);
104             }
105         }
106     }
107     
108     protected String JavaDoc normalizePath(String JavaDoc inPath)
109     {
110         String JavaDoc path = inPath;
111
112         if ((path != null) && !path.startsWith("/"))
113         {
114             path = "/" + path;
115         }
116         path = path.replaceAll("\\.draft\\.", ".");
117         return path;
118     }
119     public String JavaDoc getEditLockWarningPath()
120     {
121         return fieldEditLockWarningPath;
122     }
123     public void setEditLockWarningPath( String JavaDoc editLockWarningPath )
124     {
125         fieldEditLockWarningPath = editLockWarningPath;
126     }
127     
128     public void releaseEditLock( WebPageRequest inReq ) throws OpenEditException
129     {
130         String JavaDoc editPath = inReq.getRequestParameter("editPath");
131         editPath = normalizePath( editPath );
132         getEditLockRegistry().unlockPath(editPath, inReq.getUser() );
133     }
134     public void forciblyClaimEditLock( WebPageRequest inReq ) throws OpenEditException
135     {
136         User user = inReq.getUser();
137
138         if (user == null)
139         {
140             throw new OpenEditException("Cannot edit a page without logging in");
141         }
142
143         String JavaDoc editPath = inReq.getRequestParameter("editPath");
144         editPath = normalizePath( editPath );
145         getEditLockRegistry().forciblyLockPath(editPath, user);
146     }
147     public void redirectToOriginal(WebPageRequest inReq )
148     {
149         String JavaDoc editPath = inReq.getRequestParameter("editPath");
150         String JavaDoc orig = inReq.getRequestParameter("origURL");
151         if( orig != null)
152         {
153             if ( orig.indexOf("?") == -1 && editPath != null)
154             {
155                 inReq.redirect(orig + "?path=" + editPath + "&cache=false");
156             }
157             else
158             {
159                 inReq.redirect(orig);
160             }
161         }
162         else
163         {
164             log.error("No origURL specified");
165         }
166         //orig ?path=dfdsf
167
}
168     /**
169      * This must be called as a path-action
170      * @param inReq
171      * @throws Exception
172      */

173     public void forceDownload(WebPageRequest inReq) throws Exception JavaDoc
174     {
175         if( inReq.getResponse() != null)
176         {
177             Page content = inReq.getContentPage();
178             String JavaDoc filename = content.getName();
179             inReq.getResponse().setHeader("Content-Disposition", "attachment; filename=" + filename);
180         }
181     }
182 }
183
Popular Tags