All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.joschi.jersey.security.smime.SignedReader Maven / Gradle / Ivy

The newest version!
package com.github.joschi.jersey.security.smime;

import com.github.joschi.jersey.security.BouncyIntegration;
import com.github.joschi.jersey.util.Types;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.Providers;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
@Provider
@Consumes("*/*")
public class SignedReader implements MessageBodyReader {
    static {
        BouncyIntegration.init();
    }

    @Context
    private Providers providers;

    public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return SignedInput.class.isAssignableFrom(type);
    }

    public SignedInput readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap headers, InputStream entityStream) throws IOException, WebApplicationException {
        Class baseType = null;
        Type baseGenericType = null;

        if (genericType instanceof ParameterizedType) {
            ParameterizedType param = (ParameterizedType) genericType;
            baseGenericType = param.getActualTypeArguments()[0];
            baseType = Types.getRawType(baseGenericType);
        }
        try {
            ByteArrayDataSource ds = new ByteArrayDataSource(entityStream, mediaType.toString());
            MimeMultipart mm = new MimeMultipart(ds);
            SignedInputImpl input = new SignedInputImpl();
            input.setType(baseType);
            input.setGenericType(baseGenericType);
            input.setAnnotations(annotations);
            input.setBody(mm);
            input.setProviders(providers);
            return input;
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy