Home » openejb-3.1.2-src » org.apache » openejb » server » auth » [javadoc | source]

    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    * http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing, software
   13    * distributed under the License is distributed on an "AS IS" BASIS,
   14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    * See the License for the specific language governing permissions and
   16    * limitations under the License.
   17    */
   18   package org.apache.openejb.server.auth;
   19   
   20   import java.util.regex.Pattern;
   21   import java.util.regex.Matcher;
   22   import java.net.InetAddress;
   23   import java.net.Inet4Address;
   24   
   25   /**
   26    * @version $Revision$ $Date$
   27    */
   28   public class NetmaskIPAddressPermission implements IPAddressPermission {
   29       private static final Pattern MASK_VALIDATOR = Pattern.compile("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/((\\d{1,2})|(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3}))$");
   30   
   31       public static boolean canSupport(String mask) {
   32           Matcher matcher = MASK_VALIDATOR.matcher(mask);
   33           return matcher.matches();
   34       }
   35   
   36       private final byte[] networkAddressBytes;
   37       private final byte[] netmaskBytes;
   38   
   39       public NetmaskIPAddressPermission(String mask) {
   40           Matcher matcher = MASK_VALIDATOR.matcher(mask);
   41           if (false == matcher.matches()) {
   42               throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern());
   43           }
   44   
   45           networkAddressBytes = new byte[4];
   46           for (int i = 0; i < 4; i++) {
   47               String group = matcher.group(i + 1);
   48               int value = Integer.parseInt(group);
   49               if (value < 0 || 255 < value) {
   50                   throw new IllegalArgumentException("byte #" + i + " is not valid.");
   51               }
   52               networkAddressBytes[i] = (byte) value;
   53           }
   54   
   55           netmaskBytes = new byte[4];
   56           String netmask = matcher.group(6);
   57           if (null != netmask) {
   58               int value = Integer.parseInt(netmask);
   59               int pos = value / 8;
   60               int shift = 8 - value % 8;
   61               for (int i = 0; i < pos; i++) {
   62                   netmaskBytes[i] = (byte) 0xff;
   63               }
   64               netmaskBytes[pos] = (byte) (0xff << shift);
   65           } else {
   66               for (int i = 0; i < 4; i++) {
   67                   String group = matcher.group(i + 7);
   68                   int value = Integer.parseInt(group);
   69                   if (value < 0 || 255 < value) {
   70                       throw new IllegalArgumentException("byte #" + i + " is not valid.");
   71                   }
   72                   netmaskBytes[i] = (byte) value;
   73               }
   74           }
   75       }
   76   
   77       public boolean implies(InetAddress address) {
   78           if (false == address instanceof Inet4Address) {
   79               return false;
   80           }
   81   
   82           byte[] byteAddress = address.getAddress();
   83           for (int i = 0; i < 4; i++) {
   84               if ((netmaskBytes[i] & byteAddress[i]) != networkAddressBytes[i]) {
   85                   return false;
   86               }
   87           }
   88           return true;
   89       }
   90   }

Home » openejb-3.1.2-src » org.apache » openejb » server » auth » [javadoc | source]