/** * Basic setter */ private void setCredentials(UsernamePasswordCredentials credentials) { this.credentials = credentials; log.info("Username=" + ((credentials != null) ? "\"" + credentials.getUserName() + "\"" : "IS NULL")); }
public static String toBasicAuthorizationValue(UsernamePasswordCredentials credentials) { return "Basic "+Base64.encodeBase64String( (credentials.getUserName()+":"+credentials.getPassword()).getBytes() ); }
try { // You can use HTTPS or HTTP as protocol. String myURL = "https://example.com/song.mp3"; mp.reset(); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setOnPreparedListener(this); mp.setOnErrorListener(this); // These are the two lines, doing the magic ;) It transforms it to a url like this: https://user:password@example.com/song.mp3 UsernamePasswordCredentials credentials = connHelper.getCredetials(); myURL = myURL.replace("://", "://" + URLEncoder.encode(credentials.getUserName(), "UTF-8") + ":" + URLEncoder.encode(credentials.getPassword(), "UTF-8") + "@"); mp.setDataSource(myURL); mp.prepareAsync(); mp.setOnCompletionListener(this); } catch (Exception e) { Log.e("StreamAudioDemo", e.getMessage()); }
/** * This method allows to propagate the authentication information from the current object. * * @param commonDataLoader {@code CommonsDataLoader} to be initialised with authentication information */ public void propagateAuthentication(final CommonDataLoader commonDataLoader) { for (final Map.Entry<HttpHost, UsernamePasswordCredentials> credentialsEntry : authenticationMap.entrySet()) { final HttpHost httpHost = credentialsEntry.getKey(); final UsernamePasswordCredentials credentials = credentialsEntry.getValue(); commonDataLoader.addAuthentication(httpHost.getHostName(), httpHost.getPort(), httpHost.getSchemeName(), credentials.getUserName(), credentials.getPassword()); } }
default String getJsonBodyOfOsgiConfiguration(String osgiConfigurationUrl, UsernamePasswordCredentials credentials, String instanceName) throws AuthenticationException, IOException { DefaultHttpClient authorizedClient = new DefaultHttpClient(); HttpUriRequest request = new HttpGet(osgiConfigurationUrl); request.addHeader(new BasicScheme().authenticate(credentials, request, null)); HttpResponse response = authorizedClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpURLConnection.HTTP_OK) { throw new AuthenticationException( "Cannot authenticate user " + credentials.getUserName() + " to " + instanceName + " instance"); } return EntityUtils.toString(response.getEntity()); }
/** * This method allows to propagate the authentication information from the * current object. * * @param commonsDataLoader * {@code CommonsDataLoader} to be initialized with * authentication information */ public void propagateAuthentication(final CommonsDataLoader commonsDataLoader) { for (final Map.Entry<HttpHost, UsernamePasswordCredentials> credentialsEntry : authenticationMap.entrySet()) { final HttpHost httpHost = credentialsEntry.getKey(); final UsernamePasswordCredentials credentials = credentialsEntry.getValue(); commonsDataLoader.addAuthentication(httpHost.getHostName(), httpHost.getPort(), httpHost.getSchemeName(), credentials.getUserName(), credentials.getPassword()); } }
private boolean testReversReplicationUser(UsernamePasswordCredentials credentials, String instanceName, String url) throws AuthenticationException, IOException { String agentUrl = url + "/etc/replication/agents.author/publish_reverse/jcr:content.json"; HttpUriRequest request = new HttpGet(agentUrl); request.addHeader(new BasicScheme().authenticate(credentials, request, null)); return invokeTest(request, credentials.getUserName(), instanceName, "Reverse Replication"); }
private boolean testReplicationUser(UsernamePasswordCredentials credentials, String instanceName, String url) throws IOException, AuthenticationException { String agentUrl = url + "/etc/replication/agents.author/publish/jcr:content.json"; HttpUriRequest request = new HttpGet(agentUrl); request.addHeader(new BasicScheme().authenticate(credentials, request, null)); return invokeTest(request, credentials.getUserName(), instanceName, "Replication"); }
@Override public boolean doTest(String url, String instanceName) throws Exception { UsernamePasswordCredentials credentials = getUsernamePasswordCredentials(instanceName); String agentUrl = url + "/system/console/bundles.json"; HttpUriRequest request = new HttpGet(agentUrl); request.addHeader(new BasicScheme().authenticate(credentials, request, null)); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(request); String body = EntityUtils.toString(response.getEntity()); if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { throw new AuthenticationException("Cannot authenticate user " + credentials.getUserName()); } else if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { @SuppressWarnings("unchecked") Map<String, List<Map<String, String>>> map = GSON .fromJson(body, Map.class); String[] bundlesConfig = config.getStringList("bundles"); ArrayList<String> bundles = new ArrayList<>(Arrays.asList(bundlesConfig)); for (Map<String, String> item : map.get("data")) { if (bundles.contains(item.get("name"))) { addErrorMessage("Bundle [%s] already exists on [%s]", item.get("name"), instanceName); bundles.remove(item.get("name")); } } for (String bundle : bundles) { addInfoMessage("Bundle [%s] doesn't exists on [%s]", bundle, instanceName); } } return true; }
/** * {@inheritDoc} */ @Override public synchronized void setCredentials(final AuthScope authscope, final Credentials credentials) { if (authscope == null) { throw new IllegalArgumentException("Authentication scope may not be null"); } final CredentialsFactory factory; if (credentials instanceof UsernamePasswordCredentials) { final UsernamePasswordCredentials userCredentials = (UsernamePasswordCredentials) credentials; factory = new UsernamePasswordCredentialsFactory(userCredentials.getUserName(), userCredentials.getPassword()); } else if (credentials instanceof NTCredentials) { final NTCredentials ntCredentials = (NTCredentials) credentials; factory = new NTCredentialsFactory(ntCredentials.getUserName(), ntCredentials.getPassword(), ntCredentials.getWorkstation(), ntCredentials.getDomain()); } else { throw new IllegalArgumentException("Unsupported Credential type: " + credentials.getClass().getName()); } credentialsMap_.put(new AuthScopeProxy(authscope), factory); }
if (creds != null && creds.getUserName() != null && creds.getUserName().length() > 0) { client = getHttpClient(url, creds); } else {
if (creds != null && creds.getUserName() != null && creds.getUserName().length() > 0) { client = getHttpClient(url, creds); } else {
if (creds instanceof UsernamePasswordCredentials) { UsernamePasswordCredentials c = (UsernamePasswordCredentials) creds; username = c.getUserName(); password = c.getPassword();
private void connectAuthenticatedSensors() { hostsHttpFeed = HttpFeed.builder() .entity(this) .period(1000, TimeUnit.MILLISECONDS) .baseUri(String.format("%s/api/v1/hosts", ambariUri)) .credentials(usernamePasswordCredentials.getUserName(), usernamePasswordCredentials.getPassword()) .header(HttpHeaders.AUTHORIZATION, HttpTool.toBasicAuthorizationValue(usernamePasswordCredentials)) .poll(new HttpPollConfig<List<String>>(REGISTERED_HOSTS) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), getHosts())) .onFailureOrException(Functions.<List<String>>constant(ImmutableList.<String>of()))) .build(); clusterHttpFeed = HttpFeed.builder() .entity(this) .period(1000, TimeUnit.MILLISECONDS) .baseUri(String.format("%s/api/v1/clusters/%s/requests/%d", ambariUri, getConfig(AmbariCluster.CLUSTER_NAME), 1)) .credentials(usernamePasswordCredentials.getUserName(), usernamePasswordCredentials.getPassword()) .header(HttpHeaders.AUTHORIZATION, HttpTool.toBasicAuthorizationValue(usernamePasswordCredentials)) .poll(new HttpPollConfig<String>(CLUSTER_STATE) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), getRequestState())) .onFailureOrException(Functions.<String>constant(null))) .build(); }
public static void writeServiceReference(SparqlServiceReference serviceRef, String targetGraph, Resource root, Model model) throws IllegalAccessException { Resource dgs = ResourceUtils.createSubResource(root, "defaultGraphs"); Resource ngs = ResourceUtils.createSubResource(root, "namedGraphs"); // An update can only go to a single graph model.add(root, RDF.type, V.RdfDataset); model.add(root, V.endpoint, model.createResource(serviceRef.getServiceURL())); model.add(root, V.target, model.createResource(targetGraph)); UsernamePasswordCredentials credentials = serviceRef.getCredentials(); //if(auth instanceof SimpleAuthenticator) { if(credentials != null) { //SimpleAuthenticator a = (SimpleAuthenticator)auth; //String username = (String)FieldUtils.readField(a, "username" , true); //char[] password = (char[])FieldUtils.readField(a, "password" , true); model.add(root, V.username, model.createLiteral(credentials.getUserName())); model.add(root, V.password, model.createLiteral(credentials.getPassword())); } }
Element login = newEpicureElement("login"); Element password = newEpicureElement("password"); login.setText(credentials.getUserName()); password.setText(credentials.getPassword()); epicurLite.addContent(login);