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.util.StringTokenizer;
   23   import java.net.InetAddress;
   24   import java.net.Inet6Address;
   25   
   26   /**
   27    * @version $Revision$ $Date$
   28    */
   29   public class ExactIPv6AddressPermission implements IPAddressPermission {
   30       private static final Pattern MASK_VALIDATOR = Pattern.compile("^(([a-fA-F0-9]{1,4}:){7})([a-fA-F0-9]{1,4})$");
   31   
   32       public static boolean canSupport(String mask) {
   33           Matcher matcher = MASK_VALIDATOR.matcher(mask);
   34           return matcher.matches();
   35       }
   36   
   37       private final byte[] bytes;
   38   
   39       public ExactIPv6AddressPermission(byte[] bytes) {
   40           this.bytes = bytes;
   41       }
   42   
   43       public ExactIPv6AddressPermission(String mask) {
   44           Matcher matcher = MASK_VALIDATOR.matcher(mask);
   45           if (false == matcher.matches()) {
   46               throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern());
   47           }
   48   
   49           bytes = new byte[16];
   50           int pos = 0;
   51           StringTokenizer tokenizer = new StringTokenizer(mask, ":");
   52           while (tokenizer.hasMoreTokens()) {
   53               String token = tokenizer.nextToken();
   54               int value = Integer.parseInt(token, 16);
   55               bytes[pos++] = (byte) ((value & 0xff00) >> 8);
   56               bytes[pos++] = (byte) value;
   57           }
   58       }
   59   
   60       public boolean implies(InetAddress address) {
   61           if (false == address instanceof Inet6Address) {
   62               return false;
   63           }
   64   
   65           byte[] byteAddress = address.getAddress();
   66           for (int i = 0; i < 16; i++) {
   67               if (byteAddress[i] != bytes[i]) {
   68                   return false;
   69               }
   70           }
   71           return true;
   72       }
   73   }

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