List of usage examples for org.apache.http.entity FileEntity FileEntity
public FileEntity(File file, ContentType contentType)
From source file:uk.co.md87.android.sensorlogger.UploaderService.java
public void run() { final HttpPost post = new HttpPost("http://chris.smith.name/android/upload"); final File file = getFileStreamPath("sensors.log"); if (file.exists() && file.length() > 10) { // The file exists and contains a non-trivial amount of information final FileEntity entity = new FileEntity(file, "text/plain"); for (Map.Entry<String, String> entry : headers.entrySet()) { post.setHeader("x-" + entry.getKey(), entry.getValue()); }//ww w . j ava 2 s .c o m post.setEntity(entity); try { int code = new DefaultHttpClient().execute(post).getStatusLine().getStatusCode(); } catch (IOException ex) { Log.e(getClass().getName(), "Unable to upload sensor logs", ex); } } file.delete(); try { service.setState(7); } catch (RemoteException ex) { Log.e(getClass().getName(), "Unable to update state", ex); } stopSelf(); }
From source file:com.mindprotectionkit.freephone.monitor.Uploader.java
public void attemptUpload() throws IOException { AndroidHttpClient client = AndroidHttpClient.newInstance("RedPhone"); try {/*from ww w . j a v a2s .c om*/ String hostName = String.format("https://%s/collector/%s/%s/%s/%d", Release.DATA_COLLECTION_SERVER_HOST, callId, clientId, dataSource, attemptId); Log.d("Uploader", "Posting to RedPhone DCS: " + hostName + " clientId: " + clientId + " callId: " + callId); HttpPost post = new HttpPost(hostName); post.setEntity(new FileEntity(datafile, "application/json")); post.setHeader("Content-Encoding", "gzip"); HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() != 200) { Log.d("Uploader", "Redphone DCS response: " + response.toString()); response.getEntity().consumeContent(); client.getConnectionManager().shutdown(); throw new IllegalStateException("Upload failed"); } response.getEntity().consumeContent(); client.getConnectionManager().shutdown(); } finally { client.close(); } }
From source file:at.ac.tuwien.infosys.jcloudscale.datastore.rest.RequestHandlerImpl.java
private void setContent(HttpUriRequest httpUriRequest, Request request) throws UnsupportedEncodingException { if (request.getContent() == null || !vaildRequestTypeForContent(request)) { return;/*from w ww. ja v a 2 s. c o m*/ } HttpEntityEnclosingRequestBase entityRequest = (HttpEntityEnclosingRequestBase) httpUriRequest; HttpEntity httpEntity = null; if (FileUtil.isFile(request.getContent().getClass())) { File file = (File) request.getContent(); httpEntity = new FileEntity(file, request.getContentType()); } else { String content = (String) request.getContent(); httpEntity = new StringEntity(content); } entityRequest.setEntity(httpEntity); setContentType(httpUriRequest, request.getContentType()); }
From source file:org.emonocot.job.common.GetResourceClientTest.java
/** * * @throws IOException/*w w w . j av a 2s. co m*/ * if the test file cannot be found */ @Before public final void setUp() throws IOException { getResourceClient.setHttpClient(httpClient); httpResponse.setEntity(new FileEntity(content.getFile(), "application/zip")); }
From source file:com.lonepulse.robozombie.util.Entities.java
/** * <p>Discovers which implementation of {@link HttpEntity} is suitable for wrapping the given object. * This discovery proceeds in the following order by checking the runtime-type of the object:</p> * * <ol>/*from ww w . java 2 s. c o m*/ * <li>org.apache.http.{@link HttpEntity} --> returned as-is.</li> * <li>{@code byte[]}, {@link Byte}[] --> {@link ByteArrayEntity}</li> * <li>java.io.{@link File} --> {@link FileEntity}</li> * <li>java.io.{@link InputStream} --> {@link BufferedHttpEntity}</li> * <li>{@link CharSequence} --> {@link StringEntity}</li> * <li>java.io.{@link Serializable} --> {@link SerializableEntity} (with an internal buffer)</li> * </ol> * * @param genericEntity * a generic reference to an object whose concrete {@link HttpEntity} is to be resolved * <br><br> * @return the resolved concrete {@link HttpEntity} implementation * <br><br> * @throws NullPointerException * if the supplied generic type was {@code null} * <br><br> * @throws EntityResolutionFailedException * if the given generic instance failed to be translated to an {@link HttpEntity} * <br><br> * @since 1.3.0 */ public static final HttpEntity resolve(Object genericEntity) { assertNotNull(genericEntity); try { if (genericEntity instanceof HttpEntity) { return (HttpEntity) genericEntity; } else if (byte[].class.isAssignableFrom(genericEntity.getClass())) { return new ByteArrayEntity((byte[]) genericEntity); } else if (Byte[].class.isAssignableFrom(genericEntity.getClass())) { Byte[] wrapperBytes = (Byte[]) genericEntity; byte[] primitiveBytes = new byte[wrapperBytes.length]; for (int i = 0; i < wrapperBytes.length; i++) { primitiveBytes[i] = wrapperBytes[i].byteValue(); } return new ByteArrayEntity(primitiveBytes); } else if (genericEntity instanceof File) { return new FileEntity((File) genericEntity, null); } else if (genericEntity instanceof InputStream) { BasicHttpEntity basicHttpEntity = new BasicHttpEntity(); basicHttpEntity.setContent((InputStream) genericEntity); return new BufferedHttpEntity(basicHttpEntity); } else if (genericEntity instanceof CharSequence) { return new StringEntity(((CharSequence) genericEntity).toString()); } else if (genericEntity instanceof Serializable) { return new SerializableEntity((Serializable) genericEntity, true); } else { throw new EntityResolutionFailedException(genericEntity); } } catch (Exception e) { throw (e instanceof EntityResolutionFailedException) ? (EntityResolutionFailedException) e : new EntityResolutionFailedException(genericEntity, e); } }
From source file:org.docx4j.services.client.ConverterHttp.java
/** * Convert File fromFormat to toFormat, streaming result to OutputStream os. * /*from w w w . j a v a 2 s. co m*/ * fromFormat supported: DOC, DOCX * * toFormat supported: PDF * * @param f * @param fromFormat * @param toFormat * @param os * @throws IOException * @throws ConversionException */ public void convert(File f, Format fromFormat, Format toFormat, OutputStream os) throws IOException, ConversionException { checkParameters(fromFormat, toFormat); // CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient = HttpClients.custom().setRetryHandler(new MyRetryHandler()).build(); try { HttpPost httppost = getUrlForFormat(toFormat); HttpEntity reqEntity = new FileEntity(f, map(fromFormat)); httppost.setEntity(reqEntity); execute(httpclient, httppost, os); log.debug("..done"); } finally { httpclient.close(); } }
From source file:org.jboss.capedwarf.tools.BulkLoader.java
private static void sendPacket(UploadPacket packet, DefaultHttpClient client, String url) throws IOException { HttpPut put = new HttpPut(url); System.out.println("Uploading packet of " + packet.size() + " entities"); put.setEntity(new FileEntity(packet.getFile(), "application/capedwarf-data")); HttpResponse response = client.execute(put); response.getEntity().writeTo(new ByteArrayOutputStream()); System.out.println("Received response " + response); }
From source file:de.unikassel.android.sdcframework.transmission.SimpleHttpProtocol.java
/** * Method for an HTTP upload of file stream * // w ww .j a v a2s. co m * @param file * the input file * * @return true if successful, false otherwise */ private final boolean httpUpload(File file) { DefaultHttpClient client = new DefaultHttpClient(); try { String fileName = FileUtils.fileNameFromPath(file.getName()); String contentType = getContentType(fileName); URL url = getURL(); configureForAuthentication(client, url); HttpPost httpPost = new HttpPost(url.toURI()); FileEntity fileEntity = new FileEntity(file, contentType); fileEntity.setContentType(contentType); fileEntity.setChunked(true); httpPost.setEntity(fileEntity); httpPost.addHeader("filename", fileName); httpPost.addHeader("uuid", getUuid().toString()); HttpResponse response = client.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); boolean success = statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_NO_CONTENT; Logger.getInstance().debug(this, "Server returned: " + statusCode); // clean up if necessary HttpEntity resEntity = response.getEntity(); if (resEntity != null) { resEntity.consumeContent(); } if (!success) { doHandleError("Unexpected server response: " + response.getStatusLine()); success = false; } return success; } catch (ClientProtocolException e) { e.printStackTrace(); doHandleError(PROTOCOL_EXCEPTION + ": " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); doHandleError(IO_EXCEPTION + ": " + e.getMessage()); } catch (URISyntaxException e) { setURL(null); doHandleError(INVALID_URL); } finally { client.getConnectionManager().shutdown(); } return false; }
From source file:org.sonatype.nexus.testsuite.repo.nexus4548.Nexus4548RepoTargetPermissionMatchesPathInRepoIT.java
private HttpResponse put(final String gavPath, final int code) throws Exception { HttpPut putMethod = new HttpPut(getNexusTestRepoUrl() + gavPath); putMethod.setEntity(new FileEntity(getTestFile("pom-a.pom"), "text/xml")); final HttpResponse response = RequestFacade.executeHTTPClientMethod(putMethod); assertThat(response.getStatusLine().getStatusCode(), Matchers.is(code)); return response; }
From source file:com.is.rest.client.DefaultRestClientImpl.java
@Override public <T> void postFile(String url, File file, Callback<T> delegate) { prepareRequest(delegate);/*w ww. j av a 2 s . co m*/ client.post(delegate.getContext(), url, delegate.getAdditionalHeaders(), new FileEntity(file, delegate.getRequestContentType()), delegate.getRequestContentType(), delegate); }