Пример #1
0
  private void doit() {
    DataFlavor xfer_flavors[];
    Object content = null;

    // now let's create a DataHandler
    dh = new DataHandler(fds);
    dh.setCommandMap(cmdMap);
    System.out.println("DCHTest2: DataHandler created");

    // get the dataflavors
    xfer_flavors = dh.getTransferDataFlavors();
    System.out.println(
        "DCHTest2: dh.getTransferDF returned " + xfer_flavors.length + " data flavors.");

    // get the content:
    try {
      content = dh.getContent();
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (content == null) System.out.println("DCHTest2: no content to be had!!!");
    else
      System.out.println(
          "DCHTest2: got content of the following type: " + content.getClass().getName());
  }
Пример #2
0
  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);
  }
Пример #3
0
  /**
   * 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;
 }
 @Transformer
 public String dataHandlerToObject(DataHandler source) throws IOException {
   return (String) source.getContent();
 }