-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add WebServiceHandler interface. #416
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
293814d
Add WebServiceHandler interface.
mikkokar 30063d7
Rearrange methods that were accidentally moved around.
mikkokar 69aa44d
Add Kotlin `WebServiceHandler.handle` extension method for unit tests.
mikkokar d729612
Rearrange method order.
mikkokar bf35ead
Address code review comments.
mikkokar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
components/api/src/main/java/com/hotels/styx/api/WebServiceHandler.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,31 @@ | ||
/* | ||
Copyright (C) 2013-2019 Expedia Inc. | ||
|
||
Licensed 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 com.hotels.styx.api; | ||
|
||
/** | ||
* A web service handler that handles a {@link HttpRequest}, returning an {@link Eventual} that is expected to publish | ||
* a single {@link HttpResponse} value. | ||
*/ | ||
@FunctionalInterface | ||
public interface WebServiceHandler { | ||
/** | ||
* Processes an incoming request. | ||
* | ||
* @param request the current incoming request | ||
* @return an {@link Eventual} that is expected to publish a single response | ||
*/ | ||
Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context); | ||
} |
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
122 changes: 0 additions & 122 deletions
122
...nents/common/src/main/java/com/hotels/styx/common/http/handler/CachedBodyHttpHandler.java
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
components/common/src/main/java/com/hotels/styx/common/http/handler/HttpAggregator.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,64 @@ | ||
/* | ||
Copyright (C) 2013-2019 Expedia Inc. | ||
|
||
Licensed 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 com.hotels.styx.common.http.handler; | ||
|
||
import com.hotels.styx.api.Eventual; | ||
import com.hotels.styx.api.HttpHandler; | ||
import com.hotels.styx.api.HttpInterceptor; | ||
import com.hotels.styx.api.HttpResponse; | ||
import com.hotels.styx.api.LiveHttpRequest; | ||
import com.hotels.styx.api.LiveHttpResponse; | ||
import com.hotels.styx.api.WebServiceHandler; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Adapts a static WebServiceHandler to streaming HttpHandler interface. | ||
*/ | ||
public class HttpAggregator implements HttpHandler { | ||
|
||
private static final int KILOBYTE = 1024; | ||
|
||
private final WebServiceHandler delegate; | ||
private final int bytes; | ||
|
||
/** | ||
* HttpAggregator Constructor. | ||
* | ||
* @param bytes max number of bytes to aggregate | ||
* @param delegate adapted WebServiceHandler endpoint | ||
*/ | ||
public HttpAggregator(int bytes, WebServiceHandler delegate) { | ||
this.delegate = requireNonNull(delegate); | ||
this.bytes = bytes; | ||
} | ||
|
||
/** | ||
* HttpAggregator constructor. | ||
* | ||
* @param delegate adapted WebServiceHandler endpoint | ||
*/ | ||
public HttpAggregator(WebServiceHandler delegate) { | ||
this(120 * KILOBYTE, delegate); | ||
dvlato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public Eventual<LiveHttpResponse> handle(LiveHttpRequest request, HttpInterceptor.Context context) { | ||
return request.aggregate(bytes) | ||
.flatMap(aggregated -> this.delegate.handle(aggregated, context)) | ||
.map(HttpResponse::stream); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
components/common/src/main/java/com/hotels/styx/common/http/handler/HttpStreamer.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,61 @@ | ||
/* | ||
Copyright (C) 2013-2019 Expedia Inc. | ||
|
||
Licensed 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 com.hotels.styx.common.http.handler; | ||
|
||
import com.hotels.styx.api.Eventual; | ||
import com.hotels.styx.api.HttpHandler; | ||
import com.hotels.styx.api.HttpInterceptor; | ||
import com.hotels.styx.api.HttpRequest; | ||
import com.hotels.styx.api.HttpResponse; | ||
import com.hotels.styx.api.WebServiceHandler; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Adapts Streaming HttpHandler API to STATIC WebServiceHandler interface. | ||
*/ | ||
public class HttpStreamer implements WebServiceHandler { | ||
private static final int KILOBYTE = 1024; | ||
|
||
private int maxContentBytes; | ||
private HttpHandler delegate; | ||
|
||
/** | ||
* HttpStreamer constructor. | ||
* | ||
* @param maxContentBytes max number of content maxContentBytes to aggregate | ||
* @param delegate adapted HttpHandler instance | ||
*/ | ||
public HttpStreamer(int maxContentBytes, HttpHandler delegate) { | ||
this.maxContentBytes = maxContentBytes; | ||
this.delegate = requireNonNull(delegate); | ||
} | ||
|
||
/** | ||
* HttpStreamer constructor. | ||
* | ||
* @param delegate adapted HttpHandler instance | ||
*/ | ||
public HttpStreamer(HttpHandler delegate) { | ||
this(125 * KILOBYTE, delegate); | ||
} | ||
|
||
@Override | ||
public Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context) { | ||
return delegate.handle(request.stream(), context) | ||
.flatMap(live -> live.aggregate(maxContentBytes)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don´t know if this "Web service" describes the functionality correctly. The handler does not have to be used to implement web services, and a webservice might need to stream the body of the request. I guess we should describe that´s a handler that works in a non-streaming fashion.
However I am also wondering if this is the best way to fix the issue... couldn´t it appear in cases in which we cannot use a WebserviceHandler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @dvlato.
I'm not 100% happy with this name either. Seems contrived. However it hints at most likely usage: web service endpoints which usually deal with finite sized objects. Should we consider naming it to
StaticHttpHandler
or something similar? WDYT?You are correct. There are cases where an
HttpRequest
can trigger a streamingLiveHttpRequest
, and this interface won't help there. However being pragmatic, just look at our admin interface. This new interface caters for pretty every admin interface use case. Also going forward we need to improve the bridging betweenHttpHandler
andWebServiceHandler
. Please share any other ideas :-)Also worth noticing I'm not attempting to solve every content body issue in this PR. Just trying getting the admin interface (low hanging fruit) sorted. I still need to do more follow on work to cover the rest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StaticHttpHandler could be ok in this case.