use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.
the class TestVertxRestServer method testDoSend.
@Test
public void testDoSend() {
boolean status = false;
try {
HttpServerResponse httpServerResponse = Mockito.mock(HttpServerResponse.class);
ProduceProcessor produceProcessor = Mockito.mock(ProduceProcessor.class);
Response response = Response.create(0, "reasonPhrase", new Object());
instance.doSendResponse(httpServerResponse, produceProcessor, response);
} catch (Exception e) {
status = true;
}
Assert.assertFalse(status);
}
use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.
the class AbstractRestServer method runOnExecutor.
protected void runOnExecutor(RestServerRequestInternal restRequest, RestOperationMeta restOperation, HTTP_RESPONSE httpResponse) throws Exception {
String acceptType = restRequest.getHeaderParam("Accept");
ProduceProcessor produceProcessor = locateProduceProcessor(restRequest, httpResponse, restOperation, acceptType);
if (produceProcessor == null) {
// locateProduceProcessor内部已经应答了
return;
}
Object[] args = RestCodec.restToArgs(restRequest, restOperation);
Invocation invocation = InvocationFactory.forProvider(transport.getEndpoint(), restOperation.getOperationMeta(), args);
this.setContext(invocation, restRequest);
this.setHttpRequestContext(invocation, restRequest);
invocation.next(resp -> {
sendResponse(restRequest, httpResponse, produceProcessor, resp);
});
}
use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.
the class AbstractRestServer method locateProduceProcessor.
protected ProduceProcessor locateProduceProcessor(RestServerRequestInternal restRequest, HTTP_RESPONSE httpResponse, RestOperationMeta restOperation, String acceptType) {
ProduceProcessor produceProcessor = restOperation.ensureFindProduceProcessor(acceptType);
if (produceProcessor != null) {
return produceProcessor;
}
String msg = String.format("Accept %s is not supported", acceptType);
InvocationException exception = new InvocationException(Status.NOT_ACCEPTABLE, msg);
sendFailResponse(restRequest, httpResponse, exception);
return null;
}
use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.
the class RestOperationMeta method createProduceProcessors.
protected void createProduceProcessors() {
if (null == produces || produces.isEmpty()) {
for (ProduceProcessor processor : ProduceProcessorManager.INSTANCE.values()) {
this.produceProcessorMap.put(processor.getName(), processor);
}
return;
}
for (String produce : produces) {
ProduceProcessor processor = ProduceProcessorManager.INSTANCE.findValue(produce);
if (processor == null) {
LOGGER.error("produce {} is not supported", produce);
continue;
}
this.produceProcessorMap.put(produce, processor);
}
defaultProcessor = getDefaultOrFirstProcessor();
}
use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.
the class VertxHttpMethod method handleResponse.
protected void handleResponse(Invocation invocation, HttpClientResponse httpResponse, RestOperationMeta restOperation, AsyncResponse asyncResp) {
ProduceProcessor produceProcessor = null;
String contentType = httpResponse.getHeader("Content-Type");
if (contentType != null) {
String contentTypeForFind = contentType;
int idx = contentType.indexOf(';');
if (idx != -1) {
contentTypeForFind = contentType.substring(0, idx);
}
produceProcessor = restOperation.findProduceProcessor(contentTypeForFind);
}
if (produceProcessor == null) {
String msg = String.format("path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", restOperation.getAbsolutePath(), httpResponse.statusCode(), httpResponse.statusMessage(), contentType);
Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
asyncResp.fail(invocation.getInvocationType(), exception);
return;
}
ProduceProcessor finalProduceProcessor = produceProcessor;
httpResponse.bodyHandler(responseBuf -> {
invocation.getResponseExecutor().execute(() -> {
try {
ResponseMeta responseMeta = restOperation.getOperationMeta().findResponseMeta(httpResponse.statusCode());
Object result = finalProduceProcessor.decodeResponse(responseBuf, responseMeta.getJavaType());
Response response = Response.create(httpResponse.statusCode(), httpResponse.statusMessage(), result);
for (String headerName : responseMeta.getHeaders().keySet()) {
List<String> headerValues = httpResponse.headers().getAll(headerName);
for (String headerValue : headerValues) {
response.getHeaders().addHeader(headerName, headerValue);
}
}
asyncResp.complete(response);
} catch (Throwable e) {
asyncResp.fail(invocation.getInvocationType(), e);
}
});
});
}
Aggregations