List of usage examples for org.apache.http.client.methods HttpPut setEntity
public void setEntity(final HttpEntity entity)
From source file:org.activiti.rest.service.api.identity.UserResourceTest.java
/** * Test updating a single user passing in no fields in the json, user should remain unchanged. *///w w w . j a v a 2s. co m public void testUpdateUserNoFields() throws Exception { User savedUser = null; try { User newUser = identityService.newUser("testuser"); newUser.setFirstName("Fred"); newUser.setLastName("McDonald"); newUser.setEmail("no-reply@activiti.org"); identityService.saveUser(newUser); savedUser = newUser; ObjectNode taskUpdateRequest = objectMapper.createObjectNode(); HttpPut httpPut = new HttpPut( SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId())); httpPut.setEntity(new StringEntity(taskUpdateRequest.toString())); CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK); JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent()); closeResponse(response); assertNotNull(responseNode); assertEquals("testuser", responseNode.get("id").textValue()); assertEquals("Fred", responseNode.get("firstName").textValue()); assertEquals("McDonald", responseNode.get("lastName").textValue()); assertEquals("no-reply@activiti.org", responseNode.get("email").textValue()); assertTrue(responseNode.get("url").textValue() .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId()))); // Check user is updated in activiti newUser = identityService.createUserQuery().userId(newUser.getId()).singleResult(); assertEquals("McDonald", newUser.getLastName()); assertEquals("Fred", newUser.getFirstName()); assertEquals("no-reply@activiti.org", newUser.getEmail()); assertNull(newUser.getPassword()); } finally { // Delete user after test fails if (savedUser != null) { identityService.deleteUser(savedUser.getId()); } } }
From source file:com.liferay.ide.server.remote.ServerManagerConnection.java
public Object updateApplication(String appName, String absolutePath, IProgressMonitor monitor) throws APIException { try {//from w ww . j a v a 2 s.c om File file = new File(absolutePath); FileBody fileBody = new FileBody(file); MultipartEntity entity = new MultipartEntity(); entity.addPart(file.getName(), fileBody); HttpPut httpPut = new HttpPut(); httpPut.setEntity(entity); Object response = httpJSONAPI(httpPut, _getUpdateURI(appName)); if (response instanceof JSONObject) { JSONObject jsonObject = (JSONObject) response; if (_isSuccess(jsonObject)) { System.out.println("updateApplication: success.\n\n"); } else { if (_isError(jsonObject)) { return jsonObject.getString("error"); } else { return "updateApplication error " + _getDeployURI(appName); } } } httpPut.releaseConnection(); } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } return null; }
From source file:com.googlesource.gerrit.plugins.its.rtc.network.Transport.java
public <T> T put(final String path, final Type typeOrClass, JsonObject data, String etag) throws IOException { HttpPut request = new HttpPut(toUri(path)); if (log.isDebugEnabled()) log.debug("Preparing PUT against " + request.getURI() + " using etag " + etag + " and data " + data); request.setEntity(new StringEntity(data.toString(), StandardCharsets.UTF_8)); if (etag != null) request.addHeader("If-Match", etag); return invoke(request, typeOrClass, APP_OSLC, APP_OSLC); }
From source file:edu.rit.csh.androidwebnews.HttpsPutAsyncTask.java
/** * The method that gets run when execute() is run. This sends the URL with the * PUT parameters to the server and gets the results * * @param params - [0] is the URL to got to, the rest are parameters to the request * @return String representation of page results */// ww w . j a va 2 s . co m @Override protected String doInBackground(BasicNameValuePair... params) { ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>(); String line; try { HttpPut request = new HttpPut(params[0].getValue()); request.addHeader("accept", "application/json"); //params = Arrays.copyOfRange(params, 1, params.length); for (BasicNameValuePair param : params) { nvp.add(new BasicNameValuePair(param.getName(), param.getValue())); } request.setEntity(new UrlEncodedFormEntity(nvp)); HttpResponse response = httpclient.execute(request); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder sb = new StringBuilder(""); String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line).append(NL); } in.close(); return sb.toString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:org.activiti.rest.service.api.identity.UserResourceTest.java
/** * Test updating a single user passing in no fields in the json, user should remain unchanged. *///from www . jav a 2 s . c om public void testUpdateUserNullFields() throws Exception { User savedUser = null; try { User newUser = identityService.newUser("testuser"); newUser.setFirstName("Fred"); newUser.setLastName("McDonald"); newUser.setEmail("no-reply@activiti.org"); identityService.saveUser(newUser); savedUser = newUser; ObjectNode taskUpdateRequest = objectMapper.createObjectNode(); taskUpdateRequest.putNull("firstName"); taskUpdateRequest.putNull("lastName"); taskUpdateRequest.putNull("email"); taskUpdateRequest.putNull("password"); HttpPut httpPut = new HttpPut( SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId())); httpPut.setEntity(new StringEntity(taskUpdateRequest.toString())); CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK); JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent()); closeResponse(response); assertNotNull(responseNode); assertEquals("testuser", responseNode.get("id").textValue()); assertTrue(responseNode.get("firstName").isNull()); assertTrue(responseNode.get("lastName").isNull()); assertTrue(responseNode.get("email").isNull()); assertTrue(responseNode.get("url").textValue() .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId()))); // Check user is updated in activiti newUser = identityService.createUserQuery().userId(newUser.getId()).singleResult(); assertNull(newUser.getLastName()); assertNull(newUser.getFirstName()); assertNull(newUser.getEmail()); } finally { // Delete user after test fails if (savedUser != null) { identityService.deleteUser(savedUser.getId()); } } }
From source file:io.vertx.ext.consul.dc.ConsulAgent.java
public String createAclToken(String rules) throws IOException { HttpClient client = new DefaultHttpClient(); HttpPut put = new HttpPut("http://" + getHost() + ":" + getHttpPort() + "/v1/acl/create"); put.addHeader("X-Consul-Token", masterToken); Map<String, String> tokenRequest = new HashMap<>(); tokenRequest.put("Type", "client"); tokenRequest.put("Rules", rules); put.setEntity(new StringEntity(mapper.writeValueAsString(tokenRequest))); HttpResponse response = client.execute(put); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Bad response"); }/*from www. j a v a 2 s. c o m*/ Map<String, String> tokenResponse = mapper.readValue(EntityUtils.toString(response.getEntity()), new TypeReference<Map<String, String>>() { }); return tokenResponse.get("ID"); }
From source file:org.activiti.rest.service.api.identity.UserResourceTest.java
/** * Test updating a single user.//from w w w .j av a2 s. c o m */ public void testUpdateUser() throws Exception { User savedUser = null; try { User newUser = identityService.newUser("testuser"); newUser.setFirstName("Fred"); newUser.setLastName("McDonald"); newUser.setEmail("no-reply@activiti.org"); identityService.saveUser(newUser); savedUser = newUser; ObjectNode taskUpdateRequest = objectMapper.createObjectNode(); taskUpdateRequest.put("firstName", "Tijs"); taskUpdateRequest.put("lastName", "Barrez"); taskUpdateRequest.put("email", "no-reply@alfresco.org"); taskUpdateRequest.put("password", "updatedpassword"); HttpPut httpPut = new HttpPut( SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId())); httpPut.setEntity(new StringEntity(taskUpdateRequest.toString())); CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK); JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent()); closeResponse(response); assertNotNull(responseNode); assertEquals("testuser", responseNode.get("id").textValue()); assertEquals("Tijs", responseNode.get("firstName").textValue()); assertEquals("Barrez", responseNode.get("lastName").textValue()); assertEquals("no-reply@alfresco.org", responseNode.get("email").textValue()); assertTrue(responseNode.get("url").textValue() .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId()))); // Check user is updated in activiti newUser = identityService.createUserQuery().userId(newUser.getId()).singleResult(); assertEquals("Barrez", newUser.getLastName()); assertEquals("Tijs", newUser.getFirstName()); assertEquals("no-reply@alfresco.org", newUser.getEmail()); assertEquals("updatedpassword", newUser.getPassword()); } finally { // Delete user after test fails if (savedUser != null) { identityService.deleteUser(savedUser.getId()); } } }
From source file:com.googlesource.gerrit.plugins.hooks.rtc.network.Transport.java
public <T> T put(final String path, final Type typeOrClass, JsonObject data, String etag) throws IOException { HttpPut request = new HttpPut(toUri(path)); if (log.isDebugEnabled()) log.debug("Preparing PUT against " + request.getURI() + " using etag " + etag + " and data " + data); request.setEntity(new StringEntity(data.toString(), HTTP.UTF_8)); if (etag != null) request.addHeader("If-Match", etag); return invoke(request, typeOrClass, APP_OSLC, APP_OSLC); }
From source file:com.strato.hidrive.api.connection.httpgateway.request.JSONPutRequest.java
protected HttpRequestBase createHttpRequest(String requestUri, HttpRequestParamsVisitor<?> visitor) throws UnsupportedEncodingException { HttpPut httpPost = new HttpPut(requestUri); String jsonString = ""; if (jsonObject != null) { jsonString = jsonObject.toString(); } else if (jsonArray != null) { jsonString = jsonArray.toString(); }//from ww w . ja v a2 s.c o m StringEntity stringEntity = new StringEntity(jsonString); stringEntity.setContentType(CONTENT_TYPE_APPLICATION_JSON); httpPost.setEntity(stringEntity); return httpPost; }
From source file:org.fcrepo.integration.connector.file.FileConnectorIT.java
/** * I should be able to copy objects from the repository to a federated filesystem. * * @throws IOException exception thrown during this function **//*from www . j a v a 2 s. co m*/ @Ignore("Enabled once the FedoraFileSystemConnector becomes readable/writable") public void testCopyToProjection() throws IOException { // create object in the repository final String pid = randomUUID().toString(); final HttpPut put = new HttpPut(serverAddress + pid + "/ds1"); put.setEntity(new StringEntity("abc123")); put.setHeader("Content-Type", TEXT_PLAIN); client.execute(put); // copy to federated filesystem final HttpCopy request = new HttpCopy(serverAddress + pid); request.addHeader("Destination", serverAddress + "files/copy-" + pid); assertEquals(CREATED.getStatusCode(), getStatus(request)); // federated copy should now exist final HttpGet copyGet = new HttpGet(serverAddress + "files/copy-" + pid); assertEquals(OK.getStatusCode(), getStatus(copyGet)); // repository copy should still exist final HttpGet originalGet = new HttpGet(serverAddress + pid); assertEquals(OK.getStatusCode(), getStatus(originalGet)); }