001: /*
002: * DirectReference.java
003: *
004: * Created on August 7, 2006, 1:46 PM
005: *
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.ws.security.opt.impl.reference;
028:
029: import com.sun.istack.NotNull;
030: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
031: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036: import javax.xml.bind.JAXBException;
037: import javax.xml.bind.Marshaller;
038: import javax.xml.bind.JAXBElement;
039: import javax.xml.namespace.QName;
040: import javax.xml.stream.XMLStreamException;
041: import javax.xml.stream.XMLStreamReader;
042: import javax.xml.stream.XMLStreamWriter;
043:
044: import com.sun.xml.ws.security.secext10.ReferenceType;
045: import com.sun.xml.ws.security.secext10.ObjectFactory;
046: import com.sun.xml.stream.buffer.XMLStreamBufferResult;
047: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
048: import com.sun.xml.wss.impl.MessageConstants;
049:
050: import java.util.Map;
051: import java.io.OutputStream;
052: import com.sun.xml.ws.api.SOAPVersion;
053:
054: /**
055: * Class for DirectReference reference type inside a STR
056: * @author Ashutosh.Shahi@sun.com
057: */
058: public class DirectReference extends ReferenceType implements
059: com.sun.xml.ws.security.opt.api.reference.DirectReference,
060: SecurityHeaderElement, SecurityElementWriter {
061:
062: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
063:
064: /** Creates a new instance of DirectReference */
065: public DirectReference(SOAPVersion sv) {
066: this .soapVersion = sv;
067: }
068:
069: /**
070: *
071: * @return the valueType attribute of direct reference
072: */
073: public String getValueType() {
074: return super .getValueType();
075: }
076:
077: /**
078: *
079: * @param valueType sets the valueType attribute
080: */
081: public void setValueType(final String valueType) {
082: super .setValueType(valueType);
083: }
084:
085: /**
086: *
087: * @return the URI attribute
088: */
089: public String getURI() {
090: return super .getURI();
091: }
092:
093: /**
094: *
095: * @param uri sets the uri attribute
096: */
097: public void setURI(final String uri) {
098: super .setURI(uri);
099: }
100:
101: /**
102: *
103: * @return the reference type used
104: */
105: public String getType() {
106: return MessageConstants.DIRECT_REFERENCE_TYPE;
107: }
108:
109: public String getId() {
110: QName qname = new QName(MessageConstants.WSU_NS, "Id",
111: MessageConstants.WSU_PREFIX);
112: Map<QName, String> otherAttributes = this .getOtherAttributes();
113: return otherAttributes.get(qname);
114: }
115:
116: public void setId(final String id) {
117: QName qname = new QName(MessageConstants.WSU_NS, "Id",
118: MessageConstants.WSU_PREFIX);
119: Map<QName, String> otherAttributes = this .getOtherAttributes();
120: otherAttributes.put(qname, id);
121: }
122:
123: public String getNamespaceURI() {
124: return MessageConstants.WSSE_NS;
125: }
126:
127: public String getLocalPart() {
128: return "Reference";
129: }
130:
131: public String getAttribute(@NotNull
132: String nsUri, @NotNull
133: String localName) {
134: QName qname = new QName(nsUri, localName);
135: Map<QName, String> otherAttributes = this .getOtherAttributes();
136: return otherAttributes.get(qname);
137: }
138:
139: public String getAttribute(@NotNull
140: QName name) {
141: Map<QName, String> otherAttributes = this .getOtherAttributes();
142: return otherAttributes.get(name);
143: }
144:
145: public XMLStreamReader readHeader() throws XMLStreamException {
146: XMLStreamBufferResult xbr = new XMLStreamBufferResult();
147: JAXBElement<ReferenceType> deirectRefElem = new ObjectFactory()
148: .createReference(this );
149: try {
150: getMarshaller().marshal(deirectRefElem, xbr);
151:
152: } catch (JAXBException je) {
153: throw new XMLStreamException(je);
154: }
155: return xbr.getXMLStreamBuffer().readAsXMLStreamReader();
156: }
157:
158: /**
159: * Writes out the header.
160: *
161: * @throws XMLStreamException
162: * if the operation fails for some reason. This leaves the
163: * writer to an undefined state.
164: */
165: public void writeTo(XMLStreamWriter streamWriter)
166: throws XMLStreamException {
167: JAXBElement<ReferenceType> deirectRefElem = new ObjectFactory()
168: .createReference(this );
169: try {
170: // If writing to Zephyr, get output stream and use JAXB UTF-8 writer
171: if (streamWriter instanceof Map) {
172: OutputStream os = (OutputStream) ((Map) streamWriter)
173: .get("sjsxp-outputstream");
174: if (os != null) {
175: streamWriter.writeCharacters(""); // Force completion of open elems
176: getMarshaller().marshal(deirectRefElem, os);
177: return;
178: }
179: }
180:
181: getMarshaller().marshal(deirectRefElem, streamWriter);
182: } catch (JAXBException e) {
183: throw new XMLStreamException(e);
184: }
185: }
186:
187: /**
188: *
189: * @param streamWriter
190: * @param props
191: * @throws javax.xml.stream.XMLStreamException
192: */
193: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
194: HashMap props) throws javax.xml.stream.XMLStreamException {
195: try {
196: Marshaller marshaller = getMarshaller();
197: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
198: .iterator();
199: while (itr.hasNext()) {
200: Map.Entry<Object, Object> entry = itr.next();
201: marshaller.setProperty((String) entry.getKey(), entry
202: .getValue());
203: }
204: writeTo(streamWriter);
205: } catch (JAXBException jbe) {
206: throw new XMLStreamException(jbe);
207: }
208: }
209:
210: private Marshaller getMarshaller() throws JAXBException {
211: return JAXBUtil.createMarshaller(soapVersion);
212: }
213:
214: /**
215: *
216: * @param os
217: */
218: public void writeTo(OutputStream os) {
219: }
220:
221: public List<String> getReferencedSecHeaderElements() {
222: return Collections.emptyList();
223: }
224:
225: public void addReferencedSecHeaderElement(String id) {
226: }
227:
228: /**
229: *
230: * @param id
231: * @return
232: */
233: public boolean refersToSecHdrWithId(String id) {
234: return false;
235: }
236:
237: }
|