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.wss4j.common.token;
21  
22  import org.apache.wss4j.common.WSS4JConstants;
23  import org.apache.wss4j.common.util.DOM2Writer;
24  import org.apache.wss4j.common.util.CommaDelimiterRfc2253Name;
25  import org.apache.wss4j.common.util.XMLUtils;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  
29  import java.math.BigInteger;
30  
31  import javax.security.auth.x500.X500Principal;
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              XMLUtils.getDirectChildElement(element, "X509IssuerName", WSS4JConstants.SIG_NS);
49          issuer = XMLUtils.getElementText(issuerNameElement);
50  
51          Element serialNumberElement =
52              XMLUtils.getDirectChildElement(element, "X509SerialNumber", WSS4JConstants.SIG_NS);
53  
54          String serialNumberStr = XMLUtils.getElementText(serialNumberElement);
55          if (serialNumberStr != null) {
56              serialNumber = new BigInteger(serialNumberStr);
57          } else {
58              serialNumber = null;
59          }
60  
61      }
62  
63      /**
64       * Constructor.
65       */
66      public DOMX509IssuerSerial(Document doc, String issuer, BigInteger serialNumber) {
67          this(doc,issuer,serialNumber,false);
68      }
69  
70      /**
71       * Constructor.
72       */
73      public DOMX509IssuerSerial(Document doc, String issuer, BigInteger serialNumber, boolean isCommaDelimited) {
74          if (issuer == null) {
75              throw new NullPointerException("The issuerName cannot be null");
76          }
77          if (serialNumber == null) {
78              throw new NullPointerException("The serialNumber cannot be null");
79          }
80          if (isCommaDelimited) {
81              this.issuer = new CommaDelimiterRfc2253Name().execute(new X500Principal(issuer).getName());
82          } else {
83              this.issuer = new X500Principal(issuer).getName();
84          }
85          this.serialNumber = serialNumber;
86  
87          element =
88              doc.createElementNS(WSS4JConstants.SIG_NS, "ds:X509IssuerSerial");
89  
90          Element issuerNameElement =
91              doc.createElementNS(WSS4JConstants.SIG_NS, "ds:X509IssuerName");
92          issuerNameElement.appendChild(doc.createTextNode(this.issuer));
93          element.appendChild(issuerNameElement);
94  
95          Element serialNumberElement =
96              doc.createElementNS(WSS4JConstants.SIG_NS, "ds:X509SerialNumber");
97          serialNumberElement.appendChild(doc.createTextNode(serialNumber.toString()));
98          element.appendChild(serialNumberElement);
99      }
100 
101 
102     /**
103      * return the dom element.
104      *
105      * @return the dom element.
106      */
107     public Element getElement() {
108         return element;
109     }
110 
111     /**
112      * Return the issuer name.
113      */
114     public String getIssuer() {
115         return issuer;
116     }
117 
118     /**
119      * Return the Serial Number.
120      */
121     public BigInteger getSerialNumber() {
122         return serialNumber;
123     }
124 
125     /**
126      * return the string representation of the token.
127      *
128      * @return the string representation of the token.
129      */
130     public String toString() {
131         return DOM2Writer.nodeToString(element);
132     }
133 
134 }