1 19 20 package com.sslexplorer.core.forms; 21 22 import javax.servlet.http.HttpServletRequest ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.struts.Globals; 27 import org.apache.struts.action.ActionErrors; 28 import org.apache.struts.action.ActionMapping; 29 30 import com.sslexplorer.core.BundleActionMessage; 31 import com.sslexplorer.policyframework.ResourceType; 32 import com.sslexplorer.wizard.AbstractWizardSequence; 33 import com.sslexplorer.wizard.forms.DefaultWizardForm; 34 35 42 public abstract class AbstractResourceDetailsWizardForm extends DefaultWizardForm { 43 private String resourceName; 44 private String resourceDescription; 45 private ResourceType resourceTypeForAccessRights; 46 47 final static Log log = LogFactory.getLog(AbstractResourceDetailsWizardForm.class); 48 49 52 public final static String ATTR_RESOURCE_NAME = "resourceName"; 53 54 57 public final static String ATTR_RESOURCE_DESCRIPTION = "resourceDescription"; 58 59 62 public static final String ATTR_RESOURCE_PERMISSION_CLASS = "class"; 63 64 77 public AbstractResourceDetailsWizardForm(boolean nextAvailable, boolean previousAvailable, String page, String focussedField, 78 boolean autoComplete, boolean finishAvailable, String pageName, String resourceBundle, String resourcePrefix, 79 int stepIndex, ResourceType resourceTypeForAccessRights) { 80 super(nextAvailable, previousAvailable, page, focussedField, autoComplete, finishAvailable, pageName, resourceBundle, 81 resourcePrefix, stepIndex); 82 this.resourceTypeForAccessRights = resourceTypeForAccessRights; 83 } 84 85 88 public String getResourceDescription() { 89 return resourceDescription; 90 } 91 92 95 public void setResourceDescription(String resourceDescription) { 96 this.resourceDescription = resourceDescription.trim(); 97 } 98 99 102 public String getResourceName() { 103 return resourceName; 104 } 105 106 111 public void apply(AbstractWizardSequence sequence) throws Exception { 112 super.apply(sequence); 113 sequence.putAttribute(ATTR_RESOURCE_NAME, resourceName); 114 sequence.putAttribute(ATTR_RESOURCE_DESCRIPTION, resourceDescription); 115 } 116 117 120 public void setResourceName(String resourceName) { 121 this.resourceName = resourceName.trim(); 122 } 123 124 130 public void init(AbstractWizardSequence sequence, HttpServletRequest request) throws Exception { 131 super.init(sequence, request); 132 resourceName = (String ) sequence.getAttribute(ATTR_RESOURCE_NAME, ""); 133 resourceDescription = (String ) sequence.getAttribute(ATTR_RESOURCE_DESCRIPTION, ""); 134 } 135 136 142 public void reset(ActionMapping mapping, HttpServletRequest request) { 143 super.reset(mapping, request); 144 } 145 146 152 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 153 if (resourceName != null && isCommiting()) { 154 ActionErrors errs = new ActionErrors(); 155 AbstractWizardSequence seq = getWizardSequence(request); 156 if (resourceName.equals("")) { 157 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq 158 .getCurrentPageForm().getResourcePrefix() 159 + ".error.noResourceName")); 160 } 161 if (resourceDescription.equals("")) { 162 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq 163 .getCurrentPageForm().getResourcePrefix() 164 + ".error.noResourceDescription")); 165 } 166 try { 167 if (this.getResourceTypeForAccessRights().getResourceByName(getResourceName(), seq.getSession()) != null) { 168 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq 169 .getCurrentPageForm().getResourcePrefix() 170 + ".error.duplicateName", getResourceName())); 171 } 172 } catch (Exception e) { 173 log.error("Failed to check if named resource already exists.", e); 174 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq 175 .getCurrentPageForm().getResourcePrefix() 176 + ".error.failedToCheckForName", e.getMessage())); 177 } 178 return errs; 179 } 180 return null; 181 } 182 183 186 public ResourceType getResourceTypeForAccessRights() { 187 return resourceTypeForAccessRights; 188 } 189 } 190 | Popular Tags |