View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.wss4j.common.util;
21  
22  import javax.naming.InvalidNameException;
23  import javax.naming.ldap.LdapName;
24  import javax.naming.ldap.Rdn;
25  import java.util.List;
26  
27  /**
28   * Convert a RFC 2253 String using \ to escape unicode characters into one that is compatible
29   * with Microsoft's WFC and Java.<p><br>
30   *
31   * <strong>Detail:</strong>
32   * Converts a string in RFC2253 format and replaces \ escaped characters with a string quoted representation.
33   * It also places a space before the next RDN. <p> <br>
34   * There are two alternate ways an RFC 2253 RDN can escape unicode characters, either with '\'
35   * or by using quotes. Java seems to recognize both formats but Microsoft's WFC only seems to recognize quotes.
36   * Since implementations may escape any characters and string is already in valid format no knowledge is
37   * required of escapable characters.
38   */
39  public class CommaDelimiterRfc2253Name {
40  
41      /**
42       * Return rfc2253String that delimits using quotes.<p>
43       *
44       *
45       * @param rfc2253String a string in rfc 2253 format using a \ as delimiter.
46       * @return Rdn in quoted form if required.
47       * @throws  IllegalArgumentException if an error occurred parsing the rfc2256 string.
48       * However, since its assumed that a valid RFC 2253 string, X500Principal.getName(),
49       * is passed its unlikely that this exception would ever be thrown.
50       */
51      public String execute(String rfc2253String) {
52          StringBuilder commaDNBuilder = new StringBuilder();
53          try {
54              LdapName ldapname = new LdapName(rfc2253String);
55              List<Rdn> rdns = ldapname.getRdns();
56  
57              for (int i = rdns.size() - 1; i >= 0; i--) {
58                  Rdn rdn = rdns.get(i);
59                  String rdnString = rdn.toString();
60                  String appendString;
61                  if (requiresDoubleQuoting(rdnString)) {
62                      appendString = convertToDoubleQuotes(rdnString);
63                  } else {
64                      appendString = rdnString;
65                  }
66                  if (i == rdns.size() - 1) {
67                      commaDNBuilder.append(appendString);
68                  } else {
69                      commaDNBuilder.append(", ").append(appendString);
70                  }
71              }
72          } catch (InvalidNameException e) {
73              throw new IllegalArgumentException(" The distinguished name cannot be parsed : " + rfc2253String);
74          }
75          return commaDNBuilder.toString();
76      }
77  
78      private boolean requiresDoubleQuoting(String rdnString) {
79          return rdnString.contains("\\");
80      }
81  
82      private String convertToDoubleQuotes(String rdnString) {
83          StringBuilder quotedString = new StringBuilder();
84          int indexEquals = rdnString.indexOf('=');
85          String firstPart = rdnString.substring(0, indexEquals + 1);
86          String lastPart = rdnString.substring(indexEquals + 1);
87          String secondPart = unEscapeRfc2253RdnSubPart(lastPart);
88          return quotedString.append(firstPart).append('"').append(secondPart).append('"').toString();
89      }
90  
91      String unEscapeRfc2253RdnSubPart(String value) {
92          char[] charArray = value.toCharArray();
93          boolean previousEscape = false;
94          StringBuilder unescapedRdnPart = new StringBuilder();
95          for (char currentChar : charArray) {
96              if (currentChar != '\\') {
97                  previousEscape = false;
98                  unescapedRdnPart.append(currentChar);
99              } else if (previousEscape) {
100                 unescapedRdnPart.append(currentChar);
101                 previousEscape = false;
102             } else {
103                 previousEscape = true;
104             }
105         }
106 
107         return unescapedRdnPart.toString();
108     }
109 
110 }