1 12 package com.openedit.modules.edit; 13 import javax.servlet.http.HttpServletResponse ; 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 33 public class BaseEditorModule extends BaseModule 34 { 35 private static Log log = LogFactory.getLog(BaseEditorModule.class); 36 37 protected String normalizePath(String inPath) 38 { 39 String 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 path = inContext.getRequiredParameter("editPath"); 52 String 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 String opath = path.replaceAll("\\.draft\\.", "."); 66 Page orig = getPageManager().getPage(opath); 67 if( orig.exists() ) { 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 message = inContext.getRequestParameter("message"); 80 if ( message == null || message.equalsIgnoreCase("reason for your change")) 81 { 82 message = "edited online"; 83 } 85 revision.setMessage( message ); 86 page.setContentItem( revision ); 87 getPageManager().putPage( page ); 88 } 90 catch (Exception ex) 91 { 92 throw new OpenEditException(ex); 93 } 94 } 95 96 99 public void checkExist(WebPageRequest inReq) throws Exception  100 { 101 check404(inReq); 102 } 103 public void check404(WebPageRequest inReq) throws Exception  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 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 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 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 Page p404 = pageManager.getPage("/error404.html"); 152 if ( p404.exists() ) 153 { 154 HttpServletResponse response = inReq.getResponse(); 155 if ( response != null) 156 { 157 response.setStatus(HttpServletResponse.SC_NOT_FOUND); 158 } 159 inReq.putProtectedPageValue("content", p404); 160 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 HttpServletResponse 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 187 String 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 } 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 findPathForMode(WebPageRequest inContext) 217 { 218 String path = inContext.getRequestParameter( "editPath" ); 219 if( inContext.getUser().hasProperty("oe.edit.draftmode") && path.indexOf(".draft.") == -1) 221 { 222 String root = PathUtilities.extractPagePath(path); 223 String p = root + ".draft." + PathUtilities.extractPageType(path); 224 return p; 225 } 226 return path; 227 } 228 229 230 } 231 | Popular Tags |