EMMA Coverage Report (generated Thu Nov 19 17:07:02 CET 2009)
[all classes][org.apache.james.jdkim.impl]

COVERAGE SUMMARY FOR SOURCE FILE [BodyHasherImpl.java]

nameclass, %method, %block, %line, %
BodyHasherImpl.java100% (1/1)100% (9/9)68%  (84/124)85%  (28/33)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BodyHasherImpl100% (1/1)100% (9/9)68%  (84/124)85%  (28/33)
prepareCanonicalizerOutputStream (int, boolean, OutputStream): OutputStream 100% (1/1)54%  (14/26)71%  (5/7)
BodyHasherImpl (SignatureRecord): void 100% (1/1)62%  (45/73)81%  (13/16)
getDigest (): byte [] 100% (1/1)100% (4/4)100% (1/1)
getDigesterOutputStream (): DigestOutputStream 100% (1/1)100% (3/3)100% (1/1)
getOutputStream (): OutputStream 100% (1/1)100% (3/3)100% (1/1)
getSignatureRecord (): SignatureRecord 100% (1/1)100% (3/3)100% (1/1)
setDigestOutputStream (DigestOutputStream): void 100% (1/1)100% (4/4)100% (2/2)
setOutputStream (OutputStream): void 100% (1/1)100% (4/4)100% (2/2)
setSignatureRecord (SignatureRecord): void 100% (1/1)100% (4/4)100% (2/2)

1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19 
20package org.apache.james.jdkim.impl;
21 
22import java.io.OutputStream;
23import java.security.MessageDigest;
24import java.security.NoSuchAlgorithmException;
25 
26import org.apache.james.jdkim.api.BodyHasher;
27import org.apache.james.jdkim.api.SignatureRecord;
28import org.apache.james.jdkim.canon.DebugOutputStream;
29import org.apache.james.jdkim.canon.DigestOutputStream;
30import org.apache.james.jdkim.canon.LimitedOutputStream;
31import org.apache.james.jdkim.canon.RelaxedBodyCanonicalizer;
32import org.apache.james.jdkim.canon.SimpleBodyCanonicalizer;
33import org.apache.james.jdkim.exceptions.PermFailException;
34 
35public class BodyHasherImpl implements BodyHasher {
36 
37    private static final boolean DEEP_DEBUG = false;
38    private SignatureRecord sign;
39    private DigestOutputStream digesterOS;
40    private OutputStream out;
41 
42    public BodyHasherImpl(SignatureRecord sign) throws PermFailException {
43        MessageDigest md;
44        try {
45            md = MessageDigest.getInstance(sign.getHashAlgo().toString());
46        } catch (NoSuchAlgorithmException e) {
47            throw new PermFailException("Unsupported algorythm: "
48                    + sign.getHashAlgo(), e);
49        }
50 
51        int limit = sign.getBodyHashLimit();
52 
53        // TODO enhance this to use a lookup service.
54        boolean relaxedBody = SignatureRecord.RELAXED.equals(sign
55                .getBodyCanonicalisationMethod());
56 
57        if (!relaxedBody
58                && !SignatureRecord.SIMPLE.equals(sign
59                        .getBodyCanonicalisationMethod())) {
60            throw new PermFailException(
61                    "Unsupported body canonicalization method: "
62                            + sign.getBodyCanonicalisationMethod());
63        }
64 
65        DigestOutputStream dout = new DigestOutputStream(md);
66 
67        OutputStream out = dout;
68        if (DEEP_DEBUG)
69            out = new DebugOutputStream(out);
70        out = prepareCanonicalizerOutputStream(limit, relaxedBody, out);
71 
72        setSignatureRecord(sign);
73        setDigestOutputStream(dout);
74        setOutputStream(out);
75    }
76 
77    static OutputStream prepareCanonicalizerOutputStream(int limit,
78            boolean relaxedBody, OutputStream dout) {
79        OutputStream out = dout;
80        if (limit != -1)
81            out = new LimitedOutputStream(out, limit);
82        if (relaxedBody)
83            out = new RelaxedBodyCanonicalizer(out);
84        else
85            out = new SimpleBodyCanonicalizer(out);
86        return out;
87    }
88 
89    /**
90     * @see org.apache.james.jdkim.api.BodyHasher#getOutputStream()
91     */
92    public OutputStream getOutputStream() {
93        return out;
94    }
95 
96    /**
97     * @see org.apache.james.jdkim.api.BodyHasher#getSignatureRecord()
98     */
99    public SignatureRecord getSignatureRecord() {
100        return sign;
101    }
102 
103    private DigestOutputStream getDigesterOutputStream() {
104        return digesterOS;
105    }
106 
107    /**
108     * @see org.apache.james.jdkim.api.BodyHasher#getDigest()
109     */
110    public byte[] getDigest() {
111        return getDigesterOutputStream().getDigest();
112    }
113 
114    public void setSignatureRecord(SignatureRecord sign) {
115        this.sign = sign;
116    }
117 
118    public void setDigestOutputStream(DigestOutputStream dout) {
119        this.digesterOS = dout;
120    }
121 
122    public void setOutputStream(OutputStream out) {
123        this.out = out;
124    }
125 
126}

[all classes][org.apache.james.jdkim.impl]
EMMA 2.0.5312 (C) Vladimir Roubtsov