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

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

Introduction

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

Prototype

public void setEntity(final HttpEntity entity) 

Source Link

Usage

From source file:org.activiti.rest.service.api.identity.GroupResourceTest.java

/**
 * Test updating a single group passing in no fields in the json, user should remain unchanged.
 *///from   w w w  .j  av  a2 s . com
public void testUpdateGroupNoFields() throws Exception {
    try {
        Group testGroup = identityService.newGroup("testgroup");
        testGroup.setName("Test group");
        testGroup.setType("Test type");
        identityService.saveGroup(testGroup);

        ObjectNode requestNode = objectMapper.createObjectNode();

        HttpPut httpPut = new HttpPut(
                SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_GROUP, "testgroup"));
        httpPut.setEntity(new StringEntity(requestNode.toString()));
        CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);

        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertEquals("testgroup", responseNode.get("id").textValue());
        assertEquals("Test group", responseNode.get("name").textValue());
        assertEquals("Test type", responseNode.get("type").textValue());
        assertTrue(responseNode.get("url").textValue()
                .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_GROUP, testGroup.getId())));

        Group createdGroup = identityService.createGroupQuery().groupId("testgroup").singleResult();
        assertNotNull(createdGroup);
        assertEquals("Test group", createdGroup.getName());
        assertEquals("Test type", createdGroup.getType());

    } finally {
        try {
            identityService.deleteGroup("testgroup");
        } catch (Throwable ignore) {
            // Ignore, since the group may not have been created in the test
            // or already deleted
        }
    }
}

From source file:org.activiti.rest.service.api.identity.GroupResourceTest.java

/**
 * Test updating a single user passing in null-values.
 *///from w w w .  j a  va 2  s. c  om
public void testUpdateGroupNullFields() throws Exception {
    try {
        Group testGroup = identityService.newGroup("testgroup");
        testGroup.setName("Test group");
        testGroup.setType("Test type");
        identityService.saveGroup(testGroup);

        ObjectNode requestNode = objectMapper.createObjectNode();
        requestNode.put("name", (JsonNode) null);
        requestNode.put("type", (JsonNode) null);

        HttpPut httpPut = new HttpPut(
                SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_GROUP, "testgroup"));
        httpPut.setEntity(new StringEntity(requestNode.toString()));
        CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertEquals("testgroup", responseNode.get("id").textValue());
        assertNull(responseNode.get("name").textValue());
        assertNull(responseNode.get("type").textValue());
        assertTrue(responseNode.get("url").textValue()
                .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_GROUP, testGroup.getId())));

        Group createdGroup = identityService.createGroupQuery().groupId("testgroup").singleResult();
        assertNotNull(createdGroup);
        assertNull(createdGroup.getName());
        assertNull(createdGroup.getType());

    } finally {
        try {
            identityService.deleteGroup("testgroup");
        } catch (Throwable ignore) {
            // Ignore, since the group may not have been created in the test
            // or already deleted
        }
    }
}

From source file:org.flowable.rest.service.api.identity.GroupResourceTest.java

/**
 * Test updating a single user passing in null-values.
 *//*from w ww.  java  2 s.com*/
public void testUpdateGroupNullFields() throws Exception {
    try {
        Group testGroup = identityService.newGroup("testgroup");
        testGroup.setName("Test group");
        testGroup.setType("Test type");
        identityService.saveGroup(testGroup);

        ObjectNode requestNode = objectMapper.createObjectNode();
        requestNode.set("name", null);
        requestNode.set("type", null);

        HttpPut httpPut = new HttpPut(
                SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_GROUP, "testgroup"));
        httpPut.setEntity(new StringEntity(requestNode.toString()));
        CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertEquals("testgroup", responseNode.get("id").textValue());
        assertNull(responseNode.get("name").textValue());
        assertNull(responseNode.get("type").textValue());
        assertTrue(responseNode.get("url").textValue()
                .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_GROUP, testGroup.getId())));

        Group createdGroup = identityService.createGroupQuery().groupId("testgroup").singleResult();
        assertNotNull(createdGroup);
        assertNull(createdGroup.getName());
        assertNull(createdGroup.getType());

    } finally {
        try {
            identityService.deleteGroup("testgroup");
        } catch (Throwable ignore) {
            // Ignore, since the group may not have been created in the test
            // or already deleted
        }
    }
}

