Search in sources :

Example 1 with AbstractContentHandler

use of org.apache.james.mime4j.parser.AbstractContentHandler in project X-Road by nordic-institute.

the class CertHashBasedOcspResponderClient method getOcspResponsesFromServer.

/**
 * Creates a GET request to the internal cert hash based OCSP responder and expects OCSP responses.
 *
 * @param destination URL of the OCSP response provider
 * @return list of OCSP response objects
 * @throws IOException   if I/O errors occurred
 * @throws OCSPException if the response could not be parsed
 */
public static List<OCSPResp> getOcspResponsesFromServer(URL destination) throws IOException, OCSPException {
    HttpURLConnection connection = (HttpURLConnection) destination.openConnection();
    connection.setRequestProperty("Accept", MimeTypes.MULTIPART_RELATED);
    connection.setDoOutput(true);
    connection.setConnectTimeout(SystemProperties.getOcspResponderClientConnectTimeout());
    connection.setReadTimeout(SystemProperties.getOcspResponderClientReadTimeout());
    connection.setRequestMethod(METHOD);
    connection.connect();
    if (!VALID_RESPONSE_CODES.contains(connection.getResponseCode())) {
        log.error("Invalid HTTP response ({}) from responder: {}", connection.getResponseCode(), connection.getResponseMessage());
        throw new IOException(connection.getResponseMessage());
    }
    MimeConfig config = new MimeConfig.Builder().setHeadlessParsing(connection.getContentType()).build();
    final List<OCSPResp> responses = new ArrayList<>();
    final MimeStreamParser parser = new MimeStreamParser(config);
    parser.setContentHandler(new AbstractContentHandler() {

        @Override
        public void startMultipart(BodyDescriptor bd) {
            parser.setFlat();
        }

        @Override
        public void body(BodyDescriptor bd, InputStream is) throws MimeException, IOException {
            if (bd.getMimeType().equalsIgnoreCase(MimeTypes.OCSP_RESPONSE)) {
                responses.add(new OCSPResp(IOUtils.toByteArray(is)));
            }
        }
    });
    try (InputStream is = connection.getInputStream()) {
        parser.parse(is);
    } catch (MimeException e) {
        throw new OCSPException("Error parsing response", e);
    }
    return responses;
}
Also used : BodyDescriptor(org.apache.james.mime4j.stream.BodyDescriptor) AbstractContentHandler(org.apache.james.mime4j.parser.AbstractContentHandler) MimeStreamParser(org.apache.james.mime4j.parser.MimeStreamParser) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) OCSPResp(org.bouncycastle.cert.ocsp.OCSPResp) HttpURLConnection(java.net.HttpURLConnection) MimeConfig(org.apache.james.mime4j.stream.MimeConfig) OCSPException(org.bouncycastle.cert.ocsp.OCSPException) MimeException(org.apache.james.mime4j.MimeException)

Example 2 with AbstractContentHandler

use of org.apache.james.mime4j.parser.AbstractContentHandler in project X-Road by nordic-institute.

the class ProxyMessageDecoder method parseAttachments.

private void parseAttachments(String attachmentContentType, InputStream is) throws MimeException, IOException {
    MimeConfig config = new MimeConfig.Builder().setHeadlessParsing(attachmentContentType).build();
    final MimeStreamParser attachmentParser = new MimeStreamParser(config);
    attachmentParser.setContentHandler(new AbstractContentHandler() {

        private Map<String, String> headers;

        private String partContentType;

        @Override
        public void startHeader() throws MimeException {
            headers = new HashMap<>();
            partContentType = null;
        }

        @Override
        public void field(Field field) throws MimeException {
            if (field.getName().toLowerCase().equals(HEADER_CONTENT_TYPE)) {
                partContentType = field.getBody();
            } else {
                headers.put(field.getName(), field.getBody());
            }
        }

        @Override
        public void startMultipart(BodyDescriptor bd) {
            attachmentParser.setFlat();
        }

        @Override
        public void body(BodyDescriptor bd, InputStream is) throws IOException {
            LOG.trace("attachment body: {}", bd.getMimeType());
            try {
                DigestCalculator dc = CryptoUtils.createDigestCalculator(getHashAlgoId());
                CountingOutputStream cos = new CountingOutputStream(dc.getOutputStream());
                TeeInputStream proxyIs = new TeeInputStream(is, cos, true);
                callback.attachment(partContentType, proxyIs, headers);
                attachmentsByteCount += cos.getByteCount();
                verifier.addPart(MessageFileNames.attachment(++attachmentNo), getHashAlgoId(), dc.getDigest());
            } catch (Exception ex) {
                throw translateException(ex);
            }
        }
    });
    attachmentParser.parse(is);
}
Also used : BodyDescriptor(org.apache.james.mime4j.stream.BodyDescriptor) AbstractContentHandler(org.apache.james.mime4j.parser.AbstractContentHandler) HashMap(java.util.HashMap) MimeStreamParser(org.apache.james.mime4j.parser.MimeStreamParser) TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) DigestCalculator(org.bouncycastle.operator.DigestCalculator) IOException(java.io.IOException) TeeInputStream(org.apache.commons.io.input.TeeInputStream) MimeException(org.apache.james.mime4j.MimeException) ErrorCodes.translateException(ee.ria.xroad.common.ErrorCodes.translateException) IOException(java.io.IOException) CodedException(ee.ria.xroad.common.CodedException) Field(org.apache.james.mime4j.stream.Field) CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) MimeConfig(org.apache.james.mime4j.stream.MimeConfig) MimeException(org.apache.james.mime4j.MimeException)

Aggregations

IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MimeException (org.apache.james.mime4j.MimeException)2 AbstractContentHandler (org.apache.james.mime4j.parser.AbstractContentHandler)2 MimeStreamParser (org.apache.james.mime4j.parser.MimeStreamParser)2 BodyDescriptor (org.apache.james.mime4j.stream.BodyDescriptor)2 MimeConfig (org.apache.james.mime4j.stream.MimeConfig)2 CodedException (ee.ria.xroad.common.CodedException)1 ErrorCodes.translateException (ee.ria.xroad.common.ErrorCodes.translateException)1 HttpURLConnection (java.net.HttpURLConnection)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TeeInputStream (org.apache.commons.io.input.TeeInputStream)1 CountingOutputStream (org.apache.commons.io.output.CountingOutputStream)1 Field (org.apache.james.mime4j.stream.Field)1 OCSPException (org.bouncycastle.cert.ocsp.OCSPException)1 OCSPResp (org.bouncycastle.cert.ocsp.OCSPResp)1 DigestCalculator (org.bouncycastle.operator.DigestCalculator)1