List of usage examples for org.apache.http.entity.mime MultipartEntityBuilder addTextBody
public MultipartEntityBuilder addTextBody(final String name, final String text)
From source file:org.archive.modules.fetcher.FetchHTTPRequest.java
protected HttpEntity buildPostRequestEntity(CrawlURI curi) { String enctype = (String) curi.getData().get(CoreAttributeConstants.A_SUBMIT_ENCTYPE); if (enctype == null) { enctype = ContentType.APPLICATION_FORM_URLENCODED.getMimeType(); }/* w w w .ja va 2s . c om*/ @SuppressWarnings("unchecked") List<NameValue> submitData = (List<NameValue>) curi.getData().get(CoreAttributeConstants.A_SUBMIT_DATA); if (enctype.equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) { LinkedList<NameValuePair> nvps = new LinkedList<NameValuePair>(); for (NameValue nv : submitData) { nvps.add(new BasicNameValuePair(nv.name, nv.value)); } try { return new UrlEncodedFormEntity(nvps, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } } else if (enctype.equals(ContentType.MULTIPART_FORM_DATA.getMimeType())) { MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); for (NameValue nv : submitData) { entityBuilder.addTextBody(escapeForMultipart(nv.name), escapeForMultipart(nv.value)); } return entityBuilder.build(); } else { throw new IllegalStateException("unsupported form submission enctype='" + enctype + "'"); } }
From source file:com.adobe.ags.curly.controller.ActionRunner.java
private void addPostParams(HttpEntityEnclosingRequestBase request) throws UnsupportedEncodingException { final MultipartEntityBuilder multipartBuilder = MultipartEntityBuilder.create(); List<NameValuePair> formParams = new ArrayList<>(); postVariables.forEach((name, values) -> values.forEach(value -> { if (multipart) { if (value.startsWith("@")) { File f = new File(value.substring(1)); multipartBuilder.addBinaryBody(name, f, ContentType.DEFAULT_BINARY, f.getName()); } else { multipartBuilder.addTextBody(name, value); }//from ww w . j av a2 s. c o m } else { formParams.add(new BasicNameValuePair(name, value)); } })); if (multipart) { request.setEntity(multipartBuilder.build()); } else { request.setEntity(new UrlEncodedFormEntity(formParams)); } }
From source file:hello.MyPostHTTP.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectionRequestTimeout( context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); requestConfigBuilder.setConnectTimeout( context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); requestConfigBuilder.setRedirectsEnabled(false); requestConfigBuilder//w w w . j a va 2 s . c om .setSocketTimeout(context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); final RequestConfig requestConfig = requestConfigBuilder.build(); final StreamThrottler throttler = throttlerRef.get(); final ProcessorLog logger = getLogger(); String lastUrl = null; long bytesToSend = 0L; final List<FlowFile> toSend = new ArrayList<>(); CloseableHttpClient client = null; final String transactionId = UUID.randomUUID().toString(); final ObjectHolder<String> dnHolder = new ObjectHolder<>("none"); while (true) { FlowFile flowFile = session.get(); if (flowFile == null) { break; } final String url = context.getProperty(URL).evaluateAttributeExpressions(flowFile).getValue(); try { new java.net.URL(url); } catch (final MalformedURLException e) { logger.error( "After substituting attribute values for {}, URL is {}; this is not a valid URL, so routing to failure", new Object[] { flowFile, url }); flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); continue; } // If this FlowFile doesn't have the same url, throw it back on the queue and stop grabbing FlowFiles if (lastUrl != null && !lastUrl.equals(url)) { session.transfer(flowFile); break; } lastUrl = url; toSend.add(flowFile); if (client == null) { final Config config = getConfig(url, context); final HttpClientConnectionManager conMan = config.getConnectionManager(); final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(conMan); clientBuilder.addInterceptorFirst(new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException { final HttpCoreContext coreContext = HttpCoreContext.adapt(httpContext); final ManagedHttpClientConnection conn = coreContext .getConnection(ManagedHttpClientConnection.class); if (!conn.isOpen()) { return; } final SSLSession sslSession = conn.getSSLSession(); if (sslSession != null) { final X509Certificate[] certChain = sslSession.getPeerCertificateChain(); if (certChain == null || certChain.length == 0) { throw new SSLPeerUnverifiedException("No certificates found"); } final X509Certificate cert = certChain[0]; dnHolder.set(cert.getSubjectDN().getName().trim()); } } }); clientBuilder.disableAutomaticRetries(); clientBuilder.disableContentCompression(); client = clientBuilder.build(); } bytesToSend += flowFile.getSize(); break; } if (toSend.isEmpty()) { return; } final String url = lastUrl; final HttpPost post = new HttpPost(url); final List<FlowFile> flowFileList = toSend; String userName = "Chris"; String password = "password"; final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("userName", userName); builder.addTextBody("password", password); for (final FlowFile flowFile : flowFileList) { session.read(flowFile, new InputStreamCallback() { @Override public void process(final InputStream rawIn) throws IOException { InputStream in = new ByteArrayInputStream(IOUtils.toByteArray(rawIn)); builder.addBinaryBody("file", in, ContentType.DEFAULT_BINARY, "filename"); } }); } final HttpEntity entity2 = builder.build(); post.setEntity(entity2); post.setConfig(requestConfig); final String contentType; contentType = DEFAULT_CONTENT_TYPE; post.setHeader(CONTENT_TYPE_HEADER, contentType); post.setHeader(FLOWFILE_CONFIRMATION_HEADER, "true"); post.setHeader(PROTOCOL_VERSION_HEADER, PROTOCOL_VERSION); post.setHeader(TRANSACTION_ID_HEADER, transactionId); // Do the actual POST final String flowFileDescription = toSend.size() <= 10 ? toSend.toString() : toSend.size() + " FlowFiles"; final String uploadDataRate; final long uploadMillis; CloseableHttpResponse response = null; try { final StopWatch stopWatch = new StopWatch(true); response = client.execute(post); // consume input stream entirely, ignoring its contents. If we // don't do this, the Connection will not be returned to the pool EntityUtils.consume(response.getEntity()); stopWatch.stop(); uploadDataRate = stopWatch.calculateDataRate(bytesToSend); uploadMillis = stopWatch.getDuration(TimeUnit.MILLISECONDS); } catch (final IOException e) { logger.error("Failed to Post {} due to {}; transferring to failure", new Object[] { flowFileDescription, e }); context.yield(); for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } return; } finally { if (response != null) { try { response.close(); } catch (final IOException e) { getLogger().warn("Failed to close HTTP Response due to {}", new Object[] { e }); } } } // If we get a 'SEE OTHER' status code and an HTTP header that indicates that the intent // of the Location URI is a flowfile hold, we will store this holdUri. This prevents us // from posting to some other webservice and then attempting to delete some resource to which // we are redirected final int responseCode = response.getStatusLine().getStatusCode(); final String responseReason = response.getStatusLine().getReasonPhrase(); String holdUri = null; if (responseCode == HttpServletResponse.SC_SEE_OTHER) { final Header locationUriHeader = response.getFirstHeader(LOCATION_URI_INTENT_NAME); if (locationUriHeader != null) { if (LOCATION_URI_INTENT_VALUE.equals(locationUriHeader.getValue())) { final Header holdUriHeader = response.getFirstHeader(LOCATION_HEADER_NAME); if (holdUriHeader != null) { holdUri = holdUriHeader.getValue(); } } } if (holdUri == null) { for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); logger.error( "Failed to Post {} to {}: sent content and received status code {}:{} but no Hold URI", new Object[] { flowFile, url, responseCode, responseReason }); session.transfer(flowFile, REL_FAILURE); } return; } } if (holdUri == null) { if (responseCode == HttpServletResponse.SC_SERVICE_UNAVAILABLE) { for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); logger.error( "Failed to Post {} to {}: response code was {}:{}; will yield processing, " + "since the destination is temporarily unavailable", new Object[] { flowFile, url, responseCode, responseReason }); session.transfer(flowFile, REL_FAILURE); } context.yield(); return; } if (responseCode >= 300) { for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); logger.error("Failed to Post {} to {}: response code was {}:{}", new Object[] { flowFile, url, responseCode, responseReason }); session.transfer(flowFile, REL_FAILURE); } return; } logger.info("Successfully Posted {} to {} in {} at a rate of {}", new Object[] { flowFileDescription, url, FormatUtils.formatMinutesSeconds(uploadMillis, TimeUnit.MILLISECONDS), uploadDataRate }); for (final FlowFile flowFile : toSend) { session.getProvenanceReporter().send(flowFile, url, "Remote DN=" + dnHolder.get(), uploadMillis, true); session.transfer(flowFile, REL_SUCCESS); } return; } // // the response indicated a Hold URI; delete the Hold. // // determine the full URI of the Flow File's Hold; Unfortunately, the responses that are returned have // changed over the past, so we have to take into account a few different possibilities. String fullHoldUri = holdUri; if (holdUri.startsWith("/contentListener")) { // If the Hold URI that we get starts with /contentListener, it may not really be /contentListener, // as this really indicates that it should be whatever we posted to -- if posting directly to the // ListenHTTP component, it will be /contentListener, but if posting to a proxy/load balancer, we may // be posting to some other URL. fullHoldUri = url + holdUri.substring(16); } else if (holdUri.startsWith("/")) { // URL indicates the full path but not hostname or port; use the same hostname & port that we posted // to but use the full path indicated by the response. int firstSlash = url.indexOf("/", 8); if (firstSlash < 0) { firstSlash = url.length(); } final String beforeSlash = url.substring(0, firstSlash); fullHoldUri = beforeSlash + holdUri; } else if (!holdUri.startsWith("http")) { // Absolute URL fullHoldUri = url + (url.endsWith("/") ? "" : "/") + holdUri; } final HttpDelete delete = new HttpDelete(fullHoldUri); delete.setHeader(TRANSACTION_ID_HEADER, transactionId); while (true) { try { final HttpResponse holdResponse = client.execute(delete); EntityUtils.consume(holdResponse.getEntity()); final int holdStatusCode = holdResponse.getStatusLine().getStatusCode(); final String holdReason = holdResponse.getStatusLine().getReasonPhrase(); if (holdStatusCode >= 300) { logger.error( "Failed to delete Hold that destination placed on {}: got response code {}:{}; routing to failure", new Object[] { flowFileDescription, holdStatusCode, holdReason }); for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } return; } logger.info("Successfully Posted {} to {} in {} milliseconds at a rate of {}", new Object[] { flowFileDescription, url, uploadMillis, uploadDataRate }); for (final FlowFile flowFile : toSend) { session.getProvenanceReporter().send(flowFile, url); session.transfer(flowFile, REL_SUCCESS); } return; } catch (final IOException e) { logger.warn("Failed to delete Hold that destination placed on {} due to {}", new Object[] { flowFileDescription, e }); } if (!isScheduled()) { context.yield(); logger.warn( "Failed to delete Hold that destination placed on {}; Processor has been stopped so routing FlowFile(s) to failure", new Object[] { flowFileDescription }); for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } return; } } }
From source file:com.buffalokiwi.api.API.java
/** * This will take a list of key/value pairs and add them to the entity * builder instance./*w w w. j a va 2s .c o m*/ * @param formData data to add * @param builder builder instance */ private void setMultipartFormData(final List<NameValuePair> formData, final MultipartEntityBuilder builder) { if (formData == null) return; for (final NameValuePair pair : formData) { //..Add the text builder.addTextBody(pair.getName(), pair.getValue()); APILog.trace(LOG, pair.getName(), ":", pair.getValue()); } }
From source file:org.syncany.plugins.php.PhpTransferManager.java
@Override public void upload(File localFile, RemoteFile remoteFile) throws StorageException { logger.info("Uploading: " + localFile.getName() + " to " + remoteFile.getName()); try {/*from www . ja v a2 s . co m*/ final String remote_name = remoteFile.getName(); final File f = localFile; int r = operate("upload", new IPost() { List<NameValuePair> _nvps; public void mutateNVPS(List<NameValuePair> nvps) throws Exception { _nvps = nvps; nvps.add(new BasicNameValuePair("filename", remote_name)); } public int mutatePost(HttpPost p) throws Exception { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addPart("file", new FileBody(f)); Iterator<NameValuePair> it = _nvps.iterator(); while (it.hasNext()) { NameValuePair nvp = it.next(); builder.addTextBody(nvp.getName(), nvp.getValue()); } p.setEntity(builder.build()); return -1; } @Override public int consumeResponse(InputStream s) throws Exception { String response = getAnswer(s); if (response.equals("true")) { return 1; } else { throw new Exception(response); } } }); if (r != 1) { throw new Exception("Unexpected error, result code = " + r); } } catch (Exception e) { throw new StorageException("Cannot upload file " + remoteFile, e); } }
From source file:com.oneops.client.api.resource.Design.java
/** * Adds specific platform from Yaml/Json file input * //from w w w .j av a 2 s . c o m * @param platformName * @return * @throws OneOpsClientAPIException */ public JsonPath loadFile(String filecontent) throws OneOpsClientAPIException { if (filecontent == null || filecontent.length() == 0) { String msg = String.format("Missing input file content"); throw new OneOpsClientAPIException(msg); } RequestSpecification request = createRequest(); request.header("Content-Type", "multipart/text"); MultipartEntityBuilder meb = MultipartEntityBuilder.create(); meb.addTextBody("data", filecontent); JSONObject jo = new JSONObject(); jo.put("data", filecontent); Response response = request.parameter("data", filecontent).put(DESIGN_URI + "load"); if (response != null) { if (response.getStatusCode() == 200 || response.getStatusCode() == 302) { return response.getBody().jsonPath(); } else { String msg = String.format("Failed to load yaml content due to %s", response.getStatusLine()); throw new OneOpsClientAPIException(msg); } } String msg = String.format("Failed to load yaml content due to null response"); throw new OneOpsClientAPIException(msg); }
From source file:org.wso2.store.client.ArtifactPublisher.java
/** * Upload assets to ES//from w w w . j av a 2s .c om * POST asset details to asset upload REST API * If attribute is a physical file seek a file in a resources directory and upload as multipart attachment. * @param assetArr Array of assets * @param dir resource files directory */ private void uploadAssets(Asset[] assetArr, File dir) { HashMap<String, String> attrMap; MultipartEntityBuilder multiPartBuilder; List<String> fileAttributes; File imageFile; String responseJson; StringBuilder publisherUrlBuilder; String uploadUrl = hostUrl + ArtifactUploadClientConstants.PUBLISHER_URL + "/"; HttpPost httpPost; CloseableHttpClient httpClient = clientBuilder.build(); CloseableHttpResponse response = null; for (Asset asset : assetArr) { publisherUrlBuilder = new StringBuilder(); if (asset.getId() != null) { publisherUrlBuilder.append(uploadUrl).append(asset.getId()).append("?type=") .append(asset.getType()); } else { publisherUrlBuilder.append(uploadUrl).append("?type=").append(asset.getType()); } multiPartBuilder = MultipartEntityBuilder.create(); multiPartBuilder.addTextBody("sessionId", sessionId); multiPartBuilder.addTextBody("asset", gson.toJson(asset)); attrMap = asset.getAttributes(); httpPost = new HttpPost(publisherUrlBuilder.toString()); //get file type attributes list for asset type fileAttributes = rxtFileAttributesMap.get(asset.getType()); for (String attrKey : attrMap.keySet()) { //check attribute one by one whether is it a file type if (fileAttributes != null && fileAttributes.contains(attrKey)) { imageFile = new File(dir + File.separator + ArtifactUploadClientConstants.RESOURCE_DIR_NAME + File.separator + attrMap.get(attrKey)); multiPartBuilder.addBinaryBody(attrKey, imageFile); } } httpPost.setEntity(multiPartBuilder.build()); try { response = httpClient.execute(httpPost, httpContext); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) { log.info("Asset " + asset.getName() + " uploaded successfully"); } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_ACCEPTED) { log.info("Asset " + asset.getName() + " updated successfully"); } else { responseJson = EntityUtils.toString(response.getEntity()); log.info("Asset " + asset.getName() + " not uploaded successfully " + responseJson); } } catch (IOException ex) { log.error("Asset Id:" + asset.getId() + " Name;" + asset.getName()); log.error("Error in asset Upload", ex); log.debug("Asset upload fail:" + asset); } finally { IOUtils.closeQuietly(response); } } IOUtils.closeQuietly(response); IOUtils.closeQuietly(httpClient); }
From source file:com.ge.research.semtk.sparqlX.SparqlEndpointInterface.java
/** * Execute an auth query using POST/*from w w w. j a v a 2s . com*/ * @return a JSONObject wrapping the results. in the event the results were tabular, they can be obtained in the JsonArray "@Table". if the results were a graph, use "@Graph" for json-ld * @throws Exception */ public JSONObject executeAuthUploadOwl(byte[] owl) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.userName, this.password)); String[] serverNoProtocol = this.server.split("://"); //System.err.println("the new server name is: " + serverNoProtocol[1]); HttpHost targetHost = new HttpHost(serverNoProtocol[1], Integer.valueOf(this.port), "http"); DigestScheme digestAuth = new DigestScheme(); AuthCache authCache = new BasicAuthCache(); digestAuth.overrideParamter("realm", "SPARQL"); // Suppose we already know the expected nonce value digestAuth.overrideParamter("nonce", "whatever"); authCache.put(targetHost, digestAuth); BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); // add new stuff HttpPost httppost = new HttpPost(getUploadURL()); String resultsFormat = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; httppost.addHeader("Accept", resultsFormat); httppost.addHeader("X-Sparql-default-graph", this.dataset); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("graph-uri", this.dataset); builder.addBinaryBody("res-file", owl); HttpEntity entity = builder.build(); httppost.setEntity(entity); /* THIS IS THE MULTIPART FORMAT WE NEED TO SEND. Content-Type: multipart/form-data; boundary=---------------------------32932166721282 Content-Length: 234 -----------------------------32932166721282 Content-Disposition: form-data; name="graph-uri" http://www.kdl.ge.com/changeme -----------------------------32932166721282 Content-Disposition: form-data; name="res-file"; filename="employee.owl" Content-Type: application/octet-stream <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns="http://kdl.ge.com/pd/employee#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" . . . </rdf:RDF> -----------------------------32932166721282-- */ executeTestQuery(); HttpResponse response_http = httpclient.execute(targetHost, httppost, localcontext); HttpEntity resp_entity = response_http.getEntity(); // get response with HTML tags removed String responseTxt = EntityUtils.toString(resp_entity, "UTF-8").replaceAll("\\<.*?>", " "); SimpleResultSet ret = new SimpleResultSet(); if (responseTxt.trim().isEmpty()) { // success or bad login :-( ret.setSuccess(true); } else { ret.setSuccess(false); ret.addRationaleMessage(responseTxt); } resp_entity.getContent().close(); return ret.toJson(); }
From source file:nzilbb.bas.BAS.java
/** * Invoke the Pho2Syl service to syllabify a phonemic transcription. * @param lng <a href="https://tools.ietf.org/html/rfc5646">RFC 5646</a> tag for identifying the language. * @param i Phonemic transcription of the utterance to be segmented. Format is a <a href="http://www.bas.uni-muenchen.de/forschung/Bas/BasFormatseng.html">BAS Partitur Format (BPF)</a> file with a KAN tier. * @param tier Name of tier in the annotation file, whose content is to be syllabified. * @param wsync Whether each word boundary is considered as syllable boundary. * @param oform Output format:/*from w w w. j a v a 2 s . c o m*/ * <ul> * <li>"bpf" - BAS Partiture format</li> * <li>"tg" - TextGrid format</li> * </ul> * @param rate Only needed if <var>oform</var> = "tg" (TextGrid); Sample rate to convert sample values from BAS partiture file to seconds in TextGrid. * @return The response to the request. * @throws IOException If an IO error occurs. * @throws ParserConfigurationException If the XML parser for parsing the response could not be configured. */ public BASResponse Pho2Syl(String lng, InputStream i, String tier, Boolean wsync, String oform, Integer rate) throws IOException, ParserConfigurationException { HttpPost request = new HttpPost(getPho2SylUrl()); MultipartEntityBuilder builder = MultipartEntityBuilder.create().addTextBody("lng", languageTagger.tag(lng)) .addBinaryBody("i", i, ContentType.create("text/plain-bas"), "BAS.par").addTextBody("tier", tier) .addTextBody("oform", oform); if (wsync != null) builder.addTextBody("wsync", wsync ? "yes" : "no"); if (rate != null) builder.addTextBody("rate", rate.toString()); HttpEntity entity = builder.build(); request.setEntity(entity); HttpResponse httpResponse = httpclient.execute(request); HttpEntity result = httpResponse.getEntity(); return new BASResponse(result.getContent()); }