Skip to content

Commit

Permalink
generalize HttpResponseMaker to allow for extension (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
efronbs authored and QubitPi committed Sep 25, 2018
1 parent 2a054dd commit 09024e8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ pull request if there was one.

### Added:

- [HttpResponseMaker header building made extendable](https://github.com/yahoo/fili/issues/783)
* Added `buildAndAddResponseHeaders` method in `HttpResponseMaker` which handles building and adding headers to a
response builder. This logic was moved from `createResponseBuilder`.
* Made `createResponseBuilder` method protected to open up the class to be more extendable.

- [Add factory for build ApiFilter objects](https://github.com/yahoo/fili/issues/771)
* `ApiFilter` changed into a simple value object.
* `ApiFilter` constructor using filter clause from API request moved to factory as static `build` method.
* `ApiFilter` union method moved to factory.

- [Add interfance to FilterOperation for easy extension](https://github.com/yahoo/fili/issues/771)
- [Add interface to FilterOperation for easy extension](https://github.com/yahoo/fili/issues/771)
* Changed existing version of `FilterOperation` to `DefaulFilterOperation` and made `FilterOperation` into an interface.
* Changed code that depended on the enum to be dependent on the new interfaces instead.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public javax.ws.rs.core.Response buildResponse(
*
* @return Build response with requested format and associated meta data info.
*/
private ResponseBuilder createResponseBuilder(
protected ResponseBuilder createResponseBuilder(
ResultSet resultSet,
ResponseContext responseContext,
ApiRequest apiRequest,
Expand Down Expand Up @@ -187,20 +187,13 @@ private ResponseBuilder createResponseBuilder(
responseWriter.write(apiRequest, responseData, outputStream);
};

// pass stream handler as response
// pass stream handler as response
ResponseBuilder rspBuilder = javax.ws.rs.core.Response.ok(stream);

if (CSV.accepts(responseFormatType)) {
return rspBuilder
.header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=utf-8")
.header(
HttpHeaders.CONTENT_DISPOSITION,
responseUtils.getCsvContentDispositionValue(containerRequestContext)
);
} else {
return rspBuilder
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON + "; charset=utf-8");
}
return buildAndAddResponseHeaders(
rspBuilder,
responseFormatType,
containerRequestContext
);
}

/**
Expand Down Expand Up @@ -260,4 +253,30 @@ protected ResponseData buildResponseData(
paginationLinks
);
}

/**
* Builds the headers for the response and gives them to the provided response builder to be added to the response.
*
* @param rspBuilder ResponseBuilder that handles adding the headers to the response
* @param responseFormatType The type of the response
* @param containerRequestContext The request context
* @return the response builder that has had the headers added
*/
protected ResponseBuilder buildAndAddResponseHeaders(
ResponseBuilder rspBuilder,
ResponseFormatType responseFormatType,
ContainerRequestContext containerRequestContext
) {
if (CSV.accepts(responseFormatType)) {
return rspBuilder
.header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=utf-8")
.header(
HttpHeaders.CONTENT_DISPOSITION,
responseUtils.getCsvContentDispositionValue(containerRequestContext)
);
} else {
return rspBuilder
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON + "; charset=utf-8");
}
}
}

0 comments on commit 09024e8

Please sign in to comment.