protected ModelAndView doHandle()

in s3-artifact-storage-server/src/main/java/jetbrains/buildServer/artifacts/s3/web/S3PreSignedUrlController.java [87:152]


  protected ModelAndView doHandle(@NotNull HttpServletRequest httpServletRequest, @NotNull HttpServletResponse httpServletResponse) throws Exception {
    try {
      if (!isPost(httpServletRequest)) {
        throw new HttpServerErrorException(HttpStatus.METHOD_NOT_ALLOWED, httpServletRequest.getMethod() + " not allowed");
      }

      AuthorizationHeader header = AuthorizationHeader.getFrom(httpServletRequest);

      if (header == null) {
        LOG.debug("Failed to provide presigned urls for request " + httpServletRequest + ". No authorization provided.");
        throw new HttpServerErrorException(HttpStatus.UNAUTHORIZED, "No authorization header in request");
      }

      final SimpleCredentials credentials = header.getBasicAuthCredentials();

      final RunningBuildEx runningBuild = getRunningBuild(credentials);
      if (runningBuild == null) {
        LOG.debug("Failed to provide presigned urls for request " + httpServletRequest + ". Can't resolve running build.");
        throw new HttpServerErrorException(HttpStatus.BAD_REQUEST, "build is missing in request");
      }

      final String receivedPassword = credentials.getPassword();
      if (!StringUtil.areEqual(runningBuild.getAgentAccessCode(), receivedPassword)) {
        LOG.debug("Failed to provide presigned urls for request " + httpServletRequest + ". Wrong access code provided.");
        throw new HttpServerErrorException(HttpStatus.UNAUTHORIZED, "Invalid credentials provided");
      }

      final Pair<RequestType, CloudFrontSettings> request = parseRequest(httpServletRequest, runningBuild);

      httpServletResponse.setContentType("application/xml; charset=" + StandardCharsets.UTF_8.name());
      if (request.getFirst() == RequestType.FINISH_MULTIPART_UPLOAD) {
        finishMultipartUpload(httpServletRequest, request.getSecond());
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
      } else {
        final CloudFrontSettings settings = request.getSecond();
        final PresignedUrlListRequestDto urlsRequest = PresignedUrlRequestSerializer.deserializeRequest(StreamUtil.readTextFrom(httpServletRequest.getReader()));

        if(TeamCityProperties.getBooleanOrTrue(S3_VALIDATE_KEYS))
          validateUrlsRequest(urlsRequest, runningBuild);

        final Long customTtl = urlsRequest.getCustomTtl();
        if (customTtl != null) {
          settings.setTtl(customTtl);
        }

        Disposable threadName = NamedDaemonThreadFactory.patchThreadName("Generating " + urlsRequest.getPresignedUrlRequests().size() + " pre-signed URLs"
                                                                         + " for a running build with id: " + runningBuild.getBuildId());
        final String response;
        try {
          response = urlsRequest.isVersion2()
                     ? presignedUrlsV2(urlsRequest, settings)
                     : presignedUrlsV1(urlsRequest, settings);
        } finally {
          threadName.dispose();
        }

        httpServletResponse.getWriter().append(response);
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
      }
      return null;
    } catch (final Exception e) {
      logError(httpServletRequest, e);
      handleException(httpServletResponse, e);
      return null;
    }
  }