Commits

John Paulett  committed 7823b90

Allow HTTP authentication if user info is set in the URL.

For example, http://user:pass@example.com/ will encode and send the username
and password. Involved moving the UploadAssistant's Util.base64encode to
Base64.encode in this project.

  • Participants
  • Parent commits 5d6a5c0

Comments (0)

Files changed (2)

File src/main/java/org/nrg/util/Base64.java

+package org.nrg.util;
+
+
+public class Base64 {
+	/**
+	 * Base64 encoding adapted from
+	 * http://www.wikihow.com/Encode-a-String-to-Base64-With-Java Article
+	 * authors John Comeau, et al.
+	 */
+	private static final String base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+	private static final int splitLinesAt = 76;
+
+	public static String encode(final String string) {
+		final StringBuilder encoded = new StringBuilder();
+		byte[] stringArray;
+		try {
+			stringArray = string.getBytes("UTF-8"); // use appropriate encoding
+													// string
+		} catch (Exception ignored) {
+			stringArray = string.getBytes(); // use locale default rather than
+												// croak
+		}
+
+		// determine how many padding bytes to add to the output
+		int paddingCount = (3 - (stringArray.length % 3)) % 3;
+
+		// add any necessary padding to the input
+		stringArray = zeroPad(stringArray.length + paddingCount, stringArray);
+
+		// process 3 bytes at a time, churning out 4 output bytes
+		// worry about CRLF insertions later
+		for (int i = 0; i < stringArray.length; i += 3) {
+			final int j = (stringArray[i] << 16) + (stringArray[i + 1] << 8) + stringArray[i + 2];
+			encoded.append(base64code.charAt((j >> 18) & 0x3f));
+			encoded.append(base64code.charAt((j >> 12) & 0x3f));
+			encoded.append(base64code.charAt((j >> 6) & 0x3f));
+			encoded.append(base64code.charAt(j & 0x3f));
+		}
+
+		// replace encoded padding nulls with "="
+		return splitLines(encoded.substring(0, encoded.length() - paddingCount) + "==".substring(0, paddingCount));
+	}
+
+	public static String splitLines(final String string) {
+		final StringBuilder lines = new StringBuilder();
+		lines.append(string.substring(0, Math.min(string.length(), splitLinesAt)));
+		for (int i = splitLinesAt; i < string.length(); i += splitLinesAt) {
+			lines.append("\r\n");
+			lines.append(string.substring(i, Math.min(string.length(), i + splitLinesAt)));
+		}
+		return lines.toString();
+	}
+
+	public static byte[] zeroPad(final int length, final byte[] bytes) {
+		final byte[] padded = new byte[length];
+		System.arraycopy(bytes, 0, padded, 0, bytes.length);
+		return padded;
+	}
+
+}

File src/main/java/org/nrg/util/HttpClient.java

 import java.net.URL;
 
 public class HttpClient implements IHttpClient {
-
+	private static final String AUTHORIZATION_HEADER = "Authorization";
+	
 	public String request(final URL url) throws IOException {
 		final HttpURLConnection connection;
 		try {
 		} catch (ClassCastException e) {
 			throw new IOException("unable to make HTTP/HTTPS connection to URL " + url);
 		}
+
+		if (url.getUserInfo() != null) {
+			final StringBuilder authHeader = new StringBuilder("Basic ");
+			authHeader.append(Base64.encode(url.getUserInfo()));
+			connection.setRequestProperty(AUTHORIZATION_HEADER, authHeader.toString());
+		}
+
+		connection.connect();
 		if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
 			throw new IOException("request failed: " + connection.getResponseCode() + " " + connection.getResponseMessage());
 		}