Search in sources :

Example 66 with Map

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();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ElasticsearchException(org.elasticsearch.ElasticsearchException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) Maps.newHashMap(org.elasticsearch.common.collect.Maps.newHashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 67 with Map

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);
    }
}
Also used : ElasticsearchIllegalArgumentException(org.elasticsearch.ElasticsearchIllegalArgumentException) Map(java.util.Map) NoClassSettingsException(org.elasticsearch.common.settings.NoClassSettingsException) Settings(org.elasticsearch.common.settings.Settings)

Example 68 with Map

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;
}
Also used : HttpHealthCheck(com.spotify.helios.common.descriptors.HttpHealthCheck) TcpHealthCheck(com.spotify.helios.common.descriptors.TcpHealthCheck) HttpHealthCheck(com.spotify.helios.common.descriptors.HttpHealthCheck) ExecHealthCheck(com.spotify.helios.common.descriptors.ExecHealthCheck) TcpHealthCheck(com.spotify.helios.common.descriptors.TcpHealthCheck) HealthCheck(com.spotify.helios.common.descriptors.HealthCheck) ExecHealthCheck(com.spotify.helios.common.descriptors.ExecHealthCheck) Map(java.util.Map)

Example 69 with Map

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());
}
Also used : Instant(org.joda.time.Instant) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) Map(java.util.Map) URI(java.net.URI) Test(org.junit.Test)

Example 70 with Map

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;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) List(java.util.List) Map(java.util.Map) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

Map (java.util.Map)18519 HashMap (java.util.HashMap)11152 ArrayList (java.util.ArrayList)4318 List (java.util.List)3718 Test (org.junit.Test)3076 Set (java.util.Set)2132 HashSet (java.util.HashSet)1884 IOException (java.io.IOException)1729 LinkedHashMap (java.util.LinkedHashMap)1605 Iterator (java.util.Iterator)1517 TreeMap (java.util.TreeMap)1160 ImmutableMap (com.google.common.collect.ImmutableMap)1043 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)869 File (java.io.File)788 Collection (java.util.Collection)765 Collectors (java.util.stream.Collectors)652 ConcurrentMap (java.util.concurrent.ConcurrentMap)412 LinkedList (java.util.LinkedList)409 Collections (java.util.Collections)388 InputStream (java.io.InputStream)362