List of usage examples for org.apache.http.impl.auth BasicScheme authenticate
@Deprecated public Header authenticate(final Credentials credentials, final HttpRequest request) throws AuthenticationException
From source file:com.jonbanjo.cups.operations.AuthHeader.java
static void makeAuthHeader(HttpRequest request, AuthInfo auth) { if (auth.reason == AuthInfo.AUTH_NOT_SUPPORTED) { return;/*from ww w. j a v a 2s . c om*/ } if (auth.username.equals("") || (auth.password.equals(""))) { auth.reason = AuthInfo.AUTH_REQUIRED; return; } UsernamePasswordCredentials creds = new UsernamePasswordCredentials(auth.username, auth.password); if (auth.getType().equals("Basic")) { BasicScheme basicScheme = new BasicScheme(); try { auth.setAuthHeader(basicScheme.authenticate(creds, request)); } catch (Exception e) { System.err.println(e.toString()); auth.reason = AuthInfo.AUTH_BAD; } } else if (auth.getType().equals("Digest")) { try { DigestScheme digestScheme = new DigestScheme(); digestScheme.processChallenge(auth.getHttpHeader()); auth.setAuthHeader(digestScheme.authenticate(creds, request)); String test0 = auth.getHttpHeader().getValue(); String test1 = auth.getAuthHeader().getValue(); System.out.println(); } catch (Exception e) { System.err.println(e.toString()); auth.reason = AuthInfo.AUTH_BAD; } } else { auth.reason = AuthInfo.AUTH_NOT_SUPPORTED; } }
From source file:com.fibon.maven.confluence.ExportPageConfluenceMojo.java
private HttpGet prepareExportPageRequest(Format format, Long pageId) throws MojoFailureException { HttpGet get = new HttpGet(url + format.url + "?pageId=" + pageId); AuthenticationInfo info = wagonManager.getAuthenticationInfo(serverId); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(info.getUserName(), info.getPassword());// w w w. ja v a2 s. co m BasicScheme scheme = new BasicScheme(); try { Header authorizationHeader = scheme.authenticate(credentials, get); get.addHeader(authorizationHeader); return get; } catch (AuthenticationException e) { throw fail("Unable to set authentication data", e); } }
From source file:mx.bigdata.utils.pubsubhubbub.Subscriber.java
public int executeAction(String action, String hub, String topic_url, String uname, String passwd) throws Exception { String callback = webserver.getCallbackUrl(); String vtoken = UUID.randomUUID().toString(); logger.debug("hub.callback: " + callback); logger.debug("hub.mode: " + action); logger.debug("hub.topic: " + topic_url); logger.trace("hub.secret: " + webserver.getKey()); logger.trace("hub.verify: " + "async"); logger.trace("hub.verify_token: " + vtoken); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("hub.callback", callback)); nvps.add(new BasicNameValuePair("hub.mode", action)); nvps.add(new BasicNameValuePair("hub.topic", topic_url)); nvps.add(new BasicNameValuePair("hub.secret", webserver.getKey())); nvps.add(new BasicNameValuePair("hub.verify", "async")); nvps.add(new BasicNameValuePair("hub.verify_token", vtoken)); webserver.addAction(action, topic_url, vtoken); if (logger.isTraceEnabled()) { logger.trace("postBody: " + URLEncodedUtils.format(nvps, "UTF-8")); }//from w ww .j a v a2 s. c om HttpPost httppost = new HttpPost(hub); httppost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); httppost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); httppost.setHeader("User-agent", "RSS pubsubhubbub 0.3"); httppost.setHeader("Accept", "application/json"); httppost.setHeader("Accept-Charset", "utf-8"); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(uname, passwd); BasicScheme scheme = new BasicScheme(); Header authorizationHeader = scheme.authenticate(credentials, httppost); httppost.addHeader(authorizationHeader); HttpResponse httpresponse = execute(httpClient, httppost); return httpresponse.getStatusLine().getStatusCode(); }
From source file:ca.etsmtl.applets.etsmobile.ProfileActivity.java
private void getBandwith(String phase, String appt) { appt_input.setEnabled(false);/* www . j a v a 2 s . c o m*/ phase_input.setEnabled(false); final Editor edit = prefs.edit(); creds.setPhase(phase); creds.setAppt(appt); edit.putString(UserCredentials.REZ, phase); edit.putString(UserCredentials.APPT, appt); edit.commit(); // get bandwidth from cooptel, parse html, extract floats, etc etc new AsyncTask<String, Void, float[]>() { final Pattern usageRegex = Pattern.compile( "<TR><TD>(.*)</TD><TD>(.*)</TD><TD ALIGN=\"RIGHT\">(.*)</TD><TD ALIGN=\"RIGHT\">(.*)</TD></TR>"); final Pattern quotaRegex = Pattern.compile( "<TR><TD>Quota permis pour la période</TD><TD ALIGN=\"RIGHT\">(.*)</TD></TD></TR>"); @Override protected float[] doInBackground(String... params) { final float[] result = new float[2]; try { final HttpGet get = new HttpGet(URI.create( String.format("http://www2.cooptel.qc.ca/services/temps/?mois=%d&cmd=Visualiser", Calendar.getInstance().get(Calendar.MONTH) + 1))); final BasicScheme scheme = new BasicScheme(); final Credentials credentials = new UsernamePasswordCredentials( "ets-res" + params[0] + "-" + params[1], "ets" + params[1]); try { final Header h = scheme.authenticate(credentials, get); get.addHeader(h); final HttpClient client = new DefaultHttpClient(); final HttpResponse re = client.execute(get); // if HTTP200 if (re.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final String ent = EntityUtils.toString(re.getEntity()); final Matcher matcher = usageRegex.matcher(ent); float total = 0; // String[] usageResult = matcher.; // parse all results while (matcher.find()) { final Number upload = Float.parseFloat(matcher.group(3)); final Number download = Float.parseFloat(matcher.group(4)); total += upload.floatValue(); total += download.floatValue(); } final Matcher quotaResult = quotaRegex.matcher(ent); float totalBandwithAvail = 0; if (quotaResult.find()) { totalBandwithAvail = Float.parseFloat(quotaResult.group(1)); } result[0] = total / 1024; result[1] = totalBandwithAvail / 1024; } } catch (final AuthenticationException e) { e.printStackTrace(); } } catch (final IOException e) { e.printStackTrace(); } return result; } @Override protected void onPostExecute(float[] result) { handlerBandwith.obtainMessage(2, result).sendToTarget(); super.onPostExecute(result); } }.execute(phase, appt); }