public void handleRequest()

in hms-lambda-handler/src/main/java/com/amazonaws/athena/hms/MetadataHandler.java [105:138]


  public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException
  {
    try (MetadataRequest metadataRequest = objectMapper.readValue(inputStream, MetadataRequest.class)) {
      // cloud watch logs, we need them for monitoring and debugging
      context.getLogger().log("RequestContext: " + metadataRequest.getContext());
      String apiName = metadataRequest.getApiName();
      context.getLogger().log("API: " + apiName);
      HandlerContext handlerContext = handlers.get(apiName);
      if (handlerContext == null) {
        throw new RuntimeException("Cannot find handler for API " + apiName);
      }
      ApiRequest apiRequest = metadataRequest.getApiRequest();
      ApiResponse apiResponse = (ApiResponse) handlerContext.getHandler().handleRequest(apiRequest, context);
      // serialize ApiResponse to String to get its size. Please be aware this isn't accurate
      String responseAsString = objectMapper.writerFor(handlerContext.getResponseClass()).writeValueAsString(apiResponse);
      // get the response String size
      long responseSize = responseAsString.getBytes().length;
      context.getLogger().log("Response size: " + responseSize);
      MetadataResponse response;
      if (responseSize >= responseSpillThreshold) {
        String spillPath = spillToS3(context, responseAsString);
        context.getLogger().log("Response size " + responseAsString.length() + " exceeded threshold "
            + responseSpillThreshold + ", is saved to s3: " + spillPath);
        response = new MetadataResponse(apiName, true, spillPath, null);
      }
      else {
        response = new MetadataResponse(apiName, false, null, apiResponse);
      }
      objectMapper.writeValue(outputStream, response);
    }
    catch (Exception e) {
      throw new IOException(e);
    }
  }