1 /*
   2  * Copyright (c) 2014, 2016, 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  * @modules java.xml.ws
  30  * @run shell compile-wsdl.sh
  31  * @run main/othervm Test
  32  */
  33 
  34 import com.sun.net.httpserver.HttpServer;
  35 
  36 import javax.xml.transform.Source;
  37 import javax.xml.transform.Transformer;
  38 import javax.xml.transform.TransformerException;
  39 import javax.xml.transform.TransformerFactory;
  40 import javax.xml.transform.stream.StreamResult;
  41 import javax.xml.transform.stream.StreamSource;
  42 import javax.xml.ws.Dispatch;
  43 import javax.xml.ws.Endpoint;
  44 import javax.xml.ws.Service;
  45 import java.io.ByteArrayOutputStream;
  46 import java.io.IOException;
  47 import java.io.StringReader;
  48 import java.net.InetSocketAddress;
  49 import java.net.URL;
  50 import java.nio.file.FileVisitResult;
  51 import java.nio.file.Files;
  52 import java.nio.file.Path;
  53 import java.nio.file.Paths;
  54 import java.nio.file.SimpleFileVisitor;
  55 import java.nio.file.attribute.BasicFileAttributes;
  56 
  57 import static java.nio.file.FileVisitResult.CONTINUE;
  58 
  59 public class Test {
  60 
  61     private static HttpServer httpServer;
  62     private static Endpoint endpoint;
  63     private static final String NL = System.getProperty("line.separator");
  64 
  65     private static final String XS_ANY_MIXED_PART =
  66             "<AppHdr xmlns=\"urn:head.001\">" + NL +
  67             "      <Fr>" + NL + NL +
  68             "<FIId xmlns=\"urn:head.009\">" + NL + NL +
  69             "        any" + NL +
  70             "    white" + NL +
  71             "      space" + NL + NL +
  72             "        <FinInstnId>... and" + NL + NL +
  73             "            NO namespace prefixes!!!" + NL + NL +
  74             "        </FinInstnId>" + NL + NL +
  75             "  </FIId>" + NL +
  76             "</Fr>" + NL +
  77             "</AppHdr>";
  78 
  79     private static final String XML_REQUEST = "<soap:Envelope " +
  80             "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
  81             "xmlns:ws=\"http://ws.somewhere.org/\">" +
  82             "<soap:Header/><soap:Body>" +
  83             "<ws:echoRequest>" + NL +
  84                 XS_ANY_MIXED_PART + NL +
  85             "</ws:echoRequest>" +
  86             "</soap:Body></soap:Envelope>";
  87 
  88     private static String deployWebservice() throws IOException {
  89         // Manually create HttpServer here using ephemeral address for port
  90         // so as to not end up with attempt to bind to an in-use port
  91         httpServer = HttpServer.create(new InetSocketAddress(0), 0);
  92         httpServer.start();
  93         endpoint = Endpoint.create(new ServiceImpl());
  94         endpoint.publish(httpServer.createContext("/wservice"));
  95 
  96         String wsdlAddress = "http://localhost:" + httpServer.getAddress().getPort() + "/wservice?wsdl";
  97         log("address = " + wsdlAddress);
  98         return wsdlAddress;
  99     }
 100 
 101     private static void stopWebservice() {
 102         if (endpoint != null && endpoint.isPublished()) {
 103             endpoint.stop();
 104         }
 105         if (httpServer != null) {
 106             httpServer.stop(0);
 107         }
 108     }
 109 
 110     public static void main(String[] args) throws IOException, TransformerException {
 111 
 112         try {
 113             String address = deployWebservice();
 114             Service service = Service.create(new URL(address), ServiceImpl.SERVICE_NAME);
 115 
 116             Dispatch<Source> d = service.createDispatch(ServiceImpl.PORT_NAME, Source.class, Service.Mode.MESSAGE);
 117             Source response = d.invoke(new StreamSource(new StringReader(XML_REQUEST)));
 118 
 119             String resultXml = toString(response);
 120 
 121             log("= request ======== \n");
 122             log(XML_REQUEST);
 123             log("= result ========= \n");
 124             log(resultXml);
 125             log("\n==================");
 126 
 127             boolean xsAnyMixedPartSame = resultXml.contains(XS_ANY_MIXED_PART);
 128             log("resultXml.contains(XS_ANY_PART) = " + xsAnyMixedPartSame);
 129             if (!xsAnyMixedPartSame) {
 130                 fail("The xs:any content=mixed part is supposed to be same in request and response.");
 131                 throw new RuntimeException();
 132             }
 133 
 134             log("TEST PASSED");
 135         } finally {
 136             stopWebservice();
 137 
 138             // if you need to debug or explore wsdl generation result
 139             // comment this line out:
 140             deleteGeneratedFiles();
 141         }
 142     }
 143 
 144     private static String toString(Source response) throws TransformerException, IOException {
 145         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 146         TransformerFactory transformerFactory = TransformerFactory.newInstance();
 147         Transformer transformer = transformerFactory.newTransformer();
 148         transformer.transform(response, new StreamResult(bos));
 149         bos.close();
 150         return new String(bos.toByteArray());
 151     }
 152 
 153     private static void fail(String message) {
 154         log("TEST FAILED.");
 155         throw new RuntimeException(message);
 156     }
 157 
 158     private static void log(String msg) {
 159         System.out.println(msg);
 160     }
 161 
 162     private static void deleteGeneratedFiles() {
 163         Path p = Paths.get("..", "classes", "javax", "xml", "ws", "xsanymixed", "org");
 164         System.out.println("performing cleanup, deleting wsdl compilation result: " + p.toFile().getAbsolutePath());
 165         if (Files.exists(p)) {
 166             try {
 167                 Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
 168                     @Override
 169                     public FileVisitResult visitFile(
 170                             Path file,
 171                             BasicFileAttributes attrs) throws IOException {
 172 
 173                         System.out.println("deleting file [" + file.toFile().getAbsoluteFile() + "]");
 174                         Files.delete(file);
 175                         return CONTINUE;
 176                     }
 177 
 178                     @Override
 179                     public FileVisitResult postVisitDirectory(
 180                             Path dir,
 181                             IOException exc) throws IOException {
 182 
 183                         System.out.println("deleting dir [" + dir.toFile().getAbsoluteFile() + "]");
 184                         if (exc == null) {
 185                             Files.delete(dir);
 186                             return CONTINUE;
 187                         } else {
 188                             throw exc;
 189                         }
 190                     }
 191                 });
 192             } catch (IOException ioe) {
 193                 ioe.printStackTrace();
 194             }
 195         }
 196     }
 197 
 198 }