View Javadoc

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  
20  package org.apache.ws.security.message.token;
21  
22  import org.apache.ws.security.WSConstants;
23  import org.apache.ws.security.util.DOM2Writer;
24  import org.apache.ws.security.util.RFC2253Parser;
25  import org.apache.ws.security.util.WSSecurityUtil;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  import org.w3c.dom.Node;
29  import org.w3c.dom.Text;
30  
31  import java.math.BigInteger;
32  
33  /**
34   * An X.509 Issuer Serial token.
35   */
36  public final class DOMX509IssuerSerial {
37      private final Element element;
38      private final String issuer;
39      private final BigInteger serialNumber;
40      
41      /**
42       * Constructor.
43       */
44      public DOMX509IssuerSerial(Element issuerSerialElement) {
45          element = issuerSerialElement;
46          
47          Element issuerNameElement = 
48              WSSecurityUtil.getDirectChildElement(
49                  element, WSConstants.X509_ISSUER_NAME_LN, WSConstants.SIG_NS
50              );
51          issuer = getChildText(issuerNameElement);
52          
53          Element serialNumberElement = 
54              WSSecurityUtil.getDirectChildElement(
55                  element, WSConstants.X509_SERIAL_NUMBER_LN, WSConstants.SIG_NS
56              );
57          String serialNumberStr = getChildText(serialNumberElement);
58          if (serialNumberStr != null) {
59              serialNumber = new BigInteger(serialNumberStr);
60          } else {
61              serialNumber = null;
62          }
63          
64      }
65  
66      /**
67       * Constructor.
68       */
69      public DOMX509IssuerSerial(Document doc, String issuer, BigInteger serialNumber) {
70          if (issuer == null) {
71              throw new NullPointerException("The issuerName cannot be null");
72          }
73          if (serialNumber == null) {
74              throw new NullPointerException("The serialNumber cannot be null");
75          }
76          this.issuer = RFC2253Parser.normalize(issuer);
77          this.serialNumber = serialNumber;
78          
79          element = 
80              doc.createElementNS(
81                  WSConstants.SIG_NS, WSConstants.SIG_PREFIX + ":" + WSConstants.X509_ISSUER_SERIAL_LN
82              );
83          
84          Element issuerNameElement = 
85              doc.createElementNS(
86                  WSConstants.SIG_NS, WSConstants.SIG_PREFIX + ":" + WSConstants.X509_ISSUER_NAME_LN
87              );
88          issuerNameElement.appendChild(doc.createTextNode(this.issuer));
89          element.appendChild(issuerNameElement);
90          
91          Element serialNumberElement = 
92              doc.createElementNS(
93                  WSConstants.SIG_NS, WSConstants.SIG_PREFIX + ":" + WSConstants.X509_SERIAL_NUMBER_LN
94              );
95          serialNumberElement.appendChild(doc.createTextNode(serialNumber.toString()));
96          element.appendChild(serialNumberElement);
97      }
98      
99  
100     /**
101      * return the dom element.
102      * 
103      * @return the dom element.
104      */
105     public Element getElement() {
106         return element;
107     }
108     
109     /**
110      * Return the issuer name.
111      */
112     public String getIssuer() {
113         return issuer;
114     }
115     
116     /**
117      * Return the Serial Number.
118      */
119     public BigInteger getSerialNumber() {
120         return serialNumber;
121     }
122 
123     /**
124      * return the string representation of the token.
125      * 
126      * @return the string representation of the token.
127      */
128     public String toString() {
129         return DOM2Writer.nodeToString((Node)element);
130     }
131     
132     
133     private String getChildText(Node parentNode) {
134         if (parentNode == null) {
135             return null;
136         }
137         Node node = parentNode.getFirstChild();
138         StringBuilder buffer = new StringBuilder();
139         while (node != null) {
140             if (Node.TEXT_NODE == node.getNodeType()) {
141                 buffer.append(((Text)node).getData());
142             }
143             node = node.getNextSibling();
144         }
145         return buffer.toString();
146     }
147 }