Package com.gateway.util

Source Code of com.gateway.util.ProviderClient

package com.gateway.util;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.apache.http.NameValuePair;
import com.newrelic.api.agent.NewRelic;

public class ProviderClient
{
  public static Map<String, Object> post(String url, Map<String, Object> body, Integer connectionTimeout, Integer soTimeout) throws Exception
  {
    HttpClient client = getHttpClient(url, connectionTimeout, soTimeout);
    HttpPost httppost = new HttpPost(url);
    List<NameValuePair> formParams = setParams(httppost, body);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
    httppost.setEntity(entity);
    long t1 = System.currentTimeMillis();
    HttpResponse response = client.execute(httppost);
    long t2 = System.currentTimeMillis();
    NewRelic.addCustomParameter(System.currentTimeMillis()+"--"+url, (t2 - t1) + " ms");
    HttpEntity resEntity = response.getEntity();
    String contentCharSet = EntityUtils.getContentCharSet(resEntity);
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("status", response.getStatusLine().getStatusCode());
    map.put("response", contentCharSet != null ? new String(EntityUtils.toByteArray(resEntity), contentCharSet) : new String(EntityUtils.toByteArray(resEntity)));
    return map;
  }

  public static Map<String, Object> get(String url, Integer connectionTimeout, Integer soTimeout, Map<String, Object> authMap) throws Exception
  {
    HttpClient client = getHttpClient(url, connectionTimeout, soTimeout);
    HttpGet httpclient = new HttpGet(url);
    if (authMap != null)
      httpclient.addHeader(new BasicScheme().authenticate(new UsernamePasswordCredentials((String) authMap.get("user"), (String) authMap.get("password")), httpclient));
    HttpResponse response = client.execute(httpclient);
    HttpEntity resEntity = response.getEntity();
    String contentCharSet = EntityUtils.getContentCharSet(resEntity);
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("status", response.getStatusLine().getStatusCode());
    map.put("response", contentCharSet != null ? new String(EntityUtils.toByteArray(resEntity), contentCharSet) : new String(EntityUtils.toByteArray(resEntity)));
    return map;
  }

  private static HttpClient getHttpClient(String url, Integer connectionTimeout, Integer soTimeout) throws NoSuchAlgorithmException, KeyManagementException
  {
    Scheme httpsScheme = null;
    if (url.startsWith("https"))
    {
      SSLContext sslContext = SSLContext.getInstance("SSL");
      sslContext.init(null, new TrustManager[] { new X509TrustManager() {
              public X509Certificate[] getAcceptedIssuers()
              {
                return null;
              }
        public void checkClientTrusted(X509Certificate[] chain,  String authType) throws CertificateException
        {
        }
        public void checkServerTrusted(X509Certificate[] chain,  String authType) throws CertificateException
        {
        }
      }}, new SecureRandom());
      SSLSocketFactory sf = new SSLSocketFactory(sslContext, new AllowAllHostnameVerifier());
      httpsScheme = new Scheme("https", 443, sf);
    }
    else
      httpsScheme = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);
    HttpParams params = getHttpParams(connectionTimeout, soTimeout);
    ClientConnectionManager cm = new ThreadSafeClientConnManager(schemeRegistry);
    return new DefaultHttpClient(cm, params);
  }

  private static HttpParams getHttpParams(Integer connectionTimeout, Integer soTimeout)
  {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
    HttpConnectionParams.setSoTimeout(params, soTimeout);
    return params;
  }

  private static List<NameValuePair> setParams(HttpPost httppost, Map<String, Object> body)
  {
    List<NameValuePair> formParams = new ArrayList<NameValuePair>();
    Set<Entry<String, Object>> set = body.entrySet();
    for (Entry<String, Object> e: set)
      formParams.add(new BasicNameValuePair(e.getKey(), (e.getValue() != null) ? e.getValue().toString() : null));
    return formParams;
  }

  public static void main(String args[]) throws Exception
  {
    System.out.println("BEGIN");
    Map<String, Object> map = get("https://209.225.11.99/gateway/jalive", 1000, 5000, null);
    System.out.println("map="+map);
  }
}
TOP

Related Classes of com.gateway.util.ProviderClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.