001: /*
002: * SignedMessagePart.java
003: *
004: * Created on August 24, 2006, 2:19 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.dsig;
028:
029: import com.sun.xml.ws.security.opt.api.SecurityElement;
030: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
031: import com.sun.xml.ws.security.opt.api.SignedData;
032: import com.sun.xml.ws.security.opt.impl.message.SOAPBody;
033: import com.sun.xml.wss.impl.XWSSecurityRuntimeException;
034: import java.io.IOException;
035: import java.io.OutputStream;
036: import java.io.ByteArrayOutputStream;
037: import java.util.HashMap;
038:
039: /**
040: *
041: * @author Ashutosh.Shahi@sun.com
042: */
043: public class SignedMessagePart implements SecurityElement, SignedData,
044: SecurityElementWriter {
045: protected boolean isCanonicalized = false;
046: private SecurityElement se = null;
047: private SOAPBody body = null;
048: private boolean contentOnly = false;
049:
050: private ByteArrayOutputStream storedStream = new ByteArrayOutputStream();
051:
052: protected byte[] digestValue = null;
053:
054: /** Creates a new instance of SignedMessagePart */
055: public SignedMessagePart() {
056: }
057:
058: public SignedMessagePart(SecurityElement se) {
059: this .se = se;
060: }
061:
062: public SignedMessagePart(SOAPBody body, boolean contentOnly) {
063: this .body = body;
064: this .contentOnly = contentOnly;
065: }
066:
067: public String getId() {
068: if (body != null) {
069: if (!contentOnly) {
070: return body.getId();
071: } else {
072: return body.getBodyContentId();
073: }
074: } else {
075: return se.getId();
076: }
077: }
078:
079: public void setId(String id) {
080: if (body != null) {
081: if (!contentOnly) {
082: body.setId(id);
083: } else {
084: body.setBodyContentId(id);
085: }
086: } else {
087: se.setId(id);
088: }
089: }
090:
091: public String getNamespaceURI() {
092: if (body != null) {
093: if (!contentOnly) {
094: return body.getSOAPVersion().nsUri;
095: } else {
096: return body.getPayloadNamespaceURI();
097: }
098: } else {
099: return se.getNamespaceURI();
100: }
101: }
102:
103: public String getLocalPart() {
104: if (body != null) {
105: if (!contentOnly) {
106: return "Body";
107: } else {
108: return body.getPayloadLocalPart();
109: }
110: } else {
111: return se.getLocalPart();
112: }
113: }
114:
115: public javax.xml.stream.XMLStreamReader readHeader()
116: throws javax.xml.stream.XMLStreamException {
117: throw new UnsupportedOperationException();
118: }
119:
120: public void writeTo(OutputStream os) {
121: try {
122: if (isCanonicalized)
123: writeCanonicalized(os);
124: } catch (IOException ioe) {
125: throw new XWSSecurityRuntimeException(ioe);
126: }
127: }
128:
129: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
130: throws javax.xml.stream.XMLStreamException {
131: if (body != null) {
132: body.cachePayLoad();//will be replaced with 2nd round of optimization.
133: if (!contentOnly) {
134: body.writeTo(streamWriter);
135: } else {
136: body.writePayload(streamWriter);
137: }
138: } else {
139: ((SecurityElementWriter) se).writeTo(streamWriter);
140: }
141:
142: }
143:
144: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
145: HashMap props) throws javax.xml.stream.XMLStreamException {
146: writeTo(streamWriter);
147: }
148:
149: public void writeCanonicalized(OutputStream os) throws IOException {
150: if (storedStream == null)
151: return;
152: storedStream.writeTo(os);
153: }
154:
155: public void setDigestValue(byte[] digestValue) {
156: this .digestValue = digestValue;
157: }
158:
159: public byte[] getDigestValue() {
160: return digestValue;
161: }
162: }
|