Search in sources :

Example 1 with MimePartWithoutContentException

use of com.axway.ats.action.objects.model.MimePartWithoutContentException in project ats-framework by Axway.

the class MimePackage method getPartChecksum.

/**
     * Return the CRC checksum of a given part
     *
     * @param partIndex
     *            the index of the part
     * @param isAttachment
     *            true if the part is an attachment
     * @return the part checksum
     * @throws PackageException
     */
@PublicAtsApi
public long getPartChecksum(int partIndex, boolean isAttachment) throws PackageException {
    InputStream partDataStream = getPartData(partIndex, isAttachment);
    if (partDataStream != null) {
        try {
            SeekInputStream seekDataStream = new SeekInputStream(partDataStream);
            seekDataStream.seek(0);
            // create a new crc and reset it
            CRC32 crc = new CRC32();
            // use checked stream to get the checksum
            CheckedInputStream stream = new CheckedInputStream(seekDataStream, crc);
            int bufLen = 4096;
            byte[] buffer = new byte[bufLen];
            int numBytesRead = bufLen;
            while (numBytesRead == bufLen) {
                numBytesRead = stream.read(buffer, 0, bufLen);
            }
            long checksum = stream.getChecksum().getValue();
            stream.close();
            return checksum;
        } catch (IOException ioe) {
            throw new PackageException(ioe);
        }
    } else {
        throw new MimePartWithoutContentException("MIME part does not have any content");
    }
}
Also used : SeekInputStream(com.axway.ats.core.io.SeekInputStream) CRC32(java.util.zip.CRC32) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SeekInputStream(com.axway.ats.core.io.SeekInputStream) InputStream(java.io.InputStream) MimePartWithoutContentException(com.axway.ats.action.objects.model.MimePartWithoutContentException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) IOException(java.io.IOException) CheckedInputStream(java.util.zip.CheckedInputStream) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 2 with MimePartWithoutContentException

use of com.axway.ats.action.objects.model.MimePartWithoutContentException in project ats-framework by Axway.

the class Test_MimePartWithoutContentException method constructors.

@Test
public void constructors() {
    MimePartWithoutContentException exception;
    exception = new MimePartWithoutContentException("test");
    assertEquals("test", exception.getMessage());
    assertNull(exception.getCause());
    Exception helperException = new Exception();
    exception = new MimePartWithoutContentException("test", helperException);
    assertEquals("test", exception.getMessage());
    assertEquals(helperException, exception.getCause());
    exception = new MimePartWithoutContentException(helperException);
    assertEquals("java.lang.Exception", exception.getMessage());
    assertEquals(helperException, exception.getCause());
}
Also used : MimePartWithoutContentException(com.axway.ats.action.objects.model.MimePartWithoutContentException) MimePartWithoutContentException(com.axway.ats.action.objects.model.MimePartWithoutContentException) BaseTest(com.axway.ats.rbv.BaseTest) Test(org.junit.Test)

Aggregations

MimePartWithoutContentException (com.axway.ats.action.objects.model.MimePartWithoutContentException)2 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)1 PackageException (com.axway.ats.action.objects.model.PackageException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 SeekInputStream (com.axway.ats.core.io.SeekInputStream)1 BaseTest (com.axway.ats.rbv.BaseTest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CRC32 (java.util.zip.CRC32)1 CheckedInputStream (java.util.zip.CheckedInputStream)1 Test (org.junit.Test)1