1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8036981 8038966 8051441
  27  * @summary the content of xs:any content:mixed should remain as is,
  28  *          no white space changes and no changes to namespace prefixes
  29  * @run shell compile-wsdl.sh
  30  * @run main/othervm Test
  31  */
  32 
  33 import com.sun.net.httpserver.HttpServer;
  34 
  35 import javax.xml.transform.Source;
  36 import javax.xml.transform.Transformer;
  37 import javax.xml.transform.TransformerException;
  38 import javax.xml.transform.TransformerFactory;
  39 import javax.xml.transform.stream.StreamResult;
  40 import javax.xml.transform.stream.StreamSource;
  41 import javax.xml.ws.Dispatch;
  42 import javax.xml.ws.Endpoint;
  43 import javax.xml.ws.Service;
  44 import java.io.ByteArrayOutputStream;
  45 import java.io.IOException;
  46 import java.io.StringReader;
  47 import java.net.InetSocketAddress;
  48 import java.net.URL;
  49 import java.nio.file.FileVisitResult;
  50 import java.nio.file.Files;
  51 import java.nio.file.Path;
  52 import java.nio.file.Paths;
  53 import java.nio.file.SimpleFileVisitor;
  54 import java.nio.file.attribute.BasicFileAttributes;
  55 
  56 import static java.nio.file.FileVisitResult.CONTINUE;
  57 
  58 public class Test {
  59 
  60     private static HttpServer httpServer;
  61     private static Endpoint endpoint;
  62 
  63     private static final String XS_ANY_MIXED_PART = "<AppHdr xmlns=\"urn:head.001\">\n" +
  64             "      <Fr>\n\n" +
  65             "<FIId xmlns=\"urn:head.009\">\n\n" +
  66             "        any\n" +
  67             "    white\n" +
  68             "      space\n\n" +
  69             "        <FinInstnId>... and\n\n" +
  70             "            NO namespace prefixes!!!\n\n" +
  71             "        </FinInstnId>\n\n" +
  72             "  </FIId>\n" +
  73             "</Fr>\n" +
  74             "</AppHdr>";
  75 
  76     private static final String XML_REQUEST = "<soap:Envelope " +
  77             "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
  78             "xmlns:ws=\"http://ws.somewhere.org/\">" +
  79             "<soap:Header/><soap:Body>" +
  80             "<ws:echoRequest>" + XS_ANY_MIXED_PART + "</ws:echoRequest>" +
  81             "</soap:Body></soap:Envelope>";
  82 
  83     private static String deployWebservice() throws IOException {
  84         // Manually create HttpServer here using ephemeral address for port
  85         // so as to not end up with attempt to bind to an in-use port
  86         httpServer = HttpServer.create(new InetSocketAddress(0), 0);
  87         httpServer.start();
  88         endpoint = Endpoint.create(new ServiceImpl());
  89         endpoint.publish(httpServer.createContext("/wservice"));
  90 
  91         String wsdlAddress = "http://localhost:" + httpServer.getAddress().getPort() + "/wservice?wsdl";
  92         log("address = " + wsdlAddress);
  93         return wsdlAddress;
  94     }
  95 
  96     private static void stopWebservice() {
  97         if (endpoint != null && endpoint.isPublished()) {
  98             endpoint.stop();
  99         }
 100         if (httpServer != null) {
 101             httpServer.stop(0);
 102         }
 103     }
 104 
 105     public static void main(String[] args) throws IOException, TransformerException {
 106 
 107         try {
 108             String address = deployWebservice();
 109             Service service = Service.create(new URL(address), ServiceImpl.SERVICE_NAME);
 110 
 111             Dispatch<Source> d = service.createDispatch(ServiceImpl.PORT_NAME, Source.class, Service.Mode.MESSAGE);
 112             Source response = d.invoke(new StreamSource(new StringReader(XML_REQUEST)));
 113 
 114             String resultXml = toString(response);
 115 
 116             log("= request ======== \n");
 117             log(XML_REQUEST);
 118             log("= result ========= \n");
 119             log(resultXml);
 120             log("\n==================");
 121 
 122             boolean xsAnyMixedPartSame = resultXml.contains(XS_ANY_MIXED_PART);
 123             log("resultXml.contains(XS_ANY_PART) = " + xsAnyMixedPartSame);
 124             if (!xsAnyMixedPartSame) {
 125                 fail("The xs:any content=mixed part is supposed to be same in request and response.");
 126                 throw new RuntimeException();
 127             }
 128 
 129             log("TEST PASSED");
 130         } finally {
 131             stopWebservice();
 132             deleteGeneratedFiles();
 133         }
 134     }
 135 
 136     private static String toString(Source response) throws TransformerException, IOException {
 137         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 138         TransformerFactory transformerFactory = TransformerFactory.newInstance();
 139         Transformer transformer = transformerFactory.newTransformer();
 140         transformer.transform(response, new StreamResult(bos));
 141         bos.close();
 142         return new String(bos.toByteArray());
 143     }
 144 
 145     private static void fail(String message) {
 146         log("TEST FAILED.");
 147         throw new RuntimeException(message);
 148     }
 149 
 150     private static void log(String msg) {
 151         System.out.println(msg);
 152     }
 153 
 154     private static void deleteGeneratedFiles() {
 155         Path p = Paths.get(getTestSrcPath() + "/org");
 156         if (Files.exists(p)) {
 157             try {
 158                 Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
 159                     @Override
 160                     public FileVisitResult visitFile(
 161                             Path file,
 162                             BasicFileAttributes attrs) throws IOException {
 163 
 164                         Files.delete(file);
 165                         return CONTINUE;
 166                     }
 167 
 168                     @Override
 169                     public FileVisitResult postVisitDirectory(
 170                             Path dir,
 171                             IOException exc) throws IOException {
 172 
 173                         if (exc == null) {
 174                             Files.delete(dir);
 175                             return CONTINUE;
 176                         } else {
 177                             throw exc;
 178                         }
 179                     }
 180                 });
 181             } catch (IOException ioe) {
 182                 ioe.printStackTrace();
 183             }
 184         }
 185     }
 186 
 187     private static String getTestSrcPath() {
 188         String ret = System.getenv("TESTSRC");
 189         if (ret == null) {
 190             ret = System.getProperty("test.src", ".");
 191         }
 192         return ret;
 193     }
 194 
 195 }