List of usage examples for org.apache.http.message BasicStatusLine BasicStatusLine
public BasicStatusLine(ProtocolVersion protocolVersion, int i, String str)
From source file:org.esigate.servlet.impl.ResponseCapturingWrapper.java
@Override public void sendRedirect(String location) { httpClientResponse.setStatusLine(/*from w ww . j a v a 2 s.co m*/ new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Temporary redirect")); httpClientResponse.setHeader(HttpHeaders.LOCATION, location); }
From source file:co.cask.cdap.client.rest.RestClientTest.java
@Test public void testNotImplementedResponseCodeAnalysis() { StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_NOT_IMPLEMENTED, "Not Implemented"); when(response.getStatusLine()).thenReturn(statusLine); try {/*from www . j av a 2 s . c o m*/ RestClient.responseCodeAnalysis(response); Assert.fail("Expected UnsupportedOperationException"); } catch (UnsupportedOperationException e) { } verify(response).getStatusLine(); }
From source file:com.zlk.bigdemo.android.volley.toolbox.HurlStack.java
private HttpResponse performNormalREquest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); HashMap<String, String> map = new HashMap<String, String>(); map.putAll(request.getHeaders());/*from w w w . ja va 2 s .c o m*/ map.putAll(additionalHeaders); if (mUrlRewriter != null) { String rewritten = mUrlRewriter.rewriteUrl(url); if (rewritten == null) { throw new IOException("URL blocked by rewriter: " + url); } url = rewritten; } URL parsedUrl = new URL(url); HttpURLConnection connection = openConnection(parsedUrl, request); for (String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } setConnectionParametersForRequest(connection, request); // Initialize HttpResponse with data from the HttpURLConnection. ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); int responseCode = connection.getResponseCode(); if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could not be retrieved. // Signal to the caller that something was wrong with the connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } return response; }
From source file:org.sentilo.common.test.rest.RESTClientImplTest.java
@Test public void getWithParameters() throws Exception { final String path = "/data"; final String responseContent = "Lorem ipsum"; final StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_0, HttpStatus.SC_OK, ""); when(httpClient.execute(notNull(HttpGet.class))).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(httpResponse.getEntity()).thenReturn(new StringEntity(responseContent)); final String result = restClient.get(path, requestParameters); verify(httpClient).execute(any(HttpGet.class)); Assert.assertEquals(responseContent, result); }
From source file:com.navercorp.volleyextensions.volleyer.multipart.stack.MultipartHurlStack.java
@Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); HashMap<String, String> map = new HashMap<String, String>(); map.putAll(request.getHeaders());//ww w .j a va 2 s.co m map.putAll(additionalHeaders); if (mUrlRewriter != null) { String rewritten = mUrlRewriter.rewriteUrl(url); if (rewritten == null) { throw new IOException("URL blocked by rewriter: " + url); } url = rewritten; } URL parsedUrl = new URL(url); HttpURLConnection connection = openConnection(parsedUrl, request); for (String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } setConnectionParametersForRequest(connection, request); // Initialize HttpResponse with data from the HttpURLConnection. ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); int responseCode = connection.getResponseCode(); if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could not be retrieved. // Signal to the caller that something was wrong with the connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } return response; }
From source file:org.wildfly.camel.test.hipchat.HipchatConsumerIntegrationTest.java
@Test public void sendInOnly() throws Exception { CamelContext camelctx = createCamelContext(); MockEndpoint result = camelctx.getEndpoint("mock:result", MockEndpoint.class); result.expectedMessageCount(1);/*from ww w . j a v a 2 s. c om*/ camelctx.start(); try { String expectedResponse = "{\n" + " \"items\" : [\n" + " {\n" // + " \"date\" : \"2015-01-19T22:07:11.030740+00:00\",\n" // + " \"from\" : {\n" // + " \"id\" : 1647095,\n" // + " \"links\" : {\n" // + " \"self\" : \"https://api.hipchat.com/v2/user/1647095\"\n" // + " },\n" // + " \"mention_name\" : \"notifier\",\n" // + " \"name\" : \"Message Notifier\"\n" // + " },\n" // + " \"id\" : \"6567c6f7-7c1b-43cf-bed0-792b1d092919\",\n" // + " \"mentions\" : [ ],\n" // + " \"message\" : \"Unit test Alert\",\n" // + " \"type\" : \"message\"\n" // + " }\n" // + " ],\n" // + " \"links\" : {\n" // + " \"self\" : \"https://api.hipchat.com/v2/user/%40ShreyasPurohit/history/latest\"\n" // + " },\n" // + " \"maxResults\" : 1,\n" // + " \"startIndex\" : 0\n" // + "}"; HttpEntity mockHttpEntity = mock(HttpEntity.class); when(mockHttpEntity.getContent()) .thenReturn(new ByteArrayInputStream(expectedResponse.getBytes(StandardCharsets.UTF_8))); when(closeableHttpResponse.getEntity()).thenReturn(mockHttpEntity); when(closeableHttpResponse.getStatusLine()) .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "")); result.assertIsSatisfied(); assertCommonResultExchange(result.getExchanges().get(0)); } finally { camelctx.stop(); } }
From source file:org.apache.nifi.processors.aws.wag.TestInvokeAmazonGatewayApiMock.java
@Test public void testSendAttributes() throws Exception { HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK")); BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(new ByteArrayInputStream("test payload".getBytes())); resp.setEntity(entity);/*from w w w.j a va 2 s. c o m*/ Mockito.doReturn(resp).when(mockSdkClient).execute(any(HttpUriRequest.class), any(HttpContext.class)); // add dynamic property runner.setProperty("dynamicHeader", "yes!"); // set the regex runner.setProperty(InvokeAWSGatewayApi.PROP_ATTRIBUTES_TO_SEND, "F.*"); final Map<String, String> attributes = new HashMap<>(); attributes.put(CoreAttributes.MIME_TYPE.key(), "application/plain-text"); attributes.put("Foo", "Bar"); runner.enqueue("Hello".getBytes("UTF-8"), attributes); // execute runner.assertValid(); runner.run(1); Mockito.verify(mockSdkClient, times(1)).execute(argThat(new RequestMatcher<HttpUriRequest>(x -> { return x.getMethod().equals("GET") && x.getFirstHeader("x-api-key").getValue().equals("abcd") && x.getFirstHeader("Authorization").getValue().startsWith("AWS4") && x.getFirstHeader("dynamicHeader").getValue().equals("yes!") && x.getFirstHeader("Foo").getValue().equals("Bar") && x.getURI().toString().equals("https://foobar.execute-api.us-east-1.amazonaws.com/TEST"); })), any(HttpContext.class)); // check runner.assertTransferCount(InvokeAWSGatewayApi.REL_SUCCESS_REQ, 1); runner.assertTransferCount(InvokeAWSGatewayApi.REL_RESPONSE, 1); runner.assertTransferCount(InvokeAWSGatewayApi.REL_RETRY, 0); runner.assertTransferCount(InvokeAWSGatewayApi.REL_NO_RETRY, 0); runner.assertTransferCount(InvokeAWSGatewayApi.REL_FAILURE, 0); final List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(InvokeAWSGatewayApi.REL_RESPONSE); final MockFlowFile ff0 = flowFiles.get(0); ff0.assertAttributeEquals(InvokeAWSGatewayApi.STATUS_CODE, "200"); ff0.assertContentEquals("test payload"); ff0.assertAttributeExists(InvokeAWSGatewayApi.TRANSACTION_ID); ff0.assertAttributeEquals(InvokeAWSGatewayApi.RESOURCE_NAME_ATTR, "/TEST"); }
From source file:com.joyent.manta.client.multipart.ServerSideMultipartManagerTest.java
public void canUploadPartSuccess() throws IOException { final UUID uploadId = UUID.randomUUID(); final String partsDirectory = "/test/uploads/a/abcdef"; final String path = "/test/stor/object"; final ServerSideMultipartUpload upload = new ServerSideMultipartUpload(uploadId, path, partsDirectory); final String etag = UUID.randomUUID().toString(); final ServerSideMultipartManager mngr = buildMockManager( new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NO_CONTENT, "No Content"), "", (response) -> when(response.getFirstHeader(HttpHeaders.ETAG)) .thenReturn(new BasicHeader(HttpHeaders.ETAG, etag))); final MantaMultipartUploadPart part = mngr.uploadPart(upload, 1, new byte[0]); Assert.assertEquals(part.getObjectPath(), path); Assert.assertEquals(part.getPartNumber(), 1); Assert.assertEquals(part.getEtag(), etag); }
From source file:org.apache.camel.component.hipchat.HipchatComponentConsumerTest.java
@Test public void sendInOnlyNoResponse() throws Exception { result.expectedMessageCount(0);//from w ww .j a va 2 s . c o m HttpEntity mockHttpEntity = mock(HttpEntity.class); when(mockHttpEntity.getContent()).thenReturn(null); when(closeableHttpResponse.getEntity()).thenReturn(mockHttpEntity); when(closeableHttpResponse.getStatusLine()) .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "")); assertMockEndpointsSatisfied(); }
From source file:com.baasbox.android.net.OkClient.java
@Override public HttpResponse execute(HttpRequest request) throws BaasException { String contentType = request.headers.get("Content-Type"); Request.Builder okRequestBuilder = new Request.Builder(); boolean contentLengthSet = false; for (String name : request.headers.keySet()) { if (!contentLengthSet && "Content-Length".equals(name)) { contentLengthSet = true;//from w w w . jav a2s .c om } okRequestBuilder.addHeader(name, request.headers.get(name)); } if (!contentLengthSet) { okRequestBuilder.addHeader("Content-Length", "0"); } RequestBody rb; switch (request.method) { case HttpRequest.GET: okRequestBuilder.get(); break; case HttpRequest.POST: rb = buildBody(contentType, request.body); //InputRequestBody rb = new InputRequestBody(contentType,request.body); okRequestBuilder.post(rb); break; case HttpRequest.PUT: rb = buildBody(contentType, request.body); okRequestBuilder.put(rb); break; case HttpRequest.DELETE: okRequestBuilder.delete(); break; case HttpRequest.PATCH: rb = buildBody(contentType, request.body); okRequestBuilder.patch(rb); break; } okRequestBuilder.url(request.url); Request okRequest = okRequestBuilder.build(); try { Response resp = mOkHttp.newCall(okRequest).execute(); Protocol protocol = resp.protocol(); ProtocolVersion pv; switch (protocol) { case HTTP_1_0: pv = new ProtocolVersion("HTTP", 1, 0); break; case HTTP_1_1: pv = new ProtocolVersion("HTTP", 1, 1); break; case HTTP_2: pv = new ProtocolVersion("HTTP", 2, 0); break; case SPDY_3: pv = new ProtocolVersion("spdy", 3, 1); break; default: throw new BaasIOException("Invalid protocol"); } StatusLine line = new BasicStatusLine(pv, resp.code(), resp.message()); BasicHttpResponse bresp = new BasicHttpResponse(line); bresp.setEntity(asEntity(resp)); for (String name : resp.headers().names()) { String val = resp.headers().get(name); bresp.addHeader(name, val); } return bresp; } catch (IOException e) { throw new BaasIOException(e); } }