<commit_before>/*
 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * 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.
 *
 * Contributors:
 *     <NAME>
 *     <NAME>
 */

package org.nuxeo.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Helper class to parse a URI or build one given parameters.
 *
 * @author <NAME>
 * @author <NAME>
 */
public final class URIUtils {

    private static final Log log = LogFactory.getLog(URIUtils.class);

    // This is an utility class.
    private URIUtils() {
    }

    /**
     * Creates an URI query given the request parameters.
     *
     * @return an URI query given the request parameters.
     */
    public static String getURIQuery(Map<String, String> parameters) {
        String query = null;
        if (parameters != null) {
            try {
                List<String> items = new ArrayList<String>();
                for (Map.Entry<String, String> paramInfo : parameters.entrySet()) {
                    String key = paramInfo.getKey();
                    String value = paramInfo.getValue();
                    if (key != null) {
                        if (value == null) {
                            value = "";
                        }
                        items.add(String.format("%s=%s",
                                URLEncoder.encode(key, "UTF-8"),
                                URLEncoder.encode(value, "UTF-8")));
                    }
                }
                query = org.apache.commons.lang.StringUtils.join(items, "&");
            } catch (UnsupportedEncodingException e) {
                log.error("Failed to get uri query", e);
            }
        }
        return query;
    }

    /**
     * Returns an URI path given the uri.
     */
    public static String getURIPath(String uri) {
        if (uri == null) {
            return null;
        }
        String path = uri;
        int index = uri.indexOf('?');
        if (index != -1) {
            path = uri.substring(0, index);
        }
        return path;
    }

    /**
     * @return a map with request parameters information given an URI query.
     */
    public static Map<String, String> getRequestParameters(String uriQuery) {
        Map<String, String> parameters = null;
        if (uriQuery != null && uriQuery.length() > 0) {
            try {
                int index = uriQuery.indexOf("?");
                if (index != -1) {
                    uriQuery = uriQuery.substring(index + 1);
                }
                String[] items = uriQuery.split("&");
                if (items != null && items.length > 0) {
                    parameters = new LinkedHashMap<String, String>();
                    for (String item : items) {
                        String[] param = item.split("=");
                        if (param != null) {
                            if (param.length == 2) {
                                parameters.put(
                                        URLDecoder.decode(param[0], "UTF-8"),
                                        URLDecoder.decode(param[1], "UTF-8"));
                            } else if (param.length == 1) {
                                parameters.put(
                                        URLDecoder.decode(param[0], "UTF-8"),
                                        null);
                            }
                        }
                    }
                }
            } catch (UnsupportedEncodingException e) {
                log.error("Failed to get request parameters from uri", e);
            }
        }
        return parameters;
    }

    public static String addParametersToURIQuery(String uriString,
            Map<String, String> parameters) {
        if (uriString == null || parameters == null || parameters.isEmpty()) {
            return uriString;
        }
        String uriPath = getURIPath(uriString);
        Map<String, String> existingParams = getRequestParameters(uriString.substring(uriPath.length()));
        if (existingParams == null) {
            existingParams = new LinkedHashMap<String, String>();
        }
        existingParams.putAll(parameters);
        String res = null;
        if (!existingParams.isEmpty()) {
            String newQuery = getURIQuery(existingParams);
            res = uriPath + '?' + newQuery;
        } else {
            res = uriPath;
        }
        return res;
    }

    public static String quoteURIPathComponent(String s, boolean quoteSlash) {
        return quoteURIPathComponent(s, quoteSlash, true);
    }

    /**
     * Quotes a URI path component, with ability to quote "/" and "@"
     * characters or not depending on the URI path
     *
     * @since 5.6
     * @param the uri path to quote
     * @param quoteSlash true if "/" character should be quoted
     * @param quoteAt true if "@" character should be quoted
     */
    public static String quoteURIPathComponent(String s, boolean quoteSlash,
            boolean quoteAt) {
        if ("".equals(s)) {
            return s;
        }
        URI uri;
        try {
            // fake scheme so that a colon is not mistaken as a scheme
            uri = new URI("x", s, null);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Illegal characters in: " + s, e);
        }
        String r = uri.toASCIIString().substring(2);
        // replace reserved characters ;:$&+=?/[]@
        // FIXME: find a better way to do this...
        r = r.replace(";", "%3B");
        r = r.replace(":", "%3A");
        r = r.replace("$", "%24");
        r = r.replace("&", "%26");
        r = r.replace("+", "%2B");
        r = r.replace("=", "%3D");
        r = r.replace("?", "%3F");
        r = r.replace("[", "%5B");
        r = r.replace("]", "%5D");
        // replace single (') and double (") quotes
        // see https://jira.nuxeo.com/browse/NXP-11194
        r = r.replace("'", "%27");
        r = r.replace("\"", "%22");
        if (quoteAt) {
            r = r.replace("@", "%40");
        }
        if (quoteSlash) {
            r = r.replace("/", "%2F");
        }
        return r;
    }

    public static String unquoteURIPathComponent(String s) {
        if ("".equals(s)) {
            return s;
        }
        URI uri;
        try {
            uri = new URI("http://x/" + s);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Illegal characters in: " + s, e);
        }
        String path = uri.getPath();
        if (path.startsWith("/") && !s.startsWith("/")) {
            path = path.substring(1);
        }
        return path;
    }

}
<commit_msg>NXP-10974: cleanup code thanks to sonar<commit_after>/*
 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * 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.
 *
 * Contributors:
 *     <NAME>
 *     <NAME>
 */

