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