microsoft-azure-storage/src/com/microsoft/azure/storage/core/UriQueryBuilder.java
public URI addToURI(final URI uri) throws URISyntaxException, StorageException {
        final String origRawQuery = uri.getRawQuery();
        final String rawFragment = uri.getRawFragment();
        final String uriString = uri.resolve(uri).toASCIIString();

        final HashMap<String, String[]> origQueryMap = PathUtility.parseQueryString(origRawQuery);

        // Try/Insert original queries to map

        for (final Entry<String, String[]> entry : origQueryMap.entrySet()) {
            for (final String val : entry.getValue()) {
                this.insertKeyValue(entry.getKey(), val);
            }
        }

        final StringBuilder retBuilder = new StringBuilder();

        // has a fragment
        if (Utility.isNullOrEmpty(origRawQuery) && !Utility.isNullOrEmpty(rawFragment)) {
            final int bangDex = uriString.indexOf('#');
            retBuilder.append(uriString.substring(0, bangDex));
        }
        else if (!Utility.isNullOrEmpty(origRawQuery)) {
            // has a query
            final int queryDex = uriString.indexOf('?');
            retBuilder.append(uriString.substring(0, queryDex));
        }
        else {
            // no fragment or query
            retBuilder.append(uriString);
            if (uri.getRawPath().length() <= 0) {
                retBuilder.append("/");
            }
        }

        final String finalQuery = this.toString();

        if (finalQuery.length() > 0) {
            retBuilder.append("?");
            retBuilder.append(finalQuery);
        }

        if (!Utility.isNullOrEmpty(rawFragment)) {
            retBuilder.append("#");
            retBuilder.append(rawFragment);
        }

        return new URI(retBuilder.toString());
    }
public URI addToURI(final URI uri) throws URISyntaxException, StorageException {
        final String origRawQuery = uri.getRawQuery();
        final String rawFragment = uri.getRawFragment();
        final String uriString = uri.resolve(uri).toASCIIString();

        final HashMap<String, String[]> origQueryMap = PathUtility.parseQueryString(origRawQuery);

        // Try/Insert original queries to map

        for (final Entry<String, String[]> entry : origQueryMap.entrySet()) {
            for (final String val : entry.getValue()) {
                this.insertKeyValue(entry.getKey(), val);
            }
        }

        final StringBuilder retBuilder = new StringBuilder();

        // has a fragment
        if (Utility.isNullOrEmpty(origRawQuery) && !Utility.isNullOrEmpty(rawFragment)) {
            final int bangDex = uriString.indexOf('#');
            retBuilder.append(uriString.substring(0, bangDex));
        }
        else if (!Utility.isNullOrEmpty(origRawQuery)) {
            // has a query
            final int queryDex = uriString.indexOf('?');
            retBuilder.append(uriString.substring(0, queryDex));
        }
        else {
            // no fragment or query
            retBuilder.append(uriString);
            if (uri.getRawPath().length() <= 0) {
                retBuilder.append("/");
            }
        }

        final String finalQuery = this.toString();

        if (finalQuery.length() > 0) {
            retBuilder.append("?");
            retBuilder.append(finalQuery);
        }

        if (!Utility.isNullOrEmpty(rawFragment)) {
            retBuilder.append("#");
            retBuilder.append(rawFragment);
        }

        return new URI(retBuilder.toString());
    }
Add query parameter to an existing Uri. This takes care of any existing query parameters in the Uri.

@param uri
the original uri.
@return the appended uri
@throws URISyntaxException
if the resulting uri is invalid.
@throws StorageException