📄 abstractresourcedetailswizardform.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.core.forms;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;
import com.sslexplorer.core.BundleActionMessage;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.policyframework.Resource;
import com.sslexplorer.policyframework.ResourcePermission;
import com.sslexplorer.policyframework.ResourceType;
import com.sslexplorer.policyframework.ResourceUtil;
import com.sslexplorer.policyframework.forms.AbstractResourcePolicySelectionForm;
import com.sslexplorer.security.User;
import com.sslexplorer.wizard.AbstractWizardSequence;
import com.sslexplorer.wizard.forms.DefaultWizardForm;
public abstract class AbstractResourceDetailsWizardForm extends DefaultWizardForm {
private String resourceName;
private String resourceDescription;
private List availableParentPermissions;
private int parentResourcePermission;
private ResourceType resourceTypeForResourcePermissions;
private boolean root;
final static Log log = LogFactory.getLog(AbstractResourceDetailsWizardForm.class);
// Statics for sequence attributes
public final static String ATTR_RESOURCE_NAME = "resourceName";
public final static String ATTR_RESOURCE_DESCRIPTION = "resourceDescription";
public final static String ATTR_PARENT_RESOURCE_PERMISSION = "parentResourcePermission";
public static final String ATTR_RESOURCE_PERMISSION_CLASS = "class";
public AbstractResourceDetailsWizardForm(boolean nextAvailable, boolean previousAvailable, String page, String focussedField,
boolean autoComplete, boolean finishAvailable, String pageName, String resourceBundle,
String resourcePrefix, int stepIndex, ResourceType resourceTypeForResourcePermissions) {
super(nextAvailable, previousAvailable, page, focussedField, autoComplete, finishAvailable, pageName, resourceBundle,
resourcePrefix, stepIndex);
this.resourceTypeForResourcePermissions = resourceTypeForResourcePermissions;
}
public boolean getRootResourcePermission() {
return root;
}
public int getParentResourcePermission() {
return parentResourcePermission;
}
public void setParentResourcePermission(int parentResourcePermission) {
this.parentResourcePermission = parentResourcePermission;
}
public List getAvailableParentResourcePermissions() {
return availableParentPermissions;
}
/**
* @return Returns the resourceDescription.
*/
public String getResourceDescription() {
return resourceDescription;
}
/**
* @param resourceDescription The resourceDescription to set.
*/
public void setResourceDescription(String resourceDescription) {
this.resourceDescription = resourceDescription.trim();
}
/**
* @return Returns the resourceName.
*/
public String getResourceName() {
return resourceName;
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.wizard.forms.DefaultWizardForm#apply(com.sslexplorer.wizard.AbstractWizardSequence)
*/
public void apply(AbstractWizardSequence sequence) throws Exception {
super.apply(sequence);
sequence.putAttribute(ATTR_RESOURCE_NAME, resourceName);
sequence.putAttribute(ATTR_RESOURCE_DESCRIPTION, resourceDescription);
int oldParent = ((Integer) sequence.getAttribute(ATTR_PARENT_RESOURCE_PERMISSION, new Integer(0))).intValue();
int newParent = root ? 0 : getParentResourcePermission();
if (newParent == 0 && !CoreServlet.getServlet().getLogonController().isAdministrator(sequence.getSession().getUser())) {
throw new Error("Violation of policy.");
}
sequence.putAttribute(ATTR_PARENT_RESOURCE_PERMISSION, new Integer(newParent));
if (oldParent != newParent) {
sequence.removeAttribute(AbstractResourcePolicySelectionForm.ATTR_SELECTED_POLICIES);
parentResourcePermissionChanged(sequence);
}
}
public abstract void parentResourcePermissionChanged(AbstractWizardSequence sequence);
/**
* @param resourceName The resourceName to set.
*/
public void setResourceName(String resourceName) {
this.resourceName = resourceName.trim();
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.wizard.forms.DefaultWizardForm#init(com.sslexplorer.wizard.AbstractWizardSequence,
* javax.servlet.http.HttpServletRequest)
*/
public void init(AbstractWizardSequence sequence, HttpServletRequest request) throws Exception {
super.init(sequence, request);
resourceName = (String) sequence.getAttribute(ATTR_RESOURCE_NAME, "");
resourceDescription = (String) sequence.getAttribute(ATTR_RESOURCE_DESCRIPTION, "");
User user = CoreServlet.getServlet().getLogonController().getUser(request);
if (!CoreServlet.getServlet().getLogonController().isAdministrator(user)) {
List rp = getAvailableParentResourcePermissions(sequence);
if (rp.size() == 0) {
throw new Exception("No resource permissions permit the creation of resource type "
+ resourceTypeForResourcePermissions.getResourceTypeId() + ".");
}
availableParentPermissions = ResourceUtil.resourceListAsLabelValueBeanList(rp);
parentResourcePermission = ((Integer) sequence.getAttribute(ATTR_PARENT_RESOURCE_PERMISSION, new Integer(
((ResourcePermission) rp.get(0)).getResourceId()))).intValue();
root = false;
} else {
root = true;
parentResourcePermission = 0;
availableParentPermissions = new ArrayList();
availableParentPermissions.add(new LabelValueBean("Super User", "0"));
}
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.wizard.forms.AbstractWizardForm#reset(org.apache.struts.action.ActionMapping,
* javax.servlet.http.HttpServletRequest)
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.reset(mapping, request);
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.wizard.forms.DefaultWizardForm#validate(org.apache.struts.action.ActionMapping,
* javax.servlet.http.HttpServletRequest)
*/
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
if (resourceName != null && isCommiting()) {
ActionErrors errs = new ActionErrors();
AbstractWizardSequence seq = getWizardSequence(request);
if (resourceName.equals("")) {
errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
.getCurrentPageForm().getResourcePrefix()
+ ".error.noResourceName"));
}
if (resourceDescription.equals("")) {
errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
.getCurrentPageForm().getResourcePrefix()
+ ".error.noResourceDescription"));
}
try {
if (getResourceByName(getResourceName()) != null) {
errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
.getCurrentPageForm().getResourcePrefix()
+ ".error.duplicateName", getResourceName()));
}
} catch (Exception e) {
log.error("Failed to check if named resource already exists.", e);
errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
.getCurrentPageForm().getResourcePrefix()
+ ".error.failedToCheckForName", e.getMessage()));
}
return errs;
}
return null;
}
public ResourceType getResourceTypeForResourcePermissions() {
return resourceTypeForResourcePermissions;
}
public abstract Resource getResourceByName(String name) throws Exception;
protected List getAvailableParentResourcePermissions(AbstractWizardSequence sequence) throws Exception {
return CoreServlet.getServlet().getPolicyDatabase().getPermittingResourcePermissions(resourceTypeForResourcePermissions,
null, (String) sequence.getAttribute(ATTR_RESOURCE_PERMISSION_CLASS, null), sequence.getSession().getUser(), false, false, true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -