1 12 package com.openedit.modules.edit; 13 import java.net.URLEncoder ; 14 15 import javax.servlet.ServletException ; 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 30 public class EditModule extends BaseModule 31 { 32 protected static final String WARNING_PAGE_PATH = "/openedit/editors/lock-warning.html"; 33 private static Log log = LogFactory.getLog(EditModule.class); 34 protected EditLockRegistry fieldEditLockRegistry; 35 protected String fieldEditLockWarningPath = WARNING_PAGE_PATH; 36 41 public EditLockRegistry getEditLockRegistry() 42 { 43 if (fieldEditLockRegistry == null) 44 { 45 fieldEditLockRegistry = new EditLockRegistry(); 46 } 47 return fieldEditLockRegistry; 48 } 49 59 public void claimEditLock( WebPageRequest inReq ) throws OpenEditException, ServletException  60 { 61 String 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 return; 75 } 76 77 String editPath = inReq.getRequiredParameter( "editPath" ); 79 editPath = normalizePath( editPath ); 80 User oldUser = getEditLockRegistry().getLockOwner(editPath); 81 82 if (!getEditLockRegistry().canLock(editPath, user)) 83 { 84 String redirectURL = getEditLockWarningPath(); 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 e) 97 { 98 throw new OpenEditException(e); 99 } 100 } 101 else 102 { 103 getEditLockRegistry().lockPath(editPath, user); 104 } 105 } 106 } 107 108 protected String normalizePath(String inPath) 109 { 110 String 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 getEditLockWarningPath() 120 { 121 return fieldEditLockWarningPath; 122 } 123 public void setEditLockWarningPath( String editLockWarningPath ) 124 { 125 fieldEditLockWarningPath = editLockWarningPath; 126 } 127 128 public void releaseEditLock( WebPageRequest inReq ) throws OpenEditException 129 { 130 String 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 editPath = inReq.getRequestParameter("editPath"); 144 editPath = normalizePath( editPath ); 145 getEditLockRegistry().forciblyLockPath(editPath, user); 146 } 147 public void redirectToOriginal(WebPageRequest inReq ) 148 { 149 String editPath = inReq.getRequestParameter("editPath"); 150 String 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 } 168 173 public void forceDownload(WebPageRequest inReq) throws Exception  174 { 175 if( inReq.getResponse() != null) 176 { 177 Page content = inReq.getContentPage(); 178 String filename = content.getName(); 179 inReq.getResponse().setHeader("Content-Disposition", "attachment; filename=" + filename); 180 } 181 } 182 } 183 | Popular Tags |