List of usage examples for org.apache.http.client.methods HttpUriRequest setHeader
void setHeader(String str, String str2);
From source file:com.asquera.elasticsearch.plugins.http.auth.integration.EmptyWhitelistIntegrationTest.java
@Test public void localhostClientNotBasicAuthenticated() throws Exception { HttpUriRequest request = httpRequest(); String credentials = "admin:wrong"; request.setHeader("Authorization", "Basic " + Base64.encodeBytes(credentials.getBytes())); CloseableHttpResponse response = closeableHttpClient().execute(request); assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.UNAUTHORIZED.getStatus())); }
From source file:com.asquera.elasticsearch.plugins.http.auth.integration.EmptyWhitelistIntegrationTest.java
@Test public void localhostClientIsBasicAuthenticated() throws Exception { HttpUriRequest request = httpRequest(); String credentials = "admin:admin_pw"; request.setHeader("Authorization", "Basic " + Base64.encodeBytes(credentials.getBytes())); CloseableHttpResponse response = closeableHttpClient().execute(request); assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); }
From source file:com.asquera.elasticsearch.plugins.http.auth.integration.EmptyWhitelistIntegrationTest.java
@Test public void localhostClientIsBasicAuthenticatedPassingXForward() throws Exception { HttpUriRequest request = httpRequest(); String credentials = "admin:admin_pw"; request.setHeader("Authorization", "Basic " + Base64.encodeBytes(credentials.getBytes())); request.setHeader("X-Forwarded-For", "1.1.1.1"); CloseableHttpResponse response = closeableHttpClient().execute(request); assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); }
From source file:at.ac.tuwien.infosys.jcloudscale.datastore.rest.RequestHandlerImpl.java
private void setContentType(HttpUriRequest httpUriRequest, String contentType) { if (contentType != null) { httpUriRequest.setHeader(CONTENT_TYPE_FIELD, contentType); }/*from w w w .ja v a 2 s . c om*/ }
From source file:com.foundationdb.http.CrossOriginITBase.java
@Test public void simpleMethod() throws Exception { URI uri = new URI("http", getUserInfo(), "localhost", port, entityEndpoint(), null, null); HttpUriRequest request = new HttpGet(uri); request.setHeader("Origin", ORIGIN); response = client.execute(request);/*from w w w. ja v a 2 s . c o m*/ assertEquals("status", HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); assertEquals("Allow-Origin", ORIGIN, headerValue(response, HEADER_ALLOW_ORIGIN)); }
From source file:com.foundationdb.http.CrossOriginITBase.java
@Test public void nonSimpleMethod() throws Exception { URI uri = new URI("http", getUserInfo(), "localhost", port, entityEndpoint() + "/1", null, null); HttpUriRequest request = new HttpDelete(uri); request.setHeader("Origin", ORIGIN); response = client.execute(request);//from w w w . java2 s . c om assertEquals("status", HttpStatus.SC_NO_CONTENT, response.getStatusLine().getStatusCode()); assertEquals("Allow-Origin", ORIGIN, headerValue(response, HEADER_ALLOW_ORIGIN)); }
From source file:com.foundationdb.http.CrossOriginITBase.java
@Test public void preFlightAllowedMethod() throws Exception { URI uri = new URI("http", null /*preflight requires no auth*/, "localhost", port, entityEndpoint(), null, null);/*w w w . j a v a 2s . c om*/ HttpUriRequest request = new HttpOptions(uri); request.setHeader("Origin", ORIGIN); request.setHeader("Access-Control-Request-Method", "PUT"); response = client.execute(request); assertEquals("status", HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); assertEquals("Allow-Origin", ORIGIN, headerValue(response, HEADER_ALLOW_ORIGIN)); }
From source file:com.foundationdb.http.CrossOriginITBase.java
@Test public void preFlightDisallowedMethod() throws Exception { URI uri = new URI("http", null /*preflight requires no auth*/, "localhost", port, entityEndpoint(), null, null);// w w w. j a v a 2 s .c o m HttpUriRequest request = new HttpOptions(uri); request.setHeader("Origin", ORIGIN); request.setHeader("Access-Control-Request-Method", "DELETE"); response = client.execute(request); assertEquals("status", HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); assertEquals("Allow-Origin", null, headerValue(response, HEADER_ALLOW_ORIGIN)); }
From source file:org.apache.hawq.ranger.integration.service.tests.common.RESTClient.java
private String executeRequest(HttpUriRequest request) throws IOException { LOG.debug("--> request URI = " + request.getURI()); request.setHeader("Authorization", AUTH_HEADER); request.setHeader("Content-Type", ContentType.APPLICATION_JSON.toString()); CloseableHttpResponse response = httpClient.execute(request); String payload = null;//from w w w .java2 s . c o m try { int responseCode = response.getStatusLine().getStatusCode(); LOG.debug("<-- response code = " + responseCode); HttpEntity entity = response.getEntity(); if (entity != null) { payload = EntityUtils.toString(response.getEntity()); } LOG.debug("<-- response payload = " + payload); if (responseCode == HttpStatus.SC_NOT_FOUND) { throw new ResourceNotFoundException(); } else if (responseCode >= 300) { throw new ClientProtocolException("Unexpected HTTP response code = " + responseCode); } } finally { response.close(); } return payload; }
From source file:org.droidparts.http.wrapper.DefaultHttpClientWrapper.java
public HttpResponse getResponse(HttpUriRequest req) throws HTTPException { for (String name : headers.keySet()) { req.setHeader(name, headers.get(name)); }//from ww w .ja v a 2s .c o m req.setHeader("Accept-Encoding", "gzip,deflate"); try { HttpResponse resp = httpClient.execute(req); int respCode = resp.getStatusLine().getStatusCode(); if (respCode >= 400) { consumeResponse(resp); // TODO read response body throw new HTTPException(respCode, null); } return resp; } catch (IOException e) { throw new HTTPException(e); } }