Skip to content
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

Allow modification of response from filters #101

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion core/src/main/java/com/flipkart/gjex/core/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ public void doProcessResponseHeaders(M responseHeaders) {}
* This method should be viewed almost like a proxy for the Response body.
*
* @param response the Response body/message
* @return
*/
public void doProcessResponse(Res response) {}
public Res doProcessResponse(Res response) {
return response;
}

/**
* Call-back to handle exceptions that occur during the processing of the request or response.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ public void doProcessResponseHeaders(Metadata responseHeaders) {
* @param response The outgoing gRPC response message.
*/
@Override
public void doProcessResponse(S response) {
public S doProcessResponse(S response) {
accessLogContextBuilder
.contentLength(response.getSerializedSize())
.responseTime(System.currentTimeMillis() - startTime)
.responseStatus(Status.Code.OK.value())
.build();
logger.info(accessLogContextBuilder.build().format(format));
return response;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
* @param <Res> Proto V3 message
* @author ajay.jalgaonkar
*/
public abstract class GrpcFilter<Req extends GeneratedMessageV3, Res extends GeneratedMessageV3>
extends Filter<Req,Res, Metadata> {
public abstract class GrpcFilter<Req extends GeneratedMessageV3, Res extends GeneratedMessageV3> extends Filter<Req,Res, Metadata> {

/**
* Function for creating an instance of this {@link Filter}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ public void doProcessResponseHeaders(Map<String,String> responseHeaders) {
* Logs the client IP, requested URI, response status, content length, and the time taken to process the request.
*
* @param response The outgoing servlet response.
* @return
*/
@Override
public void doProcessResponse(ServletResponse response) {
public ServletResponse doProcessResponse(ServletResponse response) {
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
if (isSuccess(httpServletResponse.getStatus())) {
// 2xx response
Expand All @@ -104,6 +105,7 @@ public void doProcessResponse(ServletResponse response) {
.responseStatus(httpServletResponse.getStatus())
.responseTime(System.currentTimeMillis() - startTime);
logger.info(accessLogContextBuilder.build().format(format));
return response;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.flipkart.gjex.examples.helloworld.filter;

import com.flipkart.gjex.core.filter.RequestParams;
import com.flipkart.gjex.core.filter.grpc.GrpcFilter;
import com.flipkart.gjex.core.logging.Logging;
import com.google.protobuf.GeneratedMessageV3;
import io.grpc.Metadata;
import io.grpc.examples.helloworld.HelloReply;

import javax.inject.Named;
import java.util.Optional;

@Named("CustomHeaderGRPCFilter")
public class CustomHeaderGRPCFilter<Req extends GeneratedMessageV3, Res extends GeneratedMessageV3> extends GrpcFilter<Req, Res> implements Logging {

@Override
public void doProcessResponseHeaders(Metadata responseHeaders) {
super.doProcessResponseHeaders(responseHeaders);
responseHeaders.put(Metadata.Key.of("x-custom-header1", Metadata.ASCII_STRING_MARSHALLER), "value1");
}

@Override
public Res doProcessResponse(Res response) {
response = (Res) ((HelloReply) response).toBuilder().setMessage("Custom Header GRPC Filter").build();
return response;
}

@Override
public GrpcFilter<Req, Res> getInstance() {
return new CustomHeaderGRPCFilter<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ public void doProcessResponseHeaders(Map<String, String> responseHeaders) {
* Processes the response and also adds a custom header.
*
* @param response the servlet response
* @return
*/
@Override
public void doProcessResponse(ServletResponse response) {
public ServletResponse doProcessResponse(ServletResponse response) {
super.doProcessResponse(response);
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.addHeader("x-custom-header2", "value2");
return response;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public void doProcessResponseHeaders(Metadata responseHeaders) {
}

@Override
public void doProcessResponse(Res response) {
public Res doProcessResponse(Res response) {
info("Logging from filter. Response payload is : " + response.toString());
return response;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.flipkart.gjex.core.filter.http.JavaxFilterParams;
import com.flipkart.gjex.core.tracing.TracingSampler;
import com.flipkart.gjex.examples.helloworld.filter.AuthFilter;
import com.flipkart.gjex.examples.helloworld.filter.CustomHeaderGRPCFilter;
import com.flipkart.gjex.examples.helloworld.filter.CustomHeaderHttpFilter;
import com.flipkart.gjex.examples.helloworld.filter.LoggingFilter;
import com.flipkart.gjex.examples.helloworld.service.GreeterService;
Expand Down Expand Up @@ -52,7 +53,8 @@ protected void configure() {
bind(GreeterGrpc.GreeterBlockingStub.class).toInstance(GreeterGrpc.newBlockingStub(channel));
bind(BindableService.class).annotatedWith(Names.named("GreeterService")).to(GreeterService.class);
bind(GrpcFilter.class).annotatedWith(Names.named("LoggingFilter")).to(LoggingFilter.class);
bind(GrpcFilter.class).annotatedWith(Names.named("AuthFilter")).to(AuthFilter.class);
bind(GrpcFilter.class).annotatedWith(Names.named("AuthFilter")).to(AuthFilter.class);
bind(GrpcFilter.class).annotatedWith(Names.named("CustomHeaderGRPCFilter")).to(CustomHeaderGRPCFilter.class);
bind(TracingSampler.class).to(AllWhitelistTracingSampler.class);
bind(ResourceConfig.class).annotatedWith(Names.named("HelloWorldResourceConfig")).to(HelloWorldResourceConfig.class);
bind(JavaxFilterParams.class).annotatedWith(Names.named("ExampleJavaxFilter")).toInstance(JavaxFilterParams.builder().filter(new ExampleJavaxFilter()).pathSpec("/*").build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.flipkart.gjex.core.context.GJEXContext;
import com.flipkart.gjex.examples.helloworld.filter.AuthFilter;
import com.flipkart.gjex.examples.helloworld.filter.CustomHeaderGRPCFilter;
import io.dropwizard.metrics5.annotation.Timed;
import com.flipkart.gjex.core.filter.grpc.ApplicationHeaders;
import com.flipkart.gjex.core.filter.grpc.MethodFilters;
Expand Down Expand Up @@ -68,7 +69,8 @@ public GreeterService(@Named("hw.greeting") String greeting, HelloBeanService he
@Override
@Api(deadlineConfig = "apiProperties.sayhello.deadline") // specify an API level Deadline that will cascade to all @ConcurrentTask invoked in serving this API
@Timed // the Timed annotation for publishing JMX metrics via MBean
@MethodFilters({LoggingFilter.class, AuthFilter.class}) // Method level filters

@MethodFilters({LoggingFilter.class, AuthFilter.class, CustomHeaderGRPCFilter.class}) // Method level filters
@Traced(withSamplingRate=0.5f) // Start a new Trace or participate in a Client-initiated distributed trace
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public void doProcessRequest(CreateRequest request, RequestParams<Metadata> requ
public void doProcessResponseHeaders(Metadata responseHeaders) {}

@Override
public void doProcessResponse(CreateResponse response) {
public CreateResponse doProcessResponse(CreateResponse response) {
info("Response: " + response);
return response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public void doProcessResponseHeaders(Metadata reponseHeaders) {
}

@Override
public void doProcessResponse(GetResponse response) {
public GetResponse doProcessResponse(GetResponse response) {
info("Response: " + response);
return response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ public <Req, Res> Listener<Req> interceptCall(ServerCall<Req, Res> call, Metadat

ServerCall.Listener<Req> listener = next.startCall(new SimpleForwardingServerCall<Req, Res>(call) {
@Override
public void sendMessage(final Res response) {
public void sendMessage(Res response) {
Context previous = attachContext(contextWithHeaders); // attaching headers to gRPC context
try {
grpcFilters.forEach(filter -> filter.doProcessResponse(response));
for (GrpcFilter f : grpcFilters) {
response = (Res) f.doProcessResponse(response);
}
super.sendMessage(response);
} finally {
detachContext(contextWithHeaders, previous); // detach headers from gRPC context
Expand Down