org.apache.http.impl.client.SystemDefaultHttpClient Java Examples

The following examples show how to use org.apache.http.impl.client.SystemDefaultHttpClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuthenticatorTestCase.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private SystemDefaultHttpClient getHttpClient() {
  final SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient();
  httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory(true));
   Credentials use_jaas_creds = new Credentials() {
     public String getPassword() {
       return null;
     }

     public Principal getUserPrincipal() {
       return null;
     }
   };

   httpClient.getCredentialsProvider().setCredentials(
     AuthScope.ANY, use_jaas_creds);
   return httpClient;
}
 
Example #2
Source File: AuthenticatorTestCase.java    From hadoop with Apache License 2.0 6 votes vote down vote up
protected void _testAuthenticationHttpClient(Authenticator authenticator, boolean doPost) throws Exception {
  start();
  try {
    SystemDefaultHttpClient httpClient = getHttpClient();
    doHttpClientRequest(httpClient, new HttpGet(getBaseURL()));

    // Always do a GET before POST to trigger the SPNego negotiation
    if (doPost) {
      HttpPost post = new HttpPost(getBaseURL());
      byte [] postBytes = POST.getBytes();
      ByteArrayInputStream bis = new ByteArrayInputStream(postBytes);
      InputStreamEntity entity = new InputStreamEntity(bis, postBytes.length);

      // Important that the entity is not repeatable -- this means if
      // we have to renegotiate (e.g. b/c the cookie wasn't handled properly)
      // the test will fail.
      Assert.assertFalse(entity.isRepeatable());
      post.setEntity(entity);
      doHttpClientRequest(httpClient, post);
    }
  } finally {
    stop();
  }
}
 
Example #3
Source File: AuthenticatorTestCase.java    From big-c with Apache License 2.0 6 votes vote down vote up
private SystemDefaultHttpClient getHttpClient() {
  final SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient();
  httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory(true));
   Credentials use_jaas_creds = new Credentials() {
     public String getPassword() {
       return null;
     }

     public Principal getUserPrincipal() {
       return null;
     }
   };

   httpClient.getCredentialsProvider().setCredentials(
     AuthScope.ANY, use_jaas_creds);
   return httpClient;
}
 
Example #4
Source File: AuthenticatorTestCase.java    From big-c with Apache License 2.0 6 votes vote down vote up
protected void _testAuthenticationHttpClient(Authenticator authenticator, boolean doPost) throws Exception {
  start();
  try {
    SystemDefaultHttpClient httpClient = getHttpClient();
    doHttpClientRequest(httpClient, new HttpGet(getBaseURL()));

    // Always do a GET before POST to trigger the SPNego negotiation
    if (doPost) {
      HttpPost post = new HttpPost(getBaseURL());
      byte [] postBytes = POST.getBytes();
      ByteArrayInputStream bis = new ByteArrayInputStream(postBytes);
      InputStreamEntity entity = new InputStreamEntity(bis, postBytes.length);

      // Important that the entity is not repeatable -- this means if
      // we have to renegotiate (e.g. b/c the cookie wasn't handled properly)
      // the test will fail.
      Assert.assertFalse(entity.isRepeatable());
      post.setEntity(entity);
      doHttpClientRequest(httpClient, post);
    }
  } finally {
    stop();
  }
}
 
Example #5
Source File: AuthenticatorTestCase.java    From registry with Apache License 2.0 6 votes vote down vote up
private SystemDefaultHttpClient getHttpClient() {
    final SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient();
    httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory(true));
    Credentials use_jaas_creds = new Credentials() {
        public String getPassword() {
            return null;
        }

        public Principal getUserPrincipal() {
            return null;
        }
    };

    httpClient.getCredentialsProvider().setCredentials(
            AuthScope.ANY, use_jaas_creds);
    return httpClient;
}
 
Example #6
Source File: AuthenticatorTestCase.java    From registry with Apache License 2.0 6 votes vote down vote up
protected void _testAuthenticationHttpClient(Authenticator authenticator, boolean doPost) throws Exception {
    start();
    try {
        SystemDefaultHttpClient httpClient = getHttpClient();
        doHttpClientRequest(httpClient, new HttpGet(getBaseURL()));

        // Always do a GET before POST to trigger the SPNego negotiation
        if (doPost) {
            HttpPost post = new HttpPost(getBaseURL());
            byte[] postBytes = POST.getBytes();
            ByteArrayInputStream bis = new ByteArrayInputStream(postBytes);
            InputStreamEntity entity = new InputStreamEntity(bis, postBytes.length);

            // Important that the entity is not repeatable -- this means if
            // we have to renegotiate (e.g. b/c the cookie wasn't handled properly)
            // the test will fail.
            Assert.assertFalse(entity.isRepeatable());
            post.setEntity(entity);
            doHttpClientRequest(httpClient, post);
        }
    } finally {
        stop();
    }
}
 
Example #7
Source File: HttpEventPublisherClient.java    From product-cep with Apache License 2.0 6 votes vote down vote up
public static void publish(String url, String username, String password, String testCaseFolderName, String dataFileName) {
	log.info("Starting WSO2 HttpEventPublisher Client");
	KeyStoreUtil.setTrustStoreParams();
	HttpClient httpClient = new SystemDefaultHttpClient();
	try {
		HttpPost method = new HttpPost(url);
		List<String> messagesList = readMsg(getTestDataFileLocation(testCaseFolderName, dataFileName));
		for (String message : messagesList) {
			StringEntity entity = new StringEntity(message);
			log.info("Sending message:");
			log.info(message + "\n");
			method.setEntity(entity);
			if (url.startsWith("https")) {
				processAuthentication(method, username, password);
			}
			httpClient.execute(method).getEntity().getContent().close();
			Thread.sleep(1000);
		}
		Thread.sleep(500); // Waiting time for the message to be sent

	} catch (Throwable t) {
		log.error("Error when sending the messages", t);
	}
}
 
