Class OAuthUtils


  • public final class OAuthUtils
    extends java.lang.Object
    Common OAuth Utils class.

    Some methods based on the Utils class from OAuth V1.0a library available at: http://oauth.googlecode.com/svn/code/java/core/

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String AUTH_SCHEME  
      static java.lang.String MULTIPART  
    • Constructor Summary

      Constructors 
      Constructor Description
      OAuthUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String[] decodeClientAuthenticationHeader​(java.lang.String authenticationHeader)
      Decodes the Basic Authentication header into a username and password
      static java.util.Map<java.lang.String,​java.lang.Object> decodeForm​(java.lang.String form)
      Parse a form-urlencoded document.
      static java.util.Map<java.lang.String,​java.lang.String> decodeOAuthHeader​(java.lang.String header)  
      static java.lang.String decodePercent​(java.lang.String s)  
      static java.util.Set<java.lang.String> decodeScopes​(java.lang.String s)  
      static java.lang.String encodeAuthorizationBearerHeader​(java.util.Map<java.lang.String,​java.lang.Object> entries)
      Construct an Authorization Bearer header
      static java.lang.String encodeOAuthHeader​(java.util.Map<java.lang.String,​java.lang.Object> entries)
      Construct a WWW-Authenticate header
      static java.lang.String encodeScopes​(java.util.Set<java.lang.String> s)  
      static java.lang.String format​(java.util.Collection<? extends java.util.Map.Entry<java.lang.String,​java.lang.Object>> parameters, java.lang.String encoding)
      Translates parameters into application/x-www-form-urlencoded String
      static java.lang.String getAuthHeaderField​(java.lang.String authHeader)  
      static java.lang.String getAuthzMethod​(java.lang.String header)  
      static OAuthProblemException handleBadContentTypeException​(java.lang.String expectedContentType)  
      static OAuthProblemException handleMissingParameters​(java.util.Set<java.lang.String> missingParams)
      Creates OAuthProblemException that contains set of missing oauth parameters
      static OAuthProblemException handleNotAllowedParametersOAuthException​(java.util.List<java.lang.String> notAllowedParams)  
      static OAuthProblemException handleOAuthProblemException​(java.lang.String message)
      Creates invalid_request exception with given message
      static boolean hasContentType​(java.lang.String requestContentType, java.lang.String requiredContentType)  
      static boolean hasEmptyValues​(java.lang.String[] array)  
      static <T> T instantiateClass​(java.lang.Class<T> clazz)  
      static <T> T instantiateClassWithParameters​(java.lang.Class<T> clazz, java.lang.Class<?>[] paramsTypes, java.lang.Object[] paramValues)  
      static boolean isEmpty​(java.lang.String value)  
      static boolean isFormEncoded​(java.lang.String contentType)
      Return true if the given Content-Type header means FORM_ENCODED.
      static boolean isMultipart​(HttpServletRequest request)  
      static java.lang.String percentEncode​(java.lang.Iterable values)
      Construct a &-separated list of the given values, percentEncoded.
      static java.lang.String percentEncode​(java.lang.String s)  
      static java.lang.String saveStreamAsString​(java.io.InputStream is)
      Read data from Input Stream and save it as a String.
      static java.lang.String toString​(java.io.InputStream is, java.lang.String defaultCharset)
      Get the entity content as a String, using the provided default character set if none is found in the entity.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • OAuthUtils

        public OAuthUtils()
    • Method Detail

      • format

        public static java.lang.String format​(java.util.Collection<? extends java.util.Map.Entry<java.lang.String,​java.lang.Object>> parameters,
                                              java.lang.String encoding)
        Translates parameters into application/x-www-form-urlencoded String
        Parameters:
        parameters - parameters to encode
        encoding - The name of a supported character encoding.
        Returns:
        Translated string
      • saveStreamAsString

        public static java.lang.String saveStreamAsString​(java.io.InputStream is)
                                                   throws java.io.IOException
        Read data from Input Stream and save it as a String.
        Parameters:
        is - InputStream to be read
        Returns:
        String that was read from the stream
        Throws:
        java.io.IOException
      • toString

        public static java.lang.String toString​(java.io.InputStream is,
                                                java.lang.String defaultCharset)
                                         throws java.io.IOException
        Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "UTF-8" is used.
        Parameters:
        is - input stream to be saved as string
        defaultCharset - character set to be applied if none found in the entity
        Returns:
        the entity content as a String
        Throws:
        java.lang.IllegalArgumentException - if entity is null or if content length > Integer.MAX_VALUE
        java.io.IOException - if an error occurs reading the input stream
      • handleOAuthProblemException

        public static OAuthProblemException handleOAuthProblemException​(java.lang.String message)
        Creates invalid_request exception with given message
        Parameters:
        message - error message
        Returns:
        OAuthException
      • handleMissingParameters

        public static OAuthProblemException handleMissingParameters​(java.util.Set<java.lang.String> missingParams)
        Creates OAuthProblemException that contains set of missing oauth parameters
        Parameters:
        missingParams - missing oauth parameters
        Returns:
        OAuthProblemException with user friendly message about missing oauth parameters
      • handleBadContentTypeException

        public static OAuthProblemException handleBadContentTypeException​(java.lang.String expectedContentType)
      • handleNotAllowedParametersOAuthException

        public static OAuthProblemException handleNotAllowedParametersOAuthException​(java.util.List<java.lang.String> notAllowedParams)
      • decodeForm

        public static java.util.Map<java.lang.String,​java.lang.Object> decodeForm​(java.lang.String form)
        Parse a form-urlencoded document.
      • isFormEncoded

        public static boolean isFormEncoded​(java.lang.String contentType)
        Return true if the given Content-Type header means FORM_ENCODED.
      • decodePercent

        public static java.lang.String decodePercent​(java.lang.String s)
      • percentEncode

        public static java.lang.String percentEncode​(java.lang.Iterable values)
        Construct a &-separated list of the given values, percentEncoded.
      • percentEncode

        public static java.lang.String percentEncode​(java.lang.String s)
      • instantiateClassWithParameters

        public static <T> T instantiateClassWithParameters​(java.lang.Class<T> clazz,
                                                           java.lang.Class<?>[] paramsTypes,
                                                           java.lang.Object[] paramValues)
                                                    throws OAuthSystemException
        Throws:
        OAuthSystemException
      • getAuthHeaderField

        public static java.lang.String getAuthHeaderField​(java.lang.String authHeader)
      • decodeOAuthHeader

        public static java.util.Map<java.lang.String,​java.lang.String> decodeOAuthHeader​(java.lang.String header)
      • decodeClientAuthenticationHeader

        public static java.lang.String[] decodeClientAuthenticationHeader​(java.lang.String authenticationHeader)
        Decodes the Basic Authentication header into a username and password
        Parameters:
        authenticationHeader - String containing the encoded header value. e.g. "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
        Returns:
        a String if the header could be decoded into a non null username and password or null.
      • encodeOAuthHeader

        public static java.lang.String encodeOAuthHeader​(java.util.Map<java.lang.String,​java.lang.Object> entries)
        Construct a WWW-Authenticate header
      • encodeAuthorizationBearerHeader

        public static java.lang.String encodeAuthorizationBearerHeader​(java.util.Map<java.lang.String,​java.lang.Object> entries)
        Construct an Authorization Bearer header
      • isEmpty

        public static boolean isEmpty​(java.lang.String value)
      • hasEmptyValues

        public static boolean hasEmptyValues​(java.lang.String[] array)
      • getAuthzMethod

        public static java.lang.String getAuthzMethod​(java.lang.String header)
      • decodeScopes

        public static java.util.Set<java.lang.String> decodeScopes​(java.lang.String s)
      • encodeScopes

        public static java.lang.String encodeScopes​(java.util.Set<java.lang.String> s)
      • hasContentType

        public static boolean hasContentType​(java.lang.String requestContentType,
                                             java.lang.String requiredContentType)