Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Apr 2, 2024
1 parent 62bedb1 commit e37f8b3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,25 @@

public abstract class AbstractServerHttpChannelObserver implements CustomizableHttpChannelObserver<Object> {

private final HttpChannel httpChannel;

private HeadersCustomizer headersCustomizer = HeadersCustomizer.NO_OP;

private TrailersCustomizer trailersCustomizer = TrailersCustomizer.NO_OP;

private ErrorResponseCustomizer errorResponseCustomizer = ErrorResponseCustomizer.NO_OP;

private final HttpChannel httpChannel;
private HttpMessageEncoder responseEncoder;

private boolean headerSent;

private HttpMessageEncoder responseEncoder;

protected AbstractServerHttpChannelObserver(HttpChannel httpChannel) {
this.httpChannel = httpChannel;
}

public void setResponseEncoder(HttpMessageEncoder responseEncoder) {
this.responseEncoder = responseEncoder;
}

public HttpMessageEncoder getResponseEncoder() {
return responseEncoder;
@Override
public HttpChannel getHttpChannel() {
return httpChannel;
}

@Override
Expand All @@ -62,16 +59,16 @@ public void setErrorResponseCustomizer(ErrorResponseCustomizer errorResponseCust
this.errorResponseCustomizer = errorResponseCustomizer;
}

protected HeadersCustomizer getHeadersCustomizer() {
return headersCustomizer;
public HttpMessageEncoder getResponseEncoder() {
return responseEncoder;
}

protected TrailersCustomizer getTrailersCustomizer() {
return trailersCustomizer;
public void setResponseEncoder(HttpMessageEncoder responseEncoder) {
this.responseEncoder = responseEncoder;
}

@Override
public void onNext(Object data) {
public final void onNext(Object data) {
try {
doOnNext(data);
} catch (Throwable e) {
Expand All @@ -81,29 +78,13 @@ public void onNext(Object data) {

protected void doOnNext(Object data) throws Throwable {
if (!headerSent) {
HttpMetadata httpMetadata = buildMetadata(httpStatusCode(data, false));
sendHeader(httpMetadata, data);
sendHeader(buildMetadata(resolveStatusCode(data), data, null));
}
HttpOutputMessage outputMessage = encodeData(data);
sendData(outputMessage);
}

protected void preOutputMessage(HttpOutputMessage outputMessage) throws Throwable {}

protected void postOutputMessage(HttpOutputMessage outputMessage) throws Throwable {}

protected abstract HttpMetadata encodeHttpMetadata();

protected HttpOutputMessage encodeHttpOutputMessage(Object data) {
return getHttpChannel().newOutputMessage();
}

protected HttpMetadata encodeTrailers(Throwable throwable) {
return null;
sendMessage(buildMessage(data));
}

@Override
public void onError(Throwable throwable) {
public final void onError(Throwable throwable) {
if (throwable instanceof HttpResultPayloadException) {
onNext(((HttpResultPayloadException) throwable).getResult());
return;
Expand All @@ -118,89 +99,96 @@ public void onError(Throwable throwable) {
}

protected void doOnError(Throwable throwable) throws Throwable {
String httpStatusCode = httpStatusCode(throwable, true);
String statusCode = resolveStatusCode(throwable);
Object data = buildErrorResponse(statusCode, throwable);
if (!headerSent) {
HttpMetadata httpMetadata = buildMetadata(httpStatusCode);
sendHeader(httpMetadata, null);
sendHeader(buildMetadata(statusCode, data, null));
}
HttpOutputMessage httpOutputMessage = encodeData(buildErrorResponse(httpStatusCode, throwable));
sendData(httpOutputMessage);
sendMessage(buildMessage(data));
}

@Override
public void onCompleted() {
public final void onCompleted() {
doOnCompleted(null);
}

@Override
public HttpChannel getHttpChannel() {
return httpChannel;
protected void doOnCompleted(Throwable throwable) {
HttpMetadata httpMetadata = encodeTrailers(throwable);
if (httpMetadata == null) {
return;
}
trailersCustomizer.accept(httpMetadata.headers(), throwable);
getHttpChannel().writeHeader(httpMetadata);
}

protected HttpOutputMessage encodeData(Object data) throws Throwable {
if (data instanceof HttpResult) {
data = ((HttpResult<?>) data).getBody();
}
HttpOutputMessage outputMessage = encodeHttpOutputMessage(data);
preOutputMessage(outputMessage);
responseEncoder.encode(outputMessage.getBody(), data);
return outputMessage;
protected HttpMetadata encodeTrailers(Throwable throwable) {
return null;
}

protected void sendData(HttpOutputMessage outputMessage) throws Throwable {
getHttpChannel().writeMessage(outputMessage);
postOutputMessage(outputMessage);
protected HttpOutputMessage encodeHttpOutputMessage(Object data) {
return getHttpChannel().newOutputMessage();
}

protected void sendHeader(HttpMetadata httpMetadata, Object data) {
if (data instanceof HttpResult) {
HttpResult<?> result = (HttpResult<?>) data;
if (result.getHeaders() != null) {
httpMetadata.headers().putAll(result.getHeaders());
}
}
getHttpChannel().writeHeader(httpMetadata);
headerSent = true;
protected abstract HttpMetadata encodeHttpMetadata();

protected void preOutputMessage(HttpOutputMessage outputMessage) throws Throwable {}

protected void postOutputMessage(HttpOutputMessage outputMessage) throws Throwable {}

protected void preMetadata(HttpMetadata httpMetadata, HttpOutputMessage outputMessage) {}

protected final String resolveStatusCode(Object data) {
return String.valueOf(data instanceof HttpResult ? ((HttpResult<?>) data).getStatus() : HttpStatus.OK);
}

protected String httpStatusCode(Object data, boolean isError) {
String httpStatusCode = HttpStatus.OK.getStatusString();
if (data instanceof HttpResult) {
httpStatusCode = String.valueOf(((HttpResult<?>) data).getStatus());
} else if (isError) {
Throwable throwable = (Throwable) data;
int errorCode = HttpStatus.INTERNAL_SERVER_ERROR.getCode();
if (throwable instanceof HttpStatusException) {
errorCode = ((HttpStatusException) throwable).getStatusCode();
}
httpStatusCode = String.valueOf(errorCode);
}
return httpStatusCode;
protected final String resolveStatusCode(Throwable throwable) {
return String.valueOf(
throwable instanceof HttpStatusException
? ((HttpStatusException) throwable).getStatusCode()
: HttpStatus.INTERNAL_SERVER_ERROR.getCode());
}

protected ErrorResponse buildErrorResponse(String statusCode, Throwable throwable) {
protected final ErrorResponse buildErrorResponse(String statusCode, Throwable throwable) {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setStatus(statusCode);
errorResponse.setMessage(throwable.getMessage());
errorResponseCustomizer.accept(errorResponse, throwable);
return errorResponse;
}

protected HttpMetadata buildMetadata(String statusCode) {
protected final HttpOutputMessage buildMessage(Object data) throws Throwable {
if (data instanceof HttpResult) {
data = ((HttpResult<?>) data).getBody();
}
HttpOutputMessage outputMessage = encodeHttpOutputMessage(data);
preOutputMessage(outputMessage);
responseEncoder.encode(outputMessage.getBody(), data);
return outputMessage;
}

protected final void sendMessage(HttpOutputMessage outputMessage) throws Throwable {
getHttpChannel().writeMessage(outputMessage);
postOutputMessage(outputMessage);
}

protected final HttpMetadata buildMetadata(String statusCode, Object data, HttpOutputMessage httpOutputMessage) {
HttpMetadata httpMetadata = encodeHttpMetadata();
HttpHeaders headers = httpMetadata.headers();
headers.set(HttpHeaderNames.STATUS.getName(), statusCode);
headers.set(HttpHeaderNames.CONTENT_TYPE.getName(), responseEncoder.contentType());
if (data instanceof HttpResult) {
HttpResult<?> result = (HttpResult<?>) data;
if (result.getHeaders() != null) {
headers.putAll(result.getHeaders());
}
}
preMetadata(httpMetadata, httpOutputMessage);
headersCustomizer.accept(headers);
return httpMetadata;
}

protected void doOnCompleted(Throwable throwable) {
HttpMetadata httpMetadata = encodeTrailers(throwable);
if (httpMetadata == null) {
return;
}
trailersCustomizer.accept(httpMetadata.headers(), throwable);
protected final void sendHeader(HttpMetadata httpMetadata) {
getHttpChannel().writeHeader(httpMetadata);
headerSent = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,22 @@ public Http1ServerUnaryChannelObserver(HttpChannel httpChannel) {

@Override
protected void doOnNext(Object data) throws Throwable {
HttpOutputMessage httpOutputMessage = encodeData(data);
HttpMetadata httpMetadata = buildMetadata(httpStatusCode(data, false));
preMetadata(httpMetadata, httpOutputMessage);
sendHeader(httpMetadata, data);
sendData(httpOutputMessage);
HttpOutputMessage httpOutputMessage = buildMessage(data);
sendHeader(buildMetadata(resolveStatusCode(data), data, httpOutputMessage));
sendMessage(httpOutputMessage);
}

@Override
protected void doOnError(Throwable throwable) throws Throwable {
String httpStatusCode = httpStatusCode(throwable, true);
HttpOutputMessage httpOutputMessage = encodeData(buildErrorResponse(httpStatusCode, throwable));
HttpMetadata httpMetadata = buildMetadata(httpStatusCode);
preMetadata(httpMetadata, httpOutputMessage);
sendHeader(httpMetadata, null);
sendData(httpOutputMessage);
String statusCode = resolveStatusCode(throwable);
Object data = buildErrorResponse(statusCode, throwable);
HttpOutputMessage httpOutputMessage = buildMessage(data);
sendHeader(buildMetadata(statusCode, data, httpOutputMessage));
sendMessage(httpOutputMessage);
}

private void preMetadata(HttpMetadata httpMetadata, HttpOutputMessage outputMessage) {
@Override
protected void preMetadata(HttpMetadata httpMetadata, HttpOutputMessage outputMessage) {
OutputStream body = outputMessage.getBody();
if (body instanceof ByteBufOutputStream) {
int contentLength = ((ByteBufOutputStream) body).writtenBytes();
Expand Down

0 comments on commit e37f8b3

Please sign in to comment.