17
17
18
18
package org .openqa .selenium .remote .server ;
19
19
20
- import static com .google .common .net .MediaType .JSON_UTF_8 ;
21
- import static java .net .HttpURLConnection .HTTP_NOT_FOUND ;
22
- import static java .net .HttpURLConnection .HTTP_OK ;
23
- import static java .nio .charset .StandardCharsets .UTF_8 ;
24
- import static org .openqa .selenium .remote .ErrorCodes .NO_SUCH_SESSION ;
25
- import static org .openqa .selenium .remote .ErrorCodes .SUCCESS ;
26
- import static org .openqa .selenium .remote .ErrorCodes .UNKNOWN_COMMAND ;
27
-
28
20
import com .google .common .annotations .VisibleForTesting ;
29
21
import com .google .common .base .Splitter ;
30
22
import com .google .common .base .Strings ;
34
26
import com .google .gson .Gson ;
35
27
import com .google .gson .GsonBuilder ;
36
28
37
- import org .openqa .selenium .internal .BuildInfo ;
38
29
import org .openqa .selenium .remote .SessionId ;
39
30
import org .openqa .selenium .remote .http .HttpMethod ;
40
- import org .openqa .selenium .remote .http .HttpRequest ;
41
- import org .openqa .selenium .remote .http .HttpResponse ;
42
31
import org .openqa .selenium .remote .server .commandhandler .GetLogTypes ;
32
+ import org .openqa .selenium .remote .server .commandhandler .NoHandler ;
33
+ import org .openqa .selenium .remote .server .commandhandler .NoSessionHandler ;
34
+ import org .openqa .selenium .remote .server .commandhandler .Status ;
43
35
44
- import java .io .IOException ;
45
36
import java .lang .reflect .Constructor ;
46
- import java .util .Collections ;
47
- import java .util .HashMap ;
48
37
import java .util .List ;
49
38
import java .util .Map ;
50
39
import java .util .Objects ;
@@ -71,7 +60,7 @@ public AllHandlers(ActiveSessions allSessions) {
71
60
HttpMethod .DELETE , ImmutableList .of (),
72
61
HttpMethod .GET , ImmutableList .of (
73
62
handler ("/session/{sessionId}/log/types" , GetLogTypes .class ),
74
- handler ("/status" , StatusHandler .class )
63
+ handler ("/status" , Status .class )
75
64
),
76
65
HttpMethod .POST , ImmutableList .of (
77
66
handler ("/session" , BeginSession .class )
@@ -140,108 +129,6 @@ private <H extends CommandHandler> Function<String, CommandHandler> handler(
140
129
};
141
130
}
142
131
143
- private static class NoHandler implements CommandHandler {
144
-
145
- @ Override
146
- public void execute (HttpRequest req , HttpResponse resp ) throws IOException {
147
- // We're not using ImmutableMap for the outer map because it disallows null values.
148
- Map <String , Object > responseMap = new HashMap <>();
149
- responseMap .put ("sessionId" , null );
150
- responseMap .put ("status" , UNKNOWN_COMMAND );
151
- responseMap .put ("value" , ImmutableMap .of (
152
- "error" , "unknown command" ,
153
- "message" , String .format (
154
- "Unable to find command matching %s to %s" ,
155
- req .getMethod (),
156
- req .getUri ()),
157
- "stacktrace" , "" ));
158
- responseMap = Collections .unmodifiableMap (responseMap );
159
-
160
- byte [] payload = new GsonBuilder ().serializeNulls ().create ().toJson (responseMap )
161
- .getBytes (UTF_8 );
162
-
163
- resp .setStatus (HTTP_NOT_FOUND );
164
- resp .setHeader ("Content-Type" , JSON_UTF_8 .toString ());
165
- resp .setHeader ("Content-Length" , String .valueOf (payload .length ));
166
-
167
- resp .setContent (payload );
168
- }
169
- }
170
-
171
- private static class NoSessionHandler implements CommandHandler {
172
-
173
- private final SessionId sessionId ;
174
-
175
- public NoSessionHandler (SessionId sessionId ) {
176
- this .sessionId = sessionId ;
177
- }
178
-
179
- @ Override
180
- public void execute (HttpRequest req , HttpResponse resp ) throws IOException {
181
- // We're not using ImmutableMap for the outer map because it disallows null values.
182
- Map <String , Object > responseMap = new HashMap <>();
183
- responseMap .put ("sessionId" , sessionId .toString ());
184
- responseMap .put ("status" , NO_SUCH_SESSION );
185
- responseMap .put ("value" , ImmutableMap .of (
186
- "error" , "invalid session id" ,
187
- "message" , String .format ("No active session with ID %s" , sessionId ),
188
- "stacktrace" , "" ));
189
- responseMap = Collections .unmodifiableMap (responseMap );
190
-
191
- byte [] payload = new GsonBuilder ().serializeNulls ().create ().toJson (responseMap )
192
- .getBytes (UTF_8 );
193
-
194
- resp .setStatus (HTTP_NOT_FOUND );
195
- resp .setHeader ("Content-Type" , JSON_UTF_8 .toString ());
196
- resp .setHeader ("Content-Length" , String .valueOf (payload .length ));
197
-
198
- resp .setContent (payload );
199
- }
200
- }
201
-
202
- private static class StatusHandler implements CommandHandler {
203
-
204
- @ Override
205
- public void execute (HttpRequest req , HttpResponse resp ) throws IOException {
206
- ImmutableMap .Builder <String , Object > value = ImmutableMap .builder ();
207
-
208
- // W3C spec
209
- value .put ("ready" , true );
210
- value .put ("message" , "Server is running" );
211
-
212
- // And now more information
213
- BuildInfo buildInfo = new BuildInfo ();
214
- value .put ("build" , ImmutableMap .of (
215
- // We need to fix the BuildInfo to properly fill out these values.
216
- // "revision", buildInfo.getBuildRevision(),
217
- // "time", buildInfo.getBuildTime(),
218
- "version" , buildInfo .getReleaseLabel ()));
219
-
220
- value .put ("os" , ImmutableMap .of (
221
- "arch" , System .getProperty ("os.arch" ),
222
- "name" , System .getProperty ("os.name" ),
223
- "version" , System .getProperty ("os.version" )));
224
-
225
- value .put ("java" , ImmutableMap .of ("version" , System .getProperty ("java.version" )));
226
-
227
- Map <String , Object > payloadObj = ImmutableMap .of (
228
- "status" , SUCCESS ,
229
- "value" , value .build ());
230
-
231
- // Write out a minimal W3C status response.
232
- byte [] payload = new GsonBuilder ()
233
- .serializeNulls ()
234
- .create ()
235
- .toJson (payloadObj ).getBytes (UTF_8 );
236
-
237
- resp .setStatus (HTTP_OK );
238
- resp .setHeader ("Content-Type" , JSON_UTF_8 .toString ());
239
- resp .setHeader ("Content-Length" , String .valueOf (payload .length ));
240
-
241
- resp .setContent (payload );
242
- }
243
- }
244
-
245
132
@ VisibleForTesting
246
133
<T extends CommandHandler > T create (Class <T > toCreate , Set <Object > args ) {
247
134
Constructor <?> constructor = Stream .of (toCreate .getDeclaredConstructors ())
0 commit comments