You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2006/01/09 01:17:12 UTC

svn commit: r367138 - in /directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util: HttpClientError.java LdapURL.java URIException.java UrlDecoderException.java

Author: elecharny
Date: Sun Jan  8 16:17:00 2006
New Revision: 367138

URL: http://svn.apache.org/viewcvs?rev=367138&view=rev
Log:
- No more dependency on commons-httpclients
- Added HttpClientError, URIException and UrlDecoderException classes
- Added some methods in LdapURL

Added:
    directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/HttpClientError.java
    directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/URIException.java
    directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/UrlDecoderException.java
Modified:
    directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/LdapURL.java

Added: directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/HttpClientError.java
URL: http://svn.apache.org/viewcvs/directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/HttpClientError.java?rev=367138&view=auto
==============================================================================
--- directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/HttpClientError.java (added)
+++ directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/HttpClientError.java Sun Jan  8 16:17:00 2006
@@ -0,0 +1,57 @@
+/*
+ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpClientError.java,v 1.4 2004/05/13 04:03:25 mbecke Exp $
+ * $Revision: 155418 $
+ * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
+ *
+ * ====================================================================
+ *
+ *  Copyright 2002-2004 The Apache Software Foundation
+ *
+ *  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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.ldap.common.codec.util;
+
+/**
+ * Signals that an error has occurred.
+ * 
+ * @author Ortwin Gl?ck
+ * @version $Revision: 155418 $ $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
+ * @since 3.0
+ */
+public class HttpClientError extends Error {
+	final static long serialVersionUID = 1L;
+
+    /**
+     * Creates a new HttpClientError with a <tt>null</tt> detail message.
+     */
+    public HttpClientError() {
+        super();
+    }
+
+    /**
+     * Creates a new HttpClientError with the specified detail message.
+     * @param message The error message
+     */
+    public HttpClientError(String message) {
+        super(message);
+    }
+
+}

Modified: directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/LdapURL.java
URL: http://svn.apache.org/viewcvs/directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/LdapURL.java?rev=367138&r1=367137&r2=367138&view=diff
==============================================================================
--- directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/LdapURL.java (original)
+++ directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/LdapURL.java Sun Jan  8 16:17:00 2006
@@ -18,14 +18,12 @@
 
 import org.apache.asn1.codec.DecoderException;
 
-import org.apache.commons.httpclient.URIException;
-import org.apache.commons.httpclient.util.URIUtil;
-
 import org.apache.ldap.common.filter.FilterParserImpl;
+import org.apache.ldap.common.name.LdapDN;
 import org.apache.ldap.common.util.StringTools;
-import org.apache.ldap.common.codec.util.LdapDN;
 import org.apache.ldap.common.codec.util.LdapString;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 
