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