⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractresourceform.java

📁 这是linux下ssl vpn的实现程序
💻 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.policyframework.forms;

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 com.sslexplorer.boot.PropertyList;
import com.sslexplorer.core.BundleActionMessage;
import com.sslexplorer.core.forms.CoreForm;
import com.sslexplorer.input.MultiSelectSelectionModel;
import com.sslexplorer.policyframework.Resource;
import com.sslexplorer.security.User;
import com.sslexplorer.tabs.TabModel;

/**
 * Abstract implementation of {@link com.sslexplorer.core.forms.CoreForm}
 * that allows editing of {@link com.sslexplorer.policyframework.Resource}
 * instances. 
 * 
 * @author Brett Smith <a href="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
 * @version $Revision: 1.11 $
 */
public abstract class AbstractResourceForm extends CoreForm implements TabModel {
    static Log log = LogFactory.getLog(AbstractResourceForm.class);

    protected String resourceName, resourceDescription;
    protected int resourceId;
    protected MultiSelectSelectionModel policyModel;
    protected PropertyList selectedPolicies;
    protected User owner, user;
    protected String originalName;
    protected Resource resource;
    protected boolean readOnly;

    /**
     * Constructor
     */
    public AbstractResourceForm() {
        selectedPolicies = new PropertyList();
    }
    
    /**
     * Get a resource given its name. Used by the {@link #validate(ActionMapping, HttpServletRequest)}
     * method to make sure resources with the same name are not created.
     * 
     * @param resourceName resource name
     * @return resource
     * @throws Exception on any erro
     */
    public abstract Resource getResourceByName(String resourceName) throws Exception;
    
    /**
     * Get the Id of the resource being edited or 0 if this is a new resource.
     * 
     * @return resource Id
     */
    public int getResourceId() {
        return resourceId;
    }

    /**
     * Set the ID of the resource being edited or 0 if this is a new resource.
     * 
     * @param resourceId resource Id
     */
    public void setResourceID(int resourceId) {
        this.resourceId = resourceId;
    }

    /**
     * Initialise the form. All concrete classes should call this.
     * 
     * @param user user
     * @param resource resource
     * @param editing editing
     * @param policyModel policy model
     * @param selectedPolicies selected policies
     * @param owner owner
     * @throws Exception on any error
     */
    public void initialise(User user, Resource resource, boolean editing, MultiSelectSelectionModel policyModel, PropertyList selectedPolicies, User owner) throws Exception {
        this.resource = resource;
        originalName = resource.getResourceName();
        setResourceID(resource.getResourceId());
        setResourceName(resource.getResourceName());
        setResourceDescription(resource.getResourceDescription());
        if(editing) {
            setEditing();
        }
        else {
            setCreating();
        }
        setPolicyModel(policyModel);
        this.selectedPolicies = selectedPolicies;
        this.owner = owner;
        this.user = user;
    }
    
    /**
     * If this is an {@link com.sslexplorer.policyframework.OwnedResource}
     * then this method will return the owner.
     * 
     * @return owner
     */
    public User getOwner() {
        return owner;
    }
    
    /**
     * Get the user that is creating / editing this resource.
     * 
     * @return user
     */
    public User getUser() {
        return user;
    }
    
    /**
     * Get the resource being editing
     * 
     * @return resource
     */
    public Resource getResource() {
        return resource;
    }

    /**
     * Get the resource description
     * 
     * @return resource description
     */
    public String getResourceDescription() {
        return resourceDescription;
    }

    /**
     * Set the resource description.
     * 
     * @param resourceDescription resource description
     */
    public void setResourceDescription(String resourceDescription) {
        this.resourceDescription = resourceDescription.trim();
    }

    /**
     * Get the resource name
     * 
     * @return resource name
     */
    public String getResourceName() {
        return resourceName;
    }

    /* (non-Javadoc)
     * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errs = new ActionErrors();
        if (isCommiting()) {
            if (getResourceName() == null || getResourceName().equals("")) {
                errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.missingName"));
            }
            if (getResourceName().length() > Resource.MAX_RESOURCE_NAME_LENGTH) {
                errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.resourceNameTooLong", String.valueOf(Resource.MAX_RESOURCE_NAME_LENGTH)));
            }
            if (getResourceDescription().equals("")) {
                errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.missingDescription", String.valueOf(Resource.MAX_RESOURCE_NAME_LENGTH)));
            }
            if(!getEditing() || !originalName.equals(getResourceName())) {
                try {
                    Resource r = getResourceByName(getResourceName());
                    if(r != null) {
                        errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.resourceNameInUse", getResourceName()));
                    }
                }
                catch(Exception e) {
                    errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.failedToDetermineIfResourceExists", e.getMessage()));                
                }
            }
        }
        return errs;
    }

    /**
     * Set the resource name
     * 
     * @param resourceName resource name
     */
    public void setResourceName(String resourceName) {
        this.resourceName = resourceName.trim();
    }

    /**
     * Get the selected policies as a <i>Property List</i> string.
     * 
     * @return the selected policies
     * @see PropertyList
     */
    public String getSelectedPolicies() {
        return selectedPolicies.getAsTextFieldText();
    }

    /**
     * Set the selected policies as a <i>Property List</i> string.
     * 
     * @param selectedPolicies the selected policies
     * @see PropertyList
     */
    public void setSelectedPolicies(String selectedPolicies) {
        this.selectedPolicies.setAsTextFieldText(selectedPolicies);
    }

    /**
     * Get the model to use for policy selection
     * 
     * @return policy selection model
     */
    public MultiSelectSelectionModel getPolicyModel() {
        return policyModel;
    }

    /**
     * Set the model to use for policy selection
     * 
     * @param policyModel policy selection model
     */
    public void setPolicyModel(MultiSelectSelectionModel policyModel) {
        this.policyModel = policyModel;
    }
    
    /**
     * Get the list of selected policies
     * 
     * @return selected policies
     */
    public PropertyList getSelectedPoliciesList() {
        return selectedPolicies;
    }
    
    /**
     * Get if this resource is read only
     * 
     * @return resource read only
     */
    public boolean getReadOnly() {
        return readOnly;
    }
    
    /**
     * Set this resource to be read only. This does not accept a boolean
     * argument to prevent struts forms being able to modify it.
     */
    public void setReadOnly() {
        readOnly = true;        
    }
    
    /**
     * Set this resource to be writeable only. This does not accept a boolean
     * argument to prevent struts forms being able to modify it.
     */
    public void setWriteable() {
        readOnly = false;        
    }

    /**
     * Apply the collected form fields to the resource object
     * 
     * @throws Exception on any error
     */
    public void apply()  throws Exception {
        if(getReadOnly()) {
            throw new Exception("Read only");
        }
        resource.setResourceName(getResourceName());
        resource.setResourceDescription(getResourceDescription());
        applyToResource();
    }
    
    /**
     * Concrete implementations must provide this method to persist any
     * additional form fields to the resource object being edited.
     * 
     * @throws Exception on any error
     */
    public abstract void applyToResource() throws Exception;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -