001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.ac.usecases;
019:
020: import java.net.InetAddress;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.avalon.framework.container.ContainerUtil;
025: import org.apache.lenya.ac.IPRange;
026: import org.apache.lenya.ac.ItemUtil;
027: import org.apache.lenya.cms.ac.usecases.IPRangeProfile.Part;
028:
029: /**
030: * Usecase to add an IP range.
031: */
032: public class AddIPRange extends AccessControlUsecase {
033:
034: /**
035: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
036: */
037: protected void doCheckExecutionConditions() throws Exception {
038: String id = getParameterAsString(IPRangeProfile.ID);
039:
040: IPRange existingIPRange = getIpRangeManager().getIPRange(id);
041:
042: if (existingIPRange != null) {
043: addErrorMessage("This IP range already exists.");
044: }
045:
046: if (!ItemUtil.isValidId(id)) {
047: addErrorMessage("This is not a valid IP range ID.");
048: }
049:
050: IPRangeProfile.validateAddresses(this );
051: }
052:
053: /**
054: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
055: */
056: protected void doExecute() throws Exception {
057:
058: String id = getParameterAsString(IPRangeProfile.ID);
059: String name = getParameterAsString(IPRangeProfile.NAME);
060: String description = getParameterAsString(IPRangeProfile.DESCRIPTION);
061:
062: IPRange ipRange = getIpRangeManager().add(id);
063: ContainerUtil.enableLogging(ipRange, getLogger());
064:
065: ipRange.setName(name);
066: ipRange.setDescription(description);
067:
068: StringBuffer networkBuffer = new StringBuffer();
069: StringBuffer subnetBuffer = new StringBuffer();
070:
071: for (int i = 0; i < 4; i++) {
072: if (i > 0) {
073: networkBuffer.append(".");
074: subnetBuffer.append(".");
075: }
076: Part netPart = (Part) getParameter(IPRangeProfile.NETWORK_ADDRESS
077: + "-" + i);
078: networkBuffer.append(netPart.getValue());
079: Part subPart = (Part) getParameter(IPRangeProfile.SUBNET_MASK
080: + "-" + i);
081: subnetBuffer.append(subPart.getValue());
082: }
083:
084: InetAddress networkAddress = InetAddress
085: .getByName(networkBuffer.toString());
086: ipRange.setNetworkAddress(networkAddress.getAddress());
087:
088: InetAddress subnetMask = InetAddress.getByName(subnetBuffer
089: .toString());
090: ipRange.setSubnetMask(subnetMask.getAddress());
091:
092: ipRange.save();
093:
094: setExitParameter(IPRangeProfile.ID, id);
095: }
096:
097: /**
098: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
099: */
100: protected void initParameters() {
101: super .initParameters();
102: List partNumbers = new ArrayList();
103: for (byte i = 0; i < 4; i++) {
104: setParameter(IPRangeProfile.NETWORK_ADDRESS + "-" + i,
105: new Part(i));
106: setParameter(IPRangeProfile.SUBNET_MASK + "-" + i,
107: new Part(i));
108: partNumbers.add(new Integer(i));
109: }
110: setParameter(IPRangeProfile.PART_NUMBERS, partNumbers);
111: }
112: }
|