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
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/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 in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: QuoteTest.java,v 1.2 2007/07/16 16:41:26 ofung Exp $
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: /**
061: *
062: * @author SAAJ RI Development Team
063: */package bugfixes;
064:
065: import java.io.FileInputStream;
066: import java.io.StringWriter;
067: import java.util.Iterator;
068:
069: import javax.xml.parsers.DocumentBuilderFactory;
070: import javax.xml.soap.*;
071: import javax.xml.transform.Transformer;
072: import javax.xml.transform.TransformerFactory;
073: import javax.xml.transform.dom.DOMSource;
074: import javax.xml.transform.stream.StreamResult;
075: import javax.xml.transform.stream.StreamSource;
076:
077: import junit.framework.TestCase;
078:
079: public class QuoteTest extends TestCase {
080:
081: public QuoteTest(String name) {
082: super (name);
083: }
084:
085: /*
086: * getValue returned on element should not get truncated after
087: * encountering quot;
088: */
089: public void testQuoteGetValue() {
090: String arg = "src/test/bugfixes/data/quot.xml";
091: try {
092: doit1(arg);
093: } catch (Throwable t) {
094: fail("Get Quote test failed" + t.getMessage());
095: // t.printStackTrace();
096: }
097: }
098:
099: static String x509IssuerName = "CN=VeriSign Class 3 Code Signing 2001 CA,"
100: + " OU=Terms of use at https://www.verisign.com/rpa (c)01,"
101: + " OU=VeriSign Trust Network, O=\"VeriSign, Inc.\"";
102:
103: public void doit1(String fileName) throws Exception {
104:
105: DocumentBuilderFactory factory = DocumentBuilderFactory
106: .newInstance();
107:
108: /*
109: * This next statement will affect how the quote entities are
110: * processed by the parser
111: */
112:
113: MessageFactory msgFactory = MessageFactory.newInstance();
114: SOAPMessage message = msgFactory.createMessage();
115:
116: SOAPPart soapPart = message.getSOAPPart();
117: SOAPEnvelope envelope = soapPart.getEnvelope();
118:
119: StreamSource source = new StreamSource(new FileInputStream(
120: fileName));
121: soapPart.setContent(source);
122: message.saveChanges();
123:
124: SOAPHeader soapHeader = message.getSOAPPart().getEnvelope()
125: .getHeader();
126: //System.out.println("\nnodeToString=" + nodeToString(soapHeader) + "\n\n");
127:
128: Iterator iterator = null;
129: SOAPElement keyInfoElement = null;
130:
131: Name issuerSerial = SOAPFactory.newInstance().createName(
132: "X509IssuerSerial", null,
133: "http://www.w3.org/2000/09/xmldsig#");
134: iterator = soapHeader.getChildElements();
135: while (iterator.hasNext()) {
136: Object o = iterator.next();
137: if (o instanceof SOAPElement) {
138: SOAPElement soapElement = (SOAPElement) o;
139: keyInfoElement = findElement((SOAPElement) o,
140: issuerSerial);
141: //System.out.println("\nnodeToString(keyInfoElement)=" +
142: // nodeToString(keyInfoElement) + "\n\n");
143: Iterator iterator2 = keyInfoElement.getChildElements();
144: while (iterator2.hasNext()) {
145: o = iterator2.next();
146: if (o instanceof SOAPElement) {
147: soapElement = (SOAPElement) o;
148: if ("X509IssuerName"
149: .equalsIgnoreCase(soapElement
150: .getLocalName())) {
151: if (!x509IssuerName
152: .equalsIgnoreCase(soapElement
153: .getValue()))
154: fail("Wrong IssuerName returned \n"
155: + soapElement.getValue());
156: }
157: }
158: }
159: break;
160: }
161: }
162:
163: }
164:
165: /** Convert a node tree into a STRING representation */
166: public String nodeToString(org.w3c.dom.Node node) throws Exception {
167: // Use a Transformer for output
168: TransformerFactory tFactory = TransformerFactory.newInstance();
169: Transformer transformer = tFactory.newTransformer();
170: StringWriter stringWriter = new StringWriter();
171:
172: DOMSource source = new DOMSource(node);
173: StreamResult result = new StreamResult(stringWriter);
174:
175: transformer.transform(source, result);
176: return stringWriter.toString();
177: }
178:
179: public SOAPElement findElement(SOAPElement soapElement, Name name) {
180: Name n = soapElement.getElementName();
181: if (n.equals(name)) {
182: return soapElement;
183: }
184: Iterator iterator = soapElement.getChildElements();
185: while (iterator.hasNext()) {
186: Object o = (Object) iterator.next();
187: if (o instanceof SOAPElement) {
188: SOAPElement result = findElement((SOAPElement) o, name);
189: if (result != null)
190: return result;
191: }
192: }
193: return null;
194: }
195:
196: }
|