List of usage examples for org.apache.http.entity InputStreamEntity setContentType
public void setContentType(Header header)
From source file:ee.ria.xroad.common.util.AbstractHttpSender.java
protected static InputStreamEntity createInputStreamEntity(InputStream content, long contentLength, String contentType) {//from w ww . j a v a 2s. c o m InputStreamEntity entity = new InputStreamEntity(content, contentLength); if (contentLength < 0) { entity.setChunked(true); // Just in case } entity.setContentType(contentType); return entity; }
From source file:edu.jhu.pha.vospace.storage.SwiftKeystoneStorageManager.java
public static String storeStreamedObject(String container, InputStream data, String contentType, String name, Map<String, String> metadata) throws IOException, HttpException, FilesException { String objName = name;/*w ww . jav a 2s. c o m*/ if (isValidContainerName(container) && FilesClient.isValidObjectName(objName)) { HttpPut method = new HttpPut( getFileUrl + "/" + sanitizeForURI(container) + "/" + sanitizeForURI(objName)); method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut); method.setHeader(FilesConstants.X_AUTH_TOKEN, strToken); InputStreamEntity entity = new InputStreamEntity(data, -1); entity.setChunked(true); entity.setContentType(contentType); method.setEntity(entity); for (String key : metadata.keySet()) { method.setHeader(FilesConstants.X_OBJECT_META + key, sanitizeForURI(metadata.get(key))); } method.removeHeaders("Content-Length"); try { FilesResponse response = new FilesResponse(client.execute(method)); if (response.getStatusCode() == HttpStatus.SC_CREATED) { return response.getResponseHeader(FilesConstants.E_TAG).getValue(); } else { logger.error(response.getStatusLine()); throw new FilesException("Unexpected result", response.getResponseHeaders(), response.getStatusLine()); } } finally { method.abort(); } } else { if (!FilesClient.isValidObjectName(objName)) { throw new FilesInvalidNameException(objName); } else { throw new FilesInvalidNameException(container); } } }
From source file:org.apache.camel.component.http4.HttpEntityConverter.java
private static HttpEntity asHttpEntity(InputStream in, Exchange exchange) throws IOException { InputStreamEntity entity; if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class); InputStream stream = GZIPHelper.compressGzip(contentEncoding, in); entity = new InputStreamEntity(stream, stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1 : -1);//from w ww . j av a 2s . c om } else { entity = new InputStreamEntity(in, -1); } if (exchange != null) { String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class); String contentType = ExchangeHelper.getContentType(exchange); entity.setContentEncoding(contentEncoding); entity.setContentType(contentType); } return entity; }
From source file:org.apache.camel.component.http4.HttpEntityConverter.java
private static HttpEntity asHttpEntity(byte[] data, Exchange exchange) throws Exception { InputStreamEntity entity; if (exchange != null && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class); InputStream stream = GZIPHelper.compressGzip(contentEncoding, data); entity = new InputStreamEntity(stream, stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1 : -1);/*from w w w. j av a 2 s .co m*/ } else { entity = new InputStreamEntity(new ByteArrayInputStream(data), data.length); } if (exchange != null) { String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class); String contentType = ExchangeHelper.getContentType(exchange); entity.setContentEncoding(contentEncoding); entity.setContentType(contentType); } return entity; }
From source file:org.graphity.core.util.jena.HttpOp.java
/** * Executes a HTTP PUT operation/* ww w . j ava 2s. co m*/ * * @param url * URL * @param contentType * Content Type for the PUT * @param input * Input Stream to read PUT content from * @param length * Amount of content to PUT * @param httpClient * HTTP Client * @param httpContext * HTTP Context * @param authenticator * HTTP Authenticator */ public static void execHttpPut(String url, String contentType, InputStream input, long length, HttpClient httpClient, HttpContext httpContext, HttpAuthenticator authenticator) { InputStreamEntity e = new InputStreamEntity(input, length); e.setContentType(contentType); e.setContentEncoding("UTF-8"); try { execHttpPut(url, e, httpClient, httpContext, authenticator); } finally { closeEntity(e); } }
From source file:org.graphity.core.util.jena.HttpOp.java
/** * Executes a HTTP POST with request body from an input stream and response * handling./*from w w w . ja v a 2 s . com*/ * <p> * The input stream is assumed to be UTF-8. * </p> * * @param url * URL * @param contentType * Content Type to POST * @param input * Input Stream to POST content from * @param length * Length of content to POST * @param acceptType * Accept Type * @param handler * Response handler called to process the response * @param httpClient * HTTP Client * @param httpContext * HTTP Context * @param authenticator * HTTP Authenticator */ public static void execHttpPost(String url, String contentType, InputStream input, long length, String acceptType, HttpResponseHandler handler, HttpClient httpClient, HttpContext httpContext, HttpAuthenticator authenticator) { InputStreamEntity e = new InputStreamEntity(input, length); e.setContentType(contentType); e.setContentEncoding("UTF-8"); try { execHttpPost(url, e, acceptType, handler, httpClient, httpContext, authenticator); } finally { closeEntity(e); } }
From source file:service.xml.YmlLoaderServiceTest.java
@Test public void test() { HttpClient client = new DefaultHttpClient(); try {/* w ww .ja v a 2s.co m*/ Configuration config = new Configuration(IConstants.TEST_PROPERTIES); HttpPost post = new HttpPost(config.getConfigProperties().getProperty("test.url")); String testXmlPath = config.getConfigProperties().getProperty("test.xml.path"); assertNotNull(testXmlPath); logger.info("Working with xml: " + testXmlPath); InputStreamEntity entity = new InputStreamEntity(new FileInputStream(testXmlPath), -1); entity.setChunked(true); entity.setContentType("application/xml"); post.setEntity(entity); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { logger.info(line); assertEquals("Export complete", line); } } catch (IOException e) { logger.error(e.getMessage()); } }
From source file:org.mahasen.thread.MahasenReplicateWorker.java
public void run() { HttpClient uploadHttpClient = new DefaultHttpClient(); uploadHttpClient = SSLWrapper.wrapClient(uploadHttpClient); if (file.exists()) { HttpPost httppost = new HttpPost(uri); try {// w w w. java 2 s. c o m InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); httppost.setEntity(reqEntity); System.out.println("Executing Replicate request " + httppost.getRequestLine()); HttpResponse response = uploadHttpClient.execute(httppost); System.out.println("Replicate worker----------------------------------------"); System.out.println(response.getStatusLine()); if ((response.getStatusLine().getReasonPhrase().equals("OK")) && (response.getStatusLine().getStatusCode() == 200)) { log.debug(currentPart + " was replicated at " + nodeIp); //PutUtil.incrementNoOfReplicas(currentPart); mahasenResource.addSplitPartStoredIp(currentPart, nodeIp); try { nodeManager.insertIntoDHT(parentFileId, mahasenResource, true); } catch (InterruptedException e) { log.error("Interrupted while updating replicated file's metadata"); } catch (PastException e) { log.error( "Error while updating replicated file ID:" + mahasenResource.getId() + "medatada"); } } } catch (IOException e) { log.error("Error occurred in URL connection"); } } }
From source file:org.dataconservancy.dcs.access.server.FileUploadServlet.java
private void uploadfile(String depositurl, String filename, InputStream is, HttpServletResponse resp) throws IOException { /* File tmp = null; FileOutputStream fos = null;//from w w w . j av a 2 s . com PostMethod post = null; //System.out.println(filename + " -> " + depositurl); */ try { /* tmp = File.createTempFile("fileupload", null); fos = new FileOutputStream(tmp); FileUtil.copy(is, fos); HttpClient client = new HttpClient(); post = new PostMethod(depositurl); Part[] parts = {new FilePart(filename, tmp)}; post.setRequestEntity(new MultipartRequestEntity(parts, post .getParams())); */ org.apache.http.client.HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(depositurl); InputStreamEntity data = new InputStreamEntity(is, -1); data.setContentType("binary/octet-stream"); data.setChunked(false); post.setEntity(data); HttpResponse response = client.execute(post); System.out.println(response.toString()); int status = 202;//response.getStatusLine(); // int status = client.executeMethod(post); if (status == HttpStatus.SC_ACCEPTED || status == HttpStatus.SC_CREATED) { resp.setStatus(status); String src = response.getHeaders("X-dcs-src")[0].getValue(); String atomurl = response.getHeaders("Location")[0].getValue(); resp.setContentType("text/html"); resp.getWriter().print("<html><body><p>^" + src + "^" + atomurl + "^</p></body></html>"); resp.flushBuffer(); } else { resp.sendError(status, response.getStatusLine().toString()); return; } } finally { /* if (tmp != null) { tmp.delete(); } if (is != null) { is.close(); } if (fos != null) { fos.close(); } if (post != null) { post.releaseConnection(); }*/ } }
From source file:org.peterbaldwin.vlcremote.sweep.Worker.java
@Override public void run() { // Note: InetAddress#isReachable(int) always returns false for Windows // hosts because applications do not have permission to perform ICMP // echo requests. for (;;) {/*w w w . j a v a2 s.c om*/ byte[] ipAddress = mManager.pollIpAddress(); if (ipAddress == null) { break; } try { InetAddress address = InetAddress.getByAddress(ipAddress); String hostAddress = address.getHostAddress(); URL url = createUrl("http", hostAddress, mPort, mPath); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(1000); try { int responseCode = connection.getResponseCode(); String responseMessage = connection.getResponseMessage(); InputStream inputStream = (responseCode == HttpURLConnection.HTTP_OK) ? connection.getInputStream() : connection.getErrorStream(); if (inputStream != null) { try { int length = connection.getContentLength(); InputStreamEntity entity = new InputStreamEntity(inputStream, length); entity.setContentType(connection.getContentType()); // Copy the entire response body into memory // before the HTTP connection is closed: String body = EntityUtils.toString(entity); ProtocolVersion version = new ProtocolVersion("HTTP", 1, 1); String hostname = address.getHostName(); StatusLine statusLine = new BasicStatusLine(version, responseCode, responseMessage); HttpResponse response = new BasicHttpResponse(statusLine); response.setHeader(HTTP.TARGET_HOST, hostname + ":" + mPort); response.setEntity(new StringEntity(body)); mCallback.onReachable(ipAddress, response); } finally { inputStream.close(); } } else { Log.w(TAG, "InputStream is null"); } } finally { connection.disconnect(); } } catch (IOException e) { mCallback.onUnreachable(ipAddress, e); } } }