Java tutorial
/* * #%L * VisualWAS * %% * Copyright (C) 2013 - 2014 Andreas Veithen * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ package com.github.veithen.visualwas.connector.security; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import org.apache.commons.codec.binary.Base64; public final class BasicAuthCredentials implements Credentials { private final String username; private final String password; public BasicAuthCredentials(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public String getPassword() { return password; } @Override public void configure(HttpURLConnection connection) { try { connection.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64((username + ":" + password).getBytes("utf-8")), "ascii")); } catch (UnsupportedEncodingException ex) { throw new Error("Unexpected exception", ex); } } }