001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * The contents of this file are subject to the terms
039: * of the Common Development and Distribution License
040: * (the "License"). You may not use this file except
041: * in compliance with the License.
042: *
043: * You can obtain a copy of the license at
044: * https://jwsdp.dev.java.net/CDDLv1.0.html
045: * See the License for the specific language governing
046: * permissions and limitations under the License.
047: *
048: * When distributing Covered Code, include this CDDL
049: * HEADER in each file and include the License file at
050: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
051: * add the following below this CDDL HEADER, with the
052: * fields enclosed by brackets "[]" replaced with your
053: * own identifying information: Portions Copyright [yyyy]
054: * [name of copyright owner]
055: */
056: package soap;
057:
058: import java.util.*;
059: import java.io.*;
060:
061: import javax.xml.transform.Source;
062: import javax.xml.transform.stream.StreamSource;
063: import javax.xml.soap.*;
064:
065: import com.sun.xml.messaging.saaj.util.ByteInputStream;
066:
067: import junit.framework.TestCase;
068:
069: public class SourceResetTest extends TestCase {
070:
071: private String msgText[];
072:
073: public SourceResetTest(String name) {
074: super (name);
075: }
076:
077: public void testReset() throws Exception {
078: String message = messageThroughBodyElement
079: + "<ns0:helloWorld><String_1 xsi:type=\"xsd:string\">"
080: + "foo2</String_1></ns0:helloWorld>"
081: + endBodyElementMessage;
082:
083: // JAXRPC servlet creates SOAP message
084: MimeHeaders headers = new MimeHeaders();
085: headers.addHeader("Content-Type", "text/xml");
086: ByteInputStream in = new ByteInputStream(message.getBytes(),
087: message.getBytes().length);
088: //ByteArrayInputStream in = new ByteArrayInputStream(message.getBytes());
089: SOAPMessage msg = MessageFactory.newInstance().createMessage(
090: headers, in);
091:
092: // JAXRPC streaming parser consumes the of stream partially/fully
093: // Then invokes the service method
094: jaxrpcParser(msg);
095:
096: // Uncomment the following to work without exceptions. This resets the
097: // stream. Since SAAJ keeps a copy in JAXMStreamSource, SAAJ can get
098: // bytes any time. JAXRPC doesn't call close() consistently.
099: //in.close();
100:
101: // Service method calls the following to access message. This doesn't
102: // work because the stream is incorrect state.
103: SOAPPart part = msg.getSOAPPart();
104: SOAPEnvelope envelope = part.getEnvelope();
105: }
106:
107: /*
108: * Parses the SOAP message and deserializes to data types
109: */
110: protected static void jaxrpcParser(SOAPMessage msg)
111: throws Exception {
112:
113: Source source = msg.getSOAPPart().getContent();
114: InputStream istream = ((StreamSource) source).getInputStream();
115:
116: if (istream != null) {
117: byte[] buf = new byte[1024];
118: int num = 0;
119: while ((num = istream.read(buf)) != -1) {
120: }
121: }
122: }
123:
124: private static final String UTF8_DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
125:
126: private static final String messageThroughBodyElement = "<env:Envelope "
127: + "xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\" "
128: + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
129: + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
130: + "xmlns:enc=\"http://schemas.xmlsoap.org/soap/encoding/\" "
131: + "xmlns:ns0=\"http://hello.org/wsdl\" "
132: + "env:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
133: + "<env:Body>";
134: private String endBodyElementMessage = "</env:Body></env:Envelope>";
135:
136: }
|