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 StartWithIPAddressPermission implements IPAddressPermission {
   29       private static final Pattern MASK_VALIDATOR = Pattern.compile("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.0$");
   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[] bytes;
   37   
   38       public StartWithIPAddressPermission(String mask) {
   39           Matcher matcher = MASK_VALIDATOR.matcher(mask);
   40           if (false == matcher.matches()) {
   41               throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern());
   42           }
   43   
   44           Byte[] tmpBytes = new Byte[4];
   45           boolean isWildCard = false;
   46           int size = 0;
   47           for (int i = 0; i < 3; i++) {
   48               String group = matcher.group(i + 1);
   49               if (group.equals("0")) {
   50                   isWildCard = true;
   51               } else if (isWildCard) {
   52                   throw new IllegalArgumentException("0 at position " + size + " in mask");
   53               } else {
   54                   int value = Integer.parseInt(group);
   55                   if (value < 0 || 255 < value) {
   56                       throw new IllegalArgumentException("byte #" + i + " is not valid.");
   57                   }
   58                   tmpBytes[i] = new Byte((byte) value);
   59                   size++;
   60               }
   61           }
   62   
   63           bytes = new byte[size];
   64           for (int i = 0; i < bytes.length; i++) {
   65               bytes[i] = tmpBytes[i].byteValue();
   66           }
   67       }
   68   
   69       public boolean implies(InetAddress address) {
   70           if (false == address instanceof Inet4Address) {
   71               return false;
   72           }
   73   
   74           byte[] byteAddress = address.getAddress();
   75           for (int i = 0; i < bytes.length; i++) {
   76               if (byteAddress[i] != bytes[i]) {
   77                   return false;
   78               }
   79           }
   80           return true;
   81       }
   82   }

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