Example usage for java.net URLDecoder decode

List of usage examples for java.net URLDecoder decode

Introduction

In this page you can find the example usage for java.net URLDecoder decode.

Prototype

public static String decode(String s, Charset charset) 

Source Link

Document

Decodes an application/x-www-form-urlencoded string using a specific java.nio.charset.Charset Charset .

Usage

From source file:Main.java

/**
 * Splits the query parameters into key value pairs.
 * See: http://stackoverflow.com/a/13592567/534471.
 *///  ww w .  j a v  a2s .com
private static Map<String, List<String>> splitQuery(Uri uri) throws UnsupportedEncodingException {
    final Map<String, List<String>> query_pairs = new LinkedHashMap<>();
    String query = uri.getQuery();
    if (query == null)
        return query_pairs;

    final String[] pairs = query.split("&");
    for (String pair : pairs) {
        final int idx = pair.indexOf("=");
        final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
        if (!query_pairs.containsKey(key)) {
            query_pairs.put(key, new LinkedList<String>());
        }
        final String value = idx > 0 && pair.length() > idx + 1
                ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8")
                : "";
        query_pairs.get(key).add(value);
    }
    return query_pairs;
}

From source file:com.hurence.logisland.utils.HttpUtils.java

public static List<String> getQueryValues(String urlQuery) {
    try {/*from  ww w  .  j  a v a  2s.c  o  m*/
        List<String> values = new ArrayList<>();
        if (urlQuery == null) {
            return values;
        }

        for (String param : urlQuery.split("&")) {
            QueryParam queryParam = new QueryParam();
            String[] pair = param.split("=");
            String value = "";
            if (pair.length > 1) {
                value = URLDecoder.decode(pair[1], "UTF-8");
            }
            if (value != null && !value.equals("null")) {
                values.add(value);
            }
        }
        return values;
    } catch (UnsupportedEncodingException ex) {
        throw new AssertionError(ex);
    }
}

From source file:com.temenos.interaction.core.rim.HTTPHypermediaRIM.java

@SuppressWarnings("static-access")
private void decodeQueryParams(MultivaluedMap<String, String> queryParameters) {

    try {//  w w  w .  j a  v  a  2s. co  m

        if (queryParameters == null) {
            return;
        }

        URLDecoder ud = new URLDecoder();

        for (String key : queryParameters.keySet()) {
            List<String> values = queryParameters.get(key);
            if (values != null) {
                List<String> newValues = new ArrayList<String>();
                for (String value : values) {
                    if (value != null)
                        newValues.add(ud.decode(value, "UTF-8"));
                }
                queryParameters.put(key, newValues);
            }
        }

    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static List<Class<?>> getClassesForPackage(final String iPackageName, final ClassLoader iClassLoader)
        throws ClassNotFoundException {
    // This will hold a list of directories matching the pckgname.
    // There may be more than one if a package is split over multiple jars/paths
    List<Class<?>> classes = new ArrayList<Class<?>>();
    ArrayList<File> directories = new ArrayList<File>();
    try {//from  w ww. ja v  a  2  s  . com
        // Ask for all resources for the path
        final String packageUrl = iPackageName.replace('.', '/');
        Enumeration<URL> resources = iClassLoader.getResources(packageUrl);
        if (!resources.hasMoreElements()) {
            resources = iClassLoader.getResources(packageUrl + CLASS_EXTENSION);
            if (resources.hasMoreElements()) {
                throw new IllegalArgumentException(
                        iPackageName + " does not appear to be a valid package but a class");
            }
        } else {
            while (resources.hasMoreElements()) {
                URL res = resources.nextElement();
                if (res.getProtocol().equalsIgnoreCase("jar")) {
                    JarURLConnection conn = (JarURLConnection) res.openConnection();
                    JarFile jar = conn.getJarFile();
                    for (JarEntry e : Collections.list(jar.entries())) {

                        if (e.getName().startsWith(iPackageName.replace('.', '/'))
                                && e.getName().endsWith(CLASS_EXTENSION) && !e.getName().contains("$")) {
                            String className = e.getName().replace("/", ".").substring(0,
                                    e.getName().length() - 6);
                            classes.add(Class.forName(className));
                        }
                    }
                } else
                    directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8")));
            }
        }
    } catch (NullPointerException x) {
        throw new ClassNotFoundException(
                iPackageName + " does not appear to be " + "a valid package (Null pointer exception)");
    } catch (UnsupportedEncodingException encex) {
        throw new ClassNotFoundException(
                iPackageName + " does not appear to be " + "a valid package (Unsupported encoding)");
    } catch (IOException ioex) {
        throw new ClassNotFoundException(
                "IOException was thrown when trying " + "to get all resources for " + iPackageName);
    }

    // For every directory identified capture all the .class files
    for (File directory : directories) {
        if (directory.exists()) {
            // Get the list of the files contained in the package
            File[] files = directory.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    classes.addAll(findClasses(file, iPackageName));
                } else {
                    String className;
                    if (file.getName().endsWith(CLASS_EXTENSION)) {
                        className = file.getName().substring(0,
                                file.getName().length() - CLASS_EXTENSION.length());
                        classes.add(Class.forName(iPackageName + '.' + className));
                    }
                }
            }
        } else {
            throw new ClassNotFoundException(
                    iPackageName + " (" + directory.getPath() + ") does not appear to be a valid package");
        }
    }
    return classes;
}

