View Javadoc

1   /*
2    * Copyright  2003-2004 The Apache Software Foundation.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  
18  package org.apache.ws.security.message.token;
19  
20  import org.apache.ws.security.WSConstants;
21  import org.apache.ws.security.WSSConfig;
22  import org.apache.ws.security.WSSecurityException;
23  import org.apache.ws.security.util.DOM2Writer;
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.Node;
27  
28  import javax.xml.namespace.QName;
29  
30  /***
31   * Reference.
32   * <p/>
33   *
34   * @author Davanum Srinivas (dims@yahoo.com).
35   */
36  public class Reference {
37      public static final String TOKEN_LNAME = "Reference";
38      protected Element element = null;
39      protected WSSConfig wssConfig;
40  
41      /***
42       * Constructor.
43       * <p/>
44       *
45       * @param wssConfig
46       * @param elem
47       * @throws WSSecurityException
48       */
49      public Reference(WSSConfig wssConfig, Element elem) throws WSSecurityException {
50          if (elem == null) {
51              throw new WSSecurityException(WSSecurityException.INVALID_SECURITY,
52                      "noReference");
53          }
54          this.element = elem;
55          this.wssConfig = wssConfig;
56          boolean nsOK = false;
57          if (wssConfig.getProcessNonCompliantMessages()) {
58              for (int i = 0; i < WSConstants.WSSE_NS_ARRAY.length; ++i) {
59                  if (WSConstants.WSSE_NS_ARRAY[i].equals(element.getNamespaceURI())) {
60                      nsOK = true;
61                      break;
62                  }
63              }
64          } else if (wssConfig.getWsseNS().equals(element.getNamespaceURI())) {
65              nsOK = true;
66          }
67          if (!nsOK || !element.getLocalName().equals(TOKEN_LNAME)) {
68              QName el = new QName(this.element.getNamespaceURI(), this.element.getLocalName());
69              QName token = new QName(wssConfig.getWsseNS(), TOKEN_LNAME);
70              throw new WSSecurityException(WSSecurityException.FAILURE,
71                      "badElement",
72                      new Object[]{token, el});
73          }
74      }
75  
76      /***
77       * Constructor.
78       * <p/>
79       *
80       * @param wssConfig
81       * @param doc
82       */
83      public Reference(WSSConfig wssConfig, Document doc) {
84          this.wssConfig = wssConfig;
85          this.element =
86                  doc.createElementNS(wssConfig.getWsseNS(), "wsse:" + TOKEN_LNAME);
87      }
88  
89      /***
90       * get the dom element.
91       * <p/>
92       *
93       * @return
94       */
95      public Element getElement() {
96          return this.element;
97      }
98  
99      /***
100      * get the URI.
101      * <p/>
102      *
103      * @return
104      */
105     public String getValueType() {
106         return this.element.getAttribute("ValueType");
107     }
108 
109     /***
110      * get the URI.
111      * <p/>
112      *
113      * @return
114      */
115     public String getURI() {
116         return this.element.getAttribute("URI");
117     }
118 
119     /***
120      * set the Value type.
121      * <p/>
122      *
123      * @param valueType
124      */
125     public void setValueType(String valueType) {
126         this.element.setAttribute("ValueType", valueType);
127     }
128 
129     /***
130      * set the URI.
131      * <p/>
132      *
133      * @param uri
134      */
135     public void setURI(String uri) {
136         this.element.setAttribute("URI", uri);
137     }
138 
139     /***
140      * return the string representation.
141      * <p/>
142      *
143      * @return
144      */
145     public String toString() {
146         return DOM2Writer.nodeToString((Node) this.element);
147     }
148 }