package org.nuxeo.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Helper class to parse a URI or build one given parameters.
 *
 * @author <NAME>
 * @author <NAME>
 */
public final class URIUtils {

    private static final Log log = LogFactory.getLog(URIUtils.class);

    // This is an utility class.
    private URIUtils() {
    }

    /**
     * Creates an URI query given the request parameters.
     *
     * @return an URI query given the request parameters.
     */
    public static String getURIQuery(Map<String, String> parameters) {
        String query = null;
        if (parameters != null) {
            try {
                List<String> items = new ArrayList<String>();
                for (Map.Entry<String, String> paramInfo : parameters.entrySet()) {
                    String key = paramInfo.getKey();
                    String value = paramInfo.getValue();
                    if (key != null) {
                        if (value == null) {
                            value = "";
                        }
                        items.add(String.format("%s=%s",
                                URLEncoder.encode(key, "UTF-8"),
                                URLEncoder.encode(value, "UTF-8")));
                    }
                }
                query = org.apache.commons.lang.StringUtils.join(items, "&");
            } catch (UnsupportedEncodingException e) {
                log.error("Failed to get uri query", e);
            }
        }
        return query;
    }

    /**
     * Returns an URI path given the uri.
     */
    public static String getURIPath(String uri) {
        if (uri == null) {
            return null;
        }
        String path = uri;
        int index = uri.indexOf('?');
        if (index != -1) {
            path = uri.substring(0, index);
        }
        return path;
    }

    /**
     * @return a map with request parameters information given an URI query.
     */
    public static Map<String, String> getRequestParameters(String uriQuery) {
        Map<String, String> parameters = null;
        if (uriQuery != null && uriQuery.length() > 0) {
            try {
                String strippedQuery;
                int index = uriQuery.indexOf('?');
                if (index != -1) {
                    strippedQuery = uriQuery.substring(index + 1);
                } else {
                    strippedQuery = uriQuery;
                }
                String[] items = strippedQuery.split("&");
                if (items != null && items.length > 0) {
                    parameters = new LinkedHashMap<String, String>();
                    for (String item : items) {
                        String[] param = item.split("=");
                        if (param != null) {
                            if (param.length == 2) {
                                parameters.put(
                                        URLDecoder.decode(param[0], "UTF-8"),
                                        URLDecoder.decode(param[1], "UTF-8"));
                            } else if (param.length == 1) {
                                parameters.put(
                                        URLDecoder.decode(param[0], "UTF-8"),
                                        null);
                            }
                        }
                    }
                }
            } catch (UnsupportedEncodingException e) {
                log.error("Failed to get request parameters from uri", e);
            }
        }
        return parameters;
    }

    public static String addParametersToURIQuery(String uriString,
            Map<String, String> parameters) {
        if (uriString == null || parameters == null || parameters.isEmpty()) {
            return uriString;
        }
        String uriPath = getURIPath(uriString);
        Map<String, String> existingParams = getRequestParameters(uriString.substring(uriPath.length()));
        if (existingParams == null) {
            existingParams = new LinkedHashMap<String, String>();
        }
        existingParams.putAll(parameters);
        String res = null;
        if (!existingParams.isEmpty()) {
            String newQuery = getURIQuery(existingParams);
            res = uriPath + '?' + newQuery;
        } else {
            res = uriPath;
        }
        return res;
    }

    public static String quoteURIPathComponent(String s, boolean quoteSlash) {
        return quoteURIPathComponent(s, quoteSlash, true);
    }

    /**
     * Quotes a URI path component, with ability to quote "/" and "@"
     * characters or not depending on the URI path
     *
     * @since 5.6
     * @param the uri path to quote
     * @param quoteSlash true if "/" character should be quoted
     * @param quoteAt true if "@" character should be quoted
     */
    public static String quoteURIPathComponent(String s, boolean quoteSlash,
            boolean quoteAt) {
        if ("".equals(s)) {
            return s;
        }
        URI uri;
        try {
            // fake scheme so that a colon is not mistaken as a scheme
            uri = new URI("x", s, null);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Illegal characters in: " + s, e);
        }
        String r = uri.toASCIIString().substring(2);
        // replace reserved characters ;:$&+=?/[]@
        // FIXME: find a better way to do this...
        r = r.replace(";", "%3B");
        r = r.replace(":", "%3A");
        r = r.replace("$", "%24");
        r = r.replace("&", "%26");
        r = r.replace("+", "%2B");
        r = r.replace("=", "%3D");
        r = r.replace("?", "%3F");
        r = r.replace("[", "%5B");
        r = r.replace("]", "%5D");
        // replace single (') and double (") quotes
        // see https://jira.nuxeo.com/browse/NXP-11194
        r = r.replace("'", "%27");
        r = r.replace("\"", "%22");
        if (quoteAt) {
            r = r.replace("@", "%40");
        }
        if (quoteSlash) {
            r = r.replace("/", "%2F");
        }
        return r;
    }

    public static String unquoteURIPathComponent(String s) {
        if ("".equals(s)) {
            return s;
        }
        URI uri;
        try {
            uri = new URI("http://x/" + s);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Illegal characters in: " + s, e);
        }
        String path = uri.getPath();
        if (path.startsWith("/") && !s.startsWith("/")) {
            path = path.substring(1);
        }
        return path;
    }

}

{'fraction_non_alphanumeric': 0.12183177817483952, 'fraction_numerical': 0.0076103500761035, 'mean_word_length': 1.5898886032562125, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '129733', 'n_tokens_mistral': 4070, 'n_tokens_neox': 3958, 'n_words': 1166}