From source file:de.highbyte_le.weberknecht.request.RequestWrapper.java

/**
 * read parameters from query string (HTTP GET)
 *//*  w  ww . j a  v a  2s .  c  o  m*/
public static RequestWrapper createFromQueryString(HttpServletRequest request, String urlEncoding)
        throws UnsupportedEncodingException {
    RequestWrapper wrapper = new RequestWrapper();

    String query = request.getQueryString();
    if (query != null) { //use URL parameters from query string
        int startIndex = 0;
        int i;
        do {
            i = query.indexOf("&", startIndex);
            String nameValue;
            if (i != -1) {
                nameValue = query.substring(startIndex, i);
            } else {
                nameValue = query.substring(startIndex, query.length());
            }

            int j = nameValue.indexOf("=");
            if (j != -1) {
                String name = URLDecoder.decode(nameValue.substring(0, j), urlEncoding);
                String value = URLDecoder.decode(nameValue.substring(j + 1, nameValue.length()), urlEncoding);
                wrapper.addParameter(name, value);
                logger.debug("createFromQueryString(HttpServletRequest, String) - found parameter: " + name
                        + "=" + value);
            }

            startIndex = i + 1;
        } while (i != -1);
    }

    return wrapper;
}

From source file:dk.netarkivet.common.utils.cdx.CDXRecord.java

/**
 * Helper method to avoid exception in URL decoding.
 * @param s The string to unescape.// www  .  j a  v a2  s . com
 * @return the unescaped string.
 */
private static String unescape(String s) {
    try {
        return URLDecoder.decode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new ArgumentNotValid("UTF-8 is an unknown encoding. This should never happen!");
    }
}

From source file:com.bjwg.back.util.EncodeUtils.java

public static String urlDecode(String part) {

    try {/*from   w ww  . ja  v a  2 s .c om*/
        return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
    } catch (UnsupportedEncodingException e) {
        return "";
    }
}

From source file:net.ion.radon.impl.let.webdav.VFSResource.java

public static VFSPath decodeVFSPath(String encodedUri) throws VFSPathParseException {
    int qindex = encodedUri.indexOf("?");
    if (qindex != -1)
        encodedUri = encodedUri.substring(0, qindex);
    try {//from  ww w  .j a v  a2  s .co m
        encodedUri = URLDecoder.decode(encodedUri, "UTF-8");
        return VFSUtils.parseVFSPath(encodedUri);
    } catch (IllegalArgumentException ix) {
        throw new VFSPathParseException(ix);
    } catch (UnsupportedEncodingException ux) {
        throw new RuntimeException("Expected UTF-8 encoding not found in Java runtime");
    }
}

From source file:com.ecbeta.common.util.JSONUtils.java

public static JSONObject toJSON(HttpServletRequest request) {
    MapJSONBean json = new MapJSONBean();
    for (Object s : request.getParameterMap().keySet()) {
        for (String value : request.getParameterValues(s.toString())) {
            json.add(s.toString(), value);
        }//  w w w . java  2 s  .co  m
    }

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line;
        String data = "";
        while ((line = br.readLine()) != null) {
            data += line;
        }
        data = URLDecoder.decode(data, "UTF-8");
        try {
            JSONObject oo = JSONObject.fromObject(data);
            return oo;
        } catch (Exception ex) {
            Map<String, List<String>> params = parseParameters(data);
            for (Map.Entry<String, List<String>> entry : params.entrySet()) {
                for (String value : entry.getValue()) {
                    json.add(entry.getKey(), value);
                }
            }
        }

    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    JSONObject job = json.toJson();
    logger.info("~~~~~~~~~AutoCreating~~~~~~~~~");
    logger.info(json == null ? "" : job.toString(2));
    logger.info("~~~~~~~~~End~~~~~~~~~");
    return job;
}

From source file:com.adaptris.core.services.metadata.UrlDecodeMetadataService.java

@Override
public String reformat(String s, String msgCharset) throws Exception {
    return URLDecoder.decode(s, defaultIfEmpty(msgCharset, "UTF-8"));
}