-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start providing abstractions for consistent handling of HttpRequests
The new grid server makes an effort not to differentiate between local and remote execution. This is a nice model, and we should extend it to other places in the code base. This also provides an abstraction to start solving the problem of wanting to wrap an HttpClient with methods that modify requests or responses (eg. to add authentication headers). Finally, we provide enough infrastructure to make providing routes to various handlers easy to implement, allowing complicated routing mechanisms to be built up.
- Loading branch information
Showing
17 changed files
with
636 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
java/client/src/org/openqa/selenium/remote/http/Filter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.remote.http; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* Can be wrapped around an {@link HttpHandler} in order to either modify incoming | ||
* {@link HttpRequest}s or outgoing {@link HttpResponse}s using the well-known "Filter" pattern. | ||
* This is very similar to the Servlet spec's {@link javax.servlet.Filter}, but takes advantage of | ||
* lambdas: | ||
* <pre>{@code | ||
* Filter filter = next -> { | ||
* return req -> { | ||
* req.addHeader("cheese", "brie"); | ||
* HttpResponse res = next.apply(req); | ||
* res.addHeader("vegetable", "peas"); | ||
* return res; | ||
* }; | ||
* } | ||
* }</pre> | ||
* | ||
*<p>Because each filter returns an {@link HttpHandler}, it's easy to do processing before, or after | ||
* each request, as well as short-circuit things if necessary. | ||
*/ | ||
@FunctionalInterface | ||
public interface Filter extends Function<HttpHandler, HttpHandler> { | ||
|
||
default Filter andThen(Filter next) { | ||
return req -> apply(next.apply(req)); | ||
} | ||
|
||
default HttpHandler andFinally(HttpHandler end) { | ||
return request -> Filter.this.apply(end).apply(request); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
java/client/src/org/openqa/selenium/remote/http/HttpHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.remote.http; | ||
|
||
import java.util.function.Function; | ||
|
||
@FunctionalInterface | ||
public interface HttpHandler extends Function<HttpRequest, HttpResponse> { | ||
|
||
default HttpHandler with(Filter filter) { | ||
return filter.andFinally(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.