View Javadoc
1   /*
2    * Copyright (c) 2000, 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.cosnaming;
27  
28  import javax.naming.Name;
29  import javax.naming.NamingException;
30  
31  import java.net.MalformedURLException;
32  import com.sun.jndi.toolkit.url.UrlUtil;
33  
34  /**
35   * Extract components of a "corbaname" URL.
36   *
37   * The format of an corbaname URL is defined in INS 99-12-03 as follows.
38   *<p>
39   * corbaname url = "corbaname:" <corbaloc_obj> ["#" <string_name>]
40   * corbaloc_obj  = <obj_addr_list> ["/" <key_string>]
41   * obj_addr_list = as defined in a corbaloc URL
42   * key_string    = as defined in a corbaloc URL
43   * string_name   = stringified COS name | empty_string
44   *<p>
45   * Characters in <string_name> are escaped as follows.
46   * US-ASCII alphanumeric characters are not escaped. Any characters outside
47   * of this range are escaped except for the following:
48   *        ; / : ? @ & = + $ , - _ . ! ~ * ; ( )
49   * Escaped characters is escaped by using a % followed by its 2 hexadecimal
50   * numbers representing the octet.
51   *<p>
52   * The corbaname URL is parsed into two parts: a corbaloc URL and a COS name.
53   * The corbaloc URL is constructed by concatenation "corbaloc:" with
54   * <corbaloc_obj>.
55   * The COS name is <string_name> with the escaped characters resolved.
56   *<p>
57   * A corbaname URL is resolved by:
58   *<ol>
59   *<li>Construct a corbaloc URL by concatenating "corbaloc:" and <corbaloc_obj>.
60   *<li>Resolve the corbaloc URL to a NamingContext by using
61   *     nctx = ORB.string_to_object(corbalocUrl);
62   *<li>Resolve <string_name> in the NamingContext.
63   *</ol>
64   *
65   * @author Rosanna Lee
66   */
67  
68  public final class CorbanameUrl {
69      private String stringName;
70      private String location;
71  
72      /**
73       * Returns a possibly empty but non-null string that is the "string_name"
74       * portion of the URL.
75       */
76      public String getStringName() {
77          return stringName;
78      }
79  
80      public Name getCosName() throws NamingException {
81          return CNCtx.parser.parse(stringName);
82      }
83  
84      public String getLocation() {
85          return "corbaloc:" + location;
86      }
87  
88      public CorbanameUrl(String url) throws MalformedURLException {
89  
90          if (!url.startsWith("corbaname:")) {
91              throw new MalformedURLException("Invalid corbaname URL: " + url);
92          }
93  
94          int addrStart = 10;  // "corbaname:"
95  
96          int addrEnd = url.indexOf('#', addrStart);
97          if (addrEnd < 0) {
98              addrEnd = url.length();
99              stringName = "";
100         } else {
101             stringName = UrlUtil.decode(url.substring(addrEnd+1));
102         }
103         location = url.substring(addrStart, addrEnd);
104 
105         int keyStart = location.indexOf("/");
106         if (keyStart >= 0) {
107             // Has key string
108             if (keyStart == (location.length() -1)) {
109                 location += "NameService";
110             }
111         } else {
112             location += "/NameService";
113         }
114     }
115 /*
116     // for testing only
117     public static void main(String[] args) {
118         try {
119             CorbanameUrl url = new CorbanameUrl(args[0]);
120 
121             System.out.println("location: " + url.getLocation());
122             System.out.println("string name: " + url.getStringName());
123         } catch (MalformedURLException e) {
124             e.printStackTrace();
125         }
126     }
127 */
128 }