/**
 * Copyright (C) 2011-2014 Barchart, Inc. <http://www.barchart.com/>
 *
 * All rights reserved. Licensed under the OSI BSD License.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */
package com.barchart.netty.rest.client.credentials;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

import javax.xml.bind.DatatypeConverter;

import com.barchart.netty.rest.client.Credentials;
import com.barchart.netty.rest.client.RestRequest;

public class UsernamePasswordCredentials implements Credentials {

	private final static Charset UTF8 = Charset.forName("UTF-8");

	private final String username;
	private final char[] password;

	public UsernamePasswordCredentials(final String username_,
			final char[] password_) {
		username = username_;
		password = password_;
	}

	@Override
	public void authenticate(final RestRequest<?> request) {

		byte[] firstBytes;
		try {
			firstBytes =
					(URLEncoder.encode(username, "UTF-8") + ":").getBytes(UTF8);
		} catch (final UnsupportedEncodingException e) {
			firstBytes = (username + ":").getBytes(UTF8);
		}

		final byte[] secondBytes =
				UTF8.encode(CharBuffer.wrap(password)).array();

		final byte[] concat = new byte[firstBytes.length + secondBytes.length];
		System.arraycopy(firstBytes, 0, concat, 0, firstBytes.length);
		System.arraycopy(secondBytes, 0, concat, firstBytes.length,
				secondBytes.length);

		request.header("Authorization",
				"Basic " + DatatypeConverter.printBase64Binary(concat));

	}
}

@@ -382,50 +382,8 @@
 t;%0A%0A
-import javax.xml.bind.DatatypeConverter;%0A%0A
 impo
@@ -479,16 +479,65 @@
 Request;
+%0Aimport com.barchart.util.common.encoding.Base64;
 %0A%0Apublic
@@ -1515,43 +1515,21 @@
 %22 + 
-DatatypeConverter.printBase64Binary
+Base64.decode
 (con

use Base64