blob: 8e607614efc923b103efaef0ce943f8aaf4dac4f [file] [log] [blame]
package org.eclipse.fx.travisci.client.impl;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
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.Collection;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.eclipse.fx.travisci.client.BuildsEndpoint;
import org.eclipse.fx.travisci.client.Endpoint;
import org.eclipse.fx.travisci.client.TravisCIClient;
public class TravisCIClientImpl implements TravisCIClient {
private final String hostUri;
private final String applicationName;
private CloseableHttpClient client;
public TravisCIClientImpl(String applicationName, String hostUri) {
this.hostUri = hostUri;
this.applicationName = applicationName;
}
public <E extends Endpoint> E createEndpoint(Class<E> endpoint) {
if (endpoint == BuildsEndpoint.class) {
return (E) new BuildsEndpointImpl(this);
}
return null;
}
private CloseableHttpClient getClient() {
if( client == null ) {
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(new KeyManager[0], new TrustManager[] { new NoOpTrustManager() }, new SecureRandom());
client = HttpClients.custom().setSslcontext(sslcontext).build();
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (KeyManagementException e) {
throw new IllegalStateException(e);
}
}
return client;
}
public final String sendGetRequest(String servicePath, Map<String, Object> parameters) {
try {
URIBuilder builder = new URIBuilder().setScheme("https").setHost(hostUri).setPath(servicePath);
for (Entry<String, Object> p : parameters.entrySet()) {
if (p.getValue() instanceof Collection<?>) {
for (Object o : (Collection<?>) p.getValue()) {
builder.addParameter(p.getKey(), o.toString());
}
} else {
builder.addParameter(p.getKey(), p.getValue().toString());
}
}
URI uri = builder.build();
System.err.println(uri);
HttpGet request = new HttpGet(uri);
request.setHeader("User-Agent", applicationName);
request.setHeader("Accept", "application/vnd.travis-ci.2+json");
CloseableHttpResponse response = getClient().execute(request);
StatusLine statusLine = response.getStatusLine();
int rv = statusLine.getStatusCode();
if (rv >= 200 && rv < 300) {
return EntityUtils.toString(response.getEntity());
}
throw new IllegalStateException("Request failed with HTTP-Status '" + statusLine + "'");
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
} catch (ClientProtocolException e) {
throw new IllegalStateException(e);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
private static class NoOpTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
}