Java URI Decode decodeUnreserved(URI address)

Here you can find the source of decodeUnreserved(URI address)

Description

Decodes any percent-encoded URI-unreserved characters in the URI address.

License

Apache License

Parameter

Parameter Description
address URI to decode.

Return

a new URI with all unreserved characters decoded, or the same URI if there are no unreserved characters to decode.

Declaration

public static URI decodeUnreserved(URI address) 

Method Source Code


//package com.java2s;
/*/*w  ww .  j av  a2  s . c o m*/
 * Copyright 2014 Google. Inc.
 *
 * 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 java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern ENCODED_UNRESERVED = Pattern
            .compile("\\%(?:4[1-9A-F]|5[0-9A]|6[1-9A-F]|7[0-9A]|3[0-9]|2D|2E|5F|7E)", Pattern.CASE_INSENSITIVE);

    /**
     * Decodes any percent-encoded URI-unreserved characters in the URI address.
     * 
     * <p>As mentioned in <a href="http://tools.ietf.org/html/rfc3986#section-2.3"
     * target="_blank">RFC 3986, Section 2.3</a>, any unreserved characters in a
     * URI should be decoded before the URI can be safely compared or normalized.
     * Unfortunately, Java's URI implementation does not do this for us. 
     *
     * @param address URI to decode.
     * 
     * @return a new URI with all unreserved characters decoded, or the same
     *    URI if there are no unreserved characters to decode.
     * 
     * @see #decodeUnreserved(String)
     */
    public static URI decodeUnreserved(URI address) {
        String addressStr = address.toString();
        String decoded = decodeUnreserved(addressStr);
        return decoded == addressStr ? address : URI.create(decoded);
    }

    /**
     * Decodes any percent-encoded URI-unreserved characters in the string.
     * 
     * <p>The Java-included {@link java.net.URLDecoder} class has similar
     * functionality, except it decodes ALL percent-encoded characters; it is
     * designed primarily for decoding individual key/value strings in the query
     * portion of a URL after they have been extracted. For example, it will
     * decode {@code "/my+keys%3F/%62%61%6E%6B"} as {@code "/my keys?/bank"},
     * which will result in an invalid URI because {@code "/bank"} would be
     * interpreted as a query. The {@code '+'} symbol should also only be
     * interpreted as a space in the query portion, and not in any other part of
     * a URI. This method will decode the string as {@code "/my+keys%3F/bank"},
     * preserving the meaning of the address. 
     * 
     * @param str String to decode.
     * 
     * @return the string with all unreserved characters decoded.
     */
    public static String decodeUnreserved(String str) {
        Matcher m = ENCODED_UNRESERVED.matcher(str);
        boolean found = m.find();
        if (found) {
            StringBuilder sb = new StringBuilder(str.length());
            int findStart = 0;
            do {
                final int pos = m.start();
                sb.append(str, findStart, pos);

                // Assume that first hex is always a digit (restricted to the regex)
                int decoded = ((str.charAt(pos + 1) - '0') << 4);
                // Complete decoding by checking second hex
                final char hex = str.charAt(pos + 2);
                decoded += hex - (hex <= '9' ? '0' : (hex <= 'Z' ? 'A' : 'a') - 10);
                sb.append((char) decoded);

                findStart = m.end();
                found = m.find();
            } while (found);

            sb.append(str, findStart, str.length());
            return sb.toString();
        }
        return str;
    }
}

Related

  1. decode(String encodedURI)
  2. decode(String uri)
  3. decodeToUtf8(String uri)
  4. decodeURI(CharSequence uriCharSequence, String charset)
  5. decodeURI(java.net.URI uri)
  6. decodeURI(String s)
  7. decodeURI(String str, boolean fullUri)