Java tutorial
/** * Copyright (c) 2014, XebiaLabs B.V., All rights reserved. * * * The XL Deploy plugin for Jenkins is licensed under the terms of the GPLv2 * <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most XebiaLabs Libraries. * There are special exceptions to the terms and conditions of the GPLv2 as it is applied to * this software, see the FLOSS License Exception * <https://github.com/jenkinsci/deployit-plugin/blob/master/LICENSE>. * * 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; version 2 * of the License. * * 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, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth * Floor, Boston, MA 02110-1301 USA */ package com.xebialabs.deployit.ci.server; import java.io.IOException; import java.io.Serializable; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.protocol.ClientContext; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class PreemptiveAuthenticationInterceptor implements HttpRequestInterceptor, Serializable { private static final Logger LOGGER = LoggerFactory.getLogger(PreemptiveAuthenticationInterceptor.class); @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request.getFirstHeader("Authorization") == null) { LOGGER.trace("No 'Authorization' header found for request: {}", request.getRequestLine()); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); CredentialsProvider credentialsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); if (credentialsProvider != null) { Credentials credentials = credentialsProvider .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (credentials != null) { request.setHeader(new BasicScheme().authenticate(credentials, request, context)); LOGGER.trace("Set 'Authorization' header {} for request: {}", credentials.getUserPrincipal(), request.getRequestLine()); } } } } }