Example usage for org.apache.http.client.methods HttpPut HttpPut

List of usage examples for org.apache.http.client.methods HttpPut HttpPut

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPut HttpPut.

Prototype

public HttpPut(final String uri) 

Source Link

Usage

From source file:org.apache.hyracks.tests.integration.ApplicationDeploymentAPIIntegrationTest.java

protected void deployApplicationFile(int dataSize, String fileName) throws URISyntaxException, IOException {
    final String deployid = "testApp";

    String path = "/applications/" + deployid + "&" + fileName;
    URI uri = uri(path);/*from   w  ww  .j  av a 2 s  .  co m*/

    byte[] data = new byte[dataSize];
    for (int i = 0; i < data.length; ++i) {
        data[i] = (byte) i;
    }

    HttpClient client = HttpClients.createMinimal();

    // Put the data

    HttpPut put = new HttpPut(uri);
    HttpEntity entity = new ByteArrayEntity(data, ContentType.APPLICATION_OCTET_STREAM);
    put.setEntity(entity);
    client.execute(put);

    // Get it back

    HttpGet get = new HttpGet(uri);
    HttpResponse response = client.execute(get);
    HttpEntity respEntity = response.getEntity();
    Header contentType = respEntity.getContentType();

    // compare results

    Assert.assertEquals(ContentType.APPLICATION_OCTET_STREAM.getMimeType(), contentType.getValue());
    InputStream is = respEntity.getContent();

    for (int i = 0; i < dataSize; ++i) {
        Assert.assertEquals(data[i], (byte) is.read());
    }
    Assert.assertEquals(-1, is.read());
    is.close();
}

From source file:ch.cyberduck.core.onedrive.OneDriveCommonsHttpRequestExecutor.java

@Override
public Upload doPut(final URL url, final Set<RequestHeader> headers) throws IOException {
    final HttpEntityEnclosingRequestBase request = new HttpPut(url.toString());
    return this.doUpload(url, headers, request);
}

From source file:com.nominanuda.web.http.ServletHelperTest.java

@Test
public void testCopyRequest() throws Exception {
    final String msg = "mio";
    final String mediaType = CT_APPLICATION_OCTET_STREAM;
    Server server = startServer(10000, new AbstractHandler() {
        public void handle(String arg0, Request jettyReq, HttpServletRequest servletReq,
                HttpServletResponse arg3) throws IOException, ServletException {
            HttpRequest r = servletHelper.copyRequest(servletReq, false);
            asyncAssertEquals("bar", r.getFirstHeader("X-foo").getValue());
            asyncAssertEquals("PUT", r.getRequestLine().getMethod());
            HttpEntity e = ((HttpEntityEnclosingRequest) r).getEntity();
            asyncAssert(msg.getBytes("UTF-8").length == e.getContentLength(), "length");
            asyncAssert(e.getContentType().getValue().startsWith(mediaType));
            asyncAssertEquals(mediaType, ContentType.get(e).getMimeType());
            asyncAssertEquals(msg, EntityUtils.toString(e));
        }//from  ww  w . j ava  2  s. c  o m
    });
    HttpClient c = buildClient(1);
    HttpPut req = new HttpPut("http://localhost:10000/foo/bar?a=b&a=");
    req.setEntity(new StringEntity(msg, ContentType.create(mediaType, CS_UTF_8)));
    req.addHeader("X-foo", "bar");
    c.execute(req);
    server.stop();
    dumpFailures(System.err);
    Assert.assertFalse(isFailed());
}

From source file:org.talend.dataprep.api.service.command.dataset.MoveDataSet.java

/**
 * Constructor.//from   www.j a  v a 2  s .  c  o m
 *
 * @param dataSetId the requested dataset id.
 * @param folderPath the origin folder othe the dataset
 * @param newFolderPath the new folder path
 * @param newName the new name (optional) 
 */
