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.IOException;
20  import java.security.cert.Certificate;
21  import java.security.cert.CertificateEncodingException;
22  
23  import javax.xml.namespace.QName;
24  
25  import org.apache.axis.AxisFault;
26  import org.apache.axis.Constants;
27  import org.apache.axis.InternalException;
28  import org.apache.axis.encoding.Base64;
29  import org.apache.axis.encoding.SerializationContext;
30  import org.apache.axis.encoding.SimpleValueSerializer;
31  import org.apache.axis.wsdl.fromJava.Types;
32  import org.w3c.dom.Element;
33  import org.xml.sax.Attributes;
34  
35  /**
36   * Axis 1.x serializer for X509Certificate java type.
37   *
38   * @author atugores
39   * @author rruiz
40   */
41  public class CertificateSerializer implements SimpleValueSerializer {
42    /**
43     * Default Serial Version UID.
44     */
45    private static final long serialVersionUID = 1435657457424L;
46  
47    /**
48     * Qname for the XML type to serialize to.
49     */
50    private final QName xmlType;
51  
52    /**
53     * Creates a new instance.
54     *
55     * @param javaType class
56     * @param xmlType type
57     */
58    public CertificateSerializer(Class javaType, QName xmlType) {
59      this.xmlType = xmlType;
60    }
61  
62    /**
63     * {@inheritDoc}
64     */
65    public void serialize(QName name, Attributes attribs, Object value,
66      SerializationContext context) throws IOException {
67  
68      context.startElement(name, attribs);
69      context.writeSafeString(getValueAsString(value, context));
70      context.endElement();
71    }
72  
73    /**
74     * {@inheritDoc}
75     */
76    public String getValueAsString(Object value, SerializationContext context) {
77      try {
78        Certificate cert = (Certificate)value;
79        return Base64.encode(cert.getEncoded());
80      } catch (CertificateEncodingException e) {
81        throw new InternalException(e);
82      }
83    }
84  
85    /**
86     * {@inheritDoc}
87     */
88    public String getMechanismType() {
89      return Constants.AXIS_SAX;
90    }
91  
92    /**
93     * {@inheritDoc}
94     */
95    public Element writeSchema(Class javaType, Types types) throws AxisFault {
96      // SimpleType restricting xsd:string
97  
98      String base = types.writeType(byte[].class);
99  
100     Element restr = types.createElement("restriction");
101     restr.setAttribute("base", base);
102 
103     Element type = types.createElement("simpleType");
104     types.writeSchemaTypeDecl(xmlType, type);
105     type.setAttribute("name", xmlType.getLocalPart());
106     type.appendChild(restr);
107 
108     return type;
109   }
110 }