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;
}
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);
}
Aggregations