1 16 package org.outerj.daisy.frontend; 17 18 import org.outerj.daisy.frontend.util.AbstractDaisyApple; 19 import org.outerj.daisy.frontend.util.FormHelper; 20 import org.outerj.daisy.repository.Repository; 21 import org.outerj.daisy.repository.VariantKey; 22 import org.outerj.daisy.repository.RepositoryException; 23 import org.outerj.daisy.repository.CollectionManager; 24 import org.outerj.daisy.repository.variant.VariantManager; 25 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryImpl; 26 import org.outerj.daisy.repository.user.User; 27 import org.outerj.daisy.emailnotifier.Subscription; 28 import org.outerj.daisy.emailnotifier.EmailSubscriptionManager; 29 import org.outerj.daisy.emailnotifier.CollectionSubscriptionKey; 30 import org.apache.cocoon.components.flow.apples.AppleRequest; 31 import org.apache.cocoon.components.flow.apples.AppleResponse; 32 import org.apache.cocoon.forms.formmodel.Form; 33 import org.apache.cocoon.forms.formmodel.Field; 34 import org.apache.cocoon.forms.formmodel.Widget; 35 import org.apache.cocoon.forms.binding.Binding; 36 import org.apache.cocoon.forms.FormContext; 37 import org.apache.cocoon.forms.FormsConstants; 38 import org.apache.cocoon.forms.util.I18nMessage; 39 import org.apache.cocoon.forms.validation.WidgetValidator; 40 import org.apache.cocoon.forms.validation.ValidationError; 41 import org.apache.cocoon.environment.Request; 42 import org.apache.avalon.framework.service.Serviceable; 43 import org.apache.avalon.framework.service.ServiceManager; 44 import org.apache.avalon.framework.service.ServiceException; 45 import org.apache.avalon.framework.logger.LogEnabled; 46 import org.apache.avalon.framework.logger.Logger; 47 import org.apache.commons.httpclient.HttpClient; 48 import org.apache.commons.httpclient.UsernamePasswordCredentials; 49 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 50 import org.apache.commons.httpclient.methods.GetMethod; 51 52 import java.util.*; 53 import java.io.IOException ; 54 55 public class UserSettingsApple extends AbstractDaisyApple implements Serviceable, LogEnabled { 56 private ServiceManager serviceManager; 57 private boolean init = false; 58 private Locale locale; 59 private Form form; 60 private Binding userBinding; 61 private Binding subscriptionBinding; 62 private Repository repository; 63 private User user; 64 private Subscription subscription; 65 private EmailSubscriptionManager subscriptionManager; 66 private Map viewData; 67 private String returnTo; 68 private Logger logger; 69 70 public void service(ServiceManager serviceManager) throws ServiceException { 71 this.serviceManager = serviceManager; 72 } 73 74 public void enableLogging(Logger logger) { 75 this.logger = logger; 76 } 77 78 protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception { 79 if (!init) { 80 Request request = appleRequest.getCocoonRequest(); 81 82 repository = WikiHelper.getRepository(appleRequest.getCocoonRequest(), serviceManager); 83 user = repository.getUserManager().getUser(repository.getUserId(), true); 84 if (!user.isUpdateableByUser()) { 85 throw new Exception ("You do not have access to this function."); 86 } 87 88 returnTo = request.getParameter("returnTo"); 89 if (returnTo == null || returnTo.equals("")) 90 returnTo = getMountPoint() + "/"; 91 locale = WikiHelper.getLocale(appleRequest.getCocoonRequest()); 92 form = FormHelper.createForm(serviceManager, "resources/form/usersettings_definition.xml"); 93 userBinding = FormHelper.createBinding(serviceManager, "resources/form/usersettings_userbinding.xml"); 94 subscriptionBinding = FormHelper.createBinding(serviceManager, "resources/form/usersettings_subscriptionbinding.xml"); 95 96 subscriptionManager = (EmailSubscriptionManager)repository.getExtension("EmailSubscriptionManager"); 97 subscription = subscriptionManager.getSubscription(); 98 99 form.addValidator(new CurrentPasswordRequiredValidator()); 100 form.getChild("currentPassword").addValidator(new CurrentPasswordValidator()); 101 form.getChild("subscribedDocuments").addValidator(KeySetParser.getVariantKeysWidgetValidator(repository.getVariantManager(), true, false)); 102 form.getChild("subscribedCollections").addValidator(KeySetParser.getCollectionSubscriptionKeysWidgetValidator(repository, true, false)); 103 104 userBinding.loadFormFromModel(form, user); 105 subscriptionBinding.loadFormFromModel(form, subscription); 106 subscriptionAdditionalLoad(); 107 108 String path = getMountPoint() + "/usersettings/" + getContinuationId(); 109 110 viewData = new HashMap(); 111 viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext())); 112 viewData.put("submitPath", path); 113 viewData.put("locale", locale); 114 viewData.put("returnTo", returnTo); 115 viewData.put("collectionsArray", repository.getCollectionManager().getCollections(false).getArray()); 116 viewData.put("branchesArray", repository.getVariantManager().getAllBranches(false).getArray()); 117 viewData.put("languagesArray", repository.getVariantManager().getAllLanguages(false).getArray()); 118 viewData.put("locales", new AvailableLocales(serviceManager, logger).getLocales()); 119 viewData.put("user", user); 120 viewData.put("CocoonFormsInstance", form); 121 122 init = true; 123 124 appleResponse.redirectTo(path); 125 } else { 126 String methodName = appleRequest.getCocoonRequest().getMethod(); 127 if (methodName.equals("GET")) { 128 appleResponse.sendPage("Form-usersettings-Pipe", viewData); 130 } else if (methodName.equals("POST")) { 131 boolean endProcessing = form.process(new FormContext(appleRequest.getCocoonRequest(), locale)); 133 134 if (!endProcessing) { 135 appleResponse.sendPage("Form-usersettings-Pipe", viewData); 136 } else { 137 subscriptionBinding.saveFormToModel(form, subscription); 138 subscriptionAdditionalStore(); 139 subscription.save(); 140 141 userBinding.saveFormToModel(form, user); 142 user.save(); 143 144 Field newPassword = (Field)form.getChild("newPassword"); 145 if (newPassword.getValue() != null) { 146 WikiHelper.login(user.getLogin(), (String )newPassword.getValue(), appleRequest.getCocoonRequest(), serviceManager); 147 } 148 149 init = false; 151 repository = null; 152 viewData = null; 153 subscription = null; 154 subscriptionManager = null; 155 user = null; 156 157 appleResponse.redirectTo(returnTo); 158 } 159 } else { 160 throw new Exception ("Unspported HTTP method: " + methodName); 161 } 162 } 163 } 164 165 class CurrentPasswordRequiredValidator implements WidgetValidator { 166 public boolean validate(Widget widget) { 167 if (form.getChild("newPassword").getValue() != null && form.getChild("currentPassword").getValue() == null) { 168 Field currentPasswordField = (Field)form.getChild("currentPassword"); 169 if (currentPasswordField.getValidationError() == null) { 170 currentPasswordField.setValidationError(new ValidationError(new I18nMessage("general.field-required", FormsConstants.I18N_CATALOGUE))); 171 return false; 172 } 173 } 174 return true; 175 } 176 } 177 178 class CurrentPasswordValidator implements WidgetValidator { 179 public boolean validate(Widget widget) { 180 boolean success = true; 181 182 Field currentPasswordField = (Field)widget; 183 String currentPassword = (String )currentPasswordField.getValue(); 184 if (currentPassword != null) { 185 MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 186 try { 187 HttpClient httpClient = new HttpClient(connectionManager); 188 httpClient.getState().setAuthenticationPreemptive(true); 189 String login = user.getLogin().replaceAll("@", "@@"); 190 UsernamePasswordCredentials credentials = 191 new UsernamePasswordCredentials(login, currentPassword); 192 httpClient.getState().setCredentials(null, null, credentials); 193 194 String errorMessage = null; 195 196 String basePath = ((RemoteRepositoryImpl)repository).getBaseURL(); 197 GetMethod getMethod = new GetMethod(basePath + "/repository/userinfo"); 198 try { 199 httpClient.executeMethod(getMethod); 200 } catch (IOException e) { 201 errorMessage = "Error checking password: " + e.getMessage(); 202 } finally { 203 getMethod.releaseConnection(); 204 } 205 206 if (getMethod.getStatusCode() == 401) { 207 errorMessage = "Incorrect password."; 208 } else if (getMethod.getStatusCode() != 200) { 209 errorMessage = "Error checking password, unexpected response code: " + getMethod.getStatusCode(); 210 } 211 212 if (errorMessage != null) { 213 currentPasswordField.setValidationError(new ValidationError(errorMessage, false)); 214 success = false; 215 } 216 } finally { 217 connectionManager.shutdown(); 218 } 219 } 220 221 return success; 222 } 223 } 224 225 private void subscriptionAdditionalLoad() { 226 VariantManager variantManager = repository.getVariantManager(); 227 { 228 VariantKey[] variantKeys = subscription.getSubscribedVariantKeys(); 229 StringBuffer variantKeyBuffer = new StringBuffer (variantKeys.length * 25); 230 for (int i = 0; i < variantKeys.length; i++) { 231 VariantKey variantKey = variantKeys[i]; 232 String branch = getBranchString(variantKey.getBranchId(), variantManager); 233 String language = getLanguageString(variantKey.getLanguageId(), variantManager); 234 String document; 235 if (variantKey.getDocumentId() == -1) 236 document = "*"; 237 else 238 document = String.valueOf(variantKey.getDocumentId()); 239 variantKeyBuffer.append(document).append(",").append(branch).append(",").append(language).append("\n"); 240 } 241 form.getChild("subscribedDocuments").setValue(variantKeyBuffer.toString()); 242 } 243 244 { 245 CollectionManager collectionManager = repository.getCollectionManager(); 246 CollectionSubscriptionKey[] collectionKeys = subscription.getSubscribedCollectionKeys(); 247 StringBuffer collectionKeyBuffer = new StringBuffer (collectionKeys.length * 40); 248 for (int i = 0; i < collectionKeys.length; i++) { 249 CollectionSubscriptionKey collectionKey = collectionKeys[i]; 250 String branch = getBranchString(collectionKey.getBranchId(), variantManager); 251 String language = getLanguageString(collectionKey.getLanguageId(), variantManager); 252 String collection; 253 if (collectionKey.getCollectionId() == -1) { 254 collection = "*"; 255 } else { 256 try { 257 collection = collectionManager.getCollection(collectionKey.getCollectionId(), false).getName(); 258 } catch (RepositoryException e) { 259 collection = String.valueOf(collectionKey.getCollectionId()); 260 } 261 } 262 collectionKeyBuffer.append(collection).append(",").append(branch).append(",").append(language).append("\n"); 263 } 264 form.getChild("subscribedCollections").setValue(collectionKeyBuffer.toString()); 265 } 266 } 267 268 private String getBranchString(long branchId, VariantManager variantManager) { 269 String branch; 270 if (branchId == -1) { 271 branch = "*"; 272 } else { 273 try { 274 branch = variantManager.getBranch(branchId, false).getName(); 275 } catch (RepositoryException e) { 276 branch = String.valueOf(branchId); 277 } 278 } 279 return branch; 280 } 281 282 private String getLanguageString(long languageId, VariantManager variantManager) { 283 String language; 284 if (languageId == -1) { 285 language = "*"; 286 } else { 287 try { 288 language = variantManager.getLanguage(languageId, false).getName(); 289 } catch (RepositoryException e) { 290 language = String.valueOf(languageId); 291 } 292 } 293 return language; 294 } 295 296 private void subscriptionAdditionalStore() throws Exception { 297 String input = (String )form.getChild("subscribedDocuments").getValue(); 298 VariantKey[] variantKeys = KeySetParser.parseVariantKeys(input, repository.getVariantManager(), true, false); 299 subscription.setSubscribedVariantKeys(variantKeys); 300 301 input = (String )form.getChild("subscribedCollections").getValue(); 302 CollectionSubscriptionKey[] collectionKeys = KeySetParser.parseCollectionKeys(input, repository); 303 subscription.setSubscribedCollectionKeys(collectionKeys); 304 } 305 306 } 307 | Popular Tags |