From source file:org.keycloak.client.registration.HttpUtil.java

InputStream doPut(String content, String contentType, Charset charset, String acceptType, String... path)
        throws ClientRegistrationException {
    try {//w  ww.  j ava  2  s .  c  o  m
        HttpPut request = new HttpPut(getUrl(baseUri, path));

        request.setHeader(HttpHeaders.CONTENT_TYPE, contentType(contentType, charset));
        request.setHeader(HttpHeaders.ACCEPT, acceptType);
        request.setEntity(new StringEntity(content, charset));

        addAuth(request);

        HttpResponse response = httpClient.execute(request);

        InputStream responseStream = null;
        if (response.getEntity() != null) {
            responseStream = response.getEntity().getContent();
        }

        if (response.getStatusLine().getStatusCode() == 200) {
            return responseStream;
        } else {
            throw httpErrorException(response, responseStream);
        }
    } catch (IOException e) {
        throw new ClientRegistrationException("Failed to send request", e);
    }
}

From source file:org.activiti.rest.service.api.identity.GroupResourceTest.java

/**
 * Test updating a single group.// www.j  a  va2 s.co m
 */
public void testUpdateGroup() throws Exception {
    try {
        Group testGroup = identityService.newGroup("testgroup");
        testGroup.setName("Test group");
        testGroup.setType("Test type");
        identityService.saveGroup(testGroup);

        ObjectNode requestNode = objectMapper.createObjectNode();
        requestNode.put("name", "Updated group");
        requestNode.put("type", "Updated type");

        HttpPut httpPut = new HttpPut(
                SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_GROUP, "testgroup"));
        httpPut.setEntity(new StringEntity(requestNode.toString()));
        CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);

        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertEquals("testgroup", responseNode.get("id").textValue());
        assertEquals("Updated group", responseNode.get("name").textValue());
        assertEquals("Updated type", responseNode.get("type").textValue());
        assertTrue(responseNode.get("url").textValue()
                .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_GROUP, testGroup.getId())));

        Group createdGroup = identityService.createGroupQuery().groupId("testgroup").singleResult();
        assertNotNull(createdGroup);
        assertEquals("Updated group", createdGroup.getName());
        assertEquals("Updated type", createdGroup.getType());

    } finally {
        try {
            identityService.deleteGroup("testgroup");
        } catch (Throwable ignore) {
            // Ignore, since the group may not have been created in the test
            // or already deleted
        }
    }
}

From source file:org.ldp4j.server.frontend.JAXRSFrontendITest.java

@Test
@Category({ REST.class, PUT.class, ExceptionPath.class })
@OperateOnDeployment(DEPLOYMENT)/*from  w  w  w. ja  v  a  2 s.  c om*/
public void testNotAcceptableMediaTypeFailure(@ArquillianResource final URL url) throws Exception {
    String path = "resource/notAcceptable/mediaType";
    CreateEndpoint command = CommandHelper.newCreateEndpointCommand(path, "templateId").modifiable(true)
            .withContent(EXAMPLE_BODY).build();
    HELPER.base(url);
    HELPER.executeCommand(command);
    HttpPut action = HELPER.newRequest(path, HttpPut.class);
    action.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString());
    action.setEntity(DUMMY_ENTITY);
    HELPER.httpRequest(action);
}

From source file:org.mycontroller.restclient.core.RestHttpClient.java

