All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.uiuc.ncsa.security.servlet.ServiceClient Maven / Gradle / Ivy

There is a newer version: 4.3
Show newest version
package edu.uiuc.ncsa.security.servlet;

import edu.uiuc.ncsa.security.core.exceptions.GeneralException;
import edu.uiuc.ncsa.security.core.util.MyLoggingFacade;
import edu.uiuc.ncsa.security.core.util.Pool;
import edu.uiuc.ncsa.security.util.ssl.SSLConfiguration;
import edu.uiuc.ncsa.security.util.ssl.VerifyingHTTPClientFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

/**
 * This class is a client that talks to a server. It manages a client pool and does a get based on
 * pairs of strings, which are assumed to be of the form {{key1,value1},{key2,value2},...}
 * 

Created by Jeff Gaynor
* on 5/21/12 at 1:43 PM */ public class ServiceClient { SSLConfiguration sslConfiguration; public ServiceClient(URI host, SSLConfiguration sslConfiguration) { this.host = host; this.sslConfiguration = sslConfiguration; } URI host; public URI host(URI... x) { if (0 < x.length) host = x[0]; return host; } Pool clientPool = new Pool() { VerifyingHTTPClientFactory f; public VerifyingHTTPClientFactory getF() { if (f == null) { /*sslConfiguration = new SSLConfiguration(); sslConfiguration.setUseDefaultJavaKeyStore(true); sslConfiguration.setKeystore("/home/ncsa/certs/ashigaru/2012-10-18/ashigaru.ncsa.uiuc.edu.p12"); sslConfiguration.setKeystorePassword("stemEx6z"); sslConfiguration.setKeystoreType("PKCS12");*/ f = new VerifyingHTTPClientFactory(new MyLoggingFacade(getClass().getSimpleName()), sslConfiguration); } return f; } @Override public HttpClient create() { try { return getF().getClient(host.toString()); } catch (IOException e) { throw new GeneralException("Error getting https-aware client"); } } @Override public void destroy(HttpClient HttpClient) { // stateless so nothing to do really. } }; public static String ENCODING = "UTF-8"; public static String encode(String x) throws UnsupportedEncodingException { String xx = URLEncoder.encode(x, ENCODING); return xx; } public static String decode(String x) throws UnsupportedEncodingException { return URLDecoder.decode(x, ENCODING); } public String getRawResponse(Map m) { int size = m.size(); int i = 0; String[][] strings = new String[size][2]; for (Object o : m.keySet()) { Object v = m.get(o); if (v != null) { strings[i][0] = o.toString(); strings[i++][1] = v.toString(); } } return getRawResponse(strings); } protected String getRawResponse(String[][] args) { String getString = host().toString(); boolean firstPass = true; if (args != null && args.length != 0) { for (int i = 0; i < args.length; i++) { if (args[i].length != 0) { try { // We have to encode the string to UTF-8 since we are doing an http GET. // The HTML spec says non-ASCII characters must be escaped some way, but // is not specific, so we have to do this. // Other than this case, // we should not be decoding anything since UTF-8 is the encoding set in the response. getString = getString + (firstPass ? "?" : "&") + args[i][0] + "=" + encode(args[i][1]); if (firstPass) firstPass = false; } catch (UnsupportedEncodingException e) { throw new GeneralException("Error encoding argument", e); } } } } HttpGet httpGet = new HttpGet(getString); HttpClient client = clientPool.pop(); HttpResponse response = null; try { response = client.execute(httpGet); HttpEntity entity1 = response.getEntity(); return EntityUtils.toString(entity1); } catch (IOException e) { throw new GeneralException("Error invoking http client", e); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy