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

org.bouncycastle.est.ESTException Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. The APIs are designed primarily to be used in conjunction with the BC FIPS provider. The APIs may also be used with other providers although if being used in a FIPS context it is the responsibility of the user to ensure that any other providers used are FIPS certified.

There is a newer version: 2.0.7
Show newest version
package org.bouncycastle.est;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Exception emitted by EST classes.
 */
public class ESTException
    extends IOException
{
    private Throwable cause;

    private InputStream body;
    private int statusCode;

    private static final long MAX_ERROR_BODY = 8192;

    public ESTException(String msg)
    {
        this(msg, null);
    }

    public ESTException(String msg, Throwable cause)
    {
        super(msg);
        this.cause = cause;
        body = null;
        statusCode = 0;
    }

    public ESTException(String message, Throwable cause, int statusCode, InputStream body)
    {
        super(message);
        this.cause = cause;
        this.statusCode = statusCode;
        if (body != null)
        {
            byte[] b = new byte[8192];
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try
            {
                int i = body.read(b);
                while (i >= 0)
                {
                    if (bos.size() + i > MAX_ERROR_BODY)
                    {
                        i = (int)MAX_ERROR_BODY - bos.size();
                        bos.write(b, 0, i);
                        break;
                    }
                    bos.write(b, 0, i);
                    i = body.read(b);
                }
                bos.flush();
                bos.close();
                this.body = new ByteArrayInputStream(bos.toByteArray());
                body.close();
            }
            catch (Exception ex)
            {
                // This is a best effort read, input stream could be dead by this point.
            }
        }
        else
        {
            this.body = null;
        }
    }

    public Throwable getCause()
    {
        return cause;
    }
    
    public String getMessage()
    {
        return super.getMessage() + " HTTP Status Code: " + statusCode;
    }

    public InputStream getBody()
    {
        return body;
    }

    public int getStatusCode()
    {
        return statusCode;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy