-
Notifications
You must be signed in to change notification settings - Fork 826
JAV-355 Extract rest invocation #161
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
559dc85
JAV-355 optimize produces create and find logic
b919f6c
JAV-355 create AbstractRestInvocation
f723f38
JAV-355 delete useless exception declare
9302fae
JAV-355 create RestProducerInvocation
c1a92b3
JAV-355 create RestServletProducerInvocation to handle request cache …
b2a4e8d
JAV-355 RestServlet change to use rest invocation
a7c5b03
JAV-355 VertxRest change to use rest invocation
c531c63
JAV-355 delete useless code
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
160 changes: 160 additions & 0 deletions
160
common/common-rest/src/main/java/io/servicecomb/common/rest/AbstractRestInvocation.java
This file contains hidden or 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,160 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* 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 io.servicecomb.common.rest; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.Response.Status; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import io.servicecomb.common.rest.codec.produce.ProduceProcessor; | ||
import io.servicecomb.common.rest.codec.produce.ProduceProcessorManager; | ||
import io.servicecomb.common.rest.definition.RestOperationMeta; | ||
import io.servicecomb.common.rest.filter.HttpServerFilter; | ||
import io.servicecomb.core.Const; | ||
import io.servicecomb.core.Invocation; | ||
import io.servicecomb.foundation.common.utils.JsonUtils; | ||
import io.servicecomb.foundation.vertx.http.HttpServletRequestEx; | ||
import io.servicecomb.foundation.vertx.http.HttpServletResponseEx; | ||
import io.servicecomb.foundation.vertx.stream.BufferOutputStream; | ||
import io.servicecomb.swagger.invocation.Response; | ||
import io.servicecomb.swagger.invocation.exception.InvocationException; | ||
|
||
public abstract class AbstractRestInvocation { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRestInvocation.class); | ||
|
||
protected RestOperationMeta restOperationMeta; | ||
|
||
protected Invocation invocation; | ||
|
||
protected HttpServletRequestEx requestEx; | ||
|
||
protected HttpServletResponseEx responseEx; | ||
|
||
protected ProduceProcessor produceProcessor; | ||
|
||
protected List<HttpServerFilter> httpServerFilters = Collections.emptyList(); | ||
|
||
public void setHttpServerFilters(List<HttpServerFilter> httpServerFilters) { | ||
this.httpServerFilters = httpServerFilters; | ||
} | ||
|
||
protected boolean initProduceProcessor() { | ||
produceProcessor = restOperationMeta.ensureFindProduceProcessor(requestEx); | ||
if (produceProcessor == null) { | ||
String msg = String.format("Accept %s is not supported", requestEx.getHeader(HttpHeaders.ACCEPT)); | ||
InvocationException exception = new InvocationException(Status.NOT_ACCEPTABLE, msg); | ||
sendFailResponse(exception); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected void setContext() throws Exception { | ||
String strCseContext = requestEx.getHeader(Const.CSE_CONTEXT); | ||
if (StringUtils.isEmpty(strCseContext)) { | ||
return; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
Map<String, String> cseContext = | ||
JsonUtils.readValue(strCseContext.getBytes(StandardCharsets.UTF_8), Map.class); | ||
invocation.setContext(cseContext); | ||
} | ||
|
||
protected void invoke() { | ||
try { | ||
this.initProduceProcessor(); | ||
this.setContext(); | ||
invocation.getHandlerContext().put(RestConst.REST_REQUEST, requestEx); | ||
|
||
for (HttpServerFilter filter : httpServerFilters) { | ||
Response response = filter.afterReceiveRequest(invocation, requestEx); | ||
if (response != null) { | ||
sendResponseQuietly(response); | ||
return; | ||
} | ||
} | ||
|
||
doInvoke(); | ||
} catch (Throwable e) { | ||
sendFailResponse(e); | ||
} | ||
} | ||
|
||
protected abstract void doInvoke() throws Throwable; | ||
|
||
public void sendFailResponse(Throwable throwable) { | ||
if (produceProcessor == null) { | ||
produceProcessor = ProduceProcessorManager.DEFAULT_PROCESSOR; | ||
} | ||
|
||
Response response = Response.createProducerFail(throwable); | ||
sendResponseQuietly(response); | ||
} | ||
|
||
protected void sendResponseQuietly(Response response) { | ||
try { | ||
sendResponse(response); | ||
} catch (Throwable e) { | ||
LOGGER.error("Failed to send rest response, operation:{}.", | ||
invocation.getMicroserviceQualifiedName(), | ||
e); | ||
} finally { | ||
requestEx.getAsyncContext().complete(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
protected void sendResponse(Response response) throws Exception { | ||
responseEx.setStatus(response.getStatusCode(), response.getReasonPhrase()); | ||
responseEx.setContentType(produceProcessor.getName()); | ||
if (response.getHeaders().getHeaderMap() != null) { | ||
for (Entry<String, List<Object>> entry : response.getHeaders().getHeaderMap().entrySet()) { | ||
for (Object value : entry.getValue()) { | ||
responseEx.addHeader(entry.getKey(), String.valueOf(value)); | ||
} | ||
} | ||
} | ||
|
||
Object body = response.getResult(); | ||
if (response.isFailed()) { | ||
body = ((InvocationException) body).getErrorData(); | ||
} | ||
|
||
try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) { | ||
produceProcessor.encodeResponse(output, body); | ||
|
||
responseEx.setBodyBuffer(output.getBuffer()); | ||
for (HttpServerFilter filter : httpServerFilters) { | ||
filter.beforeSendResponse(invocation, responseEx); | ||
} | ||
|
||
responseEx.flushBuffer(); | ||
} | ||
} | ||
} |
228 changes: 0 additions & 228 deletions
228
common/common-rest/src/main/java/io/servicecomb/common/rest/AbstractRestServer.java
This file was deleted.
Oops, something went wrong.
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.
it looks like the filter could hijack the response message which calling the doInvoke method.
I don't think it's good idea, there are too much assumption.
Uh oh!
There was an error while loading. Please reload this page.
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.
if user decided to break the filter chain, and direct send response, user return a Response.
if do not do like this, we need to build another mechanism like handler chain or some api to send response, it's too complex.
what's your suggestion?
Uh oh!
There was an error while loading. Please reload this page.
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.
Please add some comments in the HttpServerFilter.afterReceiveRequst, to make sure user know about this kind of feature.
We still need to make sure the doInvoke() is called in the invoke method, no matter what kind of HttpServerFilter is used.
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.
already have comments in HttpServerFilter.afterReceiveRequst and HttpClientFilter.afterReceiveResponse in current PR code.
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.
OK.