free-ice commented on a change in pull request #41:
URL: https://github.com/apache/ws-wss4j/pull/41#discussion_r717710678



##########
File path: 
ws-security-common/src/main/java/org/apache/wss4j/common/util/CommaDelimiterRfc2253Name.java
##########
@@ -0,0 +1,106 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.wss4j.common.util;
+
+import javax.naming.InvalidNameException;
+import javax.naming.ldap.LdapName;
+import javax.naming.ldap.Rdn;
+import java.util.List;
+
+/**
+ * Convert a RFC 2253 String using \ to escape unicode characters into one 
that is compatible
+ * with Microsoft's WFC and Java.<p><br>
+ *
+ * <strong>Detail:</strong>
+ * Converts a string in RFC2253 format and replaces \ escaped characters with 
a string quoted representation.
+ * It also places a space before the next RDN. <p> <br>
+ * There are two alternate ways an RFC 2253 RDN can escape unicode characters, 
either with '\'
+ * or by using quotes. Java seems to recognize both formats but Microsoft's 
WFC only seems to recognize quotes.
+ * Since implementations may escape any characters and string is already in 
valid format no knowledge is
+ * required of escapable characters.
+ */
+public class CommaDelimiterRfc2253Name {
+
+    /**
+     * Return rfs2253String that delimits using quotes
+     *
+     * @param rfs2253String a string in rfc 2253 format using a \ as delimiter.
+     * @return Rdn in quoted form if required.
+     */
+    public String execute(String rfs2253String) {
+        StringBuilder commaDNBuilder = new StringBuilder();
+        try {
+            LdapName ldapname = new LdapName(rfs2253String);
+            List<Rdn> rdns = ldapname.getRdns();
+
+            for (int i = rdns.size() - 1; i >= 0; i--) {
+                Rdn rdn = rdns.get(i);
+                String rdnString = rdn.toString();
+                String appendString;
+                if (requiresDoubleQuoting(rdnString)) {
+                    appendString = convertToDoubleQuotes(rdnString);
+                } else {
+                    appendString = rdnString;
+                }
+                if (i == rdns.size() - 1) {
+                    commaDNBuilder.append(appendString);
+                } else {
+                    commaDNBuilder.append(", ").append(appendString);
+                }
+            }
+        } catch (InvalidNameException e) {
+            throw new IllegalArgumentException(" The distinguished name cannot 
be parsed : " + rfs2253String);

Review comment:
       The execute() method throws the same exception as the 
X500Principal.getName() so from an possible thrown Exception prespective that 
has not changed. 
   
   From JDK 11:
   ```
       public String getName(String format) {
           if (format != null) {
               if (format.equalsIgnoreCase(RFC1779)) {
                   return thisX500Name.getRFC1779Name();
               } else if (format.equalsIgnoreCase(RFC2253)) {
                   return thisX500Name.getRFC2253Name();
               } else if (format.equalsIgnoreCase(CANONICAL)) {
                   return thisX500Name.getRFC2253CanonicalName();
               }
           }
           throw new IllegalArgumentException("invalid format specified");
   ```
   
   That is why I wrapped the Exception so it woudl remain compatable which 
existing behavour. Since that has not changed there is nothing to test as far 
as I can see, 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@ws.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@ws.apache.org
For additional commands, e-mail: dev-h...@ws.apache.org

Reply via email to