Java tutorial
/** * Copyright 2010 Vstra Gtalandsregionen * * This library is free software; you can redistribute it and/or modify * it under the terms of version 2.1 of the GNU Lesser General Public * License as published by the Free Software Foundation. * * This library 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ package se.vgregion.pubsub.twitter.impl; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.auth.AuthState; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.protocol.ClientContext; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.protocol.HttpContext; /** * Interceptor which authenticates an HTTP request directly * */ public class PreemptiveBasicAuth implements HttpRequestInterceptor { private String username; private String password; public PreemptiveBasicAuth(String username, String password) { this.username = username; this.password = password; } public void process(HttpRequest request, HttpContext context) { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); // If not auth scheme has been initialized yet if (authState.getAuthScheme() == null) { Credentials creds = new UsernamePasswordCredentials(username, password); authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } }