List of usage examples for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials
public UsernamePasswordCredentials(final String userName, final String password)
From source file:de.unikassel.android.sdcframework.transmission.BasicAuthHttpProtocol.java
@Override protected final void configureForAuthentication(DefaultHttpClient client, URL url) { client.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), -1), new UsernamePasswordCredentials(getUserName(), getMd5Password())); }
From source file:org.uberfire.provisioning.wildfly.runtime.provider.extras.Wildfly10RemoteClient.java
public int deploy(String user, String password, String host, int port, String filePath) { // the digest auth backend CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials(user, password)); CloseableHttpClient httpclient = custom().setDefaultCredentialsProvider(credsProvider).build(); HttpPost post = new HttpPost("http://" + host + ":" + port + "/management-upload"); post.addHeader("X-Management-Client-Name", "HAL"); // the file to be uploaded File file = new File(filePath); FileBody fileBody = new FileBody(file); // the DMR operation ModelNode operation = new ModelNode(); operation.get("address").add("deployment", file.getName()); operation.get("operation").set("add"); operation.get("runtime-name").set(file.getName()); operation.get("enabled").set(true); operation.get("content").add().get("input-stream-index").set(0); // point to the multipart index used ByteArrayOutputStream bout = new ByteArrayOutputStream(); try {// ww w . j av a 2s . c o m operation.writeBase64(bout); } catch (IOException ex) { getLogger(Wildfly10RemoteClient.class.getName()).log(SEVERE, null, ex); } // the multipart MultipartEntityBuilder builder = create(); builder.setMode(BROWSER_COMPATIBLE); builder.addPart("uploadFormElement", fileBody); builder.addPart("operation", new ByteArrayBody(bout.toByteArray(), create("application/dmr-encoded"), "blob")); HttpEntity entity = builder.build(); //entity.writeTo(System.out); post.setEntity(entity); try { HttpResponse response = httpclient.execute(post); out.println(">>> Deploying Response Entity: " + response.getEntity()); out.println(">>> Deploying Response Satus: " + response.getStatusLine().getStatusCode()); return response.getStatusLine().getStatusCode(); } catch (IOException ex) { ex.printStackTrace(); getLogger(Wildfly10RemoteClient.class.getName()).log(SEVERE, null, ex); } return -1; }
From source file:com.zhch.example.commons.http.v4_5.ProxyTunnelDemo.java
/** * demo /*www .j a va 2 s. com*/ * * @throws Exception */ public final static void zhchModifyDemo() throws Exception { ProxyClient proxyClient = new ProxyClient(); HttpHost target = new HttpHost("pan.baidu.com", 80); // HttpHost proxy = new HttpHost("120.24.0.162", 80); HttpHost proxy = new HttpHost("127.0.0.1", 80); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("abc", "def"); System.out.println("before tunnel."); Socket socket = proxyClient.tunnel(proxy, target, credentials); System.out.println("after tunnel."); try { Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET); out.write("GET / HTTP/1.1\r\n"); out.write("Host: " + target.toHostString() + "\r\n"); out.write("Agent: whatever\r\n"); out.write("Connection: close\r\n"); out.write("\r\n"); out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET)); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } } finally { socket.close(); } }
From source file:makesense.ara.Collector.java
private void connect() { DefaultHttpClient httpclient = new DefaultHttpClient(); try {/*from w ww . j av a 2s . c om*/ // for the moment, we use AuthScope.ANY and SingleAuth // in the future, think about MultiAuth to allow several accounts httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(data.get("login"), data.get("password"))); HttpGet httpget = new HttpGet(data.get("URL")); HttpResponse httpResponse = httpclient.execute(httpget); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String content; BufferedReader br = new BufferedReader(new InputStreamReader(instream)); // we read as long as we do not ask to stop while (!stop) { content = br.readLine(); if (content != null) { System.out.println(content); } // if } // while } // if } // try catch (IOException ioe) { ioe.printStackTrace(); } // catch finally { httpclient.getConnectionManager().shutdown(); } // finally }
From source file:com.arcbees.vcs.bitbucket.BitbucketApi.java
public BitbucketApi(HttpClientWrapper httpClient, BitbucketApiPaths apiPaths, String userName, String password, String repositoryOwner, String repositoryName) { this.httpClient = httpClient; this.apiPaths = apiPaths; this.repositoryOwner = repositoryOwner; this.repositoryName = repositoryName; this.credentials = new UsernamePasswordCredentials(userName, password); this.gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonDateTypeAdapter()).create(); }
From source file:org.qucosa.migration.processors.PurgeFedoraObject.java
private HttpClient prepareHttpClient(String user, String password) { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager) .setDefaultCredentialsProvider(credentialsProvider).build(); return client; }
From source file:com.imagesleuth.imagesleuthclient2.Getter.java
public Getter(String url, String user, String password) { Test.testNull(url);/*from w ww . j a va 2 s .c o m*/ Test.testNull(user); Test.testNull(password); this.url = url; harray.add(new BasicHeader("Authorization", "Basic " + Base64.encodeBase64String((user + ":" + password).getBytes()))); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, password); credsProvider.setCredentials(AuthScope.ANY, credentials); requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(30000).build(); }
From source file:com.jwm123.loggly.reporter.Client.java
public List<Map<String, Object>> getReport() throws Exception { final Integer rows = 500; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(config.getAccount() + ".loggly.com", 80), new UsernamePasswordCredentials(config.getUsername(), config.getPassword())); CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); String url = "http://" + config.getAccount() + ".loggly.com/api/search?rows=" + rows + "&q=" + URLEncoder.encode(query, "UTF8"); if (StringUtils.isNotBlank(from)) { url += "&from=" + URLEncoder.encode(from, "UTF8"); }/* w w w .j av a 2s . c o m*/ if (StringUtils.isNotBlank(to)) { url += "&until=" + URLEncoder.encode(to, "UTF8"); } HttpGet get = new HttpGet(url); CloseableHttpResponse resp = null; List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); Integer offset = 0 - rows; Integer numFound = 0; try { do { offset += rows; try { get = new HttpGet(url + "&start=" + offset); resp = client.execute(get); if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String response = EntityUtils.toString(resp.getEntity()); Map<String, Object> responseMap = new Gson().fromJson(response, new TypeToken<Map<String, Object>>() { }.getType()); if (responseMap.containsKey("numFound")) { numFound = ((Number) responseMap.get("numFound")).intValue(); } if (responseMap.containsKey("data")) { Collection<?> dataCol = (Collection<?>) responseMap.get("data"); if (!dataCol.isEmpty()) { for (Object dataItem : dataCol) { if (dataItem instanceof Map) { results.add((Map<String, Object>) dataItem); } } } } } else { System.out.println("status: " + resp.getStatusLine().getStatusCode() + " Response: [" + EntityUtils.toString(resp.getEntity()) + "]"); break; } } finally { get.releaseConnection(); IOUtils.closeQuietly(resp); } } while (numFound >= offset + rows); } finally { IOUtils.closeQuietly(client); } return results; }
From source file:org.gradle.api.internal.artifacts.repositories.transport.http.HttpClientConfigurer.java
private UsernamePasswordCredentials createRepositoryCredentials(PasswordCredentials credentials) { if (GUtil.isTrue(credentials.getUsername())) { return new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword()); }// w w w . j av a2s . co m return null; }
From source file:com.marklogic.client.test.util.TestServerBootstrapper.java
private void deleteRestServer() throws ClientProtocolException, IOException { DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8002), new UsernamePasswordCredentials(username, password)); HttpDelete delete = new HttpDelete( "http://" + host + ":8002/v1/rest-apis/java-unittest?include=modules&include=content"); client.execute(delete);/*ww w . ja v a2s . c o m*/ }