private List < NameValuePair > sign ( URI uri , final List < NameValuePair > params ) { Preconditions . checkNotNull ( m_secret ) ; final List < NameValuePair > sortedParams = Lists . newArrayList ( params ) ; Collections . sort ( sortedParams , new Comparator < NameValuePair > ( ) { @ Override public int compare ( NameValuePair left , NameValuePair right ) { return left . getName ( ) . compareTo ( right . getName ( ) ) ; } } ) ; final StringBuilder paramSb = new StringBuilder ( ) ; String separator = "" ; for ( NameValuePair param : sortedParams ) { paramSb . append ( separator ) . append ( param . getName ( ) ) ; if ( param . getValue ( ) != null ) { paramSb . append ( "=" ) . append ( param . getValue ( ) ) ; } separator = "&" ; } final StringBuilder baseSb = new StringBuilder ( ) ; baseSb . append ( m_method ) . append ( ' ' ) ; baseSb . append ( uri . getHost ( ) ) . append ( ' ' ) ; baseSb . append ( uri . getPath ( ) . isEmpty ( ) ? ' ' : uri . getPath ( ) ) . append ( ' ' ) ; baseSb . append ( paramSb . toString ( ) ) ; final Mac hmac ; final Key key ; try { hmac = Mac . getInstance ( m_signatureMethod ) ; key = new SecretKeySpec ( m_secret . getBytes ( Charsets . UTF_8 ) , m_signatureMethod ) ; hmac . init ( key ) ; } catch ( NoSuchAlgorithmException e ) { // should never happen rateLimitedLogError ( m_logger , "Fail to get HMAC instance %s" , Throwables . getStackTraceAsString ( e ) ) ; return null ; } catch ( InvalidKeyException e ) { rateLimitedLogError ( m_logger , "Fail to sign the message %s" , Throwables . getStackTraceAsString ( e ) ) ; return null ; } sortedParams . add ( new BasicNameValuePair ( m_signatureName , NVPairsDecoder . percentEncode ( Encoder . base64Encode ( hmac . doFinal ( baseSb . toString ( ) . getBytes ( Charsets . UTF_8 ) ) ) ) ) ) ; return sortedParams ; }
Calculate the signature of the request using the specified secret key .