Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3aa809d
JAV-349 create abstract httpServletRequest to prepare for mock
Sep 15, 2017
f1078f6
JAV-349 create HttpServletRequest from vertx request
Sep 15, 2017
ca2db27
bug fix: print qualified operation name and correct endpoint
Sep 15, 2017
277912b
[WIP] JAV-349 change ParamValueProcessor parameter to HttpServletRequest
Sep 15, 2017
593a6db
[WIP] JAV-349 HeaderProcessor parameter change to HttpServletRequest
Sep 15, 2017
8c43914
[WIP] JAV-349 CookieProcessor parameter change to HttpServletRequest
Sep 15, 2017
cbe223a
[WIP] JAV-349 FormProcessor parameter change to HttpServletRequest
Sep 15, 2017
034233e
[WIP] JAV-349 BodyProcessor parameter change to HttpServletRequest
Sep 17, 2017
e3ee8db
[WIP] JAV-349 QueryProcessor parameter change to HttpServletRequest
Sep 17, 2017
5f07f0e
[WIP] JAV-349 PathProcessor parameter change to HttpServletRequest
Sep 17, 2017
ead9538
[WIP] JAV-349 delete useless coe
Sep 17, 2017
b110352
[WIP] JAV-349 RestCodec.restToArgs parameter change to HttpServletReq…
Sep 17, 2017
90e54d4
[WIP] JAV-349 AbstractRestServer parameter change to HttpServletRequest
Sep 17, 2017
74c1f12
[WIP] JAV-349 ServletRestServer parameter change to HttpServletRequest
Sep 17, 2017
9d9e07c
[WIP] JAV-349 VertxRestServer parameter change to HttpServletRequest
Sep 17, 2017
0f29f99
[WIP] JAV-349 create ClientToHttpServletRequest to support restToArgs…
Sep 18, 2017
5192251
[WIP] JAV-349 CseClientHttpRequest use new ClientToHttpServletRequest
Sep 18, 2017
109a35f
[WIP] JAV-349 create InvocationToHttpServletRequest
Sep 18, 2017
ba6cfe6
[WIP] JAV-349 delete httpServletRequest creator
Sep 18, 2017
89c6a61
JAV-349 delete useless code
Sep 18, 2017
82b9e53
change demo SC port from 9980 to 30100
Sep 19, 2017
142c718
JAV-349 vertx rest do not support HttpClientFilter & HttpServerFilter…
Sep 19, 2017
9840278
add some test case
Sep 19, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.slf4j.LoggerFactory;

import io.servicecomb.common.rest.codec.RestCodec;
import io.servicecomb.common.rest.codec.RestServerRequestInternal;
import io.servicecomb.common.rest.codec.produce.ProduceProcessor;
import io.servicecomb.common.rest.codec.produce.ProduceProcessorManager;
import io.servicecomb.common.rest.definition.RestOperationMeta;
Expand Down Expand Up @@ -66,8 +65,8 @@ public void setTransport(Transport transport) {
this.transport = transport;
}

