예제 #1
0
  public void testHeaders() throws Exception {
    MimeMultipart smm =
        generateMultiPartRsa("SHA1withRSA", msg, SMIMESignedGenerator.RFC3851_MICALGS);
    BodyPart bp = smm.getBodyPart(1);

    assertEquals(
        "application/pkcs7-signature; name=smime.p7s; smime-type=signed-data",
        bp.getHeader("Content-Type")[0]);
    assertEquals("attachment; filename=\"smime.p7s\"", bp.getHeader("Content-Disposition")[0]);
    assertEquals("S/MIME Cryptographic Signature", bp.getHeader("Content-Description")[0]);
  }
예제 #2
0
 private void writePart(BodyPart part, ByteArrayOutputStream bOut)
     throws MessagingException, IOException {
   if (part.getHeader("Content-Transfer-Encoding")[0].equals("binary")) {
     part.writeTo(bOut);
   } else {
     part.writeTo(new CRLFOutputStream(bOut));
   }
 }
  @Test
  public void sendTextMail() throws Exception {
    // Step 1: Create a JavaMail Session
    Properties properties = this.configuration.getAllProperties();
    assertEquals("true", properties.getProperty(DefaultMailSenderConfiguration.JAVAMAIL_SMTP_AUTH));
    Session session = Session.getInstance(properties, new XWikiAuthenticator(this.configuration));

    // Step 2: Create the Message to send
    MimeMessage message = new MimeMessage(session);
    message.setSubject("subject");
    message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("*****@*****.**"));

    // Step 3: Add the Message Body
    Multipart multipart = new MimeMultipart("mixed");
    // Add text in the body
    multipart.addBodyPart(
        this.defaultBodyPartFactory.create(
            "some text here", Collections.<String, Object>singletonMap("mimetype", "text/plain")));
    message.setContent(multipart);

    // Step 4: Send the mail
    this.sender.sendAsynchronously(Arrays.asList(message), session, null);

    // Verify that the mail has been received (wait maximum 10 seconds).
    this.mail.waitForIncomingEmail(10000L, 1);
    MimeMessage[] messages = this.mail.getReceivedMessages();

    assertEquals(1, messages.length);
    assertEquals("subject", messages[0].getHeader("Subject", null));
    assertEquals("*****@*****.**", messages[0].getHeader("To", null));

    assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());

    BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
    assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
    assertEquals("some text here", textBodyPart.getContent());
  }
