private Message readMulipartData(MultiPart multiPart) throws Exception { Message message = multiPart.getBodyParts().get(0).getEntityAs(Message.class); InputStream inputStream = null; FileOutputStream outputStream = null; try { if (message.getHeader().getExperimentid() == null || message.getHeader().getExperimentid().isEmpty()) { String newexpID = createExperimentID(); message.getHeader().setExperimentid(newexpID); } String expID = message.getHeader().getExperimentid(); BodyPartEntity entity = (BodyPartEntity) multiPart.getBodyParts().get(1).getEntity(); List<String> list = multiPart.getBodyParts().get(1).getHeaders().get("Content-Transfer-Encoding"); if (list != null && list.contains("base64")) { log.info("Encoded data recieved from the server"); inputStream = new Base64InputStream(entity.getInputStream()); } else { log.info("Data recieved from the server"); inputStream = entity.getInputStream(); } String inputLocation = properties.getProperty(ServiceConstants.INPUTLOCATION); if (inputLocation == null) { inputLocation = "."; } File file = new File(inputLocation + File.separatorChar + expID); log.info("Input Location " + file.getAbsolutePath()); if (file != null && !file.exists()) { file.mkdir(); } File tarfile = new File(file.getAbsolutePath() + File.separator + "hpcinput.tar"); outputStream = new FileOutputStream(tarfile); int i; while ((i = inputStream.read()) != -1) { outputStream.write(i); } String filePath = "file://" + tarfile.getAbsolutePath(); Parameters parameters = new Parameters(); parameters.setName("inputData"); parameters.setValue(filePath); message.getBody().getInput().getParameters().add(parameters); writeJobXML(expID, message); } finally { if (multiPart != null) { multiPart.cleanup(); multiPart.close(); } if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.flush(); outputStream.close(); } } return message; }
private File getFileFromBodyPartEntity(BodyPartEntity bpe, File file) { try { InputStream input = bpe.getInputStream(); OutputStream out = new FileOutputStream(file); byte[] buf = new byte[1024]; int len; while ((len = input.read(buf)) > 0) { out.write(buf, 0, len); } out.close(); input.close(); } catch (IOException e) { System.out.println("An error was produced : " + e.toString()); } return file; }