Example #8
Source File: WeakTLSProtocol.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    HttpClient client1 = new DefaultHttpClient(); // BAD

    HttpClient client2 = new SystemDefaultHttpClient(); // OK
    
    try {
      SSLContext context1 = SSLContext.getInstance("SSL"); // BAD
      
      SSLContext context2 = SSLContext.getInstance("TLS"); // OK
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
}
 
Example #9
Source File: Http.java    From product-cep with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
    log.info("Starting WSO2 Http Client");
    HttpUtil.setTrustStoreParams();
    String url = args[0];
    String username = args[1];
    String password = args[2];
    String sampleNumber = args[3];
    String filePath = args[4];
    HttpClient httpClient = new SystemDefaultHttpClient();
    try {
        HttpPost method = new HttpPost(url);
        filePath = HttpUtil.getMessageFilePath(sampleNumber, filePath, url);
        readMsg(filePath);
        for (String message : messagesList) {
            StringEntity entity = new StringEntity(message);
            log.info("Sending message:");
            log.info(message + "\n");
            method.setEntity(entity);
            if (url.startsWith("https")) {
                processAuthentication(method, username, password);
            }
            httpClient.execute(method).getEntity().getContent().close();
        }
        Thread.sleep(500); // Waiting time for the message to be sent
    } catch (Throwable t) {
        log.error("Error when sending the messages", t);
    }
}
 
Example #10
Source File: Http.java    From product-cep with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    HttpClient httpClient = new SystemDefaultHttpClient();
    try {
        HttpPost method = new HttpPost(url);
        log.info("Sending messages..");
        long lastTime = System.currentTimeMillis();
        DecimalFormat decimalFormat = new DecimalFormat("#");
        while (count < noOfEvents) {
            count++;
            String temp = "{\"event\": " + getRandomEvent(count).toString() + "}";
            StringEntity entity = new StringEntity(temp);
            method.setEntity(entity);
            if (url.startsWith("https")) {
                processAuthentication(method, username, password);
            }
            httpClient.execute(method).getEntity().getContent().close();
            if (count % elapsedCount == 0) {
                long currentTime = System.currentTimeMillis();
                long elapsedTime = currentTime - lastTime;
                double throughputPerSecond = (((double) elapsedCount) / elapsedTime) * 1000;
                lastTime = currentTime;
                log.info("Sent " + elapsedCount + " sensor events in " + elapsedTime +
                         " milliseconds with total throughput of " + decimalFormat.format(throughputPerSecond) +
                         " events per second.");
            }
        }
    } catch (Throwable t) {
        log.error("Error when sending the messages", t);
    }
}
 
Example #11
Source File: IAMAccountPasswordValidator.java    From aws-iam-ldap-bridge with Apache License 2.0 5 votes vote down vote up
@Override
public boolean verifyIAMPassword(Entry user, String pw) throws LdapInvalidAttributeValueException, LdapAuthenticationException {
    try {
        LOG.debug("Verifying {} {} with accessKey <hidden> and secretKey <hidden>",
                "user", user.get("uid").getString());
        HttpClient client = new SystemDefaultHttpClient();
        HttpPost post = new HttpPost("https://signin.aws.amazon.com/oauth");
        post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36");
        post.setHeader("Referer", "https://signin.aws.amazon.com/oauth");
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("client_id", "arn:aws:iam::015428540659:user/homepage"));
        urlParameters.add(new BasicNameValuePair("isIAMUser", "1"));
        urlParameters.add(new BasicNameValuePair("account", user.get("accountNumber").getString()));
        urlParameters.add(new BasicNameValuePair("username", user.get("uid").getString()));
        urlParameters.add(new BasicNameValuePair("password", pw));
        urlParameters.add(new BasicNameValuePair("Action", "login"));
        urlParameters.add(new BasicNameValuePair("redirect_uri", "https://console.aws.amazon.com/console/home?state=hashArgs%23&isauthcode=true"));
        urlParameters.add(new BasicNameValuePair("forceMobileApp", ""));
        urlParameters.add(new BasicNameValuePair("forceMobileLayout", ""));
        urlParameters.add(new BasicNameValuePair("mfaLoginFailure", ""));
        urlParameters.add(new BasicNameValuePair("RemainingExpiryPeriod", ""));
        urlParameters.add(new BasicNameValuePair("mfacode", ""));
        urlParameters.add(new BasicNameValuePair("next_mfacode", ""));
        post.setEntity(new UrlEncodedFormEntity(urlParameters, Charset.forName("UTF-8")));

        HttpResponse response = client.execute(post);
        return containsHeaders(response, "aws-account-alias", "aws-creds");
    } catch (IOException e) {
        LOG.error("Exception validating password for " + user.get("uid").getString(), e);
        return false;
    } catch (RuntimeException t) {
        LOG.error("Exception validating password for " + user.get("uid").getString(), t);
        throw t;
    }
}
 
Example #12
Source File: LayerHttpServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
public LayerHttpServiceImpl() {
	// Create a HTTP client object, which will initiate the connection:
	final HttpParams httpParams = new BasicHttpParams();
	HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT);
	setClient(new SystemDefaultHttpClient(httpParams));
}
 
Example #13
Source File: HttpFactory.java    From salt-step with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public HttpClient createHttpClient() {
    return new SystemDefaultHttpClient();
}