예제 #4
0
 private Collection<Future<HTTPResponse>> handleBodyPart(
     HttpServletRequest request, Entity blog, BodyPart bodyPart, final Settings settings)
     throws MessagingException, IOException {
   ImagesService imagesService = ImagesServiceFactory.getImagesService();
   DS ds = DS.get();
   Collection<Future<HTTPResponse>> futures = new ArrayList<>();
   String contentType = bodyPart.getContentType();
   log(contentType);
   Object content = bodyPart.getContent();
   if (content instanceof InputStream) {
     String filename = bodyPart.getFileName();
     byte[] bytes = getBytes(bodyPart);
     String digestString = DS.getDigest(bytes);
     Entity metadata = createMetadata(digestString, filename, contentType, bytes);
     if (contentType.startsWith("image/")) {
       int oWidth;
       int oHeight;
       int wWidth;
       int wHeight;
       Image image = ImagesServiceFactory.makeImage(bytes);
       if (settings.isFixPic()) {
         Transform makeImFeelingLucky = ImagesServiceFactory.makeImFeelingLucky();
         image = imagesService.applyTransform(makeImFeelingLucky, image);
       }
       oWidth = image.getWidth();
       oHeight = image.getHeight();
       if (image.getHeight() > settings.getPicMaxHeight()
           || image.getWidth() > settings.getPicMaxWidth()) {
         log(
             "shrinking ["
                 + image.getHeight()
                 + ", "
                 + image.getWidth()
                 + "] > ["
                 + settings.getPicMaxHeight()
                 + ", "
                 + settings.getPicMaxWidth()
                 + "]");
         Transform makeResize =
             ImagesServiceFactory.makeResize(
                 settings.getPicMaxHeight(), settings.getPicMaxWidth());
         Image shrinken = imagesService.applyTransform(makeResize, image);
         wWidth = shrinken.getWidth();
         wHeight = shrinken.getHeight();
         Future<HTTPResponse> res =
             postBlobs(
                 filename,
                 contentType,
                 digestString,
                 shrinken.getImageData(),
                 WebSizeProperty,
                 request,
                 wWidth,
                 wHeight);
         futures.add(res);
       } else {
         wWidth = image.getWidth();
         wHeight = image.getHeight();
         Future<HTTPResponse> res =
             postBlobs(
                 filename,
                 contentType,
                 digestString,
                 bytes,
                 WebSizeProperty,
                 request,
                 wWidth,
                 wHeight);
         futures.add(res);
       }
       Future<HTTPResponse> res =
           postBlobs(
               filename,
               contentType,
               digestString,
               bytes,
               OriginalSizeProperty,
               request,
               oWidth,
               oHeight);
       futures.add(res);
       String[] cids = bodyPart.getHeader("Content-ID");
       if (cids != null && cids.length > 0) {
         String alt = (String) metadata.getProperty(UserCommentProperty);
         if (alt == null) {
           alt = "";
         }
         replaceBlogRef(blog, cids[0], digestString, wWidth, wHeight, alt);
       }
     }
     if (contentType.startsWith("application/vnd.google-earth.kml+xml")
         || filename.endsWith(".kml")) {
       try {
         InputStream is = (InputStream) content;
         KML kml = new KML(is);
         PlacemarkUpdater pu = new PlacemarkUpdater(ds, kml, LocatorLevel.Field);
         pu.visit(kml, null);
       } catch (JAXBException ex) {
         log("reading kml failed", ex);
       }
     }
     if (contentType.startsWith("application/vnd.google-earth.kmz") || filename.endsWith(".kmz")) {
       try {
         InputStream is = (InputStream) content;
         KMZ kmz = new KMZ(is);
         PlacemarkUpdater pu = new PlacemarkUpdater(ds, kmz, LocatorLevel.Field);
         pu.visit(kmz, null);
       } catch (JAXBException ex) {
         log("reading kmz failed", ex);
       }
     }
     if (filename.endsWith(".gpx")) {
       try {
         InputStream is = (InputStream) content;
         final GPX gpx = new GPX(is);
         final OpenCPNTrackHandler handler = new OpenCPNTrackHandler(ds);
         RunInNamespace rin =
             new RunInNamespace() {
               @Override
               protected Object run() {
                 gpx.browse(
                     settings.getTrackBearingTolerance(),
                     settings.getTrackMinimumDistance(),
                     settings.getTrackMaxSpeed(),
                     handler);
                 return null;
               }
             };
         rin.doIt(null, settings.isCommonPlacemarks());
       } catch (JAXBException ex) {
         log("reading gpx failed", ex);
       }
     }
     if (filename.endsWith(".trc")) {
       InputStream is = (InputStream) content;
       final TrackInput trackInput = new TrackInput(is);
       final CompressedTrackHandler cth = new CompressedTrackHandler(ds);
       RunInNamespace rin =
           new RunInNamespace() {
             @Override
             protected Object run() {
               try {
                 cth.handle(trackInput);
               } catch (IOException ex) {
                 log(ex.getMessage(), ex);
               }
               return null;
             }
           };
       rin.doIt(null, settings.isCommonPlacemarks());
     }
     if (contentType.startsWith("application/X-jsr179-location-nmea")
         || filename.endsWith(".nmea")) {
       log("NMEA not yet supported");
     }
   }
   return futures;
 }