@SuppressWarnings("unchecked") public void process(Exchange exchange) throws Exception { CxfPayload<SoapHeader> in = exchange.getIn().getBody(CxfPayload.class); // verify request assertEquals(1, in.getBody().size()); Map<String, String> ns = new HashMap<String, String>(); ns.put("ns", MtomTestHelper.SERVICE_TYPES_NS); ns.put("xop", MtomTestHelper.XOP_NS); XPathUtils xu = new XPathUtils(ns); Element body = new XmlConverter().toDOMElement(in.getBody().get(0)); Element ele = (Element) xu.getValue("//ns:Detail/ns:photo/xop:Include", body, XPathConstants.NODE); String photoId = ele.getAttribute("href").substring(4); // skip "cid:" assertEquals(MtomTestHelper.REQ_PHOTO_CID, photoId); ele = (Element) xu.getValue("//ns:Detail/ns:image/xop:Include", body, XPathConstants.NODE); String imageId = ele.getAttribute("href").substring(4); // skip "cid:" assertEquals(MtomTestHelper.REQ_IMAGE_CID, imageId); DataHandler dr = exchange.getIn().getAttachment(photoId); assertEquals("application/octet-stream", dr.getContentType()); MtomTestHelper.assertEquals( MtomTestHelper.REQ_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream())); dr = exchange.getIn().getAttachment(imageId); assertEquals("image/jpeg", dr.getContentType()); MtomTestHelper.assertEquals( MtomTestHelper.requestJpeg, IOUtils.readBytesFromStream(dr.getInputStream())); // create response List<Source> elements = new ArrayList<Source>(); elements.add( new DOMSource( DOMUtils.readXml(new StringReader(MtomTestHelper.RESP_MESSAGE)) .getDocumentElement())); CxfPayload<SoapHeader> sbody = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null); exchange.getOut().setBody(sbody); exchange .getOut() .addAttachment( MtomTestHelper.RESP_PHOTO_CID, new DataHandler( new ByteArrayDataSource( MtomTestHelper.RESP_PHOTO_DATA, "application/octet-stream"))); exchange .getOut() .addAttachment( MtomTestHelper.RESP_IMAGE_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.responseJpeg, "image/jpeg"))); }
/** * Write binary attachment data from a {@link DataHandler}. * * @see XMLStreamWriterEx#writeBinary(DataHandler) */ public void writeBinary(final DataHandler data) throws XMLStreamException { // generate a unique id for the attachment final String id = "uuid:" + UUID.randomUUID().toString(); final DimeRecord dimeRecord = new DimeRecord(); dimeRecord.setContentId(id); dimeRecord.setData(data); if (data.getContentType() != null) { dimeRecord.setTypeNameFormat(TypeNameFormat.MEDIA_TYPE); dimeRecord.setContentType(data.getContentType()); } dimeMessage.add(dimeRecord); // write a reference to the SOAP message writeAttribute("href", id); }
/** * Sets the filename associated with this body part. * * @exception IllegalWriteException if the underlying implementation does not support modification * @exception IllegalStateException if this body part is obtained from a READ_ONLY folder */ public void setFileName(String filename) throws MessagingException { PrivilegedAction a = new GetSystemPropertyAction("mail.mime.encodefilename"); if ("true".equals(AccessController.doPrivileged(a))) { try { filename = MimeUtility.encodeText(filename); } catch (UnsupportedEncodingException e) { throw new MessagingException(e.getMessage(), e); } } String header = getHeader(CONTENT_DISPOSITION_NAME, null); if (header == null) { header = "attachment"; } ContentDisposition cd = new ContentDisposition(header); cd.setParameter("filename", filename); setHeader(CONTENT_DISPOSITION_NAME, cd.toString()); // We will also set the "name" parameter of the Content-Type field // to preserve compatibility with nonconformant MUAs header = getHeader(CONTENT_TYPE_NAME, null); if (header == null) { DataHandler dh0 = getDataHandler(); if (dh0 != null) header = dh0.getContentType(); else header = "text/plain"; } try { ContentType contentType = new ContentType(header); contentType.setParameter("name", filename); setHeader(CONTENT_TYPE_NAME, contentType.toString()); } catch (ParseException e) { } }
public void downloadBAMTool(String toolName, HttpServletResponse response) throws BAMToolboxDepolyerServiceBAMToolboxDeploymentExceptionException { try { ServletOutputStream out = response.getOutputStream(); DataHandler downloadData = stub.downloadToolBox(toolName); if (downloadData != null) { String fileName = ""; if (!toolName.endsWith(".tbox")) { fileName = toolName + ".tbox"; } else fileName = toolName; response.setHeader("Content-Disposition", "fileName=" + fileName); response.setContentType(downloadData.getContentType()); InputStream in = downloadData.getDataSource().getInputStream(); int nextChar; while ((nextChar = in.read()) != -1) { out.write((char) nextChar); } out.flush(); in.close(); } else { out.write("The requested service archive was not found on the server".getBytes()); } } catch (RemoteException e) { log.error(e.getMessage(), e); throw new BAMToolboxDepolyerServiceBAMToolboxDeploymentExceptionException(e); } catch (IOException e) { log.error(e.getMessage(), e); throw new BAMToolboxDepolyerServiceBAMToolboxDeploymentExceptionException(e); } }
private static String addStudyPOIsAndGetPatientID( List<ParticipantObjectIdentification> studyPOIs, RetrieveDocumentSetResponseType rsp) { Attributes attrs = null; String studyIUID, classUID; HashMap<String, HashMap<String, List<String>>> studySopClassMap = new HashMap<String, HashMap<String, List<String>>>(); HashMap<String, List<String>> sopClassInstanceMap; List<String> instances; for (DocumentResponse doc : rsp.getDocumentResponse()) { BufferedInputStream is = null; DicomInputStream dis = null; try { DataHandler dh = doc.getDocument(); is = new BufferedInputStream(dh.getInputStream(), BUF_SIZE); is.mark(BUF_SIZE); dis = new DicomInputStream(new BufferedInputStream(is)); attrs = dis.readDataset(-1, Tag.SeriesInstanceUID); is.reset(); doc.setDocument(new DataHandler(new InputStreamDataSource(is, dh.getContentType()))); studyIUID = attrs.getString(Tag.StudyInstanceUID); classUID = attrs.getString(Tag.SOPClassUID); sopClassInstanceMap = studySopClassMap.get(studyIUID); if (sopClassInstanceMap == null) { sopClassInstanceMap = new HashMap<String, List<String>>(); studySopClassMap.put(studyIUID, sopClassInstanceMap); instances = null; } else { instances = sopClassInstanceMap.get(classUID); } if (instances == null) { instances = new ArrayList<String>(); sopClassInstanceMap.put(classUID, instances); } instances.add(attrs.getString(Tag.SOPInstanceUID)); } catch (IOException x) { log.warn("Failed to read DICOM attachment! instanceUID:" + doc.getDocumentUniqueId(), x); } } for (Entry<String, HashMap<String, List<String>>> e : studySopClassMap.entrySet()) { ParticipantObjectDescription pod = new ParticipantObjectDescription(); studyPOIs.add(createStudyPOI(e.getKey(), pod)); for (Entry<String, List<String>> e1 : e.getValue().entrySet()) { SOPClass sc = new SOPClass(); sc.setUID(e1.getKey()); sc.setNumberOfInstances(e1.getValue().size()); for (String iuid : e1.getValue()) { Instance inst = new Instance(); inst.setUID(iuid); sc.getInstance().add(inst); } pod.getSOPClass().add(sc); } } return attrs == null ? null : attrs.getString(Tag.PatientID); }
public DHResponse echoDataHandler(DHRequest request) { this.ensureInit(); DataHandler dataHandler = request.getDataHandler(); try { if (!dataHandler.getContentType().equals("text/plain")) { throw new WebServiceException("Wrong content type"); } if (!dataHandler.getContent().equals("some string")) { throw new WebServiceException("Wrong data"); } } catch (IOException e) { throw new WebServiceException(e); } DataHandler responseData = new DataHandler("Server data", "text/plain"); return new DHResponse(responseData); }
/** * Updates the headers of this part, based on the content. * * @exception IllegalWriteException if the underlying implementation does not support modification * @exception IllegalStateException if this body part is obtained from a READ_ONLY folder */ protected void updateHeaders() throws MessagingException { if (getDataHandler() != null) { try { String contentType = dh.getContentType(); ContentType ct = new ContentType(contentType); if (ct.match("multipart/*")) { MimeMultipart mmp = (MimeMultipart) dh.getContent(); mmp.updateHeaders(); } else if (ct.match("message/rfc822")) { } else { // Update Content-Transfer-Encoding if (getHeader(CONTENT_TRANSFER_ENCODING_NAME) == null) { setHeader(CONTENT_TRANSFER_ENCODING_NAME, MimeUtility.getEncoding(dh)); } } // Update Content-Type if nonexistent, // and Content-Type "name" with Content-Disposition "filename" // parameter(see setFilename()) if (getHeader(CONTENT_TYPE_NAME) == null) { String disposition = getHeader(CONTENT_DISPOSITION_NAME, null); if (disposition != null) { ContentDisposition cd = new ContentDisposition(disposition); String filename = cd.getParameter("filename"); if (filename != null) { ct.setParameter("name", filename); contentType = ct.toString(); } } setHeader(CONTENT_TYPE_NAME, contentType); } } catch (IOException e) { throw new MessagingException("I/O error", e); } } }
@Override public Object unmarshal(Exchange exchange, InputStream stream) throws IOException, MessagingException { MimeBodyPart mimeMessage; String contentType; Message camelMessage; Object content = null; if (headersInline) { mimeMessage = new MimeBodyPart(stream); camelMessage = exchange.getOut(); MessageHelper.copyHeaders(exchange.getIn(), camelMessage, true); contentType = mimeMessage.getHeader(CONTENT_TYPE, null); // write the MIME headers not generated by javamail as Camel headers Enumeration<?> headersEnum = mimeMessage.getNonMatchingHeaders(STANDARD_HEADERS); while (headersEnum.hasMoreElements()) { Object ho = headersEnum.nextElement(); if (ho instanceof Header) { Header header = (Header) ho; camelMessage.setHeader(header.getName(), header.getValue()); } } } else { // check if this a multipart at all. Otherwise do nothing contentType = exchange.getIn().getHeader(CONTENT_TYPE, String.class); if (contentType == null) { return stream; } try { ContentType ct = new ContentType(contentType); if (!ct.match("multipart/*")) { return stream; } } catch (ParseException e) { LOG.warn("Invalid Content-Type " + contentType + " ignored"); return stream; } camelMessage = exchange.getOut(); MessageHelper.copyHeaders(exchange.getIn(), camelMessage, true); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOHelper.copyAndCloseInput(stream, bos); InternetHeaders headers = new InternetHeaders(); extractHeader(CONTENT_TYPE, camelMessage, headers); extractHeader(MIME_VERSION, camelMessage, headers); mimeMessage = new MimeBodyPart(headers, bos.toByteArray()); bos.close(); } DataHandler dh; try { dh = mimeMessage.getDataHandler(); if (dh != null) { content = dh.getContent(); contentType = dh.getContentType(); } } catch (MessagingException e) { LOG.warn("cannot parse message, no unmarshalling done"); } if (content instanceof MimeMultipart) { MimeMultipart mp = (MimeMultipart) content; content = mp.getBodyPart(0); for (int i = 1; i < mp.getCount(); i++) { BodyPart bp = mp.getBodyPart(i); DefaultAttachment camelAttachment = new DefaultAttachment(bp.getDataHandler()); @SuppressWarnings("unchecked") Enumeration<Header> headers = bp.getAllHeaders(); while (headers.hasMoreElements()) { Header header = headers.nextElement(); camelAttachment.addHeader(header.getName(), header.getValue()); } camelMessage.addAttachmentObject(getAttachmentKey(bp), camelAttachment); } } if (content instanceof BodyPart) { BodyPart bp = (BodyPart) content; camelMessage.setBody(bp.getInputStream()); contentType = bp.getContentType(); if (contentType != null && !DEFAULT_CONTENT_TYPE.equals(contentType)) { camelMessage.setHeader(CONTENT_TYPE, contentType); ContentType ct = new ContentType(contentType); String charset = ct.getParameter("charset"); if (charset != null) { camelMessage.setHeader(Exchange.CONTENT_ENCODING, MimeUtility.javaCharset(charset)); } } } else { // If we find no body part, try to leave the message alone LOG.info("no MIME part found"); } return camelMessage; }
/** * Set the Content and the MimeType from the DatHandler. This method defers to the * setContent(InputStream) method by passing it the InputStream retrieved from the DataHandler. * That means it currently blocks while reading, consuming, and encoding the InputStream. TODO: * The better approach would be to simple store the DataHandler in the exts table directly and * use a custom TypeAdapter for the BinaryObject to read and consume the DataHandler during the * actual Serialization. */ public M data(DataHandler data, Hasher hash, CompressionCodec... comps) throws IOException { data(data.getInputStream(), hash, comps); if (!a) mimeType(data.getContentType()); return (M) this; }