View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.innergrid.serializers;
18  
19  import java.io.ByteArrayInputStream;
20  import java.security.cert.CertificateException;
21  import java.security.cert.CertificateFactory;
22  import java.security.cert.X509Certificate;
23  
24  import org.apache.axis.InternalException;
25  import org.apache.axis.encoding.Base64;
26  import org.apache.axis.encoding.DeserializationContext;
27  import org.apache.axis.encoding.Deserializer;
28  import org.apache.axis.encoding.DeserializerImpl;
29  import org.xml.sax.SAXException;
30  
31  /**
32   * Axis 1.x deserializer for X509Certificate java type.
33   *
34   * @author atugores
35   * @author rruiz
36   */
37  public class CertificateDeserializer extends DeserializerImpl implements Deserializer {
38    /**
39     * Default Serial Version UID.
40     */
41    private static final long serialVersionUID = 1435657457748L;
42  
43    /**
44     * Certificate Buffer.
45     */
46    private final StringBuffer buffer = new StringBuffer();
47  
48    /**
49     * {@inheritDoc}
50     */
51    @Override public void characters(char[] chars, int start, int end) {
52      buffer.append(chars, start, end);
53    }
54  
55    /**
56     * {@inheritDoc}
57     */
58    @Override public void onEndElement(String namespace, String localName,
59        DeserializationContext context) throws SAXException {
60  
61      // Just in case this instance is somehow reused
62      String buf = buffer.toString();
63      buffer.setLength(0);
64  
65      byte[] encoded = Base64.decode(buf);
66      ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
67  
68      try {
69        CertificateFactory cf = CertificateFactory.getInstance("X.509");
70        while (bais.available() > 0) {
71          value = (X509Certificate)cf.generateCertificate(bais);
72          return;
73        }
74      } catch (CertificateException e) {
75        throw new InternalException(e);
76      }
77  
78      throw new SAXException("No encoded certificates in '" + buf + "'");
79    }
80  }