List of usage examples for org.apache.http.entity.mime MultipartEntityBuilder addBinaryBody
public MultipartEntityBuilder addBinaryBody(final String name, final InputStream stream, final ContentType contentType, final String filename)
From source file:com.adobe.aem.demo.communities.Loader.java
public static void main(String[] args) { String hostname = null;// w ww . jav a 2 s .co m String port = null; String altport = null; String csvfile = null; String location = null; String language = "en"; String analytics = null; String adminPassword = "admin"; String[] url = new String[10]; // Handling 10 levels maximum for nested comments boolean reset = false; boolean configure = false; int urlLevel = 0; int row = 0; HashMap<String, ArrayList<String>> learningpaths = new HashMap<String, ArrayList<String>>(); // Command line options for this tool Options options = new Options(); options.addOption("h", true, "Hostname"); options.addOption("p", true, "Port"); options.addOption("a", true, "Alternate Port"); options.addOption("f", true, "CSV file"); options.addOption("r", false, "Reset"); options.addOption("u", true, "Admin Password"); options.addOption("c", false, "Configure"); options.addOption("s", true, "Analytics Endpoint"); options.addOption("t", false, "Analytics Tracking"); CommandLineParser parser = new BasicParser(); try { CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("h")) { hostname = cmd.getOptionValue("h"); } if (cmd.hasOption("p")) { port = cmd.getOptionValue("p"); } if (cmd.hasOption("a")) { altport = cmd.getOptionValue("a"); } if (cmd.hasOption("f")) { csvfile = cmd.getOptionValue("f"); } if (cmd.hasOption("u")) { adminPassword = cmd.getOptionValue("u"); } if (cmd.hasOption("t")) { if (cmd.hasOption("s")) { analytics = cmd.getOptionValue("s"); } } if (cmd.hasOption("r")) { reset = true; } if (cmd.hasOption("c")) { configure = true; } if (csvfile == null || port == null || hostname == null) { System.out.println( "Request parameters: -h hostname -p port -a alternateport -u adminPassword -f path_to_CSV_file -r (true|false, delete content before import) -c (true|false, post additional properties)"); System.exit(-1); } } catch (ParseException ex) { logger.error(ex.getMessage()); } String componentType = null; try { logger.debug("AEM Demo Loader: Processing file " + csvfile); // Reading the CSV file, line by line Reader in = new FileReader(csvfile); Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in); for (CSVRecord record : records) { row = row + 1; logger.info("Row: " + row + ", new record: " + record.get(0)); // Let's see if we deal with a comment if (record.get(0).startsWith("#")) { // We can ignore the comment line and move on continue; } // Let's see if we need to terminate this process if (record.get(0).equals(KILL)) { System.exit(1); } // Let's see if we need to create a new Community site if (record.get(0).equals(SITE)) { // Building the form entity to be posted MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setCharset(MIME.UTF8_CHARSET); builder.addTextBody(":operation", "social:createSite", ContentType.create("text/plain", MIME.UTF8_CHARSET)); builder.addTextBody("_charset_", "UTF-8", ContentType.create("text/plain", MIME.UTF8_CHARSET)); String urlName = null; for (int i = 2; i < record.size() - 1; i = i + 2) { if (record.get(i) != null && record.get(i + 1) != null && record.get(i).length() > 0) { String name = record.get(i).trim(); String value = record.get(i + 1).trim(); if (value.equals("TRUE")) { value = "true"; } if (value.equals("FALSE")) { value = "false"; } if (name.equals("urlName")) { urlName = value; } if (name.equals(LANGUAGE)) { language = value; } if (name.equals(BANNER)) { File attachment = new File( csvfile.substring(0, csvfile.indexOf(".csv")) + File.separator + value); builder.addBinaryBody(BANNER, attachment, ContentType.MULTIPART_FORM_DATA, attachment.getName()); } else if (name.equals(THUMBNAIL)) { File attachment = new File( csvfile.substring(0, csvfile.indexOf(".csv")) + File.separator + value); builder.addBinaryBody(THUMBNAIL, attachment, ContentType.MULTIPART_FORM_DATA, attachment.getName()); } else { builder.addTextBody(name, value, ContentType.create("text/plain", MIME.UTF8_CHARSET)); } } } // Site creation String siteId = doPost(hostname, port, "/content.social.json", "admin", adminPassword, builder.build(), "response/siteId"); // Site publishing, if there's a publish instance to publish to if (!port.equals(altport)) { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("id", "nobot")); nameValuePairs.add(new BasicNameValuePair(":operation", "social:publishSite")); nameValuePairs .add(new BasicNameValuePair("path", "/content/sites/" + urlName + "/" + language)); doPost(hostname, port, "/communities/sites.html", "admin", adminPassword, new UrlEncodedFormEntity(nameValuePairs), null); // Wait for site to be available on Publish doWait(hostname, altport, "admin", adminPassword, (siteId != null ? siteId : urlName) + "-groupadministrators"); } continue; } // Let's see if we need to create a new Tag if (record.get(0).equals(TAG)) { // Building the form entity to be posted MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setCharset(MIME.UTF8_CHARSET); builder.addTextBody("_charset_", "UTF-8", ContentType.create("text/plain", MIME.UTF8_CHARSET)); for (int i = 1; i < record.size() - 1; i = i + 2) { if (record.get(i) != null && record.get(i + 1) != null && record.get(i).length() > 0 && record.get(i + 1).length() > 0) { String name = record.get(i).trim(); String value = record.get(i + 1).trim(); builder.addTextBody(name, value, ContentType.create("text/plain", MIME.UTF8_CHARSET)); } } // Tag creation doPost(hostname, port, "/bin/tagcommand", "admin", adminPassword, builder.build(), null); continue; } // Let's see if we need to create a new Community site template, and if we can do it (script run against author instance) if (record.get(0).equals(SITETEMPLATE)) { // Building the form entity to be posted MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setCharset(MIME.UTF8_CHARSET); builder.addTextBody(":operation", "social:createSiteTemplate", ContentType.create("text/plain", MIME.UTF8_CHARSET)); builder.addTextBody("_charset_", "UTF-8", ContentType.create("text/plain", MIME.UTF8_CHARSET)); for (int i = 2; i < record.size() - 1; i = i + 2) { if (record.get(i) != null && record.get(i + 1) != null && record.get(i).length() > 0) { String name = record.get(i).trim(); String value = record.get(i + 1).trim(); builder.addTextBody(name, value, ContentType.create("text/plain", MIME.UTF8_CHARSET)); } } // Site template creation doPost(hostname, port, "/content.social.json", "admin", adminPassword, builder.build(), null); continue; } // Let's see if we need to create a new Community group if (record.get(0).equals(GROUP)) { // Building the form entity to be posted MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setCharset(MIME.UTF8_CHARSET); builder.addTextBody(":operation", "social:createCommunityGroup", ContentType.create("text/plain", MIME.UTF8_CHARSET)); builder.addTextBody("_charset_", "UTF-8", ContentType.create("text/plain", MIME.UTF8_CHARSET)); for (int i = 3; i < record.size() - 1; i = i + 2) { if (record.get(i) != null && record.get(i + 1) != null && record.get(i).length() > 0) { String name = record.get(i).trim(); String value = record.get(i + 1).trim(); if (value.equals("TRUE")) { value = "true"; } if (value.equals("FALSE")) { value = "false"; } if (name.equals(IMAGE)) { File attachment = new File( csvfile.substring(0, csvfile.indexOf(".csv")) + File.separator + value); builder.addBinaryBody(IMAGE, attachment, ContentType.MULTIPART_FORM_DATA, attachment.getName()); } else { builder.addTextBody(name, value, ContentType.create("text/plain", MIME.UTF8_CHARSET)); } } } // Group creation String memberGroupId = doPost(hostname, port, record.get(1), getUserName(record.get(2)), getPassword(record.get(2), adminPassword), builder.build(), "response/memberGroupId"); // Wait for group to be available on Publish, if available logger.debug("Waiting for completion of Community Group creation"); doWait(hostname, port, "admin", adminPassword, memberGroupId); continue; } // Let's see if it's simple Sling Delete request if (record.get(0).equals(SLINGDELETE)) { doDelete(hostname, port, record.get(1), "admin", adminPassword); continue; } // Let's see if we need to add users to an AEM Group if ((record.get(0).equals(GROUPMEMBERS) || record.get(0).equals(SITEMEMBERS)) && record.get(GROUP_INDEX_NAME) != null) { // Checking if we have a member group for this site String groupName = record.get(GROUP_INDEX_NAME); if (record.get(0).equals(SITEMEMBERS)) { // Let's fetch the siteId for this Community Site Url String siteConfig = doGet(hostname, port, groupName, "admin", adminPassword, null); try { String siteId = new JSONObject(siteConfig).getString("siteId"); if (siteId != null) groupName = "community-" + siteId + "-members"; logger.debug("Member group name is " + groupName); } catch (Exception e) { logger.error(e.getMessage()); } } // Pause until the group can found doWait(hostname, port, "admin", adminPassword, groupName); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("filter", "[{\"operation\":\"like\",\"rep:principalName\":\"" + groupName + "\"}]")); nameValuePairs.add(new BasicNameValuePair("type", "groups")); String groupList = doGet(hostname, port, "/libs/social/console/content/content/userlist.social.0.10.json", "admin", adminPassword, nameValuePairs); logger.debug("List of groups" + groupList); if (groupList.indexOf(groupName) > 0) { logger.debug("Group was found on " + port); try { JSONArray jsonArray = new JSONObject(groupList).getJSONArray("items"); if (jsonArray.length() == 1) { JSONObject jsonObject = jsonArray.getJSONObject(0); String groupPath = jsonObject.getString("path"); logger.debug("Group path is " + groupPath); // Constructing a multi-part POST for group membership MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setCharset(MIME.UTF8_CHARSET); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); List<NameValuePair> groupNameValuePairs = buildNVP(record, 2); for (NameValuePair nameValuePair : groupNameValuePairs) { builder.addTextBody(nameValuePair.getName(), nameValuePair.getValue(), ContentType.create("text/plain", MIME.UTF8_CHARSET)); } // Adding the list of group members doPost(hostname, port, groupPath + ".rw.userprops.html", "admin", adminPassword, builder.build(), null); } else { logger.info("We have more than one match for a group with this name!"); } } catch (Exception e) { logger.error(e.getMessage()); } } continue; } // Let's see if it's user related if (record.get(0).equals(USERS)) { //First we need to get the path to the user node String json = doGet(hostname, port, "/libs/granite/security/currentuser.json", getUserName(record.get(1)), getPassword(record.get(1), adminPassword), null); if (json != null) { try { // Fetching the home property String home = new JSONObject(json).getString("home"); if (record.get(2).equals(PREFERENCES)) { home = home + "/preferences"; } else { home = home + "/profile"; } logger.debug(home); // Now we can post all the preferences or the profile List<NameValuePair> nameValuePairs = buildNVP(record, 3); doPost(hostname, port, home, "admin", adminPassword, new UrlEncodedFormEntity(nameValuePairs), null); } catch (Exception e) { logger.error(e.getMessage()); } } continue; } // Let's see if we deal with a new block of content or just a new entry if (record.get(0).equals(CALENDAR) || record.get(0).equals(SLINGPOST) || record.get(0).equals(RATINGS) || record.get(0).equals(BLOG) || record.get(0).equals(JOURNAL) || record.get(0).equals(COMMENTS) || record.get(0).equals(REVIEWS) || record.get(0).equals(FILES) || record.get(0).equals(SUMMARY) || record.get(0).equals(ACTIVITIES) || record.get(0).equals(JOIN) || record.get(0).equals(FOLLOW) || record.get(0).equals(MESSAGE) || record.get(0).equals(ASSET) || record.get(0).equals(AVATAR) || record.get(0).equals(RESOURCE) || record.get(0).equals(LEARNING) || record.get(0).equals(QNA) || record.get(0).equals(FORUM)) { // New block of content, we need to reset the processing to first Level componentType = record.get(0); url[0] = record.get(1); urlLevel = 0; if (!componentType.equals(SLINGPOST) && reset) { int pos = record.get(1).indexOf("/jcr:content"); if (pos > 0) doDelete(hostname, port, "/content/usergenerated" + record.get(1).substring(0, pos), "admin", adminPassword); } // If the Configure command line flag is set, we try to configure the component with all options enabled if (componentType.equals(SLINGPOST) || configure) { String configurePath = getConfigurePath(record.get(1)); List<NameValuePair> nameValuePairs = buildNVP(record, 2); if (nameValuePairs.size() > 2) // Only do this when really have configuration settings doPost(hostname, port, configurePath, "admin", adminPassword, new UrlEncodedFormEntity(nameValuePairs), null); } // We're done with this line, moving on to the next line in the CSV file continue; } // Let's see if we need to indent the list, if it's a reply or a reply to a reply if (record.get(1).length() != 1) continue; // We need a valid level indicator if (Integer.parseInt(record.get(1)) > urlLevel) { url[++urlLevel] = location; logger.debug("Incremented urlLevel to: " + urlLevel + ", with a new location:" + location); } else if (Integer.parseInt(record.get(1)) < urlLevel) { urlLevel = Integer.parseInt(record.get(1)); logger.debug("Decremented urlLevel to: " + urlLevel); } // Get the credentials or fall back to password String password = getPassword(record.get(0), adminPassword); String userName = getUserName(record.get(0)); // Adding the generic properties for all POST requests List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); if (!componentType.equals(RESOURCE)) nameValuePairs.add(new BasicNameValuePair("id", "nobot")); nameValuePairs.add(new BasicNameValuePair("_charset_", "UTF-8")); // Setting some specific fields depending on the content type if (componentType.equals(COMMENTS)) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createComment")); nameValuePairs.add(new BasicNameValuePair("message", record.get(2))); } // Creates a forum post (or reply) if (componentType.equals(FORUM)) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createForumPost")); nameValuePairs.add(new BasicNameValuePair("subject", record.get(2))); nameValuePairs.add(new BasicNameValuePair("message", record.get(3))); } // Follows a user (followedId) for the user posting the request if (componentType.equals(FOLLOW)) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:follow")); nameValuePairs.add(new BasicNameValuePair("userId", "/social/authors/" + userName)); nameValuePairs.add(new BasicNameValuePair("followedId", "/social/authors/" + record.get(2))); } // Uploading Avatar picture if (componentType.equals(AVATAR)) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:changeAvatar")); } // Joins a user (posting the request) to a Community Group (path) if (componentType.equals(JOIN)) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:joinCommunityGroup")); int pos = url[0].indexOf("/configuration.social.json"); if (pos > 0) nameValuePairs.add(new BasicNameValuePair("path", url[0].substring(0, pos) + ".html")); else continue; // Invalid record } // Creates a new private message if (componentType.equals(MESSAGE)) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createMessage")); nameValuePairs.add(new BasicNameValuePair("sendMail", "Sending...")); nameValuePairs.add(new BasicNameValuePair("content", record.get(4))); nameValuePairs.add(new BasicNameValuePair("subject", record.get(3))); nameValuePairs.add(new BasicNameValuePair("serviceSelector", "/bin/community")); nameValuePairs.add(new BasicNameValuePair("to", "/social/authors/" + record.get(2))); nameValuePairs.add(new BasicNameValuePair("userId", "/social/authors/" + record.get(2))); nameValuePairs.add(new BasicNameValuePair(":redirect", "//messaging.html")); nameValuePairs.add(new BasicNameValuePair(":formid", "generic_form")); nameValuePairs.add(new BasicNameValuePair(":formstart", "/content/sites/communities/messaging/compose/jcr:content/content/primary/start")); } // Creates a file or a folder if (componentType.equals(FILES)) { // Top level is always assumed to be a folder, second level files, and third and subsequent levels comments on files if (urlLevel == 0) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createFileLibraryFolder")); nameValuePairs.add(new BasicNameValuePair("name", record.get(2))); nameValuePairs.add(new BasicNameValuePair("message", record.get(3))); } else if (urlLevel == 1) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createComment")); } } // Creates a question, a reply or mark a reply as the best answer if (componentType.equals(QNA)) { if (urlLevel == 0) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createQnaPost")); nameValuePairs.add(new BasicNameValuePair("subject", record.get(2))); nameValuePairs.add(new BasicNameValuePair("message", record.get(3))); } else if (urlLevel == 1) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createQnaPost")); nameValuePairs.add(new BasicNameValuePair("message", record.get(3))); } else if (urlLevel == 2) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:selectAnswer")); } } // Creates an article or a comment if (componentType.equals(JOURNAL) || componentType.equals(BLOG)) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createJournalComment")); nameValuePairs.add(new BasicNameValuePair("subject", record.get(2))); StringBuffer message = new StringBuffer("<p>" + record.get(3) + "</p>"); //We might have more paragraphs to add to the blog or journal article for (int i = 6; i < record.size(); i++) { if (record.get(i).length() > 0) { message.append("<p>" + record.get(i) + "</p>"); } } //We might have some tags to add to the blog or journal article if (record.get(5).length() > 0) { nameValuePairs.add(new BasicNameValuePair("tags", record.get(5))); } nameValuePairs.add(new BasicNameValuePair("message", message.toString())); } // Creates a review or a comment if (componentType.equals(REVIEWS)) { nameValuePairs.add(new BasicNameValuePair("message", record.get(2))); // This might be a top level review, or a comment on a review or another comment if (urlLevel == 0) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createReview")); nameValuePairs.add(new BasicNameValuePair("ratings", record.get(3))); if (record.size() > 4 && record.get(4).length() > 0) { // If we are dealing with a non-existent resource, then the design drives the behavior nameValuePairs.add(new BasicNameValuePair("scf:resourceType", "social/reviews/components/hbs/reviews")); nameValuePairs.add(new BasicNameValuePair("scf:included", record.get(4))); } } else { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createComment")); } } // Creates a rating if (componentType.equals(RATINGS)) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:postTallyResponse")); nameValuePairs.add(new BasicNameValuePair("tallyType", "Rating")); nameValuePairs.add(new BasicNameValuePair("response", record.get(2))); } // Creates a DAM asset if (componentType.equals(ASSET) && record.get(ASSET_INDEX_NAME).length() > 0) { nameValuePairs.add(new BasicNameValuePair("fileName", record.get(ASSET_INDEX_NAME))); } // Creates an enablement resource if (componentType.equals(RESOURCE)) { nameValuePairs.add(new BasicNameValuePair(":operation", "se:createResource")); List<NameValuePair> otherNameValuePairs = buildNVP(record, RESOURCE_INDEX_PROPERTIES); nameValuePairs.addAll(otherNameValuePairs); // Adding the site nameValuePairs.add(new BasicNameValuePair("site", "/content/sites/" + record.get(RESOURCE_INDEX_SITE) + "/resources/en")); // Building the cover image fragment if (record.get(RESOURCE_INDEX_THUMBNAIL).length() > 0) { nameValuePairs.add(new BasicNameValuePair("cover-image", doThumbnail(hostname, port, adminPassword, csvfile, record.get(RESOURCE_INDEX_THUMBNAIL)))); } else { nameValuePairs.add(new BasicNameValuePair("cover-image", "")); } // Building the asset fragment String coverPath = "/content/dam/" + record.get(RESOURCE_INDEX_SITE) + "/resource-assets/" + record.get(2) + "/jcr:content/renditions/cq5dam.thumbnail.319.319.png"; String coverSource = "dam"; String assets = "[{\"cover-img-path\":\"" + coverPath + "\",\"thumbnail-source\":\"" + coverSource + "\",\"asset-category\":\"enablementAsset:dam\",\"resource-asset-name\":null,\"state\":\"A\",\"asset-path\":\"/content/dam/" + record.get(RESOURCE_INDEX_SITE) + "/resource-assets/" + record.get(2) + "\"}]"; nameValuePairs.add(new BasicNameValuePair("assets", assets)); logger.debug("assets:" + assets); } // Creates a learning path if (componentType.equals(LEARNING)) { nameValuePairs.add(new BasicNameValuePair(":operation", "se:editLearningPath")); List<NameValuePair> otherNameValuePairs = buildNVP(record, RESOURCE_INDEX_PROPERTIES); nameValuePairs.addAll(otherNameValuePairs); // Adding the site nameValuePairs.add(new BasicNameValuePair("site", "/content/sites/" + record.get(RESOURCE_INDEX_SITE) + "/resources/en")); // Building the cover image fragment if (record.get(RESOURCE_INDEX_THUMBNAIL).length() > 0) { nameValuePairs.add(new BasicNameValuePair("card-image", doThumbnail(hostname, port, adminPassword, csvfile, record.get(RESOURCE_INDEX_THUMBNAIL)))); } // Building the learning path fragment StringBuffer assets = new StringBuffer("[\""); if (learningpaths.get(record.get(2)) != null) { ArrayList<String> paths = learningpaths.get(record.get(2)); int i = 0; for (String path : paths) { assets.append("{\\\"type\\\":\\\"linked-resource\\\",\\\"path\\\":\\\""); assets.append(path); assets.append("\\\"}"); if (i++ < paths.size() - 1) { assets.append("\",\""); } } } else { logger.debug("No asset for this learning path"); } assets.append("\"]"); nameValuePairs.add(new BasicNameValuePair("learningpath-items", assets.toString())); logger.debug("Learning path:" + assets.toString()); } // Creates a calendar event if (componentType.equals(CALENDAR)) { nameValuePairs.add(new BasicNameValuePair(":operation", "social:createEvent")); try { JSONObject event = new JSONObject(); // Building the JSON fragment for a new calendar event event.accumulate("subject", record.get(2)); event.accumulate("message", record.get(3)); event.accumulate("location", record.get(4)); event.accumulate("tags", ""); event.accumulate("undefined", "update"); String startDate = record.get(5); startDate = startDate.replaceAll("YYYY", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))); startDate = startDate.replaceAll("MM", Integer.toString(1 + Calendar.getInstance().get(Calendar.MONTH))); event.accumulate("start", startDate); String endDate = record.get(6); endDate = endDate.replaceAll("YYYY", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))); endDate = endDate.replaceAll("MM", Integer.toString(1 + Calendar.getInstance().get(Calendar.MONTH))); event.accumulate("end", endDate); nameValuePairs.add(new BasicNameValuePair("event", event.toString())); } catch (Exception ex) { logger.error(ex.getMessage()); } } // Constructing a multi-part POST request MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setCharset(MIME.UTF8_CHARSET); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); for (NameValuePair nameValuePair : nameValuePairs) { builder.addTextBody(nameValuePair.getName(), nameValuePair.getValue(), ContentType.create("text/plain", MIME.UTF8_CHARSET)); } // See if we have attachments for this new post - or some other actions require a form nonetheless if ((componentType.equals(ASSET) || componentType.equals(AVATAR) || componentType.equals(FORUM) || (componentType.equals(JOURNAL)) || componentType.equals(BLOG)) && record.size() > 4 && record.get(ASSET_INDEX_NAME).length() > 0) { File attachment = new File(csvfile.substring(0, csvfile.indexOf(".csv")) + File.separator + record.get(ASSET_INDEX_NAME)); ContentType ct = ContentType.MULTIPART_FORM_DATA; if (record.get(ASSET_INDEX_NAME).indexOf(".mp4") > 0) { ct = ContentType.create("video/mp4", MIME.UTF8_CHARSET); } else if (record.get(ASSET_INDEX_NAME).indexOf(".jpg") > 0 || record.get(ASSET_INDEX_NAME).indexOf(".jpeg") > 0) { ct = ContentType.create("image/jpeg", MIME.UTF8_CHARSET); } else if (record.get(ASSET_INDEX_NAME).indexOf(".png") > 0) { ct = ContentType.create("image/png", MIME.UTF8_CHARSET); } else if (record.get(ASSET_INDEX_NAME).indexOf(".pdf") > 0) { ct = ContentType.create("application/pdf", MIME.UTF8_CHARSET); } else if (record.get(ASSET_INDEX_NAME).indexOf(".zip") > 0) { ct = ContentType.create("application/zip", MIME.UTF8_CHARSET); } builder.addBinaryBody("file", attachment, ct, attachment.getName()); logger.debug("Adding file to payload with name: " + attachment.getName() + " and type: " + ct.getMimeType()); } // If it's a resource or a learning path, we need the path to the resource for subsequent publishing String jsonElement = "location"; if (componentType.equals(RESOURCE)) { jsonElement = "changes/argument"; } if (componentType.equals(LEARNING)) { jsonElement = "path"; } if (componentType.equals(ASSET)) { jsonElement = null; } // This call generally returns the path to the content fragment that was just created location = Loader.doPost(hostname, port, url[urlLevel], userName, password, builder.build(), jsonElement); // If we are loading a DAM asset, we are waiting for all renditions to be generated before proceeding if (componentType.equals(ASSET)) { int pathIndex = url[urlLevel].lastIndexOf(".createasset.html"); if (pathIndex > 0) doWaitPath(hostname, port, adminPassword, url[urlLevel].substring(0, pathIndex) + "/" + record.get(ASSET_INDEX_NAME) + "/jcr:content/renditions", "nt:file"); } // Let's see if it needs to be added to a learning path if (componentType.equals(RESOURCE) && record.get(RESOURCE_INDEX_PATH).length() > 0 && location != null) { // Adding the location to a list of a resources for this particular Learning Path if (learningpaths.get(record.get(RESOURCE_INDEX_PATH)) == null) learningpaths.put(record.get(RESOURCE_INDEX_PATH), new ArrayList<String>()); logger.debug("Adding resource to Learning path: " + record.get(RESOURCE_INDEX_PATH)); ArrayList<String> locations = learningpaths.get(record.get(RESOURCE_INDEX_PATH)); locations.add(location); learningpaths.put(record.get(RESOURCE_INDEX_PATH), locations); } // If it's a Learning Path, we publish it when possible if (componentType.equals(LEARNING) && !port.equals(altport) && location != null) { // Publishing the learning path List<NameValuePair> publishNameValuePairs = new ArrayList<NameValuePair>(); publishNameValuePairs.add(new BasicNameValuePair(":operation", "se:publishEnablementContent")); publishNameValuePairs.add(new BasicNameValuePair("replication-action", "activate")); logger.debug("Publishing a learning path from: " + location); Loader.doPost(hostname, port, location, userName, password, new UrlEncodedFormEntity(publishNameValuePairs), null); // Waiting for the learning path to be published Loader.doWait(hostname, altport, "admin", adminPassword, location.substring(1 + location.lastIndexOf("/")) // Only search for groups with the learning path in it ); // Decorate the resources within the learning path with comments and ratings, randomly generated ArrayList<String> paths = learningpaths.get(record.get(2)); for (String path : paths) { doDecorate(hostname, altport, path, record, analytics); } } // If it's an Enablement Resource, a lot of things need to happen... // Step 1. If it's a SCORM resource, we wait for the SCORM metadata workflow to be complete before proceeding // Step 2. We publish the resource // Step 3. We set a new first published date on the resource (3 weeks earlier) so that reporting data is more meaningful // Step 4. We wait for the resource to be available on publish (checking that associated groups are available) // Step 5. We retrieve the json for the resource on publish to retrieve the Social endpoints // Step 6. We post ratings and comments for each of the enrollees on publish if (componentType.equals(RESOURCE) && !port.equals(altport) && location != null) { // Wait for the data to be fully copied doWaitPath(hostname, port, adminPassword, location + "/assets/asset", "nt:file"); // If we are dealing with a SCORM asset, we wait a little bit before publishing the resource to that the SCORM workflow is completed if (record.get(2).indexOf(".zip") > 0) { doSleep(10000, "SCORM Resource, waiting for workflow to complete"); } // Publishing the resource List<NameValuePair> publishNameValuePairs = new ArrayList<NameValuePair>(); publishNameValuePairs.add(new BasicNameValuePair(":operation", "se:publishEnablementContent")); publishNameValuePairs.add(new BasicNameValuePair("replication-action", "activate")); logger.debug("Publishing a resource from: " + location); Loader.doPost(hostname, port, location, userName, password, new UrlEncodedFormEntity(publishNameValuePairs), null); // Waiting for the resource to be published Loader.doWait(hostname, altport, "admin", adminPassword, location.substring(1 + location.lastIndexOf("/")) // Only search for groups with the resource path in it ); // Setting the first published timestamp so that reporting always comes with 3 weeks of data after building a new demo instance DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, REPORTINGDAYS); List<NameValuePair> publishDateNameValuePairs = new ArrayList<NameValuePair>(); publishDateNameValuePairs .add(new BasicNameValuePair("date-first-published", dateFormat.format(cal.getTime()))); logger.debug("Setting the publish date for a resource from: " + location); doPost(hostname, port, location, userName, password, new UrlEncodedFormEntity(publishDateNameValuePairs), null); // Adding comments and ratings for this resource doDecorate(hostname, altport, location, record, analytics); } } } catch (IOException e) { logger.error(e.getMessage()); } }
From source file:core.VirusTotalAPIHelper.java
public static CloseableHttpResponse scanFile(File fileToScan) { if (apiKey == null || apiKey.isEmpty()) { return null; }// ww w.j av a2s . c om try { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpsScanFilePost = new HttpPost(httpsPost + "file/scan"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("apikey", apiKey); builder.addBinaryBody("file", fileToScan, ContentType.APPLICATION_OCTET_STREAM, fileToScan.getName()); HttpEntity multipart = builder.build(); httpsScanFilePost.setEntity(multipart); CloseableHttpResponse response = client.execute(httpsScanFilePost); return response; } catch (IOException ex) { Exceptions.printStackTrace(ex); } return null; }
From source file:erainformatica.utility.JSONHelper.java
public static TelegramRequestResult<JSONObject> readJsonFromUrl(String url, InputFile file) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost uploadFile = new HttpPost(url); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); //builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN); builder.addBinaryBody(file.getName(), file.getFile_content(), ContentType.APPLICATION_OCTET_STREAM, file.getName() + "." + file.getExtension()); HttpEntity multipart = builder.build(); uploadFile.setEntity(multipart);// ww w. j a v a 2 s .c o m CloseableHttpResponse response = null; try { response = httpClient.execute(uploadFile); } catch (IOException ex) { Logger.getLogger(JSONHelper.class.getName()).log(Level.SEVERE, null, ex); } HttpEntity responseEntity = response.getEntity(); try { return readJsonFromInputStream(responseEntity.getContent()); } catch (Exception ex) { return new TelegramRequestResult<>(false, ex.toString(), null); } }
From source file:io.confluent.support.metrics.utils.WebClient.java
/** * Sends a POST request to a web server/* w w w . ja va2s . com*/ * @param customerId: customer Id on behalf of which the request is sent * @param bytes: request payload * @param httpPost: A POST request structure * @return an HTTP Status code */ public static int send(String customerId, byte[] bytes, HttpPost httpPost) { int statusCode = DEFAULT_STATUS_CODE; if (bytes != null && bytes.length > 0 && httpPost != null && customerId != null) { // add the body to the request MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addTextBody("cid", customerId); builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, "filename"); httpPost.setEntity(builder.build()); // set the HTTP config final RequestConfig config = RequestConfig.custom().setConnectTimeout(requestTimeoutMs) .setConnectionRequestTimeout(requestTimeoutMs).setSocketTimeout(requestTimeoutMs).build(); // send request try (CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config) .build(); CloseableHttpResponse response = httpclient.execute(httpPost)) { log.debug("POST request returned {}", response.getStatusLine().toString()); statusCode = response.getStatusLine().getStatusCode(); } catch (IOException e) { log.debug("Could not submit metrics to Confluent: {}", e.getMessage()); } } else { statusCode = HttpStatus.SC_BAD_REQUEST; } return statusCode; }
From source file:org.activiti.rest.content.service.api.HttpMultipartHelper.java
public static HttpEntity getMultiPartEntity(String fileName, String contentType, InputStream fileStream, Map<String, String> additionalFormFields) throws IOException { MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); if (additionalFormFields != null && !additionalFormFields.isEmpty()) { for (Entry<String, String> field : additionalFormFields.entrySet()) { entityBuilder.addTextBody(field.getKey(), field.getValue()); }//from w ww. ja v a2s . c om } entityBuilder.addBinaryBody(fileName, IOUtils.toByteArray(fileStream), ContentType.create(contentType), fileName); return entityBuilder.build(); }
From source file:org.rapidoid.http.HTTP.java
public static byte[] post(String uri, Map<String, String> headers, Map<String, String> data, Map<String, String> files) throws IOException, ClientProtocolException { headers = U.safe(headers);/*from w ww . j av a 2 s .c om*/ data = U.safe(data); files = U.safe(files); CloseableHttpClient client = client(uri); try { HttpPost httppost = new HttpPost(uri); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Entry<String, String> entry : files.entrySet()) { ContentType contentType = ContentType.create("application/octet-stream"); String filename = entry.getValue(); File file = IO.file(filename); builder = builder.addBinaryBody(entry.getKey(), file, contentType, filename); } for (Entry<String, String> entry : data.entrySet()) { ContentType contentType = ContentType.create("text/plain", "UTF-8"); builder = builder.addTextBody(entry.getKey(), entry.getValue(), contentType); } httppost.setEntity(builder.build()); for (Entry<String, String> e : headers.entrySet()) { httppost.addHeader(e.getKey(), e.getValue()); } Log.info("Starting HTTP POST request", "request", httppost.getRequestLine()); CloseableHttpResponse response = client.execute(httppost); try { int statusCode = response.getStatusLine().getStatusCode(); U.must(statusCode == 200, "Expected HTTP status code 200, but found: %s", statusCode); InputStream resp = response.getEntity().getContent(); return IOUtils.toByteArray(resp); } finally { response.close(); } } finally { client.close(); } }
From source file:com.frochr123.fabqr.FabQRFunctions.java
public static void uploadFabQRProject(String name, String email, String projectName, int licenseIndex, String tools, String description, String location, BufferedImage imageReal, BufferedImage imageScheme, PlfFile plfFile, String lasercutterName, String lasercutterMaterial) throws Exception { // Check for valid situation, otherwise abort if (MainView.getInstance() == null || VisicutModel.getInstance() == null || VisicutModel.getInstance().getPlfFile() == null || !isFabqrActive() || getFabqrPrivateURL() == null || getFabqrPrivateURL().isEmpty() || MaterialManager.getInstance() == null || MappingManager.getInstance() == null || VisicutModel.getInstance().getSelectedLaserDevice() == null) { throw new Exception("FabQR upload exception: Critical error"); }//from www . j a va 2 s.com // Check valid data if (name == null || email == null || projectName == null || projectName.length() < 3 || licenseIndex < 0 || tools == null || tools.isEmpty() || description == null || description.isEmpty() || location == null || location.isEmpty() || imageScheme == null || plfFile == null || lasercutterName == null || lasercutterName.isEmpty() || lasercutterMaterial == null || lasercutterMaterial.isEmpty()) { throw new Exception("FabQR upload exception: Invalid input data"); } // Convert images to byte data for PNG, imageReal is allowed to be empty byte[] imageSchemeBytes = null; ByteArrayOutputStream imageSchemeOutputStream = new ByteArrayOutputStream(); PreviewImageExport.writePngToOutputStream(imageSchemeOutputStream, imageScheme); imageSchemeBytes = imageSchemeOutputStream.toByteArray(); if (imageSchemeBytes == null) { throw new Exception("FabQR upload exception: Error converting scheme image"); } byte[] imageRealBytes = null; if (imageReal != null) { // Need to convert image, ImageIO.write messes up the color space of the original input image BufferedImage convertedImage = new BufferedImage(imageReal.getWidth(), imageReal.getHeight(), BufferedImage.TYPE_3BYTE_BGR); ColorConvertOp op = new ColorConvertOp(null); op.filter(imageReal, convertedImage); ByteArrayOutputStream imageRealOutputStream = new ByteArrayOutputStream(); ImageIO.write(convertedImage, "jpg", imageRealOutputStream); imageRealBytes = imageRealOutputStream.toByteArray(); } // Extract all URLs from used QR codes List<String> referencesList = new LinkedList<String>(); List<PlfPart> plfParts = plfFile.getPartsCopy(); for (PlfPart plfPart : plfParts) { if (plfPart.getQRCodeInfo() != null && plfPart.getQRCodeInfo().getQRCodeSourceURL() != null && !plfPart.getQRCodeInfo().getQRCodeSourceURL().trim().isEmpty()) { // Process url, if it is URL of a FabQR system, remove download flag and point to project page instead // Use regex to check for FabQR system URL structure String qrCodeUrl = plfPart.getQRCodeInfo().getQRCodeSourceURL().trim(); // Check for temporary URL structure of FabQR system Pattern fabQRUrlTemporaryPattern = Pattern .compile("^https{0,1}://.*?" + "/" + FABQR_TEMPORARY_MARKER + "/" + "([a-z]|[0-9]){7,7}$"); // Do not include link if it is just temporary if (fabQRUrlTemporaryPattern.matcher(qrCodeUrl).find()) { continue; } // Check for download URL structure of FabQR system // Change URL to point to project page instead Pattern fabQRUrlDownloadPattern = Pattern .compile("^https{0,1}://.*?" + "/" + FABQR_DOWNLOAD_MARKER + "/" + "([a-z]|[0-9]){7,7}$"); if (fabQRUrlDownloadPattern.matcher(qrCodeUrl).find()) { qrCodeUrl = qrCodeUrl.replace("/" + FABQR_DOWNLOAD_MARKER + "/", "/"); } // Add URL if it is not yet in list if (!referencesList.contains(qrCodeUrl)) { referencesList.add(qrCodeUrl); } } } String references = ""; for (String ref : referencesList) { // Add comma for non first entries if (!references.isEmpty()) { references = references + ","; } references = references + ref; } // Get bytes for PLF file byte[] plfFileBytes = null; ByteArrayOutputStream plfFileOutputStream = new ByteArrayOutputStream(); VisicutModel.getInstance().savePlfToStream(MaterialManager.getInstance(), MappingManager.getInstance(), plfFileOutputStream); plfFileBytes = plfFileOutputStream.toByteArray(); if (plfFileBytes == null) { throw new Exception("FabQR upload exception: Error saving PLF file"); } // Begin uploading data String uploadUrl = getFabqrPrivateURL() + FABQR_API_UPLOAD_PROJECT; // Create HTTP client and cusomized config for timeouts CloseableHttpClient httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(FABQR_UPLOAD_TIMEOUT) .setConnectTimeout(FABQR_UPLOAD_TIMEOUT).setConnectionRequestTimeout(FABQR_UPLOAD_TIMEOUT).build(); // Create HTTP Post request and entity builder HttpPost httpPost = new HttpPost(uploadUrl); httpPost.setConfig(requestConfig); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); // Insert file uploads multipartEntityBuilder.addBinaryBody("imageScheme", imageSchemeBytes, ContentType.APPLICATION_OCTET_STREAM, "imageScheme.png"); multipartEntityBuilder.addBinaryBody("inputFile", plfFileBytes, ContentType.APPLICATION_OCTET_STREAM, "inputFile.plf"); // Image real is allowed to be null, if it is not, send it if (imageRealBytes != null) { multipartEntityBuilder.addBinaryBody("imageReal", imageRealBytes, ContentType.APPLICATION_OCTET_STREAM, "imageReal.png"); } // Prepare content type for text data, especially needed for correct UTF8 encoding ContentType contentType = ContentType.create("text/plain", Consts.UTF_8); // Insert text data multipartEntityBuilder.addTextBody("name", name, contentType); multipartEntityBuilder.addTextBody("email", email, contentType); multipartEntityBuilder.addTextBody("projectName", projectName, contentType); multipartEntityBuilder.addTextBody("licenseIndex", new Integer(licenseIndex).toString(), contentType); multipartEntityBuilder.addTextBody("tools", tools, contentType); multipartEntityBuilder.addTextBody("description", description, contentType); multipartEntityBuilder.addTextBody("location", location, contentType); multipartEntityBuilder.addTextBody("lasercutterName", lasercutterName, contentType); multipartEntityBuilder.addTextBody("lasercutterMaterial", lasercutterMaterial, contentType); multipartEntityBuilder.addTextBody("references", references, contentType); // Assign entity to this post request HttpEntity httpEntity = multipartEntityBuilder.build(); httpPost.setEntity(httpEntity); // Set authentication information String encodedCredentials = Helper.getEncodedCredentials(FabQRFunctions.getFabqrPrivateUser(), FabQRFunctions.getFabqrPrivatePassword()); if (!encodedCredentials.isEmpty()) { httpPost.addHeader("Authorization", "Basic " + encodedCredentials); } // Send request CloseableHttpResponse res = httpClient.execute(httpPost); // React to possible server side errors if (res.getStatusLine() == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new Exception("FabQR upload exception: Server sent wrong HTTP status code: " + new Integer(res.getStatusLine().getStatusCode()).toString()); } // Close everything correctly res.close(); httpClient.close(); }
From source file:me.ixfan.wechatkit.util.HttpClientUtil.java
/** * POST data using the Content-Type <code>multipart/form-data</code>. * This enables uploading of binary files etc. * * @param url URL of request.//from w w w .ja va 2 s . com * @param textInputs Name-Value pairs of text inputs. * @param binaryInputs Name-Value pairs of binary files inputs. * @return JSON object of response. * @throws IOException If I/O error occurs. */ public static JsonObject sendMultipartRequestAndGetJsonResponse(String url, Map<String, String> textInputs, Map<String, MultipartInput> binaryInputs) throws IOException { MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); if (null != textInputs) { textInputs.forEach((k, v) -> multipartEntityBuilder.addTextBody(k, v)); } if (null != binaryInputs) { binaryInputs.forEach((k, v) -> { if (null == v.getDataBytes()) { multipartEntityBuilder.addBinaryBody(k, v.getFile(), v.getContentType(), v.getFile().getName()); } else { multipartEntityBuilder.addBinaryBody(k, v.getDataBytes(), v.getContentType(), v.getFilename()); } }); } CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(url); post.setEntity(multipartEntityBuilder.build()); return httpClient.execute(post, new JsonResponseHandler()); }
From source file:org.rapidoid.http.HttpClientUtil.java
private static NByteArrayEntity paramsBody(Map<String, Object> data, Map<String, List<Upload>> files) { data = U.safe(data);/* w w w . j a va 2s . c o m*/ files = U.safe(files); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Map.Entry<String, List<Upload>> entry : files.entrySet()) { for (Upload file : entry.getValue()) { builder = builder.addBinaryBody(entry.getKey(), file.content(), ContentType.DEFAULT_BINARY, file.filename()); } } for (Map.Entry<String, Object> entry : data.entrySet()) { String name = entry.getKey(); String value = String.valueOf(entry.getValue()); builder = builder.addTextBody(name, value, ContentType.DEFAULT_TEXT); } ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { builder.build().writeTo(stream); } catch (IOException e) { throw U.rte(e); } byte[] bytes = stream.toByteArray(); return new NByteArrayEntity(bytes, ContentType.MULTIPART_FORM_DATA); }
From source file:org.fao.geonet.api.records.editing.InspireValidatorUtils.java
/** * Upload metadata file./*from w w w .java 2s .c o m*/ * * @param endPoint the end point * @param xml the xml * @param client the client (optional) * @return the string * @throws IOException Signals that an I/O exception has occurred. * @throws JSONException the JSON exception */ private static String uploadMetadataFile(String endPoint, InputStream xml, CloseableHttpClient client) throws IOException, JSONException { boolean close = false; if (client == null) { client = HttpClients.createDefault(); close = true; } try { HttpPost request = new HttpPost(endPoint + TestObjects_URL + "?action=upload"); request.addHeader("User-Agent", USER_AGENT); request.addHeader("Accept", ACCEPT); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("fileupload", xml, ContentType.TEXT_XML, "file.xml"); HttpEntity entity = builder.build(); request.setEntity(entity); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == 200) { ResponseHandler<String> handler = new BasicResponseHandler(); String body = handler.handleResponse(response); JSONObject jsonRoot = new JSONObject(body); return jsonRoot.getJSONObject("testObject").getString("id"); } else { Log.warning(Log.SERVICE, "WARNING: INSPIRE service HTTP response: " + response.getStatusLine().getStatusCode() + " for " + TestObjects_URL); return null; } } catch (Exception e) { Log.error(Log.SERVICE, "Error calling INSPIRE service: " + endPoint, e); return null; } finally { if (close) { try { client.close(); } catch (IOException e) { Log.error(Log.SERVICE, "Error closing CloseableHttpClient: " + endPoint, e); } } } }