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.lenya.ac.IPRange;
025: import org.apache.lenya.cms.usecase.AbstractUsecase;
026:
027: /**
028: * Usecase to change the profile of an IP range.
029: */
030: public class IPRangeProfile extends AccessControlUsecase {
031:
032: protected static final String ID = "ipRangeId";
033: protected static final String NAME = "name";
034: protected static final String DESCRIPTION = "description";
035: protected static final String NETWORK_ADDRESS = "networkAddress";
036: protected static final String SUBNET_MASK = "subnetMask";
037: protected static final String PART_NUMBERS = "partNumbers";
038:
039: /**
040: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
041: */
042: protected void doCheckExecutionConditions() throws Exception {
043: IPRangeProfile.validateAddresses(this );
044: }
045:
046: /**
047: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
048: */
049: protected void doExecute() throws Exception {
050: super .doExecute();
051:
052: String name = getParameterAsString(NAME);
053: String description = getParameterAsString(DESCRIPTION);
054:
055: IPRange ipRange = getIPRange();
056:
057: ipRange.setName(name);
058: ipRange.setDescription(description);
059:
060: StringBuffer networkBuffer = new StringBuffer();
061: StringBuffer subnetBuffer = new StringBuffer();
062:
063: for (int i = 0; i < 4; i++) {
064: if (i > 0) {
065: networkBuffer.append(".");
066: subnetBuffer.append(".");
067: }
068: Part netPart = (Part) getParameter(IPRangeProfile.NETWORK_ADDRESS
069: + "-" + i);
070: networkBuffer.append(netPart.getValue());
071: Part subPart = (Part) getParameter(IPRangeProfile.SUBNET_MASK
072: + "-" + i);
073: subnetBuffer.append(subPart.getValue());
074: }
075:
076: InetAddress networkAddress = InetAddress
077: .getByName(networkBuffer.toString());
078: ipRange.setNetworkAddress(networkAddress.getAddress());
079:
080: InetAddress subnetMask = InetAddress.getByName(subnetBuffer
081: .toString());
082: ipRange.setSubnetMask(subnetMask.getAddress());
083:
084: ipRange.save();
085:
086: }
087:
088: private IPRange ipRange;
089:
090: /**
091: * @return The IP range.
092: */
093: protected IPRange getIPRange() {
094: return this .ipRange;
095: }
096:
097: /**
098: * @see org.apache.lenya.cms.usecase.Usecase#setParameter(java.lang.String,
099: * java.lang.Object)
100: */
101: public void setParameter(String name, Object value) {
102: super .setParameter(name, value);
103:
104: if (name.equals(ID)) {
105: String id = (String) value;
106: this .ipRange = getIpRangeManager().getIPRange(id);
107: if (this .ipRange == null) {
108: throw new RuntimeException("IP range [" + id
109: + "] not found.");
110: }
111:
112: setParameter(NAME, this .ipRange.getName());
113: setParameter(DESCRIPTION, this .ipRange.getDescription());
114:
115: InetAddress networkAddress = this .ipRange
116: .getNetworkAddress();
117: InetAddress subnetMask = this .ipRange.getSubnetMask();
118:
119: List partNumbers = new ArrayList();
120: for (byte i = 0; i < 4; i++) {
121: String addrPart = Integer
122: .toString(0xFF & networkAddress.getAddress()[i]);
123: setParameter(NETWORK_ADDRESS + "-" + i, new Part(i,
124: addrPart));
125: String maskPart = Integer.toString(0xFF & subnetMask
126: .getAddress()[i]);
127: setParameter(SUBNET_MASK + "-" + i, new Part(i,
128: maskPart));
129: partNumbers.add(new Integer(i));
130: }
131: setParameter(IPRangeProfile.PART_NUMBERS, partNumbers);
132:
133: }
134: }
135:
136: protected static void validateAddresses(AbstractUsecase usecase) {
137: String[] names = { "network address", "subnet mask" };
138: String[] params = { IPRangeProfile.NETWORK_ADDRESS,
139: IPRangeProfile.SUBNET_MASK };
140:
141: for (byte type = 0; type < names.length; type++) {
142: for (byte i = 0; i < 4; i++) {
143: String paramName = params[type] + "-" + i;
144: Part part = new Part(i);
145: part.setValue(usecase.getParameterAsString(paramName));
146: if (!part.isValid()) {
147: String[] parameters = { Integer.toString(i + 1),
148: names[type] };
149: usecase.addErrorMessage("invalid-ip-address-part",
150: parameters);
151: }
152: usecase.setParameter(paramName, part);
153: }
154: }
155: }
156:
157: /**
158: * IP address part holder.
159: */
160: public static class Part {
161: private String value;
162: private byte position;
163:
164: /**
165: * Ctor.
166: * @param _position The position.
167: * @param _value The value.
168: */
169: public Part(byte _position, String _value) {
170: this .value = _value;
171: this .position = _position;
172: }
173:
174: /**
175: * Ctor.
176: * @param _position The position.
177: */
178: public Part(byte _position) {
179: this (_position, "0");
180: }
181:
182: /**
183: * Returns the position
184: * @return The position
185: */
186: public byte getPosition() {
187: return this .position;
188: }
189:
190: /**
191: * Returns the value
192: * @return The value
193: */
194: public String getValue() {
195: return this .value;
196: }
197:
198: /**
199: * @param _value The value.
200: */
201: public void setValue(String _value) {
202: this .value = _value;
203: }
204:
205: /**
206: * Checks if the part is valid.
207: * @return A boolean value.
208: */
209: public boolean isValid() {
210:
211: boolean valid = true;
212: try {
213: int i = Integer.parseInt(this .value);
214: if (!(0 <= i && i <= 255)) {
215: valid = false;
216: }
217: } catch (NumberFormatException e) {
218: valid = false;
219: }
220:
221: return valid;
222: }
223:
224: }
225:
226: }
|