List of usage examples for org.apache.http.client.methods HttpPut setEntity
public void setEntity(final HttpEntity entity)
From source file:org.chaplib.HttpResource.java
public void replaceOrCreate(HttpEntity entity) { HttpPut req = new HttpPut(uri); req.setEntity(entity); consumeBodyOf(execute(req)); }
From source file:com.anrisoftware.simplerest.core.AbstractSimplePutWorker.java
/** * Makes the request and sends the data and retrieves the response. * * @param entity/*from w w w .j ava 2 s.co m*/ * the {@link HttpEntity} entity. * * @return the response. * * @throws SimpleRestException */ public T sendData(HttpEntity entity) throws SimpleRestException { CloseableHttpClient httpclient = createHttpClient(); HttpPut httpput = new HttpPut(requestUri); httpput.setEntity(entity); for (Header header : headers) { httpput.addHeader(header); } CloseableHttpResponse response = executeRequest(httpclient, httpput); StatusLine statusLine = response.getStatusLine(); return parseResponse(response, httpput, statusLine); }
From source file:com.tinyhydra.botd.BotdServerOperations.java
public static void CastVote(final Activity activity, final Handler handler, final String email, final String shopId, final String shopRef) { new Thread() { @Override/* ww w . java 2 s. co m*/ public void run() { try { URI uri = new URI(activity.getResources().getString(R.string.server_url)); HttpClient client = new DefaultHttpClient(); HttpPut put = new HttpPut(uri); JSONObject voteObj = new JSONObject(); // user's phone-account-email-address is used to prevent multiple votes // the server will validate. 'shopId' is a consistent id for a specific location // but can't be used to get more data. 'shopRef' is an id that changes based on // some criteria that google places has imposed, but will let us grab data later on // and various Ref codes with the same id will always resolve to the same location. voteObj.put(JSONvalues.email.toString(), email); voteObj.put(JSONvalues.shopId.toString(), shopId); voteObj.put(JSONvalues.shopRef.toString(), shopRef); put.setEntity(new StringEntity(voteObj.toString())); HttpResponse response = client.execute(put); InputStream is = response.getEntity().getContent(); int ch; StringBuffer sb = new StringBuffer(); while ((ch = is.read()) != -1) { sb.append((char) ch); } if (sb.toString().equals("0")) { Utils.PostToastMessageToHandler(handler, "Vote cast!", Toast.LENGTH_SHORT); // Set a local flag to prevent duplicate voting SharedPreferences settings = activity.getSharedPreferences(Const.GenPrefs, 0); SharedPreferences.Editor editor = settings.edit(); editor.putLong(Const.LastVoteDate, Utils.GetDate()); editor.commit(); } else { // The user shouldn't see this. The above SharedPreferences code will be evaluated // when the user hits the Vote button. If the user gets sneaky and deletes local data though, // the server will catch the duplicate vote based on the user's email address and send back a '1'. Utils.PostToastMessageToHandler(handler, "Vote refused. You've probably already voted today.", Toast.LENGTH_LONG); } GetTopTen(activity, handler, true); // Catch blocks. Return a generic error if anything goes wrong. //TODO: implement some better/more appropriate error handling. } catch (URISyntaxException usex) { usex.printStackTrace(); Utils.PostToastMessageToHandler(handler, "There was a problem submitting your vote. Poor signal? Please try again.", Toast.LENGTH_LONG); } catch (UnsupportedEncodingException ueex) { ueex.printStackTrace(); Utils.PostToastMessageToHandler(handler, "There was a problem submitting your vote. Poor signal? Please try again.", Toast.LENGTH_LONG); } catch (ClientProtocolException cpex) { cpex.printStackTrace(); Utils.PostToastMessageToHandler(handler, "There was a problem submitting your vote. Poor signal? Please try again.", Toast.LENGTH_LONG); } catch (IOException ioex) { ioex.printStackTrace(); Utils.PostToastMessageToHandler(handler, "There was a problem submitting your vote. Poor signal? Please try again.", Toast.LENGTH_LONG); } catch (JSONException jex) { jex.printStackTrace(); Utils.PostToastMessageToHandler(handler, "There was a problem submitting your vote. Poor signal? Please try again.", Toast.LENGTH_LONG); } } }.start(); }
From source file:org.eclipse.packagedrone.testing.server.channel.UploadApiV2Test.java
protected CloseableHttpResponse upload(final URIBuilder uri, final File file) throws IOException, URISyntaxException { final HttpPut httppost = new HttpPut(uri.build()); httppost.setEntity(new FileEntity(file)); return httpclient.execute(httppost); }
From source file:org.apache.cloudstack.storage.datastore.util.DateraUtil.java
public static void assignGroupToAppInstance(DateraObject.DateraConnection conn, String group, String appInstance) throws DateraObject.DateraError, UnsupportedEncodingException { DateraObject.InitiatorGroup initiatorGroup = getInitiatorGroup(conn, group); if (initiatorGroup == null) { throw new CloudRuntimeException("Initator group " + group + " not found "); }// w ww. j a v a 2s . c o m Map<String, DateraObject.InitiatorGroup> initiatorGroups = getAppInstanceInitiatorGroups(conn, appInstance); if (initiatorGroups == null) { throw new CloudRuntimeException("Initator group not found for appInstnace " + appInstance); } for (DateraObject.InitiatorGroup ig : initiatorGroups.values()) { if (ig.getName().equals(group)) { //already assigned return; } } HttpPut url = new HttpPut(generateApiUrl("app_instances", appInstance, "storage_instances", DateraObject.DEFAULT_STORAGE_NAME, "acl_policy", "initiator_groups")); url.setEntity(new StringEntity(gson.toJson( new DateraObject.InitiatorGroup(initiatorGroup.getPath(), DateraObject.DateraOperation.ADD)))); executeApiRequest(conn, url); }
From source file:io.crate.integrationtests.SQLHttpIntegrationTest.java
protected String upload(String table, String content) throws IOException { String digest = blobDigest(content); String url = Blobs.url(address, table, digest); HttpPut httpPut = new HttpPut(url); httpPut.setEntity(new StringEntity(content)); CloseableHttpResponse response = httpClient.execute(httpPut); assertThat(response.getStatusLine().getStatusCode(), is(201)); response.close();//from ww w. ja va 2s . com return url; }
From source file:com.microsoft.live.PutRequest.java
/** * Factory method override that constructs a HttpPut and adds a body to it. * * @return a HttpPut with the properly body added to it. *//*from ww w .j av a2s.co m*/ @Override protected HttpUriRequest createHttpRequest() throws LiveOperationException { final HttpPut request = new HttpPut(this.requestUri.toString()); request.setEntity(this.entity); return request; }
From source file:org.apache.cloudstack.storage.datastore.util.DateraUtil.java
public static void removeGroupFromAppInstance(DateraObject.DateraConnection conn, String group, String appInstance) throws DateraObject.DateraError, UnsupportedEncodingException { DateraObject.InitiatorGroup initiatorGroup = getInitiatorGroup(conn, group); if (initiatorGroup == null) { throw new CloudRuntimeException("Initator groups not found for appInstnace " + appInstance); }/*w w w.j a v a2s . c o m*/ Map<String, DateraObject.InitiatorGroup> initiatorGroups = getAppInstanceInitiatorGroups(conn, appInstance); if (initiatorGroups == null) { throw new CloudRuntimeException("Initator group not found for appInstnace " + appInstance); } boolean groupAssigned = false; for (DateraObject.InitiatorGroup ig : initiatorGroups.values()) { if (ig.getName().equals(group)) { groupAssigned = true; break; } } if (!groupAssigned) { return; // already removed } HttpPut url = new HttpPut(generateApiUrl("app_instances", appInstance, "storage_instances", DateraObject.DEFAULT_STORAGE_NAME, "acl_policy", "initiator_groups")); url.setEntity(new StringEntity(gson.toJson( new DateraObject.InitiatorGroup(initiatorGroup.getPath(), DateraObject.DateraOperation.REMOVE)))); executeApiRequest(conn, url); }
From source file:com.jaeksoft.searchlib.test.legacy.CommonTestCase.java
@SuppressWarnings("deprecation") public int postFile(File file, String contentType, String api) throws IllegalStateException, IOException { String url = SERVER_URL + "/" + api + "?use=" + INDEX_NAME + "&login=" + USER_NAME + "&key=" + API_KEY; HttpPut put = new HttpPut(url); put.setEntity(new FileEntity(file, contentType)); DefaultHttpClient httpClient = new DefaultHttpClient(); return httpClient.execute(put).getStatusLine().getStatusCode(); }
From source file:nl.esciencecenter.octopus.webservice.mac.MacITCase.java
/** * Submit status to a callback server using MAC Access authentication. * * @throws URISyntaxException/*from w w w. j a va 2s. co m*/ * @throws ClientProtocolException * @throws IOException */ @Test public void test() throws URISyntaxException, ClientProtocolException, IOException { Properties props = new Properties(); props.load(MacITCase.class.getClassLoader().getResourceAsStream("integration.props")); URI url = new URI(props.getProperty("integration.callback.url")); // TODO throw better exception than NullPointerException when property can not be found. String state = "STOPPED"; HttpPut request = new HttpPut(url); request.setEntity(new StringEntity(state)); String mac_id = props.getProperty("integration.callback.id"); String mac_key = props.getProperty("integration.callback.key"); URI scope = new URI(url.getScheme(), null, url.getHost(), url.getPort(), null, null, null); ImmutableList<MacCredential> macs = ImmutableList.of(new MacCredential(mac_id, mac_key, scope)); HttpClient httpClient = new DefaultHttpClient(); httpClient = JobLauncherService.macifyHttpClient((AbstractHttpClient) httpClient, macs); HttpResponse response = httpClient.execute(request); assertEquals(200, response.getStatusLine().getStatusCode()); }