List of usage examples for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials
public UsernamePasswordCredentials(final String userName, final String password)
From source file:net.homelinux.penecoptero.android.citybikes.app.RESTHelper.java
private static DefaultHttpClient setCredentials(DefaultHttpClient httpclient, String url, String username, String password) throws HttpException, IOException { HttpHost targetHost = new HttpHost(url); final UsernamePasswordCredentials access = new UsernamePasswordCredentials(username, password); httpclient.getCredentialsProvider()//from w ww . j a v a 2s . c o m .setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), access); httpclient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(access); } } }, 0); return httpclient; }
From source file:com.microsoft.azure.hdinsight.common.task.YarnHistoryTask.java
public YarnHistoryTask(@NotNull IClusterDetail clusterDetail, @NotNull String path, @NotNull FutureCallback<String> callback) { super(callback); this.clusterDetail = clusterDetail; this.path = path; try {//w ww .j a v a2s. c om credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials( clusterDetail.getHttpUserName(), clusterDetail.getHttpPassword())); } catch (HDIException e) { e.printStackTrace(); } }
From source file:org.apache.hadoop.gateway.filter.BasicAuthChallengeFilter.java
private static UsernamePasswordCredentials createValidatedCredentials(HttpServletRequest request) { UsernamePasswordCredentials credentials = null; String basicAuthResponse = request.getHeader(AUTH.WWW_AUTH_RESP); if (basicAuthResponse != null) { String[] parts = basicAuthResponse.split(" "); if (parts.length == 2) { String usernamePassword = new String(Base64.decodeBase64(parts[1])); parts = usernamePassword.split(":"); if (parts.length == 2) { String username = parts[0]; String password = parts[1]; if (username.length() > 0 && password.length() > 0) { credentials = new UsernamePasswordCredentials(username, password); }//from w w w . j ava 2s . c o m } } //System.out.println( basicAuthResponse ); } return credentials; }
From source file:org.apache.camel.component.http4.ProxyHttpClientConfigurer.java
public void configureHttpClient(HttpClient client) { client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(host, port, scheme)); if (username != null && password != null) { Credentials defaultcreds;//from w w w. jav a 2 s.c om if (domain != null) { defaultcreds = new NTCredentials(username, password, ntHost, domain); } else { defaultcreds = new UsernamePasswordCredentials(username, password); } ((DefaultHttpClient) client).getCredentialsProvider().setCredentials(AuthScope.ANY, defaultcreds); } }
From source file:com.streamreduce.util.PingdomClient.java
public PingdomClient(Connection connection) { super(connection); this.connection = connection; ConnectionCredentials credentials = connection.getCredentials(); if (credentials == null) { throw new IllegalArgumentException("Connection must have username/password credentials."); }/*from w w w.j av a 2 s .c o m*/ apiKey = credentials.getApiKey(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(PINGDOM_HOST, 443), new UsernamePasswordCredentials(getConnectionCredentials().getIdentity(), getConnectionCredentials().getCredential())); }
From source file:info.ajaxplorer.client.http.AjxpHttpClient.java
public void refreshCredentials(String user, String pass) { this.getCredentialsProvider().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(user, pass)); }
From source file:org.apiwatch.util.IO.java
public static APIScope getAPIData(String source, String format, String encoding, String username, String password) throws IOException, SerializationError, HttpException { File file = new File(source); APIScope scope = null;/*from w ww .j av a 2s.co m*/ if (file.isFile()) { if (format == null) { /* get format from file extension */ format = source.substring(source.lastIndexOf('.') + 1); } InputStream in = new FileInputStream(file); Reader reader = new InputStreamReader(in, encoding); scope = Serializers.loadAPIScope(reader, format); reader.close(); in.close(); } else { /* maybe source is a URL */ DefaultHttpClient client = new DefaultHttpClient(); if (username != null && password != null) { client.getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username, password)); } HttpResponse response = client.execute(new HttpGet(source)); if (response.getStatusLine().getStatusCode() >= 400) { throw new HttpException(response.getStatusLine().getReasonPhrase()); } HttpEntity entity = response.getEntity(); ContentType contentType = ContentType.fromHeader(entity.getContentType().getValue()); if (entity.getContentEncoding() != null) { encoding = entity.getContentEncoding().getValue(); } else if (contentType.charset != null) { encoding = contentType.charset; } if (format == null) { format = contentType.type; } InputStream in = entity.getContent(); Reader reader = new InputStreamReader(in, encoding); scope = Serializers.loadAPIScope(reader, format); reader.close(); in.close(); client.getConnectionManager().shutdown(); } return scope; }
From source file:com.unispezi.cpanelremotebackup.http.HTTPClient.java
public HTTPClient(String hostName, int port, boolean secure, String userName, String password) { this.httpClient = new DefaultHttpClient(); host = new HttpHost(hostName, port, secure ? "https" : null); httpClient.getCredentialsProvider().setCredentials(new AuthScope(hostName, port), new UsernamePasswordCredentials(userName, password)); // HttpHost proxy = new HttpHost("localhost", 8888); // httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); // Set up HTTP basic auth (without challenge) or "preemptive auth" // Taken from http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html#d5e1031 // Create AuthCache instance BasicAuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(host, basicAuth);/*from w w w . j a va 2 s . c o m*/ // Add AuthCache to the execution context localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); }
From source file:org.jasig.portlet.proxy.service.web.interceptor.UserInfoBasicAuthenticationPreInterceptor.java
@Override protected UsernamePasswordCredentials getCredentials(PortletRequest portletRequest) { // get the username and password attribute names configured for this // portlet instance final PortletPreferences preferences = portletRequest.getPreferences(); final String usernameKey = preferences.getValue(USERNAME_KEY, "user.login.id"); final String passwordKey = preferences.getValue(PASSWORD_KEY, "password"); // request the associated username and password attributes from the // UserInfo map @SuppressWarnings("unchecked") final Map<String, String> userInfo = (Map<String, String>) portletRequest .getAttribute(PortletRequest.USER_INFO); final String username = userInfo.get(usernameKey); final String password = userInfo.get(passwordKey); // construct a credentials object using hte attributes final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); return credentials; }