/** * Creates a long from a {@link String}; useful e.g. when creating a number * from a pass-phrase. * * @param aString The {@link String} to be converted to a long value. * * @return The long value from the {@link String} */ public static long toLong( String aString ) { byte[] theBytes; try { theBytes = aString.getBytes( Encoding.UTF_8.getCode() ); } catch ( UnsupportedEncodingException e ) { theBytes = aString.getBytes(); } long theValue; // if ( theBytes.length < 8 ) { theValue = 0; for ( int i = 0; i < theBytes.length; i++ ) { theValue = (theValue << 8) + (theBytes[i] & 0xff); } // } // else { // ByteBuffer theBuffer = ByteBuffer.wrap( theBytes ); // theValue = theBuffer.getLong(); // } return theValue; }
/** * {@inheritDoc} */ @Override public String toHttpAuthorization() { String theCredentials = getIdentity() + DELIMITER_CREDENTIALS + getSecret(); try { theCredentials = Base64.getEncoder().encodeToString( theCredentials.getBytes( Encoding.UTF_8.getCode() ) ); } catch ( UnsupportedEncodingException e ) { theCredentials = Base64.getEncoder().encodeToString( theCredentials.getBytes() ); } theCredentials = AuthType.BASIC.getName() + DELIMITER_BASIC_AUTH + theCredentials; return theCredentials; }
/** * {@inheritDoc} */ @Override public void fromHttpAuthorization( String aHttpAuthorization ) { if ( aHttpAuthorization == null ) { throw new IllegalArgumentException( "The provided HTTP authorization value does not conform to the format expected for Basic-Authentication." ); } int theMarker = aHttpAuthorization.indexOf( DELIMITER_BASIC_AUTH ); if ( theMarker == -1 || !aHttpAuthorization.substring( 0, theMarker ).equals( AuthType.BASIC.getName() ) ) { throw new IllegalArgumentException( "The provided HTTP authorization value does not conform to the format expected for Basic-Authentication." ); } byte[] theCredentialChars = Base64.getDecoder().decode( aHttpAuthorization.substring( theMarker + 1 ) ); String theCredentialsText; try { theCredentialsText = new String( theCredentialChars, Encoding.UTF_8.getCode() ); } catch ( UnsupportedEncodingException e ) { theCredentialsText = new String( theCredentialChars ); } theMarker = theCredentialsText.indexOf( DELIMITER_CREDENTIALS ); _identity = theCredentialsText.substring( 0, theMarker ); _secret = theCredentialsText.substring( theMarker + 1 ); }
/** * Determines the parent JAR file's URL for your running application (not * including the "!" which is required to address a file within that JAR). * * @return The parrent's JAR file URL or null if the application does not * seem to reside in a JAR. */ public static URL toParentJarUrl() { URL theUrl = FileUtility.class.getProtectionDomain().getCodeSource().getLocation(); if ( !theUrl.getProtocol().equals( Scheme.JAR.getName() ) ) return null; try { String theJarPath = URLDecoder.decode( theUrl.getFile(), Encoding.UTF_8.getCode() ); theJarPath = Scheme.JAR.toUrl( theJarPath ); return new URL( theJarPath ); } catch ( UnsupportedEncodingException | MalformedURLException e ) { return null; } }
/** * {@inheritDoc} */ @Override public int toEncrypted( byte[] aInput, int aInputOffset, int aInputLength, byte[] aOutput, int aOutputOffset ) throws ShortBufferException, EncryptionException { byte[] theInputHex = Arrays.copyOfRange( aInput, aInputOffset, aInputOffset + aInputLength ); String theInputText; try { theInputText = new String( theInputHex, Encoding.UTF_8.getCode() ); } catch ( UnsupportedEncodingException e ) { theInputText = new String( theInputHex ); } String theOutputText = toEncrypted( theInputText ); byte[] theOutputHex; try { theOutputHex = theOutputText.getBytes( Encoding.UTF_8.getCode() ); } catch ( UnsupportedEncodingException e ) { theOutputHex = theOutputText.getBytes(); } if ( aOutput.length < aOutputOffset + theOutputHex.length ) { throw new ShortBufferException( "The encrypted data is of length <" + theOutputHex.length + "> though your buffer with length <" + aOutput.length + "> does not provide enugh elements after offset <" + aOutputOffset + ">." ); } for ( int i = 0; i < theOutputHex.length; i++ ) { aOutput[aOutputOffset + i] = theOutputHex[i]; } return theOutputHex.length; }
/** * Generates a md5 hash of the given text. * * @param aText The text to be md5 hashed. * * @return The md5 hashed text. */ public static String toMd5Hash( String aText ) { try { byte[] theBytes = aText.getBytes(); MessageDigest theMessageDigest = MessageDigest.getInstance( Encoding.MD5.getCode() ); theMessageDigest.update( theBytes, 0, theBytes.length ); String theHash = new BigInteger( 1, theMessageDigest.digest() ).toString( 32 ); return theHash; // @formatter:off /* byte[] theBytes = aText.getBytes( "UTF-8" ); MessageDigest theMessageDigest = MessageDigest.getInstance( "MD5" ); byte[] theDigest = theMessageDigest.digest( theBytes ); return Base64.encodeToString( theDigest ); */ // @formatter:on } catch ( NoSuchAlgorithmException e ) { throw new HiddenException( "Unexpected NoSuchAlgorithmException.", e ); } }
/** * {@inheritDoc} */ @Override public int toDecrypted( byte[] aInput, int aInputOffset, int aInputLength, byte[] aOutput, int aOutputOffset ) throws ShortBufferException, DecryptionException { byte[] theInputHex = Arrays.copyOfRange( aInput, aInputOffset, aInputOffset + aInputLength ); String theInputText; try { theInputText = new String( theInputHex, Encoding.UTF_8.getCode() ); } catch ( UnsupportedEncodingException e ) { theInputText = new String( theInputHex ); } String theOutputText; theOutputText = toDecrypted( theInputText ); byte[] theOutputHex; try { theOutputHex = theOutputText.getBytes( Encoding.UTF_8.getCode() ); } catch ( UnsupportedEncodingException e ) { theOutputHex = theOutputText.getBytes(); } if ( aOutput.length < aOutputOffset + theOutputHex.length ) { throw new ShortBufferException( "The decrypted data is of length <" + theOutputHex.length + "> though your buffer with length <" + aOutput.length + "> does not provide enugh elements after offset <" + aOutputOffset + ">." ); } for ( int i = 0; i < theOutputHex.length; i++ ) { aOutput[aOutputOffset + i] = theOutputHex[i]; } return theOutputHex.length; }
List<String> theList = new ArrayList<String>(); try { String theJarPath = URLDecoder.decode( aJarUrl.getFile(), Encoding.UTF_8.getCode() ); String eJarFile; int i = Scheme.JAR.firstMarkerIndex( theJarPath );
/** * {@inheritDoc} */ @Override default String toHttpCookie() { // Cookie value URL encoding --> String theValue = null; try { theValue = URLEncoder.encode( getValue(), Encoding.UTF_8.getCode() ); } catch ( UnsupportedEncodingException ignore ) { theValue = getValue(); } // <-- Cookie value URL encoding return getKey() + Delimiter.COOKIE_TUPEL.getChar() + theValue; } }
/** * Produces an HTTP Form-Fields body {@link String} form the herein contain * HTTP query fields. * * @return The HTTP Form-Fields {@link String} representation of this * {@link FormFields} instance or null if this instance is empty. */ default String toBodyFormFields() { if ( isEmpty() ) return null; String theQueryString = ""; List<String> eValues; for ( String eKey : keySet() ) { eValues = get( eKey ); // TODO: URL-Encode the value! for ( String eValue : eValues ) { try { eValue = URLEncoder.encode( eValue, Encoding.UTF_8.getCode() ); } catch ( UnsupportedEncodingException ignore ) { /* unencoded */ } if ( theQueryString.length() != 0 && theQueryString.charAt( theQueryString.length() - 1 ) != Delimiter.URL_QUERY.getChar() && theQueryString.charAt( theQueryString.length() - 1 ) != Delimiter.WEB_FIELD.getChar() ) { theQueryString += "" + Delimiter.WEB_FIELD.getChar(); } theQueryString += eKey + Delimiter.PROPERTY.getChar() + eValue; } } return theQueryString; }
/** * Processes the HTTP Form-Fields body {@link String} and retrieves the * therein defined Form-Fields and adds them to the {@link FormFields} * instance. * * @param aHttpFormFields The HTTP Form-Fields {@link String} from which to * extract the form being added to the {@link FormFields} instance. */ default void fromBodyFormFields( String aHttpFormFields ) { if ( aHttpFormFields != null ) { String[] theFields = aHttpFormFields.split( "\\" + Delimiter.WEB_FIELD.getChar() ); String[] eFieldPair; for ( String eField : theFields ) { eFieldPair = eField.split( "" + Delimiter.PROPERTY.getChar() ); // TODO: URL-Decode the value! if ( eFieldPair.length == 1 ) { addTo( eFieldPair[0], "" ); } else { try { addTo( eFieldPair[0], URLDecoder.decode( eFieldPair[1], Encoding.UTF_8.getCode() ) ); } catch ( UnsupportedEncodingException e ) { addTo( eFieldPair[0], eFieldPair[1] ); } } } } }
/** * {@inheritDoc} */ @Override public String getHttpBody() { if ( _httpBody == null ) { synchronized ( this ) { if ( _httpBody == null ) { try { ContentType theContentType = getHeaderFields().getContentType(); String theEncoding = theContentType != null && theContentType.getCharsetParametrer() != null ? theContentType.getCharsetParametrer() : Encoding.UTF_8.getCode(); _httpBody = new InputStreamStringBuilderImpl().toString( _httpInputStream, theEncoding ); } catch ( IOException e ) { _httpBody = e.getMessage(); } } } } return _httpBody; }
/** * {@inheritDoc} */ @Override public String getHttpBody() { if ( _httpBody == null ) { synchronized ( this ) { if ( _httpBody == null ) { try { ContentType theContentType = getHeaderFields().getContentType(); String theEncoding = theContentType != null && theContentType.getCharsetParametrer() != null ? theContentType.getCharsetParametrer() : Encoding.UTF_8.getCode(); _httpBody = new InputStreamStringBuilderImpl().toString( _httpInputStream, theEncoding ); } catch ( IOException e ) { _httpBody = e.getMessage(); } } } } return _httpBody; }
/** * {@inheritDoc} */ @Override default void fromHttpCookie( String aHttpCookie ) { Property theProperty = new PropertyImpl( aHttpCookie ); setKey( theProperty.getKey() ); // Cookie value URL decoding --> try { setValue( URLDecoder.decode( theProperty.getValue(), Encoding.UTF_8.getCode() ) ); } catch ( UnsupportedEncodingException ignore ) { setValue( theProperty.getValue() ); } // <-- Cookie value URL decoding }
for ( int i = 0; i < aConfigurationFiles.length; i++ ) { try { configLocations[i] = URLDecoder.decode( aConfigurationFiles[i].toString(), Encoding.UTF_8.getCode() ); for ( int i = 0; i < aPropertyFiles.length; i++ ) { try { propertyLocations[i] = new UrlResource( URLDecoder.decode( aPropertyFiles[i].toString(), Encoding.UTF_8.getCode() ) );
setValue( URLDecoder.decode( eProperty.getValue(), Encoding.UTF_8.getCode() ) );
theValue = URLEncoder.encode( getValue(), Encoding.UTF_8.getCode() );