@@ -559,6 +557,171 @@
     }
 
     /**
+     * From commons-httpclients.
+     * 
+     * Converts the byte array of HTTP content characters to a string. If
+     * the specified charset is not supported, default system encoding
+     * is used.
+     *
+     * @param data the byte array to be encoded
+     * @param offset the index of the first byte to encode
+     * @param length the number of bytes to encode 
+     * @param charset the desired character encoding
+     * @return The result of the conversion.
+     * 
+     * @since 3.0
+     */
+    public static String getString( final byte[] data, int offset, int length, String charset ) 
+    {
+        if (data == null) 
+        {
+            throw new IllegalArgumentException("Parameter may not be null");
+        }
+
+        if (charset == null || charset.length() == 0) 
+        {
+            throw new IllegalArgumentException("charset may not be null or empty");
+        }
+
+        try 
+        {
+            return new String(data, offset, length, charset);
+        } 
+        catch (UnsupportedEncodingException e) 
+        {
+            return new String(data, offset, length);
+        }
+    }
+
+
+    /**
+     * From commons-httpclients.
+     * 
+     * Converts the byte array of HTTP content characters to a string. If
+     * the specified charset is not supported, default system encoding
+     * is used.
+     *
+     * @param data the byte array to be encoded
+     * @param charset the desired character encoding
+     * @return The result of the conversion.
+     * 
+     * @since 3.0
+     */
+    public static String getString(final byte[] data, String charset) 
+    {
+        return getString(data, 0, data.length, charset);
+    }
+
+    /**
+     * Converts the specified string to byte array of ASCII characters.
+     *
+     * @param data the string to be encoded
+     * @return The string as a byte array.
+     * 
+     * @since 3.0
+     */
+    public static byte[] getAsciiBytes(final String data) 
+    {
+
+        if (data == null) 
+        {
+            throw new IllegalArgumentException("Parameter may not be null");
+        }
+
+        try 
+        {
+            return data.getBytes("US-ASCII");
+        } 
+        catch (UnsupportedEncodingException e) 
+        {
+            throw new HttpClientError("HttpClient requires ASCII support");
+        }
+    }
+
+    /**
+     * From commons-codec.
+     * 
+     * Decodes an array of URL safe 7-bit characters into an array of 
+     * original bytes. Escaped characters are converted back to their 
+     * original representation.
+     *
+     * @param bytes array of URL safe characters
+     * @return array of original bytes 
+     * @throws DecoderException Thrown if URL decoding is unsuccessful
+     */
+    private static final byte[] decodeUrl(byte[] bytes) 
+         throws UrlDecoderException
+    {
+        if (bytes == null) 
+        {
+            return null;
+        }
+        
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
+        
+        for (int i = 0; i < bytes.length; i++) 
+        {
+            int b = bytes[i];
+            
+            if (b == '+') 
+            {
+                buffer.write(' ');
+            } 
+            else if (b == '%') 
+            {
+                try 
+                {
+                    int u = Character.digit((char)bytes[++i], 16);
+                    int l = Character.digit((char)bytes[++i], 16);
+                    
+                    if (u == -1 || l == -1) 
+                    {
+                        throw new UrlDecoderException("Invalid URL encoding");
+                    }
+                    
+                    buffer.write((char)((u << 4) + l));
+                } 
+                catch(ArrayIndexOutOfBoundsException e) 
+                {
+                    throw new UrlDecoderException("Invalid URL encoding");
+                }
+            } 
+            else 
+            {
+                buffer.write(b);
+            }
+        }
+        
+        return buffer.toByteArray(); 
+    }
+
+    /**
+     * From commons-httpclients.
+     * 
+     * Unescape and decode a given string regarded as an escaped string with the
+     * default protocol charset.
+     *
+     * @param escaped a string
+     * @return the unescaped string
+     * 
+     * @throws URIException if the string cannot be decoded (invalid)
+     * 
+     * @see URI#getDefaultProtocolCharset
+     */
+    private static String decode(String escaped) throws URIException 
+    {
+        try 
+        {
+            byte[] rawdata = decodeUrl(getAsciiBytes(escaped));
+            return getString( rawdata, "UTF-8" );
+        } 
+        catch (UrlDecoderException e) 
+        {
+            throw new URIException(e.getMessage());
+        }
+    }
+
+    /**
      * Parse a string and check that it complies with RFC 2253.
      * Here, we will just call the LdapDN parser to do the job.
      * 
@@ -578,7 +741,7 @@
 
         try
         {
-            dn = new LdapDN( URIUtil.decode( new String( chars, pos, end - pos ) ) );
+            dn = new LdapDN( decode( new String( chars, pos, end - pos ) ) );
         }
         catch ( URIException ue )
         {
@@ -636,7 +799,7 @@
                             return -1;
                         }
 
-                        String decodedAttr = URIUtil.decode( attribute );
+                        String decodedAttr = decode( attribute );
 
 
                         if ( hAttributes.contains( decodedAttr ) == false )
@@ -683,7 +846,7 @@
                     return -1;
                 }
 
-                String decodedAttr = URIUtil.decode( attribute );
+                String decodedAttr = decode( attribute );
 
 
                 if ( hAttributes.contains( decodedAttr ) == false )
@@ -720,7 +883,7 @@
 
         try
         {
-            filter       = URIUtil.decode( new String( chars, pos, end - pos ) );
+            filter       = decode( new String( chars, pos, end - pos ) );
             filterParser.parse( filter );
         }
         catch ( URIException ue )
@@ -869,7 +1032,7 @@
                     }
                     else
                     {
-                        value = new String( URIUtil.decode( new String( chars, start, i - start ) ) ).trim();
+                        value = new String( decode( new String( chars, start, i - start ) ) ).trim();
 
                         if ( value.length() == 0 )
                         {
@@ -904,7 +1067,7 @@
                     }
 
                     // An optionnal value
-                    extension = new String( URIUtil.decode( new String( chars, start, i - start ) ) )
+                    extension = new String( decode( new String( chars, start, i - start ) ) )
                             .trim();
 
                     if ( extension.length() == 0 )
@@ -935,12 +1098,12 @@
 
             if ( extension == null )
             {
-                extension = new String( URIUtil.decode(
+                extension = new String( decode(
                             new String( chars, start, chars.length - start ) ) ).trim();
             }
             else
             {
-                value = new String( URIUtil.decode(
+                value = new String( decode(
                             new String( chars, start, chars.length - start ) ) ).trim();
             }
 

Added: directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/URIException.java
URL: http://svn.apache.org/viewcvs/directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/URIException.java?rev=367138&view=auto
==============================================================================
--- directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/URIException.java (added)
+++ directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/URIException.java Sun Jan  8 16:17:00 2006
@@ -0,0 +1,109 @@
+/*
+ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/URIException.java,v 1.12 2004/09/30 18:53:20 olegk Exp $
+ * $Revision: 155418 $
+ * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
+ *
+ * ====================================================================
+ *
+ *  Copyright 2002-2004 The Apache Software Foundation
+ *
+ *  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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.ldap.common.codec.util;
+
+/**
+ * The URI parsing and escape encoding exception.
+ *
+ * @author <a href="mailto:jericho at apache.org">Sung-Gu</a>
+ * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @version $Revision: 155418 $ $Date: 2002/03/14 15:14:01 
+ */
+public class URIException extends Exception {
+	final static long serialVersionUID = 1L;
+
+    // ----------------------------------------------------------- constructors
+    /**
+     * The constructor with a reason string argument.
+     *
+     * @param reason the reason
+     */
+    public URIException(String reason) {
+        super(reason); // for backward compatibility of Throwable
+        this.reason = reason;
+        this.reasonCode = UNKNOWN;
+    }
+
+    // -------------------------------------------------------------- constants
+
+    /**
+     * No specified reason code.
+     */
+    public static final int UNKNOWN = 0;
+
+
+    /**
+     * The URI parsing error.
+     */
+    //public static final int PARSING = 1;
+
+
+    /**
+     * The unsupported character encoding.
+     */
+    //public static final int UNSUPPORTED_ENCODING = 2;
+
+
+    /**
+     * The URI escape encoding and decoding error.
+     */
+    //public static final int ESCAPING = 3;
+
+
+    /**
+     * The DNS punycode encoding or decoding error.
+     */
+    //public static final int PUNYCODE = 4;
+
+    // ------------------------------------------------------------- properties
+
+    /**
+     * The reason code.
+     */
+    protected int reasonCode;
+
+
+    /**
+     * The reason message.
+     */
+    protected String reason;
+
+    // ---------------------------------------------------------------- methods
+
+    /**
+     * Get the reason code.
+     *
+     * @return the reason code
+     */
+    public int getReasonCode() {
+        return reasonCode;
+    }
+}
+

Added: directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/UrlDecoderException.java
URL: http://svn.apache.org/viewcvs/directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/UrlDecoderException.java?rev=367138&view=auto
==============================================================================
--- directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/UrlDecoderException.java (added)
+++ directory/trunk/ldap-common/src/main/java/org/apache/ldap/common/codec/util/UrlDecoderException.java Sun Jan  8 16:17:00 2006
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * 
+ * 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.
+ */ 
+
+package org.apache.ldap.common.codec.util;
+
+/**
+ * Thrown when a Decoder has encountered a failure condition during a decode. 
+ * 
+ * @author Apache Software Foundation
+ * @version $Id: DecoderException.java,v 1.9 2004/02/29 04:08:31 tobrien Exp $
+ */
+public class UrlDecoderException extends Exception {
+	final static long serialVersionUID = 1L;
+
+    /**
+     * Creates a DecoderException
+     * 
+     * @param pMessage A message with meaning to a human
+     */
+    public UrlDecoderException(String pMessage) {
+        super(pMessage);
+    }
+
+}  
+