KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpServletResponse JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.openedit.repository.filesystem.StringItem;
18
19 import com.openedit.OpenEditException;
20 import com.openedit.WebPageRequest;
21 import com.openedit.modules.BaseModule;
22 import com.openedit.page.Page;
23 import com.openedit.page.PageRequestKeys;
24 import com.openedit.page.manage.PageManager;
25 import com.openedit.users.User;
26 import com.openedit.util.PathUtilities;
27 import com.openedit.util.URLUtilities;
28 /**
29  * This module provides the page editing functionality, and several actions to support it.
30  *
31  * @author Eric Galluzzo
32  */

33 public class BaseEditorModule extends BaseModule
34 {
35     private static Log log = LogFactory.getLog(BaseEditorModule.class);
36     
37     protected String JavaDoc normalizePath(String JavaDoc inPath)
38     {
39         String JavaDoc path = inPath;
40
41         if ((path != null) && !path.startsWith("/"))
42         {
43             path = "/" + path;
44         }
45         path = path.replaceAll("\\.draft\\.", ".");
46         return path;
47     }
48     
49     public void writeContent( WebPageRequest inContext ) throws OpenEditException
50     {
51         String JavaDoc path = inContext.getRequiredParameter("editPath");
52         String JavaDoc content = inContext.getRequiredParameter( "content");
53         User user = inContext.getUser();
54         if (user == null)
55         {
56             throw new OpenEditException("User must be logged in system before you can save");
57         }
58         log.debug("Writing content to path " + path);
59         try
60         {
61             Page page = getPageManager().getPage(path);
62             if ( page.isDraft() && !page.exists() )
63             {
64                 //make sure we save the original copy first
65
String JavaDoc opath = path.replaceAll("\\.draft\\.", ".");
66                 Page orig = getPageManager().getPage(opath);
67                 if( orig.exists() ) //it does not exist if we are in a translation
68
{
69                     StringItem revision = new StringItem( page.getPath(), orig.getContent() ,orig.getCharacterEncoding());
70                     revision.setAuthor(user.getUserName());
71                     revision.setMessage("Original");
72                     revision.setLastModified(orig.getContentItem().lastModified());
73                     page.setContentItem(revision);
74                     getPageManager().copyPage(orig, page);
75                 }
76             }
77             StringItem revision = new StringItem( page.getPath(), content ,page.getCharacterEncoding());
78             revision.setAuthor( user.getUserName() );
79             String JavaDoc message = inContext.getRequestParameter("message");
80             if ( message == null || message.equalsIgnoreCase("reason for your change"))
81             {
82                 message = "edited online";
83                 //revision.setMessage( message );
84
}
85             revision.setMessage( message );
86             page.setContentItem( revision );
87             getPageManager().putPage( page );
88             //releaseEditLock( inContext );
89
}
90         catch (Exception JavaDoc ex)
91         {
92             throw new OpenEditException(ex);
93         }
94     }
95     
96     /*
97      * @deprecated remove in OE 5.0
98      */

99     public void checkExist(WebPageRequest inReq) throws Exception JavaDoc
100     {
101         check404(inReq);
102     }
103     public void check404(WebPageRequest inReq) throws Exception JavaDoc
104     {
105         PageManager pageManager = getPageManager();
106         
107         if ( inReq.getPage().exists() )
108         {
109             return;
110         }
111         getPageManager().clearCache(inReq.getPage());
112         
113         inReq.getPageStreamer().getWebPageRequest().putPageValue("pathNotFound",inReq.getPath());
114         String JavaDoc isVirtual = inReq.getPage().get("virtual");
115         if ( Boolean.parseBoolean(isVirtual))
116         {
117             return;
118         }
119         
120         URLUtilities utils = (URLUtilities)inReq.getPageValue(PageRequestKeys.URL_UTILITIES);
121
122         if( utils != null)
123         {
124             //redirecting only works relative to a webapp
125
inReq.getPageStreamer().getWebPageRequest().putPageValue("forcedDestinationPath", utils.requestPathWithArgumentsNoContext() );
126         }
127         if ( inReq.getPage().isHtml() && inReq.isEditable() )
128         {
129             Page wizard = pageManager.getPage("/openedit/nopagefound.html");
130             if ( wizard.exists() )
131             {
132                 inReq.getPageStreamer().stream( wizard);
133                 inReq.setHasRedirected(true);
134                 return;
135             }
136         }
137         if ( !inReq.getPage().isHtml() )
138         {
139             HttpServletResponse JavaDoc response = inReq.getResponse();
140             if( response != null)
141             {
142                 response.sendError(HttpServletResponse.SC_NOT_FOUND);
143                 inReq.setHasRedirected(true);
144                 return;
145             }
146         }
147         
148         if ( inReq.getContentPage().getPath().equals( inReq.getPath()))
149         {
150             //log.info( "Could not use add page wizard. 404 error on: " + inReq.getPath() );
151
Page p404 = pageManager.getPage("/error404.html");
152             if ( p404.exists() )
153             {
154                 HttpServletResponse JavaDoc response = inReq.getResponse();
155                 if ( response != null)
156                 {
157                     response.setStatus(HttpServletResponse.SC_NOT_FOUND);
158                 }
159                 inReq.putProtectedPageValue("content", p404);
160                 //inReq.forward(p404.getPath());
161
return;
162             }
163             else
164             {
165                 log.error( "Could not report full 404 error on: " + inReq.getPath() + ". Make sure the 404 error page exists " + p404.getPath());
166                 //other users will get the standard file not found error
167
HttpServletResponse JavaDoc response = inReq.getResponse();
168                 if( response != null)
169                 {
170                     response.sendError(HttpServletResponse.SC_NOT_FOUND);
171                     inReq.setHasRedirected(true);
172                 }
173             }
174         }
175         else
176         {
177             inReq.getWriter().write("404 on " + inReq.getPath());
178             inReq.setHasRedirected(true);
179             
180         }
181     }
182     
183     protected boolean createDraft(Page inEditPage, User inUser)
184     {
185         //For draft mode unless the user has directedits permission
186

187         String JavaDoc prop = inEditPage.get("oe.edit.directedits");
188         if( prop != null && Boolean.parseBoolean(prop))
189         {
190             return false;
191         }
192         if( inEditPage.isDraft())
193         {
194             return false;
195         }
196         
197         if ( !inUser.hasProperty("oe.edit.draftmode" ))
198         {
199             if ( inUser.hasPermission("oe.edit.directedits") )
200             {
201                 //do nothing since they have permission to be direct editing
202
//or this file has a special property
203
}
204             else
205             {
206                 inUser.put("oe.edit.draftmode", "true");
207             }
208         }
209         if( inUser.hasProperty("oe.edit.draftmode") )
210         {
211             return true;
212         }
213
214         return false;
215     }
216     protected String JavaDoc findPathForMode(WebPageRequest inContext)
217     {
218         String JavaDoc path = inContext.getRequestParameter( "editPath" );
219             //See what page we should pickup. Perhaps .draft
220
if( inContext.getUser().hasProperty("oe.edit.draftmode") && path.indexOf(".draft.") == -1)
221             {
222                 String JavaDoc root = PathUtilities.extractPagePath(path);
223                 String JavaDoc p = root + ".draft." + PathUtilities.extractPageType(path);
224                 return p;
225             }
226         return path;
227     }
228
229     
230 }
231
Popular Tags