Java X500Principal toPrincipal(String globusID)

Here you can find the source of toPrincipal(String globusID)

Description

Converts Globus DN format "/O=C/OU=B/CN=A" into an X500Principal representation, which accepts RFC 2253 or 1779 formatted DN's and also attribute types as defined in RFC 2459 (e.g.

License

Apache License

Parameter

Parameter Description
globusID DN in Globus format

Return

the X500Principal representation of the given DN

Declaration

public static X500Principal toPrincipal(String globusID) 

Method Source Code


//package com.java2s;
/*//from   w w  w  .  j  a v  a 2  s  . c  o m
 * Copyright 1999-2010 University of Chicago
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

import javax.security.auth.x500.X500Principal;

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final Map<String, String> KEYWORD_MAP = new HashMap<String, String>();

    /**
     * Converts Globus DN format "/O=C/OU=B/CN=A" into an X500Principal
     * representation, which accepts RFC 2253 or 1779 formatted DN's and also
     * attribute types as defined in RFC 2459 (e.g. "CN=A,OU=B,O=C"). This
     * method should allow the forward slash, "/", to occur in attribute values
     * (see GFD.125 section 3.2.2 -- RFC 2252 allows "/" in PrintableStrings).
     * @param globusID DN in Globus format
     * @return the X500Principal representation of the given DN
     */
    public static X500Principal toPrincipal(String globusID) {

        if (globusID == null) {
            return null;
        }
        String id = globusID.trim();
        StringBuilder buf = new StringBuilder(id.length());

        if (!id.isEmpty()) {

            final int IDLE = 0;
            final int VALUE = 1;
            final int KEY = 2;

            int state = IDLE;

            int cEnd = 0;
            char[] asChars = id.toCharArray();

            /*
             * walk in reverse order and split into RDN
             */
            for (int i = asChars.length - 1; i >= 0; i--) {

                char c = asChars[i];
                switch (state) {
                case KEY:
                    if (c == '/' || c == ' ') {
                        /*
                          handle names with comma according rfc1779
                         */
                        String s = id.substring(i + 1, cEnd + 1);
                        int commaIndex = s.indexOf(',');
                        if (commaIndex != -1) {
                            s = s.substring(0, commaIndex) + "\\" + s.substring(commaIndex);
                        }
                        buf.append(s).append(',');
                        state = IDLE;
                    }
                    break;
                case VALUE:
                    if (c == '=') {
                        state = KEY;
                    }
                    break;
                case IDLE:
                default:
                    // idle
                    if (c == '/' || c == ' ') {
                        continue;
                    } else {
                        cEnd = i;
                        state = VALUE;
                    }
                }
            }

            // delete last extra comma
            buf.deleteCharAt(buf.length() - 1);
        }

        String dn = buf.toString();

        return new X500Principal(dn, KEYWORD_MAP);
    }
}

Related

  1. getDNField(String fieldID, X500Principal principal)
  2. isTGSPrincipal(KerberosPrincipal principal)
  3. isTicketGrantingServerPrincipal(KerberosPrincipal principal)
  4. serverLogin(final String serverPrincipal, final String serverPassword)
  5. toGlobusID(X500Principal principal)
  6. toPrincipal(String globusID)