/** * Constructor. * * @param apiRequest The request for which the response is being processed * @param objectMappers Jackson mappers to use for processing JSON */ public MappingResponseProcessor(DataApiRequest apiRequest, ObjectMappersSuite objectMappers) { this.apiRequest = apiRequest; this.mappers = buildResultSetMapperList(apiRequest); this.headers = buildHeaderList(); this.responseContext = new ResponseContext(apiRequest.getDimensionFields()); this.objectMappers = objectMappers; }
/** * Builds a mapped result set by running through ResultSetMappers. * * @param resultSet The result set being processed * * @return a mapped resultSet */ protected ResultSet mapResultSet(ResultSet resultSet) { ResultSet mappedResultSet = resultSet; for (ResultSetMapper resultSetMapper : getMappers()) { mappedResultSet = resultSetMapper.map(mappedResultSet); } return mappedResultSet; }
@Override public void invoke(int statusCode, String reason, String responseBody) { LOG.error(ErrorMessageFormat.ERROR_FROM_DRUID.logFormat(responseBody, statusCode, reason, druidQuery)); responseEmitter.onError(new ResponseException( statusCode, reason, responseBody, druidQuery, null, getObjectMappers().getMapper().writer() )); } };
() -> VolatileDataRequestHandler.getVolatileIntervalsWithDefault(responseContext) ); mappingResponse.getMappers().add(0, mapper); mappingResponse.getHeaders().add(PARTIAL_DATA_HEADER, true); responseContext.put(PARTIAL_DATA_HEADER, true);
/** * Adds all the required pagination links to the headers and body of the response. * * @param pages The paginated set of results containing the pages being linked to. * @param uriBuilder The uri builder to build the links * @param responseProcessor The response processor whose links are being built */ public static void addLinks( Pagination<?> pages, UriBuilder uriBuilder, MappingResponseProcessor responseProcessor ) { LinkedHashMap<String, URI> bodyLinks = new LinkedHashMap<>(); Arrays.stream(PaginationLink.values()) .forEachOrdered(link -> addLink(link, pages, uriBuilder, responseProcessor, bodyLinks)); ResponseContext responseContext = responseProcessor.getResponseContext(); responseContext.put(ResponseContextKeys.PAGINATION_LINKS_CONTEXT_KEY.getName(), bodyLinks); responseContext.put(ResponseContextKeys.PAGINATION_CONTEXT_KEY.getName(), pages); }
/** * Adds the specified link to the headers and to the map of links that will be added to the body of the response. * * @param link The type of the link being added * @param pageNumber The page being linked to * @param perPage The number of result rows in the page being linked to * @param uriBuilder The uri builder to build the links * @param responseProcessor The response processor whose links are being built * @param bodyLinks The map of links that will be added to the body of the response */ public static void addLink( PaginationLink link, int pageNumber, int perPage, UriBuilder uriBuilder, MappingResponseProcessor responseProcessor, Map<String, URI> bodyLinks ) { UriBuilder pageLink = uriBuilder.replaceQueryParam(PAGE, pageNumber).replaceQueryParam(PER_PAGE, perPage); responseProcessor.getHeaders().add( HttpHeaders.LINK, Link.fromUriBuilder(pageLink).rel(link.getHeaderName()).build().toString() ); bodyLinks.put(link.getBodyName(), pageLink.build()); }
@Override public boolean handleRequest( RequestContext context, DataApiRequest request, DruidAggregationQuery<?> druidQuery, ResponseProcessor response ) { if (request.getTopN().isPresent() && !(druidQuery instanceof TopNQuery)) { TopNResultSetMapper mapper = new TopNResultSetMapper(request.getTopN().getAsInt()); // Add topN mapper after partial data mapper and before any other mapper try { // Index is 1 because we assume that partial data result set mapper has been injected already // by the PartialDataRequestHandler ((MappingResponseProcessor) response).getMappers().add(1, mapper); } catch (ClassCastException cce) { throw new IllegalStateException("TopN request handler requires a mapping response processor.", cce); } } return next.handleRequest(context, request, druidQuery, response); } }
@Override public boolean handleRequest( RequestContext context, DataApiRequest request, DruidAggregationQuery<?> druidQuery, ResponseProcessor response ) { MappingResponseProcessor mappingResponse = (MappingResponseProcessor) response; if (request.getDateTimeSort().isPresent()) { mappingResponse.getMappers().add(new DateTimeSortMapper(request.getDateTimeSort().get().getDirection())); } return next.handleRequest(context, request, druidQuery, mappingResponse); } }
@Override public boolean handleRequest( RequestContext context, DataApiRequest request, DruidAggregationQuery<?> druidQuery, ResponseProcessor response ) { if (!(response instanceof MappingResponseProcessor)) { throw new IllegalStateException("Pagination request handler requires a mapping response."); } MappingResponseProcessor mappingResponse = (MappingResponseProcessor) response; if (request.getPaginationParameters().isPresent()) { PaginationParameters paginationParameters = request.getPaginationParameters().get(); mappingResponse.getMappers().add( new PaginationMapper( paginationParameters, mappingResponse, context.getUriBuilder() ) ); } return next.handleRequest(context, request, druidQuery, mappingResponse); } }