List of usage examples for org.apache.http.client.methods HttpPut getEntity
public HttpEntity getEntity()
From source file:com.aliyun.oss.common.comm.HttpFactoryTest.java
@Test public void testCreateHttpRequest() throws Exception { ExecutionContext context = new ExecutionContext(); String url = "http://127.0.0.1"; String content = "This is a test request"; byte[] contentBytes = null; try {/* ww w.ja v a 2 s. c o m*/ contentBytes = content.getBytes(context.getCharset()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); fail(e.getMessage()); } HttpRequestFactory factory = new HttpRequestFactory(); ServiceClient.Request request = new ServiceClient.Request(); request.setUrl(url); HttpRequestBase httpRequest = null; // GET request.setMethod(HttpMethod.GET); httpRequest = factory.createHttpRequest(request, context); HttpGet getMethod = (HttpGet) httpRequest; assertEquals(url, getMethod.getURI().toString()); // DELETE request.setMethod(HttpMethod.DELETE); httpRequest = factory.createHttpRequest(request, context); HttpDelete delMethod = (HttpDelete) httpRequest; assertEquals(url, delMethod.getURI().toString()); // HEAD request.setMethod(HttpMethod.HEAD); httpRequest = factory.createHttpRequest(request, context); HttpHead headMethod = (HttpHead) httpRequest; assertEquals(url, headMethod.getURI().toString()); //POST request.setContent(new ByteArrayInputStream(contentBytes)); request.setContentLength(contentBytes.length); request.setMethod(HttpMethod.POST); httpRequest = factory.createHttpRequest(request, context); HttpPost postMethod = (HttpPost) httpRequest; assertEquals(url, postMethod.getURI().toString()); HttpEntity entity = postMethod.getEntity(); try { assertEquals(content, readSting(entity.getContent())); } catch (IllegalStateException e) { e.printStackTrace(); fail(e.getMessage()); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); } //PUT request.setContent(new ByteArrayInputStream(contentBytes)); request.setContentLength(contentBytes.length); request.setMethod(HttpMethod.PUT); httpRequest = factory.createHttpRequest(request, context); HttpPut putMethod = (HttpPut) httpRequest; assertEquals(url, putMethod.getURI().toString()); entity = putMethod.getEntity(); try { assertEquals(content, readSting(entity.getContent())); } catch (IllegalStateException e) { e.printStackTrace(); fail(e.getMessage()); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); } try { request.close(); } catch (IOException e) { } }
From source file:tech.sirwellington.alchemy.http.AlchemyRequestMapperTest.java
@Test public void testPutWithBody() throws Exception { instance = AlchemyRequestMapper.PUT; when(request.hasBody()).thenReturn(true); HttpUriRequest result = instance.convertToApacheRequest(request); assertThat(result, instanceOf(HttpPut.class)); HttpPut put = (HttpPut) result; assertEntity(put.getEntity()); }
From source file:nl.esciencecenter.octopus.webservice.api.SandboxedJobTest.java
@Test public void testSetStatus_ChangedWithCallback_HttpClientExecute() throws UnsupportedEncodingException, ClientProtocolException, IOException, URISyntaxException { JobStatus rstatus = new JobStatusImplementation(ojob, "RUNNING", null, null, true, false, null); pollIterations = 10;//from w w w . jav a 2s.com job = new SandboxedJob(sandbox, ojob, request, httpClient, rstatus, pollIterations); job.setStatus(this.status); assertThat(job.getStatus()).isEqualTo(this.status); ArgumentCaptor<HttpPut> argument = ArgumentCaptor.forClass(HttpPut.class); verify(httpClient).execute(argument.capture()); HttpPut callback_request = argument.getValue(); assertThat(callback_request.getURI()).isEqualTo(new URI("http://localhost/job/status")); assertThat(callback_request.getEntity().getContentType().getValue()) .isEqualTo("application/json; charset=UTF-8"); String body = EntityUtils.toString(callback_request.getEntity(), Consts.UTF_8); assertThat(body).isEqualTo(jsonFixture("fixtures/status.done.json")); }
From source file:org.fcrepo.client.impl.FedoraRepositoryImplTest.java
@Test public void testCreateContentPutMethod() throws Exception { final String path = "/test"; final InputStream in = new ByteArrayInputStream("foo".getBytes()); final String mime = "image/jpeg"; final String fn = "image.jpg"; final String chk = "urn:sha1:c6bbf022f8f19d09106622aa912218417723f543"; final URI checksumURI = new URI(chk); final FedoraContent cont = new FedoraContent().setContent(in).setContentType(mime).setFilename(fn) .setChecksum(checksumURI);//from w ww.j ava 2 s . c om final HttpPut put = httpHelper.createContentPutMethod(path, null, cont); assertEquals(testRepositoryUrl + path + "?checksum=" + chk, put.getURI().toString()); assertEquals(in, put.getEntity().getContent()); assertEquals(mime, put.getFirstHeader("Content-Type").getValue()); assertEquals("attachment; filename=\"" + fn + "\"", put.getFirstHeader("Content-Disposition").getValue()); }
From source file:org.fcrepo.client.utils.HttpHelperTest.java
@Test public void testCreateTriplesPutMethod() throws Exception { final String dummyContent = "dummy content"; final InputStream in = new ByteArrayInputStream(dummyContent.getBytes()); final String mimeType = "text/rdf+n3"; final HttpPut put = helper.createTriplesPutMethod("/foo", in, mimeType); assertEquals(repoURL + "/foo", put.getURI().toString()); assertEquals(mimeType, put.getFirstHeader("Content-Type").getValue()); assertEquals(dummyContent, IOUtils.toString(put.getEntity().getContent())); }
From source file:org.fcrepo.client.utils.HttpHelperTest.java
@Test public void testCreateContentPutMethod() throws Exception { final String dummyContent = "dummy content"; final InputStream in = new ByteArrayInputStream(dummyContent.getBytes()); final String mimeType = "text/plain"; final String filename = "dummy.txt"; final FedoraContent content = new FedoraContent().setContent(in).setContentType(mimeType) .setFilename(filename);//from ww w .j a v a 2 s . c om final HttpPut put = helper.createContentPutMethod("/foo", params, content); assertEquals(repoURL + "/foo?foo=bar&foo=baz", put.getURI().toString()); assertEquals(mimeType, put.getFirstHeader("Content-Type").getValue()); assertEquals(dummyContent, IOUtils.toString(put.getEntity().getContent())); assertEquals("attachment; filename=\"" + filename + "\"", put.getFirstHeader("Content-Disposition").getValue().toString()); }
From source file:br.com.vpsa.oauth2android.token.MacTokenTypeDefinition.java
@Override public HttpPut getAuthorizedHttpPut(List<NameValuePair> additionalParameter, String requestUri, Server server, Client client, boolean useHeader) throws InvalidTokenTypeException { String ext = castToken(client).getExt(); String url = server.getResourceServer() + (requestUri.startsWith("/") ? requestUri : "/" + requestUri); HttpPut httpPut = new HttpPut(url); String body = ""; if (additionalParameter != null && !additionalParameter.isEmpty()) { httpPut.addHeader("Content-Type", "application/x-www-form-urlencoded"); try {/*from ww w . java 2 s . c o m*/ httpPut.setEntity(new UrlEncodedFormEntity(additionalParameter)); try { body = EntityUtils.toString(httpPut.getEntity()); } catch (IOException ex) { Logger.getLogger(MacTokenTypeDefinition.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(MacTokenTypeDefinition.class.getName()).log(Level.SEVERE, null, ex); } } catch (UnsupportedEncodingException ex) { // TODO: invalid url exceptions ???? } } Header header = new BasicHeader("Authorization", constructAuthorization(client, server, httpPut.getRequestLine().getUri(), Connection.HTTP_METHOD_PUT, ext, body)); httpPut.addHeader(header); return httpPut; }
From source file:com.intel.iotkitlib.LibHttp.HttpPutTask.java
private CloudResponse doWork(HttpClient httpClient, String url) { try {//from www . j av a 2 s .co m HttpContext localContext = new BasicHttpContext(); HttpPut httpPut = new HttpPut(url); if (httpBody != null) { //setting HTTP body in entity StringEntity bodyEntity = new StringEntity(httpBody, "UTF-8"); httpPut.setEntity(bodyEntity); } //adding headers one by one for (NameValuePair nvp : headerList) { httpPut.addHeader(nvp.getName(), nvp.getValue()); } if (debug) { Log.e(TAG, "URI is : " + httpPut.getURI()); Header[] headers = httpPut.getAllHeaders(); for (int i = 0; i < headers.length; i++) { Log.e(TAG, "Header " + i + " is :" + headers[i].getName() + ":" + headers[i].getValue()); } if (httpBody != null) { BufferedReader reader123 = new BufferedReader( new InputStreamReader(httpPut.getEntity().getContent(), HTTP.UTF_8)); StringBuilder builder123 = new StringBuilder(); for (String line = null; (line = reader123.readLine()) != null;) { builder123.append(line).append("\n"); } Log.e(TAG, "Body is :" + builder123.toString()); } } HttpResponse response = httpClient.execute(httpPut, localContext); if (response != null && response.getStatusLine() != null) { if (debug) Log.d(TAG, "response: " + response.getStatusLine().getStatusCode()); } HttpEntity responseEntity = response != null ? response.getEntity() : null; StringBuilder builder = new StringBuilder(); if (responseEntity != null) { BufferedReader reader = new BufferedReader( new InputStreamReader(responseEntity.getContent(), HTTP.UTF_8)); for (String line = null; (line = reader.readLine()) != null;) { builder.append(line).append("\n"); } if (debug) Log.d(TAG, "Response received is :" + builder.toString()); } CloudResponse cloudResponse = new CloudResponse(); if (response != null) { cloudResponse.code = response.getStatusLine().getStatusCode(); } cloudResponse.response = builder.toString(); return cloudResponse; } catch (java.net.ConnectException cEx) { Log.e(TAG, cEx.getMessage()); return null; } catch (Exception e) { Log.e(TAG, e.toString()); e.printStackTrace(); return null; } }
From source file:com.intel.iotkitlib.http.HttpPutTask.java
public CloudResponse doSync(final String url) { HttpClient httpClient = new DefaultHttpClient(); try {/*w w w .ja va 2s .c o m*/ HttpContext localContext = new BasicHttpContext(); HttpPut httpPut = new HttpPut(url); if (httpBody != null) { //setting HTTP body in entity StringEntity bodyEntity = new StringEntity(httpBody, "UTF-8"); httpPut.setEntity(bodyEntity); } //adding headers one by one for (NameValuePair nvp : headerList) { httpPut.addHeader(nvp.getName(), nvp.getValue()); } if (debug) { Log.e(TAG, "URI is : " + httpPut.getURI()); Header[] headers = httpPut.getAllHeaders(); for (int i = 0; i < headers.length; i++) { Log.e(TAG, "Header " + i + " is :" + headers[i].getName() + ":" + headers[i].getValue()); } if (httpBody != null) { BufferedReader reader123 = new BufferedReader( new InputStreamReader(httpPut.getEntity().getContent(), HTTP.UTF_8)); StringBuilder builder123 = new StringBuilder(); for (String line = null; (line = reader123.readLine()) != null;) { builder123.append(line).append("\n"); } Log.e(TAG, "Body is :" + builder123.toString()); } } HttpResponse response = httpClient.execute(httpPut, localContext); if (response != null && response.getStatusLine() != null) { if (debug) Log.d(TAG, "response: " + response.getStatusLine().getStatusCode()); } HttpEntity responseEntity = response != null ? response.getEntity() : null; StringBuilder builder = new StringBuilder(); if (responseEntity != null) { BufferedReader reader = new BufferedReader( new InputStreamReader(responseEntity.getContent(), HTTP.UTF_8)); for (String line = null; (line = reader.readLine()) != null;) { builder.append(line).append("\n"); } if (debug) Log.d(TAG, "Response received is :" + builder.toString()); } CloudResponse cloudResponse = new CloudResponse(); if (response != null) { cloudResponse.code = response.getStatusLine().getStatusCode(); } cloudResponse.response = builder.toString(); return cloudResponse; } catch (java.net.ConnectException cEx) { Log.e(TAG, cEx.getMessage()); return new CloudResponse(false, cEx.getMessage()); } catch (Exception e) { Log.e(TAG, e.toString()); e.printStackTrace(); return new CloudResponse(false, e.getMessage()); } }
From source file:org.sahli.asciidoc.confluence.publisher.client.http.HttpRequestFactoryTest.java
@Test public void updatePageRequest_withValidParameters_returnsValidHttpPutRequest() throws Exception { // arrange//from w w w . j a va 2s. c o m String contentId = "1234"; String ancestorId = "1"; String title = "title"; String content = "content"; Integer version = 2; // act HttpPut updatePageRequest = this.httpRequestFactory.updatePageRequest(contentId, ancestorId, title, content, version); // assert assertThat(updatePageRequest.getMethod(), is("PUT")); assertThat(updatePageRequest.getURI().toString(), is(CONFLUENCE_REST_API_ENDPOINT + "/content/" + contentId)); assertThat(updatePageRequest.getFirstHeader("Content-Type").getValue(), is(APPLICATION_JSON_UTF8)); String jsonPayload = inputStreamAsString(updatePageRequest.getEntity().getContent()); String expectedJsonPayload = fileContent(Paths.get(CLASS_LOCATION, "update-page-request.json").toString()); assertThat(jsonPayload, SameJsonAsMatcher.isSameJsonAs(expectedJsonPayload)); }