protected FormDataMultiPart createFileUploadRequest(final File file, final String repositoryPath) throws Exception { InputStream in = new FileInputStream(file); FormDataMultiPart part = new FormDataMultiPart(); part.field("importPath", repositoryPath, MediaType.MULTIPART_FORM_DATA_TYPE) .field("fileUpload", in, MediaType.MULTIPART_FORM_DATA_TYPE) .field("overwriteFile", String.valueOf(true), MediaType.MULTIPART_FORM_DATA_TYPE); part.getField("fileUpload") .setContentDisposition( FormDataContentDisposition.name("fileUpload") .fileName(URLEncoder.encode(file.getName(), "UTF-8")) .build()); return part; }
private void performImportREST() throws ParseException, FileNotFoundException, IOException { String contextURL = getOptionValue( Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_URL_Key"), Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_URL_NAME"), true, false); String path = getOptionValue( Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_PATH_KEY"), Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_PATH_NAME"), true, false); String filePath = getOptionValue( Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_FILEPATH_KEY"), Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_FILEPATH_NAME"), true, false); String charSet = getOptionValue( Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_CHARSET_KEY"), Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_CHARSET_NAME"), false, true); String logFile = getOptionValue( Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_LOGFILE_KEY"), Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_LOGFILE_NAME"), false, true); String importURL = contextURL + API_REPO_FILES_IMPORT; File fileIS = new File(filePath); InputStream in = new FileInputStream(fileIS); /* * wrap in a try/finally to ensure input stream * is closed properly */ try { initRestService(); WebResource resource = client.resource(importURL); String overwrite = getOptionValue( Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_OVERWRITE_KEY"), Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_OVERWRITE_NAME"), true, false); String retainOwnership = getOptionValue( Messages.getInstance() .getString("CommandLineProcessor.INFO_OPTION_RETAIN_OWNERSHIP_KEY"), Messages.getInstance() .getString("CommandLineProcessor.INFO_OPTION_RETAIN_OWNERSHIP_NAME"), true, false); String permission = getOptionValue( Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_PERMISSION_KEY"), Messages.getInstance().getString("CommandLineProcessor.INFO_OPTION_PERMISSION_NAME"), true, false); FormDataMultiPart part = new FormDataMultiPart(); part.field("importDir", path, MediaType.MULTIPART_FORM_DATA_TYPE); part.field( "overwriteAclPermissions", "true".equals(overwrite) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE); part.field( "retainOwnership", "true".equals(retainOwnership) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE); part.field("charSet", charSet == null ? "UTF-8" : charSet); part.field( "applyAclPermissions", "true".equals(permission) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE) .field("fileUpload", in, MediaType.MULTIPART_FORM_DATA_TYPE); // If the import service needs the file name do the following. part.getField("fileUpload") .setContentDisposition( FormDataContentDisposition.name("fileUpload").fileName(fileIS.getName()).build()); // Response response ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, part); if (response != null) { String message = response.getEntity(String.class); System.out.println( Messages.getInstance() .getString("CommandLineProcessor.INFO_REST_RESPONSE_RECEIVED", message)); if (logFile != null && !"".equals(logFile)) { writeFile(message, logFile); } } } catch (Exception e) { System.err.println(e.getMessage()); log.error(e.getMessage()); writeFile(e.getMessage(), logFile); } finally { // if we get here, close input stream in.close(); } }