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

org.scribe.utils.OAuthEncoder Maven / Gradle / Ivy

The newest version!
package org.scribe.utils;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import org.scribe.exceptions.*;

/**
 * @author: Pablo Fernandez
 */
public class OAuthEncoder
{
  private static String CHARSET = "UTF-8";
  private static final Map ENCODING_RULES;

  static
  {
    Map rules = new HashMap();
    rules.put("*", "%2A");
    rules.put("+", "%20");
    rules.put("%7E", "~");
    ENCODING_RULES = Collections.unmodifiableMap(rules);
  }

  private OAuthEncoder(){}

  public static String encode(String plain)
  {
    Preconditions.checkNotNull(plain, "Cannot encode null object");
    String encoded = "";
    try
    {
      encoded = URLEncoder.encode(plain, CHARSET);
    }
    catch (UnsupportedEncodingException uee)
    {
      throw new OAuthException("Charset not found while encoding string: " + CHARSET, uee);
    }
    for(Map.Entry rule : ENCODING_RULES.entrySet())
    {
      encoded = applyRule(encoded, rule.getKey(), rule.getValue());
    }
    return encoded;
  }

  private static String applyRule(String encoded, String toReplace, String replacement)
  {
    return encoded.replaceAll(Pattern.quote(toReplace), replacement);
  }

  public static String decode(String encoded)
  {
    Preconditions.checkNotNull(encoded, "Cannot decode null object");
    try
    {
      return URLDecoder.decode(encoded, CHARSET);
    }
    catch(UnsupportedEncodingException uee)
    {
      throw new OAuthException("Charset not found while decoding string: " + CHARSET, uee);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy