1 /*
   2  * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.jndi.dns;
  27 
  28 
  29 import java.net.MalformedURLException;
  30 import java.util.Hashtable;
  31 import java.util.StringTokenizer;
  32 
  33 import com.sun.jndi.toolkit.url.Uri;
  34 import com.sun.jndi.toolkit.url.UrlUtil;
  35 
  36 
  37 /**
  38  * A DnsUrl represents a DNS pseudo-URL of the form
  39  * <pre>
  40  *   dns://[host][:port][/[domain]]
  41  * or
  42  *   dns:[/][domain]
  43  * </pre>
  44  * The host names a DNS server.  If the host is not provided, it
  45  * indicates that the underlying platform's DNS server(s) should be
  46  * used if possible, or that "localhost" should be used otherwise.  If
  47  * the port is not provided, the DNS default port 53 will be used.
  48  * The domain indicates the domain name of the context, and is not
  49  * necessarily related to the domain of the server; if it is not
  50  * provided, the root domain "." is used.  Special characters in
  51  * the domain name must be %-escaped as described in RFC 2396.
  52  *
  53  * @author Scott Seligman
  54  */
  55 
  56 
  57 public class DnsUrl extends Uri {
  58 
  59     private String domain;      // domain name of the context
  60 
  61 
  62     /**
  63      * Given a space-separated list of DNS URLs, returns an array of DnsUrl
  64      * objects.
  65      */
  66     public static DnsUrl[] fromList(String urlList)
  67             throws MalformedURLException {
  68 
  69         DnsUrl[] urls = new DnsUrl[(urlList.length() + 1) / 2];
  70         int i = 0;              // next available index in urls
  71         StringTokenizer st = new StringTokenizer(urlList, " ");
  72 
  73         while (st.hasMoreTokens()) {
  74             urls[i++] = new DnsUrl(st.nextToken());
  75         }
  76         DnsUrl[] trimmed = new DnsUrl[i];
  77         System.arraycopy(urls, 0, trimmed, 0, i);
  78         return trimmed;
  79     }
  80 
  81     public DnsUrl(String url) throws MalformedURLException {
  82         super(url);
  83 
  84         if (!scheme.equals("dns")) {
  85             throw new MalformedURLException(
  86                     url + " is not a valid DNS pseudo-URL");
  87         }
  88 
  89         domain = path.startsWith("/")
  90             ? path.substring(1)
  91             : path;
  92         domain = domain.isEmpty()
  93             ? "."
  94             : UrlUtil.decode(domain);
  95 
  96         // Debug
  97         // System.out.println("host=" + host + " port=" + port +
  98         //                    " domain=" + domain);
  99     }
 100 
 101     /**
 102      * Returns the domain of this URL, or "." if none is provided.
 103      * Never null.
 104      */
 105     public String getDomain() {
 106         return domain;
 107     }
 108 
 109 
 110 /*
 111     // Debug
 112     public static void main(String args[]) throws MalformedURLException {
 113         DnsUrl[] urls = fromList(args[0]);
 114         for (int i = 0; i < urls.length; i++) {
 115             System.out.println(urls[i].toString());
 116         }
 117     }
 118 */
 119 }