@Override
  public void handleError(final HttpCommand command, final HttpResponse response) {
    // it is important to always read fully and close streams
    String message = parseMessage(response);
    Exception exception =
        message == null
            ? new HttpResponseException(command, response)
            : new HttpResponseException(command, response, message);
    try {
      message =
          message == null
              ? String.format(
                  "%s -> %s",
                  command.getCurrentRequest().getRequestLine(), response.getStatusLine())
              : message;
      switch (response.getStatusCode()) {
        case 400:
          if (message.contains("unauthorized_client")) {
            exception = new AuthorizationException(message, exception);
          } else {
            exception = new IllegalArgumentException(message, exception);
          }
          break;
        case 401:
        case 403:
          exception = new AuthorizationException(message, exception);
          break;

        case 404:
          if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
            exception = new ResourceNotFoundException(message, exception);
          }
          break;
        case 409:
          exception = new IllegalStateException(message, exception);
          break;
        case 429:
          exception = new AzureComputeRateLimitExceededException(response, exception);
          break;
        default:
      }
    } finally {
      Closeables2.closeQuietly(response.getPayload());
      command.setException(exception);
    }
  }
Example #2
0
  @Test
  public void testUrlHandler() throws Exception {
    if (isBlobStoreLiveConfigured()) {
      createManagedBlobStoreService("aws-s3");
      BlobStore blobStoreService = getOsgiService(BlobStore.class);
      Thread.sleep(DEFAULT_TIMEOUT);

      FeaturesService featuresService = getOsgiService(FeaturesService.class);
      featuresService.installFeature("jclouds-url-handler");

      // Let's add a bundle to S3
      String groupId = "org.jclouds.api";
      String artifactId = "byon";
      String version = System.getProperty("jclouds.version");

      System.err.println(executeCommand("jclouds:blobstore-list"));
      System.err.println(executeCommand("jclouds:blobstore-container-create itest-container"));

      URL artifactUrl = new URL("mvn:" + groupId + "/" + artifactId + "/" + version);
      URL blobUrl =
          new URL(
              "blob:aws-s3/itest-container/maven2/org/jclouds/api/byon/"
                  + version
                  + "/"
                  + artifactId
                  + "-"
                  + version
                  + ".jar");
      InputStream is = artifactUrl.openConnection().getInputStream();
      OutputStream os = blobUrl.openConnection().getOutputStream();
      try {
        ByteStreams.copy(is, os);
        os.flush();
      } finally {
        Closeables2.closeQuietly(is);
        Closeables2.closeQuietly(os);
      }
      // Make sure that only S3 is available as a repo.
      System.err.println(
          executeCommands(
              "config:edit org.ops4j.pax.url.mvn",
              "config:propset org.ops4j.pax.url.mvn.localRepository "
                  + System.getProperty("karaf.base")
                  + File.separatorChar
                  + "none",
              "config:propset org.ops4j.pax.url.mvn.repositories blob:aws-s3/itest-container/maven2@snapshots ",
              "config:update"));
      Thread.sleep(DEFAULT_TIMEOUT);

      final Set<String> installedSymbolicNames = new LinkedHashSet<String>();

      // Add a Bundle Listener
      bundleContext.addBundleListener(
          new BundleListener() {
            @Override
            public void bundleChanged(BundleEvent event) {
              if (event.getType() == BundleEvent.INSTALLED) {
                installedSymbolicNames.add(event.getBundle().getSymbolicName());
              }
            }
          });

      // Install the bundle the from S3.
      System.err.println(executeCommand("osgi:install mvn:org.jclouds.api/byon/" + version));

      // Verify that no other bundle can be installed.
      System.err.println(executeCommand("osgi:install mvn:org.jclouds.api/nova/" + version));
      assertTrue(installedSymbolicNames.contains("byon"));
      assertFalse(installedSymbolicNames.contains("nova"));
    }
  }