DoubleClickBidRequestReceiver extends BidRequestReceiver<NetworkBid.BidRequest, NetworkBid.BidResponse> { @Override public void receive(HttpReceiverContext ctx) { boolean unhandledException = true; Timer.Context timerContext = requestTimer().time(); CodedHelpers helpers = codedHelpers.get(); try { long start = clock.nanoTime(); BidRequest request = newRequest(ctx.httpRequest(), helpers).build(); if (logger.isDebugEnabled()) { logger.debug("DoubleClick request:\n{}", request.nativeRequest()); logger.debug("Open Bidder Request:\n{}", request); } BidResponse response = newResponse(ctx.httpResponse()).build(); NetworkBid.BidResponse.Builder responseBuilder = handleBidRequest(request, response); long end = clock.nanoTime(); responseBuilder.setProcessingTimeMs((int) ((end - start) / 1000000)); NetworkBid.BidResponse dcResponse = responseBuilder.build(); if (logger.isDebugEnabled()) { logger.debug("Open Bidder response:\n{}", response); logger.debug("DoubleClick response:\n{}", dcResponse); } try { try { helpers.osw.setTarget(ctx.httpResponse().content()); dcResponse.writeTo(helpers.cos); } finally { helpers.cos.flush(); } } finally { helpers.osw.close(); } ctx.httpResponse().setStatusOk(); ctx.httpResponse().setMediaType(MediaType.OCTET_STREAM); unhandledException = false; } catch (InvalidProtocolBufferException e) { logger.error("Bad DoubleClick request: {}\nUnfinished message: {}\n", e, e.getUnfinishedMessage()); ctx.httpResponse().setStatusCode(HttpStatus.SC_BAD_REQUEST); } catch (IOException e) { logger.error(e.toString()); ctx.httpResponse().setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); } finally { if (unhandledException) { interceptorOtherMeter().mark(); } timerContext.close(); } } @Inject  DoubleClickBidRequestReceiver(
      MetricRegistry metricRegistry,
      BidController controller,
      DoubleClickSnippetProcessor snippetProcessor,
      OpenRtbMapper<
          NetworkBid.BidRequest, NetworkBid.BidResponse,
          NetworkBid.BidRequest.Builder, NetworkBid.BidResponse.Builder> mapper,
      @Nullable DoubleClickValidator validator,
      Clock clock); @Override void receive(HttpReceiverContext ctx);  }
@Test public void testHttpRequest() throws IOException { HttpRequest httpRequest = DoubleClickTestUtil.newHttpRequest( NetworkBid.BidRequest.newBuilder().setId(DoubleClickTestUtil.REQUEST_ID)); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); DoubleClickBidRequestReceiver receiver = DoubleClickTestUtil.newReceiver(countingInterceptor); receiver.receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(1, countingInterceptor.invokeCount); NetworkBid.BidResponse bidResponse = NetworkBid.BidResponse.parseFrom(httpResponse.build().content()); assertEquals(1, bidResponse.getProcessingTimeMs()); }
@Test public void testHttpRequest_badRequest() { HttpRequest httpRequest = StandardHttpRequest.newBuilder() .setProtocol(Protocol.HTTP_1_1) .setMethod("POST") .setUri("http: .build(); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); DoubleClickTestUtil.newReceiver() .receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(HttpStatus.SC_BAD_REQUEST, httpResponse.getStatusCode()); }
@Test public void testPingRequest() throws IOException { HttpRequest httpRequest = DoubleClickTestUtil.newHttpRequest(NetworkBid.BidRequest.newBuilder() .setId(DoubleClickTestUtil.REQUEST_ID) .setIsPing(true)); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); DoubleClickBidRequestReceiver receiver = DoubleClickTestUtil.newReceiver(); receiver.receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(0, countingInterceptor.invokeCount); assertEquals(NetworkBid.BidResponse.parseFrom(httpResponse.build().content()), NetworkBid.BidResponse.newBuilder().setProcessingTimeMs(1).build()); }
@Test public void testHttpRequestNativeOnly() { HttpRequest httpRequest = DoubleClickTestUtil.newHttpRequest( NetworkBid.BidRequest.newBuilder().setId(DoubleClickTestUtil.REQUEST_ID)); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); MetricRegistry metricRegistry = new MetricRegistry(); DoubleClickBidRequestReceiver receiver = new DoubleClickBidRequestReceiver( metricRegistry, BiddingTestUtil.newBidController(metricRegistry, countingInterceptor), DoubleClickTestUtil.newSnippetProcessor(), NullDoubleClickOpenRtbMapper.INSTANCE, null, new FakeClock()); receiver.receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(1, countingInterceptor.invokeCount); }