001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.impl.crypto;
024:
025: import com.sun.xml.ws.api.message.Header;
026: import com.sun.xml.ws.security.opt.api.SecurityElement;
027: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
028: import com.sun.xml.wss.XWSSecurityException;
029: import com.sun.xml.ws.security.opt.crypto.JAXBData;
030: import com.sun.xml.wss.logging.LogDomainConstants;
031: import java.io.OutputStream;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034: import javax.xml.bind.JAXBContext;
035: import javax.xml.bind.JAXBElement;
036: import javax.xml.bind.JAXBException;
037: import javax.xml.bind.Marshaller;
038: import javax.xml.stream.XMLStreamException;
039: import javax.xml.stream.XMLStreamWriter;
040: import org.jvnet.staxex.NamespaceContextEx;
041: import com.sun.xml.wss.logging.impl.opt.LogStringsMessages;
042:
043: /**
044: *
045: * @author K.Venugopal@sun.com
046: */
047: public class JAXBDataImpl implements JAXBData {
048:
049: private static final Logger logger = Logger.getLogger(
050: LogDomainConstants.IMPL_OPT_DOMAIN,
051: LogDomainConstants.IMPL_OPT_DOMAIN_BUNDLE);
052:
053: private JAXBElement jb;
054: private JAXBContext jc;
055: private Header header = null;
056: private SecurityElement securityElement = null;
057: private boolean contentOnly = false;
058: private NamespaceContextEx nsContext = null;
059:
060: /** Creates a new instance of JAXBDataImpl */
061:
062: public JAXBDataImpl(JAXBElement jb, JAXBContext jc,
063: boolean contentOnly, NamespaceContextEx nsContext) {
064: this .jb = jb;
065: this .jc = jc;
066: this .contentOnly = contentOnly;
067: this .nsContext = nsContext;
068: }
069:
070: public JAXBDataImpl(Header header, boolean contentOnly,
071: NamespaceContextEx nsContext) {
072: this .header = header;
073: this .contentOnly = contentOnly;
074: this .nsContext = nsContext;
075: }
076:
077: public JAXBDataImpl(SecurityElement se,
078: NamespaceContextEx nsContext, boolean contentOnly) {
079: this .securityElement = se;
080: this .contentOnly = contentOnly;
081: this .nsContext = nsContext;
082: }
083:
084: /** Creates a new instance of JAXBDataImpl */
085: public JAXBDataImpl(JAXBElement jb, JAXBContext jc,
086: NamespaceContextEx nsContext) {
087: this .jb = jb;
088: this .jc = jc;
089: this .nsContext = nsContext;
090: }
091:
092: public JAXBDataImpl(Header header) {
093: this .header = header;
094: }
095:
096: public JAXBDataImpl(SecurityElement se) {
097: this .securityElement = se;
098: }
099:
100: public JAXBElement getJAXBElement() {
101: return jb;
102: }
103:
104: public void writeTo(XMLStreamWriter writer)
105: throws XWSSecurityException {
106: if (securityElement != null) {
107: try {
108: ((SecurityElementWriter) securityElement)
109: .writeTo(writer);
110: } catch (XMLStreamException ex) {
111: logger
112: .log(
113: Level.SEVERE,
114: LogStringsMessages
115: .WSS_1609_ERROR_SERIALIZING_ELEMENT(securityElement
116: .getLocalPart()));
117: throw new XWSSecurityException(
118: LogStringsMessages
119: .WSS_1609_ERROR_SERIALIZING_ELEMENT(securityElement
120: .getLocalPart()), ex);
121: }
122: return;
123: }
124:
125: if (header != null) {
126: try {
127: header.writeTo(writer);
128: } catch (XMLStreamException ex) {
129: logger.log(Level.SEVERE, LogStringsMessages
130: .WSS_1609_ERROR_SERIALIZING_ELEMENT(header
131: .getLocalPart()));
132: throw new XWSSecurityException(LogStringsMessages
133: .WSS_1609_ERROR_SERIALIZING_ELEMENT(header
134: .getLocalPart()), ex);
135: }
136: return;
137: }
138:
139: Marshaller mh;
140: try {
141: mh = jc.createMarshaller();
142: mh.marshal(jb, writer);
143: } catch (JAXBException ex) {
144: logger.log(Level.SEVERE, LogStringsMessages
145: .WSS_1610_ERROR_MARSHALLING_JBOBJECT(jb.getName()));
146: throw new XWSSecurityException(LogStringsMessages
147: .WSS_1610_ERROR_MARSHALLING_JBOBJECT(jb.getName()),
148: ex);
149: }
150: }
151:
152: public void writeTo(OutputStream os) throws XWSSecurityException {
153: Marshaller mh;
154: try {
155: mh = jc.createMarshaller();
156: mh.setProperty("com.sun.xml.bind.c14n", true);
157: mh.marshal(jb, os);
158: } catch (javax.xml.bind.JAXBException ex) {
159: logger.log(Level.SEVERE, LogStringsMessages
160: .WSS_1610_ERROR_MARSHALLING_JBOBJECT(jb.getName()));
161: throw new XWSSecurityException(LogStringsMessages
162: .WSS_1610_ERROR_MARSHALLING_JBOBJECT(jb.getName()),
163: ex);
164: }
165: }
166:
167: public NamespaceContextEx getNamespaceContext() {
168: return nsContext;
169: }
170:
171: public SecurityElement getSecurityElement() {
172: return securityElement;
173: }
174: }
|