public void testAuthorizeIngressICMPToSecurityGroups()
      throws SecurityException, NoSuchMethodException, IOException {
    Invokable<?, ?> method =
        Invokable.from(
            SecurityGroupAsyncClient.class.getMethod(
                "authorizeIngressICMPToSecurityGroups",
                String.class,
                int.class,
                int.class,
                Multimap.class,
                AccountInDomainOptions[].class));
    GeneratedHttpRequest httpRequest =
        processor.createRequest(
            method,
            ImmutableList.<Object>of(
                2,
                22,
                22,
                ImmutableMultimap.of("adrian", "group1", "adrian", "group2", "bob", "group1")));

    assertRequestLineEquals(httpRequest, authorizeSecurityGroupIngress2.getRequestLine());
    assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
    assertPayloadEquals(httpRequest, null, null, false);

    assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
    assertSaxResponseParserClassEquals(method, null);
    assertFallbackClassEquals(method, MapHttp4xxCodesToExceptions.class);

    checkFilters(httpRequest);
  }
    public HttpResponse call() throws Exception {

      HttpResponse response = null;
      for (; ; ) {
        HttpRequest request = command.getCurrentRequest();
        Q nativeRequest = null;
        try {
          for (HttpRequestFilter filter : request.getFilters()) {
            request = filter.filter(request);
          }
          checkRequestHasContentLengthOrChunkedEncoding(
              request,
              "After filtering, the request has neither chunked encoding nor content length: "
                  + request);
          logger.debug("Sending request %s: %s", request.hashCode(), request.getRequestLine());
          wirePayloadIfEnabled(wire, request);
          utils.logRequest(headerLog, request, ">>");
          nativeRequest = convert(request);
          response = invoke(nativeRequest);

          logger.debug("Receiving response %s: %s", request.hashCode(), response.getStatusLine());
          utils.logResponse(headerLog, response, "<<");
          if (response.getPayload() != null && wire.enabled()) wire.input(response);
          int statusCode = response.getStatusCode();
          if (statusCode >= 300) {
            if (shouldContinue(response)) continue;
            else break;
          } else {
            break;
          }
        } catch (Exception e) {
          IOException ioe = Throwables2.getFirstThrowableOfType(e, IOException.class);
          if (ioe != null) {
            if (ioe instanceof SSLException) {
              command.setException(
                  new AuthorizationException(
                      e.getMessage()
                          + " connecting to "
                          + command.getCurrentRequest().getRequestLine(),
                      e));
              break;
            } else if (ioRetryHandler.shouldRetryRequest(command, ioe)) {
              continue;
            }
          }
          command.setException(
              new HttpResponseException(
                  e.getMessage() + " connecting to " + command.getCurrentRequest().getRequestLine(),
                  command,
                  null,
                  e));
          break;
        } finally {
          cleanup(nativeRequest);
        }
      }
      if (command.getException() != null) throw command.getException();
      return response;
    }
Exemplo n.º 3
0
 public static void logRequest(Logger logger, HttpRequest request, String prefix) {
   if (logger.isDebugEnabled()) {
     logger.debug("%s %s", prefix, request.getRequestLine().toString());
     for (Entry<String, String> header : request.getHeaders().entries()) {
       if (header.getKey() != null)
         logger.debug("%s %s: %s", prefix, header.getKey(), header.getValue());
     }
   }
 }
  public void handleError(HttpCommand command, HttpResponse response) {
    HttpRequest request = command.getCurrentRequest();
    Exception exception = new HttpResponseException(command, response);
    try {
      VCloudError error = null;
      String message = null;
      if (response.getPayload() != null) {
        try {
          error = utils.parseErrorFromContent(request, response);
          if (error != null) {
            message = error.getMessage();
            exception = new VCloudResponseException(command, response, error);
          } else {
            message = Strings2.toStringAndClose(response.getPayload().getInput());
            exception =
                message != null ? new HttpResponseException(command, response, message) : exception;
          }
        } catch (IOException e) {
        } finally {
          response.getPayload().release();
        }
      }
      message =
          message != null
              ? message
              : String.format("%s -> %s", request.getRequestLine(), response.getStatusLine());

      switch (response.getStatusCode()) {
        case 400:
          if (error != null
              && ((error.getMinorErrorCode() != null
                      && error.getMinorErrorCode() == MinorCode.BUSY_ENTITY)
                  || (error.getMessage() != null
                      && error.getMessage().indexOf("is not running") != -1)))
            exception = new IllegalStateException(message, exception);
          else exception = new IllegalArgumentException(message, exception);
          break;
        case 401:
        case 403:
          if (error != null
              && ((error.getMinorErrorCode() != null
                      && error.getMinorErrorCode() == MinorCode.ACCESS_TO_RESOURCE_IS_FORBIDDEN)
                  || (error.getMessage() != null
                      && error.getMessage().indexOf("No access to entity") != -1)))
            exception = new ResourceNotFoundException(message, exception);
          else exception = new AuthorizationException(exception.getMessage(), exception);
          break;
        case 404:
          if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
            String path = command.getCurrentRequest().getEndpoint().getPath();
            Matcher matcher = RESOURCE_PATTERN.matcher(path);
            if (matcher.find()) {
              message = String.format("%s %s not found", matcher.group(1), matcher.group(2));
            } else {
              message = path;
            }
            exception = new ResourceNotFoundException(message);
          }
          break;
      }
    } finally {
      releasePayload(response);
      command.setException(exception);
    }
  }
Exemplo n.º 5
0
 public void logRequest(Logger logger, HttpRequest request, String prefix) {
   if (logger.isDebugEnabled()) {
     logger.debug("%s %s", prefix, request.getRequestLine().toString());
     logMessage(logger, request, prefix);
   }
 }