PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Spring-ws-server/src/main/java/com/sun/xml/ws/security/opt/impl/keyinfo/SecurityContextToken.java

https://github.com/pinopisello/SpringInAction
Java | 262 lines | 184 code | 36 blank | 42 comment | 16 complexity | bf46d1bdf0554b5348089ba7fed34ceb MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * The contents of this file are subject to the terms of either the GNU
  7. * General Public License Version 2 only ("GPL") or the Common Development
  8. * and Distribution License("CDDL") (collectively, the "License"). You
  9. * may not use this file except in compliance with the License. You can obtain
  10. * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
  11. * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
  12. * language governing permissions and limitations under the License.
  13. *
  14. * When distributing the software, include this License Header Notice in each
  15. * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  16. * Sun designates this particular file as subject to the "Classpath" exception
  17. * as provided by Sun in the GPL Version 2 section of the License file that
  18. * accompanied this code. If applicable, add the following below the License
  19. * Header, with the fields enclosed by brackets [] replaced by your own
  20. * identifying information: "Portions Copyrighted [year]
  21. * [name of copyright owner]"
  22. *
  23. * Contributor(s):
  24. *
  25. * If you wish your version of this file to be governed by only the CDDL or
  26. * only the GPL Version 2, indicate your decision by adding "[Contributor]
  27. * elects to include this software in this distribution under the [CDDL or GPL
  28. * Version 2] license." If you don't indicate a single choice of license, a
  29. * recipient has the option to distribute your version of this file under
  30. * either the CDDL, the GPL Version 2 or to extend the choice of license to
  31. * its licensees as provided above. However, if you add GPL Version 2 code
  32. * and therefore, elected the GPL Version 2 license, then the option applies
  33. * only if the new code is made subject to such option by the copyright
  34. * holder.
  35. */
  36. package com.sun.xml.ws.security.opt.impl.keyinfo;
  37. import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
  38. import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
  39. import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
  40. import com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory;
  41. import com.sun.xml.ws.security.secconv.impl.bindings.SecurityContextTokenType;
  42. import com.sun.xml.ws.security.trust.WSTrustElementFactory;
  43. import com.sun.xml.wss.impl.XWSSecurityRuntimeException;
  44. import com.sun.xml.wss.impl.c14n.AttributeNS;
  45. import java.io.OutputStream;
  46. import java.util.HashMap;
  47. import java.util.Iterator;
  48. import javax.xml.stream.XMLStreamException;
  49. import java.net.URI;
  50. import java.net.URISyntaxException;
  51. import java.util.ArrayList;
  52. import java.util.List;
  53. import java.util.Map;
  54. import javax.xml.bind.JAXBElement;
  55. import javax.xml.bind.JAXBException;
  56. import javax.xml.bind.Marshaller;
  57. import javax.xml.namespace.QName;
  58. import javax.xml.parsers.DocumentBuilder;
  59. import javax.xml.parsers.DocumentBuilderFactory;
  60. import org.w3c.dom.Document;
  61. import com.sun.xml.ws.api.SOAPVersion;
  62. /**
  63. * SecurityContextToken Implementation
  64. * @author Manveen Kaur manveen.kaur@sun.com
  65. * @author K.Venugopal@sun.com
  66. */
  67. public class SecurityContextToken extends SecurityContextTokenType implements SecurityHeaderElement, SecurityElementWriter, com.sun.xml.ws.security.SecurityContextToken {
  68. public final String SECURITY_CONTEXT_TOKEN = "SecurityContextToken";
  69. private String instance = null;
  70. private URI identifier = null;
  71. private List extElements = null;
  72. private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
  73. public SecurityContextToken(URI identifier, String instance, String wsuId, SOAPVersion sv) {
  74. if (identifier != null) {
  75. setIdentifier(identifier);
  76. }
  77. if (instance != null) {
  78. setInstance(instance);
  79. }
  80. if (wsuId != null){
  81. setWsuId(wsuId);
  82. }
  83. this.soapVersion = sv;
  84. }
  85. // useful for converting from JAXB to our owm impl class
  86. public SecurityContextToken(SecurityContextTokenType sTokenType, SOAPVersion sv){
  87. List<Object> list = sTokenType.getAny();
  88. for (int i = 0; i < list.size(); i++) {
  89. Object object = list.get(i);
  90. if(object instanceof JAXBElement){
  91. JAXBElement obj = (JAXBElement)object;
  92. String local = obj.getName().getLocalPart();
  93. if (local.equalsIgnoreCase("Instance")) {
  94. setInstance((String)obj.getValue());
  95. } else if (local.equalsIgnoreCase("Identifier")){
  96. try {
  97. setIdentifier(new URI((String)obj.getValue()));
  98. }catch (URISyntaxException ex){
  99. throw new RuntimeException(ex);
  100. }
  101. }
  102. }else{
  103. getAny().add(object);
  104. if(extElements == null){
  105. extElements = new ArrayList();
  106. extElements.add(object);
  107. }
  108. }
  109. }
  110. setWsuId(sTokenType.getId());
  111. this.soapVersion = sv;
  112. }
  113. public URI getIdentifier() {
  114. return identifier;
  115. }
  116. public void setIdentifier(URI identifier) {
  117. this.identifier = identifier;
  118. JAXBElement<String> iElement =
  119. (new ObjectFactory()).createIdentifier(identifier.toString());
  120. getAny().add(iElement);
  121. }
  122. public String getInstance() {
  123. return instance;
  124. }
  125. public void setInstance(String instance) {
  126. this.instance = instance;
  127. JAXBElement<String> iElement =
  128. (new ObjectFactory()).createInstance(instance);
  129. getAny().add(iElement);
  130. }
  131. public void setWsuId(String wsuId){
  132. setId(wsuId);
  133. }
  134. public String getWsuId(){
  135. return getId();
  136. }
  137. public String getType() {
  138. return SECURITY_CONTEXT_TOKEN;
  139. }
  140. public Object getTokenValue() {
  141. try {
  142. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  143. dbf.setNamespaceAware(true);
  144. DocumentBuilder db = dbf.newDocumentBuilder();
  145. Document doc = db.newDocument();
  146. javax.xml.bind.Marshaller marshaller = WSTrustElementFactory.getContext().createMarshaller();
  147. JAXBElement<SecurityContextTokenType> tElement = (new ObjectFactory()).createSecurityContextToken((SecurityContextTokenType)this);
  148. marshaller.marshal(tElement, doc);
  149. return doc.getDocumentElement();
  150. } catch (Exception ex) {
  151. throw new RuntimeException(ex.getMessage(), ex);
  152. }
  153. }
  154. public List getExtElements() {
  155. return extElements;
  156. }
  157. public String getNamespaceURI() {
  158. return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
  159. }
  160. public String getLocalPart() {
  161. return "SecurityContextToken";
  162. }
  163. public String getAttribute(String nsUri, String localName) {
  164. throw new UnsupportedOperationException();
  165. }
  166. public String getAttribute(QName name) {
  167. throw new UnsupportedOperationException();
  168. }
  169. public javax.xml.stream.XMLStreamReader readHeader() throws javax.xml.stream.XMLStreamException {
  170. throw new UnsupportedOperationException();
  171. }
  172. public void writeTo(OutputStream os) {
  173. try {
  174. JAXBElement<SecurityContextTokenType> sct =
  175. new com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory().createSecurityContextToken(this);
  176. Marshaller writer = getMarshaller();
  177. writer.marshal(sct, os);
  178. } catch (javax.xml.bind.JAXBException ex) {
  179. throw new XWSSecurityRuntimeException(ex);
  180. }
  181. }
  182. public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter) throws javax.xml.stream.XMLStreamException {
  183. JAXBElement<SecurityContextTokenType> sct =
  184. new com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory().createSecurityContextToken(this);
  185. try {
  186. // If writing to Zephyr, get output stream and use JAXB UTF-8 writer
  187. Marshaller writer = getMarshaller();
  188. if (streamWriter instanceof Map) {
  189. OutputStream os = (OutputStream) ((Map) streamWriter).get("sjsxp-outputstream");
  190. if (os != null) {
  191. streamWriter.writeCharacters(""); // Force completion of open elems
  192. writer.marshal(sct, os);
  193. return;
  194. }
  195. }
  196. writer.marshal(sct, streamWriter);
  197. } catch (JAXBException e) {
  198. throw new XMLStreamException(e);
  199. }
  200. }
  201. public byte[] canonicalize(String algorithm, List<AttributeNS> namespaceDecls) {
  202. throw new UnsupportedOperationException();
  203. }
  204. public boolean isCanonicalized() {
  205. return false;
  206. }
  207. private Marshaller getMarshaller() throws JAXBException{
  208. return JAXBUtil.createMarshaller(soapVersion);
  209. }
  210. public boolean refersToSecHdrWithId(String id) {
  211. return false;
  212. }
  213. public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter, HashMap props) throws javax.xml.stream.XMLStreamException {
  214. try{
  215. Marshaller marshaller = getMarshaller();
  216. Iterator<Map.Entry<Object, Object>> itr = props.entrySet().iterator();
  217. while(itr.hasNext()){
  218. Map.Entry<Object, Object> entry = itr.next();
  219. marshaller.setProperty((String)entry.getKey(), entry.getValue());
  220. }
  221. writeTo(streamWriter);
  222. }catch(JAXBException jbe){
  223. throw new XMLStreamException(jbe);
  224. }
  225. }
  226. }