use of java.util.Map in project elasticsearch-jetty by sonian.
the class HttpClient method request.
public HttpClientResponse request(String method, String path, byte[] data) {
ObjectMapper mapper = new ObjectMapper();
URL url;
try {
url = new URL(baseUrl, path);
} catch (MalformedURLException e) {
throw new ElasticsearchException("Cannot parse " + path, e);
}
HttpURLConnection urlConnection;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(method);
if (data != null) {
urlConnection.setDoOutput(true);
}
if (encodedAuthorization != null) {
urlConnection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
}
urlConnection.connect();
} catch (IOException e) {
throw new ElasticsearchException("", e);
}
if (data != null) {
OutputStream outputStream = null;
try {
outputStream = urlConnection.getOutputStream();
outputStream.write(data);
} catch (IOException e) {
throw new ElasticsearchException("", e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
throw new ElasticsearchException("", e);
}
}
}
}
int errorCode = -1;
try {
errorCode = urlConnection.getResponseCode();
InputStream inputStream = urlConnection.getInputStream();
return new HttpClientResponse(mapper.readValue(inputStream, Map.class), errorCode, null);
} catch (IOException e) {
InputStream errStream = urlConnection.getErrorStream();
String body = null;
try {
body = Streams.copyToString(new InputStreamReader(errStream));
} catch (IOException e1) {
throw new ElasticsearchException("problem reading error stream", e1);
}
Map m = newHashMap();
m.put("body", body);
return new HttpClientResponse(m, errorCode, e);
} finally {
urlConnection.disconnect();
}
}
use of java.util.Map in project elasticsearch-jetty by sonian.
the class FilterHttpServerTransportModule method configureFilters.
private void configureFilters() {
MapBinder<String, FilterHttpServerAdapterFactory> filterBinder = MapBinder.newMapBinder(binder(), String.class, FilterHttpServerAdapterFactory.class);
Map<String, Settings> filtersSettings = componentSettings.getGroups("http_filter");
for (Map.Entry<String, Settings> entry : filtersSettings.entrySet()) {
String filterName = entry.getKey();
Settings filterSettings = entry.getValue();
Class<? extends FilterHttpServerAdapter> type = null;
try {
type = filterSettings.getAsClass("type", null, "com.sonian.elasticsearch.http.filter.", "FilterHttpServerAdapter");
} catch (NoClassSettingsException e) {
// Ignore
}
if (type == null) {
throw new ElasticsearchIllegalArgumentException("Http Filter [" + filterName + "] must have a type associated with it");
}
filterBinder.addBinding(filterName).toProvider(FactoryProvider.newFactory(FilterHttpServerAdapterFactory.class, type)).in(Scopes.SINGLETON);
}
}
use of java.util.Map in project helios by spotify.
the class JobValidator method validateJobHealthCheck.
/**
* Validate the Job's health check.
* @param job The Job to check.
* @return A set of error Strings
*/
private Set<String> validateJobHealthCheck(final Job job) {
final HealthCheck healthCheck = job.getHealthCheck();
if (healthCheck == null) {
return emptySet();
}
final Set<String> errors = Sets.newHashSet();
if (healthCheck instanceof ExecHealthCheck) {
final List<String> command = ((ExecHealthCheck) healthCheck).getCommand();
if (command == null || command.isEmpty()) {
errors.add("A command must be defined for `docker exec`-based health checks.");
}
} else if (healthCheck instanceof HttpHealthCheck || healthCheck instanceof TcpHealthCheck) {
final String port;
if (healthCheck instanceof HttpHealthCheck) {
port = ((HttpHealthCheck) healthCheck).getPort();
} else {
port = ((TcpHealthCheck) healthCheck).getPort();
}
final Map<String, PortMapping> ports = job.getPorts();
if (isNullOrEmpty(port)) {
errors.add("A port must be defined for HTTP and TCP health checks.");
} else if (!ports.containsKey(port)) {
errors.add(format("Health check port '%s' not defined in the job. Known ports are '%s'", port, Joiner.on(", ").join(ports.keySet())));
}
}
return errors;
}
use of java.util.Map in project helios by spotify.
the class RetryingRequestDispatcherTest method testSuccessOnRetry.
@Test
public void testSuccessOnRetry() throws Exception {
when(delegate.request(any(URI.class), anyString(), any(byte[].class), Matchers.<Map<String, List<String>>>any())).thenReturn(Futures.<Response>immediateFailedFuture(new IOException())).thenReturn(Futures.<Response>immediateFuture(null));
when(clock.now()).thenReturn(new Instant(0));
dispatcher.request(new URI("http://example.com"), "GET", null, Collections.<String, List<String>>emptyMap());
// Verify the delegate was called twice if it returns successfully on the second try before the
// deadline
verify(delegate, times(2)).request(any(URI.class), anyString(), any(byte[].class), Matchers.<Map<String, List<String>>>any());
}
use of java.util.Map in project helios by spotify.
the class DefaultHttpConnector method connect0.
private HttpURLConnection connect0(final URI ipUri, final String method, final byte[] entity, final Map<String, List<String>> headers, final String endpointHost) throws IOException {
if (log.isTraceEnabled()) {
log.trace("req: {} {} {} {} {} {}", method, ipUri, headers.size(), Joiner.on(',').withKeyValueSeparator("=").join(headers), entity.length, Json.asPrettyStringUnchecked(entity));
} else {
log.debug("req: {} {} {} {}", method, ipUri, headers.size(), entity.length);
}
final HttpURLConnection connection = (HttpURLConnection) ipUri.toURL().openConnection();
handleHttps(connection, endpointHost, hostnameVerifierProvider, extraHttpsHandler);
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setInstanceFollowRedirects(false);
connection.setConnectTimeout(httpTimeoutMillis);
connection.setReadTimeout(httpTimeoutMillis);
for (final Map.Entry<String, List<String>> header : headers.entrySet()) {
for (final String value : header.getValue()) {
connection.addRequestProperty(header.getKey(), value);
}
}
if (entity.length > 0) {
connection.setDoOutput(true);
connection.getOutputStream().write(entity);
}
setRequestMethod(connection, method, connection instanceof HttpsURLConnection);
return connection;
}
Aggregations