// TODO common function see BpelUploader private void writeResource( DataHandler dataHandler, String destPath, String fileName, File bpelDest) throws IOException { File tempDestFile = new File(destPath, fileName); FileOutputStream fos = null; File destFile = new File(bpelDest, fileName); try { fos = new FileOutputStream(tempDestFile); /* File stream is copied to a temp directory in order handle hot deployment issue occurred in windows */ dataHandler.writeTo(fos); FileUtils.copyFile(tempDestFile, destFile); } catch (FileNotFoundException e) { log.error("Cannot find the file", e); throw e; } catch (IOException e) { log.error("IO error."); throw e; } finally { close(fos); } boolean isDeleted = tempDestFile.delete(); if (!isDeleted) { log.warn( "temp file: " + tempDestFile.getAbsolutePath() + " deletion failed, scheduled deletion on server exit."); tempDestFile.deleteOnExit(); } }
@Override public void storeArtifact( String applicationId, String version, String revision, DataHandler data, String fileName) throws AppFactoryException { String path = getApplicationStorageDirectoryPath(applicationId, version, revision) + File.separator + fileName; try { File destFile = new File(path); if (destFile.exists() == true) { destFile.delete(); } if (!destFile.getParentFile().exists()) { if (!destFile.getParentFile().mkdirs()) { throw new IOException("Failed to create directory structure."); } } FileOutputStream fileOutputStream = new FileOutputStream(destFile); data.writeTo(fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); } catch (IOException e) { log.error("Error storing the artifact file : " + e.getMessage(), e); throw new AppFactoryException("Error storing the artifact file : " + e.getMessage(), e); } }
public static MessageContext sendUsingSwA(String fileName, String targetEPR) throws IOException { Options options = new Options(); options.setTo(new EndpointReference(targetEPR)); options.setAction("urn:uploadFileUsingSwA"); options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP); MessageContext mc = new MessageContext(); System.out.println("Sending file : " + fileName + " as SwA"); FileDataSource fileDataSource = new FileDataSource(new File(fileName)); DataHandler dataHandler = new DataHandler(fileDataSource); String attachmentID = mc.addAttachment(dataHandler); SOAPFactory factory = OMAbstractFactory.getSOAP11Factory(); SOAPEnvelope env = factory.getDefaultEnvelope(); OMNamespace ns = factory.createOMNamespace("http://services.samples/xsd", "m0"); OMElement payload = factory.createOMElement("uploadFileUsingSwA", ns); OMElement request = factory.createOMElement("request", ns); OMElement imageId = factory.createOMElement("imageId", ns); imageId.setText(attachmentID); request.addChild(imageId); payload.addChild(request); env.getBody().addChild(payload); mc.setEnvelope(env); mepClient.addMessageContext(mc); mepClient.execute(true); MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE); SOAPBody body = response.getEnvelope().getBody(); String imageContentId = body.getFirstChildWithName( new QName("http://services.samples/xsd", "uploadFileUsingSwAResponse")) .getFirstChildWithName(new QName("http://services.samples/xsd", "response")) .getFirstChildWithName(new QName("http://services.samples/xsd", "imageId")) .getText(); Attachments attachment = response.getAttachmentMap(); dataHandler = attachment.getDataHandler(imageContentId); File tempFile = File.createTempFile("swa-", ".gif"); FileOutputStream fos = new FileOutputStream(tempFile); dataHandler.writeTo(fos); fos.flush(); fos.close(); System.out.println("Saved response to file : " + tempFile.getAbsolutePath()); return response; }
private void manageAttachments(KpeopleAction action, DataHandler stream) throws IOException { InputStream is = null; String hashcode = null; try { is = stream.getInputStream(); hashcode = Hashcode.getHashcode(is, Hashcode.SHA512); } catch (IOException e) { e.printStackTrace(); throw e; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } String path = (String) currentProperties.get(KpeopleLabel.getEventXMLPath()); try { path = ActionDataProcessorUtil.checkEventXMLPath(path); } catch (Exception e1) { e1.printStackTrace(); } FileOutputStream outputStream = null; File tmpFilePath = null; try { path = path + action.getIdAction() + "-" + fileName; tmpFilePath = new File(path); outputStream = new FileOutputStream(tmpFilePath); stream.writeTo(outputStream); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // ActionDataProcessorUtil.deleteEventFile(tmpFilePath); String idAttachmentId = Long.toString(action.getIdAction()) + "-" + hashcode.substring(0, 6); KpeopleAttachment attachment = new KpeopleAttachment(); attachment.setAttachmentName(fileName); attachment.setAttachmentHashcode(hashcode); attachment.setAttachmentData(path); attachment.setIdAttachment(idAttachmentId); attachment.setAttachmentType(eventResult.getContentType()); eventResult.addAttachment(attachment); }
public byte[] toGWT(DataHandler jaxbObject, GWTMappingContext context) throws GWTMappingException { if (jaxbObject == null) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); try { jaxbObject.writeTo(out); out.flush(); } catch (IOException e) { throw new GWTMappingException(e); } return out.toByteArray(); }
/** * Przy zwrocie wyniku translacji, zapisuje xliffa do pliku i zwraca go jako wynik. * * @param dh - wrapper dla przetłumaczonej treści */ public File saveResultFile(DataHandler dh) throws IOException { /* * File xliff = new File(getXliff()); String name = xliff.getName(); * name = name.replaceFirst(".old", ""); */ File xliffResult = new File(getXliff() + ".transl"); if (!xliffResult.exists()) xliffResult.createNewFile(); FileOutputStream out = new FileOutputStream(xliffResult); dh.writeTo(out); out.flush(); out.close(); return xliffResult; }