-
Notifications
You must be signed in to change notification settings - Fork 96
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
Adds a factory for building ResponseProcessor
.
#663
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
package com.yahoo.bard.webservice.web.endpoints; | ||
|
||
import static com.yahoo.bard.webservice.web.handlers.workflow.DruidWorkflow.REQUEST_WORKFLOW_TIMER; | ||
|
||
import static javax.ws.rs.core.Response.Status.BAD_REQUEST; | ||
import static javax.ws.rs.core.Response.Status.GATEWAY_TIMEOUT; | ||
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; | ||
|
@@ -52,7 +53,8 @@ | |
import com.yahoo.bard.webservice.web.handlers.RequestContext; | ||
import com.yahoo.bard.webservice.web.handlers.RequestHandlerUtils; | ||
import com.yahoo.bard.webservice.web.handlers.workflow.RequestWorkflowProvider; | ||
import com.yahoo.bard.webservice.web.responseprocessors.ResultSetResponseProcessor; | ||
import com.yahoo.bard.webservice.web.responseprocessors.ResponseProcessor; | ||
import com.yahoo.bard.webservice.web.responseprocessors.ResponseProcessorFactory; | ||
import com.yahoo.bard.webservice.web.util.BardConfigResources; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
|
@@ -122,6 +124,7 @@ public class DataServlet extends CORSPreflightServlet implements BardConfigResou | |
private final ObjectMappersSuite objectMappers; | ||
private final HttpResponseMaker httpResponseMaker; | ||
private final ResponseFormatResolver formatResolver; | ||
private final ResponseProcessorFactory responseProcessorFactory; | ||
|
||
private final DataApiRequestFactory dataApiRequestFactory; | ||
|
||
|
@@ -153,6 +156,7 @@ public class DataServlet extends CORSPreflightServlet implements BardConfigResou | |
* @param formatResolver The formatResolver for determining correct response format | ||
* @param dataApiRequestFactory A factory to build dataApiRequests | ||
* {@link com.yahoo.bard.webservice.async.preresponses.stores.PreResponseStore} | ||
* @param responseProcessorFactory Builds the object that performs post processing on a Druid response | ||
*/ | ||
@Inject | ||
public DataServlet( | ||
|
@@ -172,7 +176,8 @@ public DataServlet( | |
BroadcastChannel<String> preResponseStoredNotifications, | ||
HttpResponseMaker httpResponseMaker, | ||
ResponseFormatResolver formatResolver, | ||
DataApiRequestFactory dataApiRequestFactory | ||
DataApiRequestFactory dataApiRequestFactory, | ||
ResponseProcessorFactory responseProcessorFactory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are changing contract of this method and there are possibilities of extending There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We haven't been doing that for the DataServlet. And we could, but seeing as how Also, although it is technically possible to override the Especially since this is an easy enough thing to fix. |
||
) { | ||
this.resourceDictionaries = resourceDictionaries; | ||
this.druidQueryBuilder = druidQueryBuilder; | ||
|
@@ -192,6 +197,7 @@ public DataServlet( | |
this.httpResponseMaker = httpResponseMaker; | ||
this.formatResolver = formatResolver; | ||
this.dataApiRequestFactory = dataApiRequestFactory; | ||
this.responseProcessorFactory = responseProcessorFactory; | ||
|
||
LOG.trace( | ||
"Initialized with ResourceDictionaries: {} \n\n" + | ||
|
@@ -426,7 +432,7 @@ public void getData( | |
httpResponseMaker | ||
); | ||
|
||
ResultSetResponseProcessor response = new ResultSetResponseProcessor( | ||
ResponseProcessor responseProcessor = responseProcessorFactory.build( | ||
apiRequest, | ||
queryResultsEmitter, | ||
druidResponseParser, | ||
|
@@ -441,7 +447,7 @@ public void getData( | |
|
||
// Process the request | ||
RequestLog.startTiming(REQUEST_WORKFLOW_TIMER); | ||
boolean complete = dataRequestHandler.handleRequest(context, apiRequest, druidQuery, response); | ||
boolean complete = dataRequestHandler.handleRequest(context, apiRequest, druidQuery, responseProcessor); | ||
if (!complete) { | ||
throw new IllegalStateException("No request handler accepted request."); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2018 Yahoo Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.web.responseprocessors; | ||
|
||
import com.yahoo.bard.webservice.application.ObjectMappersSuite; | ||
import com.yahoo.bard.webservice.data.DruidResponseParser; | ||
import com.yahoo.bard.webservice.data.HttpResponseMaker; | ||
import com.yahoo.bard.webservice.web.DataApiRequest; | ||
import com.yahoo.bard.webservice.web.PreResponse; | ||
|
||
import rx.subjects.Subject; | ||
|
||
/** | ||
* A {@link ResponseProcessor} relies on things that are directly constructed at request time (i.e. the | ||
* {@link DataApiRequest}). Therefore, we can't inject a `ResponseProcessor` directly. We can however inject a factory. | ||
*/ | ||
public interface ResponseProcessorFactory { | ||
|
||
/** | ||
* Constructs a custom ResponseProcessor. | ||
* | ||
* @param apiRequest The current request | ||
* @param responseEmitter Generates the response to be processed | ||
* @param druidResponseParser Transforms a druid response into a {@link ResultSet} | ||
* @param objectMappers Dictates how to format | ||
* @param httpResponseMaker Crafts an HTTP response to be sent back to the user from a ResultSet or error message | ||
* | ||
* @return An object that handles parsing and post-processing of Druid requests | ||
*/ | ||
ResponseProcessor build( | ||
DataApiRequest apiRequest, | ||
Subject<PreResponse, PreResponse> responseEmitter, | ||
DruidResponseParser druidResponseParser, | ||
ObjectMappersSuite objectMappers, | ||
HttpResponseMaker httpResponseMaker | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2018 Yahoo Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.web.responseprocessors; | ||
|
||
import com.yahoo.bard.webservice.application.ObjectMappersSuite; | ||
import com.yahoo.bard.webservice.data.DruidResponseParser; | ||
import com.yahoo.bard.webservice.data.HttpResponseMaker; | ||
import com.yahoo.bard.webservice.web.DataApiRequest; | ||
import com.yahoo.bard.webservice.web.PreResponse; | ||
|
||
import rx.subjects.Subject; | ||
|
||
/** | ||
* Builds the default Druid response processor: {@link ResultSetResponseProcessor}. | ||
*/ | ||
public class ResultSetResponseProcessorFactory implements ResponseProcessorFactory { | ||
|
||
@Override | ||
public ResponseProcessor build( | ||
DataApiRequest apiRequest, | ||
Subject<PreResponse, PreResponse> responseEmitter, | ||
DruidResponseParser druidResponseParser, | ||
ObjectMappersSuite objectMappers, | ||
HttpResponseMaker httpResponseMaker | ||
) { | ||
return new ResultSetResponseProcessor( | ||
apiRequest, | ||
responseEmitter, | ||
druidResponseParser, | ||
objectMappers, | ||
httpResponseMaker | ||
); | ||
} | ||
} |
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.
Prob. not a blocker for this PR, but what if other
ResponseProcessor
implementations also need custom injection?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.
Erm, that's the whole idea of this PR? If somebody wants to inject their
own custom
ResponseProcessor
then they:CustomResponseProcessor
.CustomResponseProcessorFactory
.CustomResponseProcessorFactory.class
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.
Do you mean if they have non-empty constructors? Then they'd need to make their
constructor injectable so that HK2 can handle it.
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.
Make sense