public MoveDataSet(String dataSetId, String folderPath, String newFolderPath, String newName) {
    super(GenericCommand.DATASET_GROUP);
    execute(() -> {
        try {
            URIBuilder uriBuilder = new URIBuilder(datasetServiceUrl + "/datasets/move/" + dataSetId);
            if (StringUtils.isNotEmpty(folderPath)) {
                uriBuilder.addParameter("folderPath", folderPath);
            }
            if (StringUtils.isNotEmpty(newFolderPath)) {
                uriBuilder.addParameter("newFolderPath", newFolderPath);
            }
            if (StringUtils.isNotEmpty(newName)) {
                uriBuilder.addParameter("newName", newName);
            }
            return new HttpPut(uriBuilder.build());
        } catch (URISyntaxException e) {
            throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
        }
    });

    onError(e -> new TDPException(APIErrorCodes.UNABLE_TO_COPY_DATASET_CONTENT, e,
            build().put("id", dataSetId)));

    on(HttpStatus.OK, HttpStatus.BAD_REQUEST).then((httpRequestBase, httpResponse) -> {
        try {
            // we transfer status code and content type
            return new HttpResponse(httpResponse.getStatusLine().getStatusCode(), //
                    IOUtils.toString(httpResponse.getEntity().getContent()), //
                    httpResponse.getStatusLine().getStatusCode() == HttpStatus.BAD_REQUEST.value() ? //
            APPLICATION_JSON_VALUE : TEXT_PLAIN_VALUE);
        } catch (IOException e) {
            throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
        } finally {
            httpRequestBase.releaseConnection();
        }
    });
}

From source file:com.arrow.acn.client.api.CoreEventApi.java

public StatusModel putSucceeded(String hid, Map<String, String> parameters) {
    String method = "putSucceeded";
    try {//ww  w.ja v  a2s .com
        URI uri = buildUri(PUT_SUCCEEDED_URL.replace("{hid}", hid));
        StatusModel result = execute(new HttpPut(uri), StatusModel.class);
        log(method, result);
        return result;
    } catch (Throwable e) {
        logError(method, e);
        throw new AcnClientException(method, e);
    }
}

From source file:no.norrs.projects.andronary.service.utils.HttpUtil.java

public static HttpResponse PUT(String uri, List<NameValuePair> data) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPut httpPost = new HttpPut(uri);
    //httpPost.addHeader("Accept", "application/json");
    httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
    httpPost.addHeader("User-Agent", "Andronary/0.1");
    httpPost.addHeader("Connection", "close");
    StringEntity e = new StringEntity(data.get(0).getValue(), HTTP.UTF_8);
    //httpPost.setEntity(new UrlEncodedFormEntity(data));
    httpPost.setEntity(e);/* ww w  . j  a va  2s.  co  m*/
    HttpResponse response;

    return httpClient.execute(httpPost);
    //return response.getStatusLine().getStatusCode();
    /*HttpEntity entity = response.getEntity();
    return entity.getContent();*/

}

From source file:org.apache.camel.component.cxf.jaxrs.CxfRsConvertBodyToTest.java

@Test
public void testPutConsumer() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);//from  w ww .ja v a2s . co m
    mock.message(0).body().isInstanceOf(Customer.class);

    HttpPut put = new HttpPut("http://localhost:" + CXT + "/rest/customerservice/customers");
    StringEntity entity = new StringEntity(PUT_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    put.addHeader("test", "header1;header2");
    put.setEntity(entity);
    HttpClient httpclient = new DefaultHttpClient();

    try {
        HttpResponse response = httpclient.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.foundationdb.http.CsrfProtectionITBase.java

@Test
public void requestBlockedWithMissingReferer() throws Exception {
    HttpUriRequest request = new HttpPut(defaultURI());

    response = client.execute(request);//from w  w w  .  j ava2s .  com
    assertEquals("status", HttpStatus.SC_FORBIDDEN, response.getStatusLine().getStatusCode());
    assertThat("reason", response.getStatusLine().getReasonPhrase(), containsString("Referer"));
}

From source file:org.sharetask.controller.UserControllerIT.java

@Test
public void testUpdateUser() throws IOException {
    //given/*from ww  w.ja  va 2s. com*/
    final HttpPut httpPut = new HttpPut(URL_USER);
    httpPut.addHeader(new BasicHeader("Content-Type", "application/json"));
    final StringEntity httpEntity = new StringEntity("{\"username\":\"dev3@shareta.sk\","
            + "\"name\":\"Integration\"," + "\"surName\":\"Test\"," + "\"language\":\"en\"}");
    httpPut.setEntity(httpEntity);

    //when
    final HttpResponse response = getClient().execute(httpPut);

    //then
    Assert.assertEquals(HttpStatus.OK.value(), response.getStatusLine().getStatusCode());
    final String responseData = EntityUtils.toString(response.getEntity());
    Assert.assertTrue(responseData.contains("\"name\":\"Integration\""));
}

From source file:org.chaplib.HttpResource.java

public void replaceOrCreate(HttpEntity entity) {
    HttpPut req = new HttpPut(uri);
    req.setEntity(entity);
    consumeBodyOf(execute(req));
}