Java Code Examples for org.apache.http.HttpHeaders#CONTENT_TYPE

The following examples show how to use org.apache.http.HttpHeaders#CONTENT_TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeartbeatManagerTest.java    From emissary with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnauthorizedHeartbeat() throws ClientProtocolException, IOException {
    CloseableHttpClient mockClient = mock(CloseableHttpClient.class);
    CloseableHttpResponse mockResponse = mock(CloseableHttpResponse.class);
    HttpEntity mockHttpEntity = mock(HttpEntity.class);
    StatusLine mockStatusLine = mock(StatusLine.class);

    when(mockClient.execute(any(HttpUriRequest.class), any(HttpContext.class))).thenReturn(mockResponse);
    when(mockResponse.getStatusLine()).thenReturn(mockStatusLine);
    when(mockStatusLine.getStatusCode()).thenReturn(401);
    when(mockResponse.getEntity()).thenReturn(mockHttpEntity);
    String responseString = "Unauthorized heartbeat man";
    when(mockHttpEntity.getContent()).thenReturn(IOUtils.toInputStream(responseString));
    BasicHeader header1 = new BasicHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
    Header[] headers = new Header[] {header1};
    when(mockResponse.getHeaders(any())).thenReturn(headers);

    EmissaryClient client = new EmissaryClient(mockClient);

    String fromPlace = "EMISSARY_DIRECTORY_SERVICES.DIRECTORY.STUDY.http://localhost:8001/DirectoryPlace";
    String toPlace = "*.*.*.http://localhost:1233/DirectoryPlace";
    EmissaryResponse response = HeartbeatManager.getHeartbeat(fromPlace, toPlace, client); // use that client
    assertThat(response.getContentString(), containsString("Bad request -> status: 401 message: " + responseString));
}
 
Example 2
Source File: ElasticSearchFilter.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@JsonCreator
public ElasticSearchFilter(@JsonProperty("hostname") String hostname, @JsonProperty("port") int port,
                           @JsonProperty("username") Optional<String> username,
                           @JsonProperty("password") Optional<String> password) {
  Header[] headers = {
    new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
    new BasicHeader("Role", "Read")};
  final RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(hostname, port))
    .setDefaultHeaders(headers);
  if (username.isPresent() && !username.get().isEmpty() && password.isPresent() && !password.get().isEmpty()) {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

    credentialsProvider.setCredentials(
      AuthScope.ANY,
      new UsernamePasswordCredentials(username.get(), password.get())
    );

    restClientBuilder.setHttpClientConfigCallback(b -> b.setDefaultCredentialsProvider(credentialsProvider));
  }
  restClient = restClientBuilder.build();
  mapper = new ObjectMapper();
}
 
Example 3
Source File: ESConfiguration.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
@Bean
public RestHighLevelClient elasticsearchRestConnection(ESConfiguration esConfiguration) {
    String auth = esConfiguration.getServiceElasticsearchUsername() + ":" + esConfiguration.getServiceElasticsearchPassword();
    String authB64;
    try {
        authB64 = Base64.getEncoder().encodeToString(auth.getBytes("utf-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Impossible encoding for user " + esConfiguration.getServiceElasticsearchUsername() + " and password " + esConfiguration.getServiceElasticsearchPassword() + " msg " + e);
    }
    RestClientBuilder builder = RestClient.builder(
            new HttpHost(esConfiguration.getHost(), Integer.valueOf(esConfiguration.getPort()), "http"));
    Header[] defaultHeaders = new Header[]{
            new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
            new BasicHeader("cluster.name", esConfiguration.getClusterName()),
            new BasicHeader(HttpHeaders.AUTHORIZATION, "Basic " + authB64)
    };
    builder.setDefaultHeaders(defaultHeaders);
    builder.setMaxRetryTimeoutMillis(esConfiguration.getSocketTimeout() * 1000);
    builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
        @Override
        public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
            return requestConfigBuilder
                    .setConnectionRequestTimeout(connectionRequestTimeout)
                    .setConnectTimeout(esConfiguration.getConnectionTimeout() * 1000)
                    .setSocketTimeout(esConfiguration.getSocketTimeout() * 1000);
        }
    });
    return new RestHighLevelClient(builder);
}
 
Example 4
Source File: Indexer.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns a basic HTTP write headers
 * 
 * 
 * @return headers
 */
private static Header[] getWriteHeaders() {

	Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
			new BasicHeader("Role", "Write")};
	return headers;
}
 
Example 5
Source File: Indexer.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns a basic HTTP read headers
 * 
 * 
 * @return headers
 */
private static Header[] getReadHeaders() {

	Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
			new BasicHeader("Role", "Read")};
	return headers;
}
 
Example 6
Source File: HttpClientHeadersLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void givenConfigOnClient_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
    final Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
    final List<Header> headers = Lists.newArrayList(header);
    client = HttpClients.custom().setDefaultHeaders(headers).build();
    final HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build();
    response = client.execute(request);
}
 
Example 7
Source File: AnomalyDetectorRestTestCase.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
public ToXContentObject[] getAnomalyDetector(String detectorId, boolean returnJob) throws IOException {
    BasicHeader header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
    return getAnomalyDetector(detectorId, header, returnJob);
}