Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/ws/8172297/Main.java
38853 views
1
/*
2
* Copyright (c) 2017, 2018, 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 8172297 8196491
27
* @summary Test that carriage-return and new-line characters
28
* are preserved in webservice parameters
29
* @compile ws/HelloWorld.java ws/HelloWorldImpl.java Main.java
30
* @run testng/othervm Main
31
*/
32
33
import java.io.IOException;
34
import java.net.ServerSocket;
35
import java.net.URL;
36
import java.util.Collections;
37
import java.util.Set;
38
import java.util.concurrent.CountDownLatch;
39
40
import javax.xml.namespace.QName;
41
import javax.xml.soap.SOAPMessage;
42
import javax.xml.ws.Endpoint;
43
import javax.xml.ws.Service;
44
import javax.xml.ws.handler.Handler;
45
import javax.xml.ws.handler.MessageContext;
46
import javax.xml.ws.handler.soap.SOAPHandler;
47
import javax.xml.ws.handler.soap.SOAPMessageContext;
48
49
import org.testng.Assert;
50
import org.testng.annotations.DataProvider;
51
import org.testng.annotations.Test;
52
53
import ws.HelloWorld;
54
import ws.HelloWorldImpl;
55
56
public class Main {
57
58
@Test(dataProvider="callHandlerDataProvider")
59
public void runTest(boolean callGetMessageInHandler) throws Exception {
60
CountDownLatch serverInitSignal = new CountDownLatch(1);
61
CountDownLatch testDoneSignal = new CountDownLatch(1);
62
63
WebserviceRunner serverThread = new WebserviceRunner(serverInitSignal, testDoneSignal);
64
(new Thread(serverThread)).start();
65
66
serverInitSignal.await();
67
68
boolean paramModified = runClientCode(serverThread.getPort(), callGetMessageInHandler);
69
70
testDoneSignal.countDown();
71
72
Assert.assertEquals(callGetMessageInHandler, paramModified,
73
"WS parameter has not been processed as expected");
74
}
75
76
@DataProvider
77
public Object[][] callHandlerDataProvider() {
78
return new Object[][]{{true}, {false}};
79
}
80
81
/*
82
* Connects to launched web service endpoint, sends message with CR/NL symbols and
83
* checks if it was modified during the round trip client/server communication.
84
*/
85
private boolean runClientCode(int port, boolean callGetMessage) throws Exception {
86
System.out.println("Launching WS client connection on " + port + " port");
87
URL url = new URL("http://localhost:" + port + "/ws/hello?wsdl");
88
QName qname = new QName("http://ws/", "HelloWorldImplService");
89
Service service = Service.create(url, qname);
90
91
registerHandler(service, callGetMessage);
92
93
HelloWorld hello = (HelloWorld) service.getPort(HelloWorld.class);
94
95
logStringContent("Client input parameter", WS_PARAM_VALUE);
96
97
String response = hello.getHelloWorldAsString(WS_PARAM_VALUE);
98
logStringContent("Client response parameter", response);
99
100
return !WS_PARAM_VALUE.equals(response);
101
}
102
103
/*
104
* Register message handler and call SOAPMessageContext.getMessage
105
* to emulate issue reported in JDK-8196491
106
*/
107
private void registerHandler(Service service, final boolean callGetMessage) {
108
System.out.printf( "Client %s call getMessage inside message handler%n",
109
callGetMessage ? "will" : "will not" );
110
// Set custom SOAP message handler resolver
111
service.setHandlerResolver(portInfo -> {
112
Handler h = new SOAPHandler<SOAPMessageContext>() {
113
114
@Override
115
public boolean handleMessage(SOAPMessageContext context) {
116
if (callGetMessage) {
117
// Trigger exception from JDK-8196491
118
SOAPMessage msg = context.getMessage();
119
}
120
return true;
121
}
122
123
@Override
124
public boolean handleFault(SOAPMessageContext context) {
125
return true;
126
}
127
128
@Override
129
public void close(MessageContext context) {
130
}
131
132
@Override
133
public Set<QName> getHeaders() {
134
return null;
135
}
136
137
};
138
return Collections.singletonList(h);
139
});
140
}
141
142
/*
143
* Outputs the parameter value with newline and carriage-return symbols
144
* replaced with #CR and #NL text abbreviations.
145
*/
146
private static void logStringContent(String description, String parameter) {
147
String readableContent = parameter.replaceAll("\r", "#CR")
148
.replaceAll("\n", "#NL");
149
System.out.println(description + ": '" + readableContent + "'");
150
}
151
152
/* Web service parameter value with newline and carriage-return symbols */
153
private final static String WS_PARAM_VALUE = "\r\r\n\r\r CarriageReturn and "
154
+"NewLine \r\n\n Test \r\r\r\r";
155
156
/*
157
* Web service server thread that publishes WS on vacant port and waits
158
* for client to finalize testing
159
*/
160
class WebserviceRunner implements Runnable {
161
// Latch used to signalize when WS endpoint is initialized
162
private final CountDownLatch initSignal;
163
// Latch used to signalize when client completed testing
164
private final CountDownLatch doneSignal;
165
// Port where WS endpoint is published
166
private volatile int port = 0;
167
168
// Constructor
169
WebserviceRunner(CountDownLatch initSignal, CountDownLatch doneSignal) {
170
this.initSignal = initSignal;
171
this.doneSignal = doneSignal;
172
}
173
174
// Returns port of the published endpoint
175
public int getPort() {
176
return port;
177
}
178
179
/*
180
* Publish web service on vacant port and waits for the client to
181
* complete testing.
182
*/
183
public void run() {
184
try {
185
// Find vacant port number
186
ServerSocket ss = new ServerSocket(0);
187
port = ss.getLocalPort();
188
ss.close();
189
190
// Publish WebService
191
System.out.println("Publishing WebService on " + port + " port");
192
Endpoint ep = Endpoint.publish("http://localhost:" + port + "/ws/hello", new HelloWorldImpl());
193
194
// Notify main thread that WS endpoint is published
195
initSignal.countDown();
196
197
// Wait for main thread to complete testing
198
System.out.println("Waiting for done signal from test client.");
199
doneSignal.await();
200
201
// Terminate WS endpoint
202
System.out.println("Got done signal from the client. Stopping WS endpoint.");
203
ep.stop();
204
} catch (IOException ioe) {
205
System.out.println("Failed to get vacant port number:" + ioe);
206
} catch (InterruptedException ie) {
207
System.out.println("Failed to wait for test completion:" + ie);
208
}
209
}
210
}
211
}
212
213