Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.joschi.jersey.security.smime;
  
 
 import java.util.List;
 import java.util.Map;

Author(s):
Bill Burke
Version:
$Revision: 1 $
 
 @Produces("*/*")
 public class EnvelopedWriter implements MessageBodyWriter<EnvelopedOutput> {
     static {
         BouncyIntegration.init();
     }
 
     @Context
     protected Providers providers;
 
     @Override
     public boolean isWriteable(Class<?> typeType genericTypeAnnotation[] annotationsMediaType mediaType) {
         return EnvelopedOutput.class.isAssignableFrom(type);
     }
 
     @Override
     public long getSize(EnvelopedOutput smimeOutputClass<?> typeType genericTypeAnnotation[] annotationsMediaType mediaType) {
         return -1;
     }
 
     @Override
     public void writeTo(EnvelopedOutput outClass<?> typeType genericTypeAnnotation[] annotationsMediaType mediaTypeMultivaluedMap<StringObjectheadersOutputStream osthrows IOExceptionWebApplicationException {
         ByteArrayOutputStream baos = null;
         OutputStream encrypted = null;
         try {
             headers.putSingle("Content-Disposition""attachment; filename=\"smime.p7m\"");
             headers.putSingle("Content-Type""application/pkcs7-mime; smime-type=enveloped-data; name=\"smime.p7m\"");
             headers.putSingle("Content-Transfer-Encoding""base64");
 
             OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(.)
                     .setProvider("BC")
                     .build();
             if (out.getCertificate() == nullthrow new NullPointerException("The certificate object was not set.");
             JceKeyTransRecipientInfoGenerator infoGenerator = new JceKeyTransRecipientInfoGenerator(out.getCertificate());
             infoGenerator.setProvider("BC");
             CMSEnvelopedDataStreamGenerator generator = new CMSEnvelopedDataStreamGenerator();
             generator.addRecipientInfoGenerator(infoGenerator);
 
 
             MimeBodyPart _msg = createBodyPart(out);
 
             baos = new ByteArrayOutputStream();
             encrypted = generator.open(baosencryptor);
 
             _msg.writeTo(encrypted);
             encrypted.close();
             byte[] bytes = baos.toByteArray();
             String str = Base64.encodeBytes(bytes.);
             os.write(str.getBytes());
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
 
     public static MimeBodyPart createBodyPart(Providers providersSMIMEOutput outthrows IOExceptionMessagingException {
         ByteArrayOutputStream bodyOs = new ByteArrayOutputStream();
         MessageBodyWriter writer = providers.getMessageBodyWriter(out.getType(), out.getGenericType(), nullout.getMediaType());
         if (writer == null) {
             throw new RuntimeException("Failed to find writer for type: " + out.getType().getName());
         }
         MultivaluedMap<StringStringbodyHeaders = new MultivaluedMapImpl();
         bodyHeaders.add("Content-Type"out.getMediaType().toString());
         writer.writeTo(out.getEntity(), out.getType(), out.getGenericType(), nullout.getMediaType(), bodyHeadersbodyOs);
 
 
        InternetHeaders ih = new InternetHeaders();
        for (Map.Entry<StringList<String>> entry : bodyHeaders.entrySet()) {
            for (String value : entry.getValue()) {
                ih.addHeader(entry.getKey(), value);
            }
        }
        return new MimeBodyPart(ihbodyOs.toByteArray());
    }
New to GrepCode? Check out our FAQ X