protected RestHttpResponse doPut(String url, RestHeader header, HttpEntity entity,
        Integer expectedResponseCode) {
    HttpPut put = new HttpPut(url);
    header.updateHeaders(put);//w w  w.j  a  va2 s. c  o m
    put.setEntity(entity);
    // execute
    RestHttpResponse httpResponse = execute(put);
    // validate response
    validateResponse(httpResponse, expectedResponseCode);
    return httpResponse;
}

From source file:org.wrml.runtime.service.rest.RestService.java

@Override
public Model save(final Model model) {

    final Document document = (Document) model;
    final URI uri = document.getUri();

    final HttpPut httpPut = new HttpPut(uri);

    final Context context = model.getContext();

    final ModelContentProducer httpWriter = new ModelContentProducer(context, null, model);
    httpPut.setEntity(new EntityTemplate(httpWriter));

    final HttpResponse response = executeRequest(httpPut);
    final Dimensions responseDimensions = RestUtils.extractResponseDimensions(context, response,
            model.getDimensions());/*from  w ww .  ja va2 s . c  o  m*/

    return readResponseModel(response, model.getContext(), model.getKeys(), responseDimensions);
}

From source file:io.apiman.manager.api.gateway.rest.GatewayClient.java

/**
 * @see io.apiman.gateway.api.rest.contract.IApplicationResource#register(io.apiman.gateway.engine.beans.Application)
 *///from   w ww . j  a  va  2 s .  c  o m
public void register(Application application) throws RegistrationException, NotAuthorizedException {
    try {
        URI uri = new URI(this.endpoint + APPLICATIONS);
        HttpPut put = new HttpPut(uri);
        put.setHeader("Content-Type", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$
        String jsonPayload = mapper.writer().writeValueAsString(application);
        HttpEntity entity = new StringEntity(jsonPayload);
        put.setEntity(entity);
        HttpResponse response = httpClient.execute(put);
        int actualStatusCode = response.getStatusLine().getStatusCode();
        if (actualStatusCode >= 300) {
            throw new Exception("Application registration failed: " + actualStatusCode); //$NON-NLS-1$
        }
    } catch (Exception e) {
        // TODO log this error
        throw new RuntimeException(e);
    }
}

From source file:com.google.dataconnector.client.fetchrequest.HttpFetchStrategy.java

/**
 * Based on the inbound request type header, determine the correct http
 * method to use.  If a method cannot be determined (or not specified),
 * HTTP GET is attempted./*from w  ww .  j av  a  2s.  c  o m*/
 */
HttpRequestBase getMethod(FetchRequest request) {
    String method = null;
    for (MessageHeader h : request.getHeadersList()) {
        if ("x-sdc-http-method".equalsIgnoreCase(h.getKey())) {
            method = h.getValue().toUpperCase();
        }
    }

    LOG.info(request.getId() + ": method=" + method + ", resource=" + request.getResource()
            + ((request.hasContents()) ? ", payload_size=" + request.getContents().size() : ""));

    if (method == null) {
        LOG.info(request.getId() + ": No http method specified. Default to GET.");
        method = "GET";
    }

    if ("GET".equals(method)) {
        return new HttpGet(request.getResource());
    }
    if ("POST".equals(method)) {
        HttpPost httpPost = new HttpPost(request.getResource());
        if (request.hasContents()) {
            LOG.debug(request.getId() + ": Content = " + new String(request.getContents().toByteArray()));
            httpPost.setEntity(new ByteArrayEntity(request.getContents().toByteArray()));
        }
        return httpPost;
    }
    if ("PUT".equals(method)) {
        HttpPut httpPut = new HttpPut(request.getResource());
        if (request.hasContents()) {
            LOG.debug(request.getId() + ": Content = " + new String(request.getContents().toByteArray()));
            httpPut.setEntity(new ByteArrayEntity(request.getContents().toByteArray()));
        }
        return httpPut;
    }
    if ("DELETE".equals(method)) {
        return new HttpDelete(request.getResource());
    }
    if ("HEAD".equals(method)) {
        return new HttpHead(request.getResource());
    }
    LOG.info(request.getId() + ": Unknown method " + method);
    return null;
}