17
17
18
18
package org .openqa .selenium .grid .sessionqueue ;
19
19
20
- import com .google .common .collect .ImmutableMap ;
21
20
import org .openqa .selenium .events .EventBus ;
22
21
import org .openqa .selenium .grid .data .NewSessionErrorResponse ;
23
22
import org .openqa .selenium .grid .data .NewSessionRejectedEvent ;
39
38
import java .util .logging .Logger ;
40
39
41
40
import static java .net .HttpURLConnection .HTTP_INTERNAL_ERROR ;
41
+ import static java .util .Collections .singletonMap ;
42
42
import static org .openqa .selenium .remote .http .Contents .asJson ;
43
43
import static org .openqa .selenium .remote .http .Contents .bytes ;
44
44
@@ -99,9 +99,7 @@ private void setErrorResponse(NewSessionErrorResponse sessionResponse) {
99
99
100
100
if (sessionRequest .isPresent ()) {
101
101
NewSessionRequest request = sessionRequest .get ();
102
- request .setSessionResponse (new HttpResponse ()
103
- .setStatus (HTTP_INTERNAL_ERROR )
104
- .setContent (asJson (ImmutableMap .of ("message" , sessionResponse .getMessage ()))));
102
+ request .setSessionResponse (internalErrorResponse (sessionResponse .getMessage ()));
105
103
request .getLatch ().countDown ();
106
104
}
107
105
}
@@ -116,11 +114,8 @@ public HttpResponse add(HttpRequest request) {
116
114
knownRequests .put (requestId , requestIdentifier );
117
115
118
116
if (!sessionRequests .offerLast (request , requestId )) {
119
- return new HttpResponse ()
120
- .setStatus (HTTP_INTERNAL_ERROR )
121
- .setContent (asJson (ImmutableMap .of (
122
- "message" ,
123
- "Session request could not be created. Error while adding to the session queue." )));
117
+ return internalErrorResponse (
118
+ "Session request could not be created. Error while adding to the session queue." );
124
119
}
125
120
126
121
try {
@@ -129,20 +124,24 @@ public HttpResponse add(HttpRequest request) {
129
124
latch .await ();
130
125
return requestIdentifier .getSessionResponse ();
131
126
} catch (InterruptedException e ) {
132
- LOG .log (Level .WARNING , "The thread waiting for new session response interrupted. {0}" ,
133
- e .getMessage ());
127
+ LOG .log (Level .WARNING ,
128
+ "The thread waiting for new session response interrupted. {0}" ,
129
+ e .getMessage ());
134
130
Thread .currentThread ().interrupt ();
135
131
136
- return new HttpResponse ()
137
- .setStatus (HTTP_INTERNAL_ERROR )
138
- .setContent (asJson (ImmutableMap .of (
139
- "message" ,
140
- "Session request could not be created. Error while processing the session request." )));
132
+ return internalErrorResponse (
133
+ "Session request could not be created. Error while processing the session request." );
141
134
} finally {
142
135
removeRequest (requestId );
143
136
}
144
137
}
145
138
139
+ private HttpResponse internalErrorResponse (String message ) {
140
+ return new HttpResponse ()
141
+ .setStatus (HTTP_INTERNAL_ERROR )
142
+ .setContent (asJson (singletonMap ("message" , message )));
143
+ }
144
+
146
145
private void removeRequest (RequestId id ) {
147
146
knownRequests .remove (id );
148
147
}
0 commit comments