protected void setContext(Invocation invocation, RestServerRequestInternal restRequest) throws Exception {
String strCseContext = restRequest.getHeaderParam(Const.CSE_CONTEXT);
protected void setContext(Invocation invocation, HttpServletRequest request) throws Exception {
String strCseContext = request.getHeader(Const.CSE_CONTEXT);
if (StringUtils.isEmpty(strCseContext)) {
return;
}
Expand All @@ -77,65 +76,63 @@ protected void setContext(Invocation invocation, RestServerRequestInternal restR
invocation.setContext(cseContext);
}

protected void handleRequest(RestServerRequestInternal restRequest, HTTP_RESPONSE httpResponse) {
protected void handleRequest(HttpServletRequest request, HTTP_RESPONSE httpResponse) {
if (transport == null) {
transport = CseContext.getInstance().getTransportManager().findTransport(Const.RESTFUL);
}

try {
RestOperationMeta restOperation = findRestOperation(restRequest);
RestOperationMeta restOperation = findRestOperation(request);
OperationMeta operationMeta = restOperation.getOperationMeta();

operationMeta.getExecutor().execute(() -> {
try {
runOnExecutor(restRequest, restOperation, httpResponse);
runOnExecutor(request, restOperation, httpResponse);
} catch (Exception e) {
LOGGER.error("rest server onRequest error", e);
sendFailResponse(null, restRequest, httpResponse, e);
sendFailResponse(null, request, httpResponse, e);
}
});
} catch (Exception e) {
LOGGER.error("rest server onRequest error", e);
sendFailResponse(null, restRequest, httpResponse, e);
sendFailResponse(null, request, httpResponse, e);
}
}

protected void runOnExecutor(RestServerRequestInternal restRequest, RestOperationMeta restOperation,
protected void runOnExecutor(HttpServletRequest request, RestOperationMeta restOperation,
HTTP_RESPONSE httpResponse) throws Exception {
String acceptType = restRequest.getHeaderParam("Accept");
String acceptType = request.getHeader("Accept");
ProduceProcessor produceProcessor =
locateProduceProcessor(null, restRequest, httpResponse, restOperation, acceptType);
locateProduceProcessor(null, request, httpResponse, restOperation, acceptType);
if (produceProcessor == null) {
// locateProduceProcessor内部已经应答了
return;
}

Object[] args = RestCodec.restToArgs(restRequest, restOperation);
Object[] args = RestCodec.restToArgs(request, restOperation);
Invocation invocation =
InvocationFactory.forProvider(transport.getEndpoint(),
restOperation.getOperationMeta(),
args);

this.setContext(invocation, restRequest);
this.setHttpRequestContext(invocation, restRequest);
this.setContext(invocation, request);
invocation.getHandlerContext().put(RestConst.REST_REQUEST, request);

if (HttpServletRequest.class.isInstance(restRequest.getHttpRequest())) {
for (HttpServerFilter filter : httpServerFilters) {
Response response = filter.afterReceiveRequest(invocation, restRequest.getHttpRequest());
if (response != null) {
sendResponse(invocation, restRequest, httpResponse, produceProcessor, response);
return;
}
for (HttpServerFilter filter : httpServerFilters) {
Response response = filter.afterReceiveRequest(invocation, request);
if (response != null) {
sendResponse(invocation, request, httpResponse, produceProcessor, response);
return;
}
}

invocation.next(resp -> {
sendResponse(invocation, restRequest, httpResponse, produceProcessor, resp);
sendResponse(invocation, request, httpResponse, produceProcessor, resp);
});
}

protected RestOperationMeta findRestOperation(RestServerRequestInternal restRequest) {
String targetMicroserviceName = restRequest.getHeaderParam(Const.TARGET_MICROSERVICE);
protected RestOperationMeta findRestOperation(HttpServletRequest request) {
String targetMicroserviceName = request.getHeader(Const.TARGET_MICROSERVICE);
if (targetMicroserviceName == null) {
// for compatible
targetMicroserviceName = RegistryUtils.getMicroservice().getServiceName();
Expand All @@ -148,15 +145,14 @@ protected RestOperationMeta findRestOperation(RestServerRequestInternal restRequ
throw new InvocationException(Status.NOT_FOUND, Status.NOT_FOUND.getReasonPhrase());
}

OperationLocator locator =
servicePathManager.producerLocateOperation(restRequest.getPath(), restRequest.getMethod());
restRequest.setPathParamMap(locator.getPathVarMap());
OperationLocator locator = servicePathManager.producerLocateOperation(request.getRequestURI(), request.getMethod());
request.setAttribute(RestConst.PATH_PARAMETERS, locator.getPathVarMap());

return locator.getOperation();
}

// 找不到processor,则已经完成了应答,外界不必再处理
protected ProduceProcessor locateProduceProcessor(Invocation invocation, RestServerRequestInternal restRequest,
protected ProduceProcessor locateProduceProcessor(Invocation invocation, HttpServletRequest request,
HTTP_RESPONSE httpResponse,
RestOperationMeta restOperation, String acceptType) {
ProduceProcessor produceProcessor = restOperation.ensureFindProduceProcessor(acceptType);
Expand All @@ -166,18 +162,18 @@ protected ProduceProcessor locateProduceProcessor(Invocation invocation, RestSer

String msg = String.format("Accept %s is not supported", acceptType);
InvocationException exception = new InvocationException(Status.NOT_ACCEPTABLE, msg);
sendFailResponse(invocation, restRequest, httpResponse, exception);
sendFailResponse(invocation, request, httpResponse, exception);
return null;
}

public void sendFailResponse(Invocation invocation, RestServerRequestInternal restRequest, HTTP_RESPONSE httpResponse,
public void sendFailResponse(Invocation invocation, HttpServletRequest request, HTTP_RESPONSE httpResponse,
Throwable throwable) {
Response response = Response.createProducerFail(throwable);
sendResponse(invocation, restRequest, httpResponse, ProduceProcessorManager.DEFAULT_PROCESSOR, response);
sendResponse(invocation, request, httpResponse, ProduceProcessorManager.DEFAULT_PROCESSOR, response);
}

// 成功、失败的统一应答处理,这里不能再出异常了,再出了异常也没办法处理
protected void sendResponse(Invocation invocation, RestServerRequestInternal restRequest,
protected void sendResponse(Invocation invocation, HttpServletRequest request,
HTTP_RESPONSE httpServerResponse,
ProduceProcessor produceProcessor, Response response) {
try {
Expand All @@ -188,17 +184,12 @@ protected void sendResponse(Invocation invocation, RestServerRequestInternal res
// 并且本次调用本身可能就是500进来的
LOGGER.error("send response failed.", e);
} finally {
if (restRequest != null) {
restRequest.complete();
}
request.getAsyncContext().complete();
}
}

// 成功、失败的统一应答处理
protected abstract void doSendResponse(Invocation invocation, HTTP_RESPONSE httpServerResponse,
ProduceProcessor produceProcessor,
Response response) throws Exception;

// 将http request注入到invocation的handler context
protected abstract void setHttpRequestContext(Invocation invocation, RestServerRequestInternal restRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ private RestConst() {
}

public static final String CONFIG_COPY_REQUEST = "servicecomb.rest.copy-request";
public static final String HTTP_REQUEST_CREATOR = "server-http-request-creator";

public static final String REST_CLIENT_REQUEST_PATH = "rest-client-request-path";

Expand All @@ -32,4 +31,16 @@ private RestConst() {
public static final String SCHEME = "cse";

public static final String URI_PREFIX = SCHEME + "://";

// in HttpServletRequest attribute
public static final String PATH_PARAMETERS = "servicecomb-paths";

// in HttpServletRequest attribute
public static final String BODY_PARAMETER = "servicecomb-body";

// in HttpServletRequest attribute
public static final String FORM_PARAMETERS = "servicecomb-forms";

// in producer invocation handler context
public static final String REST_REQUEST = "servicecomb-rest-request";
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -49,7 +51,7 @@ public static void argsToRest(Object[] args, RestOperationMeta restOperation,
}
}

public static Object[] restToArgs(RestServerRequest request,
public static Object[] restToArgs(HttpServletRequest request,
RestOperationMeta restOperation) throws InvocationException {
List<RestParam> paramList = restOperation.getParamList();

Expand All @@ -62,7 +64,9 @@ public static Object[] restToArgs(RestServerRequest request,

return paramValues;
} catch (Exception e) {
LOG.error("Parameter is not valid, cause " + e.getMessage());
LOG.error("Parameter is not valid for operation {}, cause ",
restOperation.getOperationMeta().getMicroserviceQualifiedName(),
e.getMessage());
throw ExceptionFactory.convertProducerException(e, "Parameter is not valid.");
}
}
Expand Down

This file was deleted.

Loading