Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java
41154 views
1
/*
2
* Copyright (c) 2013, 2019, 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
import com.sun.net.httpserver.*;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.OutputStream;
28
import java.net.*;
29
30
/*
31
* @test
32
* @bug 7157360
33
* @modules jdk.httpserver
34
* @summary HttpURLConnection: HTTP method DELETE doesn't support output
35
*/
36
public class PostOnDelete {
37
38
/* string to send */
39
private static String msg = "Hello Server";
40
/* length of the string to verify */
41
private int len = msg.length();
42
43
public static void main(String[] args) throws Exception {
44
new PostOnDelete().runTest();
45
}
46
47
public void runTest() throws Exception {
48
Server s = null;
49
try {
50
s = new Server();
51
s.startServer();
52
URL url = new URL("http://" + s.getAuthority());
53
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
54
urlConnection.setRequestMethod("DELETE");
55
urlConnection.setDoOutput(true);
56
OutputStream os = urlConnection.getOutputStream();
57
os.write(msg.getBytes());
58
os.close();
59
int code = urlConnection.getResponseCode();
60
61
if (code != 200) {
62
throw new RuntimeException("Request entity for DELETE failed!");
63
}
64
} finally {
65
s.stopServer();
66
}
67
}
68
69
class Server {
70
HttpServer server;
71
72
public void startServer() {
73
InetAddress loopback = InetAddress.getLoopbackAddress();
74
InetSocketAddress addr = new InetSocketAddress(loopback,0);
75
try {
76
server = HttpServer.create(addr, 0);
77
} catch (IOException ioe) {
78
throw new RuntimeException("Server could not be created");
79
}
80
81
server.createContext("/", new EmptyPathHandler());
82
server.start();
83
}
84
85
public String getAuthority() {
86
InetAddress address = server.getAddress().getAddress();
87
String hostaddr = address.isAnyLocalAddress() ? "localhost" : address.getHostAddress();
88
hostaddr = (hostaddr.indexOf(':') >= 0) ? ("[" + hostaddr + "]") : hostaddr;
89
return hostaddr + ":" + getPort();
90
}
91
92
public int getPort() {
93
return server.getAddress().getPort();
94
}
95
96
public void stopServer() {
97
server.stop(0);
98
}
99
}
100
101
class EmptyPathHandler implements HttpHandler {
102
103
@Override
104
public void handle(HttpExchange exchange) throws IOException {
105
String requestMethod = exchange.getRequestMethod();
106
107
if (requestMethod.equalsIgnoreCase("DELETE")) {
108
InputStream is = exchange.getRequestBody();
109
110
int count = 0;
111
while (is.read() != -1) {
112
count++;
113
}
114
is.close();
115
116
Headers responseHeaders = exchange.getResponseHeaders();
117
responseHeaders.set("Content-Type", "text/plain");
118
exchange.sendResponseHeaders((count == len) ? 200 : 400, 0);
119
OutputStream os = exchange.getResponseBody();
120
String str = "Hello from server!";
121
os.write(str.getBytes());
122
os.flush();
123
os.close();
124
}
125
}
126
}
127
}
128
129