All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.echobox.api.tiktok.util.URLUtils Maven / Gradle / Ivy

/**
 * Copyright (c) 2010-2017 Mark Allen, Norbert Bartels.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
//Source - https://restfb.com/

package com.echobox.api.tiktok.util;

import static java.lang.String.format;
import static java.net.URLDecoder.decode;
import static java.net.URLEncoder.encode;
import static java.util.Collections.emptyMap;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mark Allen
 */
public final class URLUtils {
  /**
   * Default charset to use for encoding/decoding strings.
   */
  public static final String ENCODING_CHARSET = "UTF-8";

  /**
   * Prevents instantiation.
   */
  private URLUtils() {
    // Prevents instantiation
  }

  /**
   * URL-encodes a string.
   * 

* Assumes {@code string} is in {@value #ENCODING_CHARSET} format. * * @param string The string to URL-encode. * * @return The URL-encoded version of the input string, or {@code null} if {@code string} is * {@code null}. * @throws IllegalStateException If unable to URL-encode because the JVM doesn't support * {@value #ENCODING_CHARSET}. */ public static String urlEncode(String string) { if (string == null) { return null; } try { return encode(string, ENCODING_CHARSET); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Platform doesn't support " + ENCODING_CHARSET, e); } } /** * URL-decodes a string. *

* Assumes {@code string} is in {@value #ENCODING_CHARSET} format. * * @param string The string to URL-decode. * * @return The URL-decoded version of the input string, or {@code null} if {@code string} is * {@code null}. * @throws IllegalStateException If unable to URL-decode because the JVM doesn't support * {@value #ENCODING_CHARSET}. */ public static String urlDecode(String string) { if (string == null) { return null; } try { return decode(string, ENCODING_CHARSET); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Platform doesn't support " + ENCODING_CHARSET, e); } } /** * For the given {@code queryString}, extract a mapping of query string parameter names to values. *

* Example of a {@code queryString} is {@code accessToken=123&expires=345}. * * @param queryString The URL query string from which parameters are extracted. * @return A mapping of query string parameter names to values. If {@code queryString} is * {@code null}, an empty {@code Map} is returned. * @throws IllegalStateException If unable to URL-decode because the JVM doesn't support * {@value #ENCODING_CHARSET}. */ public static Map> extractParametersFromQueryString(String queryString) { if (queryString == null) { return emptyMap(); } // If there is no ? character at the front of the string, append it. return extractParametersFromUrl( format("ebx-tiktok-sdk://url%s", queryString.startsWith("?") ? queryString : "?" + queryString)); } /** * For the given {@code url}, extract a mapping of query string parameter names to values. *

* Adapted from an implementation by BalusC and dfrankow, available at * http://stackoverflow.com/questions/1667278/parsing-query-strings-in-java. * * @param url The URL from which parameters are extracted. * @return A mapping of query string parameter names to values. If {@code url} is {@code null}, * an empty {@code Map} is returned. * @throws IllegalStateException If unable to URL-decode because the JVM doesn't support * {@value #ENCODING_CHARSET}. */ public static Map> extractParametersFromUrl(String url) { if (url == null) { return emptyMap(); } Map> parameters = new HashMap<>(); String[] urlParts = url.split("\\?"); if (urlParts.length > 1) { String query = urlParts[1]; for (String param : query.split("&")) { String[] pair = param.split("="); String key = urlDecode(pair[0]); String value = ""; if (pair.length > 1) { value = urlDecode(pair[1]); } List values = parameters .computeIfAbsent(key, k -> new ArrayList<>()); values.add(value); } } return parameters; } /** * Replace of add a query parameter. * @param url the URL. * @param key the key to add. * @param value the value to add. * * @return the URL with the updated query parameter. */ public static String replaceOrAddQueryParameter(String url, String key, String value) { String[] urlParts = url.split("\\?"); String qParameter = key + "=" + value; if (urlParts.length == 2) { Map> paramMap = extractParametersFromQueryString(urlParts[1]); if (paramMap.containsKey(key)) { String queryValue = paramMap.get(key).get(0); return url.replace(key + "=" + queryValue, qParameter); } else { return url + "&" + qParameter; } } else { return url + "?" + qParameter; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy