/**
   * GET an URL on the Google Apps apis as the current user.
   *
   * @param googlePath The google API path to get, not including the "www.googleapis.com"
   * @return The Google API server's response.
   */
  @GET
  @Path("/{googlepath:.*}")
  @Produces({MediaType.APPLICATION_JSON})
  public Response get(
      @PathParam(value = "googlepath") String googlePath, @Context HttpServletRequest request) {
    String userID = request.getRemoteUser();
    if (userID == null) {
      throw new UnauthorizedException("Google proxy is not supported for anonymous users");
    }

    Credential credential = getCredential(userID);

    try {
      return doMethod(
          HttpMethod.GET,
          RestUtils.convertToEntity(request, credential.getAccessToken()),
          RestUtils.pathPlusQueryString(googlePath, request));
    } catch (HttpClientErrorException error4xx) {
      if (error4xx.getStatusCode().value() == HttpResponseCodes.SC_UNAUTHORIZED) {
        oAuth2Dao.delete(userID, GoogleCredentialStore.GOOGLE_APP_ID);
      }
      return ServerResponse.ok()
          .status(error4xx.getStatusCode().value())
          .entity(error4xx.getMessage())
          .build();
    }
  }
  @Test
  public void testPostProcessWithPaging() {
    when(page.getPageRequest()).thenReturn(pageRequest);
    when(page.getMaxRecords()).thenReturn(15);
    when(pageRequest.isPaging()).thenReturn(true);
    when(pageRequest.getPage()).thenReturn(2);
    when(pageRequest.getPerPage()).thenReturn(5);

    // We're going to take the quick path through buildBaseUrl.
    when(config.containsKey(eq(ConfigProperties.PREFIX_APIURL))).thenReturn(false);
    when(request.getRequestURL()).thenReturn(new StringBuffer("https://example.com/candlepin"));
    when(request.getQueryString()).thenReturn("order=asc&page=1&per_page=10");

    MultivaluedMap<String, Object> map = new MultivaluedMapImpl<String, Object>();
    when(response.getMetadata()).thenReturn(map);

    ResteasyProviderFactory.pushContext(Page.class, page);
    ResteasyProviderFactory.pushContext(HttpServletRequest.class, request);

    interceptor.postProcess(response);
    String header = (String) map.getFirst(LinkHeaderPostInterceptor.LINK_HEADER);

    // It would be a bit much to parse the entire header, so let's just make
    // sure that we have first, last, next, and prev links.
    assertTrue(header.contains("rel=\"first\""));
    assertTrue(header.contains("rel=\"last\""));
    assertTrue(header.contains("rel=\"next\""));
    assertTrue(header.contains("rel=\"prev\""));
  }
  @Override
  public void postProcess(ServerResponse response) {
    if (response.getEntity() != null) {
      return; // let the MBW handle this
    }

    MultivaluedMap<String, Object> headers = response.getMetadata();
    List<DKIMSignature> list = getHeaders(headers);

    for (DKIMSignature dosetaSignature : list) {
      try {
        sign(null, headers, null, dosetaSignature);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
  }
 private Response doMethod(HttpMethod method, HttpEntity entity, String googleUrl) {
   String url = googleBaseUrl + StringUtils.trimLeadingCharacter(googleUrl, '/');
   LOGGER.info("Doing " + method.toString() + " on url " + url);
   RestTemplate restTemplate = new RestTemplate();
   ResponseEntity<String> response = null;
   try {
     response = restTemplate.exchange(url, method, entity, String.class);
     LOGGER.debug("Response: " + response.getStatusCode() + "; body = " + response.getBody());
     LOGGER.debug("Response headers: " + response.getHeaders().toSingleValueMap());
     return ServerResponse.ok()
         .status(response.getStatusCode().value())
         .entity(response.getBody())
         .build();
   } catch (HttpServerErrorException error5xx) {
     LOGGER.error(error5xx.getMessage(), error5xx);
     if (response != null) {
       return ServerResponse.serverError()
           .status(response.getStatusCode().value())
           .entity(response.getBody())
           .build();
     }
   }
   return null;
 }
  /** Postprocess all the REST requests.. Add an entry into the audit log for every rest service */
  public void postProcess(ServerResponse response) {
    logger.debug("AuditRestInterceptor:postProcess IN");

    try {
      String serviceUrl = InterceptorUtilities.getServiceUrl(request);
      HashMap<String, String> parameters =
          InterceptorUtilities.getRequestParameters(request, servletRequest);
      Object responseBody = response.getEntity();
      if (responseBody != null) {
        String responseBodyString = response.getEntity().toString();
        parameters.put("Response Body", responseBodyString);
      }

      UserProfile profile =
          (UserProfile) servletRequest.getSession().getAttribute(IEngUserProfile.ENG_USER_PROFILE);

      String actionCode =
          "[Service:"
              + serviceUrl
              + " ; Class:"
              + response.getResourceClass()
              + " ; Method:"
              + response.getResourceMethod()
              + "]";
      String result = "";
      if (response.getStatus() == 200) {
        result = "OK";
      } else {
        result = "ERR (" + response.getStatus() + ")";
      }
      AuditLogUtilities.updateAudit(servletRequest, profile, actionCode, parameters, result);
    } catch (Exception e) {
      logger.error("Error updating audit", e);
    } finally {
      logger.debug("AuditRestInterceptor:postProcess OUT");
    }
  }