@Override
 public void handleError(ClientHttpResponse response) throws IOException {
   HttpStatus statusCode = response.getStatusCode();
   switch (statusCode.series()) {
     case CLIENT_ERROR:
       CloudFoundryException exception =
           new CloudFoundryException(statusCode, response.getStatusText());
       ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
       if (response.getBody() != null) {
         try {
           @SuppressWarnings("unchecked")
           Map<String, Object> map = mapper.readValue(response.getBody(), Map.class);
           exception.setDescription(CloudUtil.parse(String.class, map.get("description")));
         } catch (JsonParseException e) {
           exception.setDescription("Client error");
         } catch (IOException e) {
           exception.setDescription("Client error");
         }
       } else {
         exception.setDescription("Client error");
       }
       throw exception;
     case SERVER_ERROR:
       throw new HttpServerErrorException(statusCode, response.getStatusText());
     default:
       throw new RestClientException("Unknown status code [" + statusCode + "]");
   }
 }
Exemplo n.º 2
0
  public OAuth2AccessToken getToken(String username, String password, String clientId) {
    OAuth2ProtectedResourceDetails resource = getResourceDetails(username, password, clientId);
    AccessTokenRequest request = createAccessTokenRequest(username, password);

    ResourceOwnerPasswordAccessTokenProvider provider =
        createResourceOwnerPasswordAccessTokenProvider();
    try {
      return provider.obtainAccessToken(resource, request);
    } catch (OAuth2AccessDeniedException oauthEx) {
      HttpStatus status = HttpStatus.valueOf(oauthEx.getHttpErrorCode());
      CloudFoundryException cfEx = new CloudFoundryException(status, oauthEx.getMessage());
      cfEx.setDescription(oauthEx.getSummary());
      throw cfEx;
    }
  }
Exemplo n.º 3
0
  public Hashtable<String, String> launchStudio() {
    Hashtable<String, String> result = new Hashtable<String, String>();
    if (spinupController.isNewDeployment(loginCredentials)) {
      if (dailyLimit()) {
        result.put(
            "ERROR",
            "Sorry, we have reached the preview limit for today.<BR>Please try back again tomorrow.");
        log(ERROR, "DAILY LIMIT REACHED: " + dailyCounter + " limit is: " + dailyLimit);
        return result;
      }
    }

    try {
      log(INFO, "Performing spinup for: " + loginCredentials.getUsername());
      result =
          spinupController.performSpinup(
              loginCredentials,
              secret,
              transportToken,
              RuntimeAccess.getInstance().getResponse(),
              false);
      recordUserLog(loginCredentials.getUsername());
      log(INFO, "Counter now: " + ++counter);
    } catch (CloudFoundryException cfe) {
      log(ERROR, "Failed to Launch Studio " + cfe.getMessage() + " " + cfe.toString());
      String msg = cfe.toString();
      if (msg.contains("Not enough memory capacity")) {
        String allowed = msg.substring(msg.indexOf("(") + 1, msg.indexOf(")"));
        result.put(
            "ERROR",
            "Insufficent memory to deploy studio to your account.<BR> "
                + allowed
                + "<BR>512M is required to start Studio.");
      } else {
        result.put(
            "ERROR", "Unable to deploy studio to your account.<BR> " + "Cause: " + cfe.toString());
      }
    } catch (Exception e) {
      log(ERROR, "Studio Launch has failed", e);
      result.put("ERROR", "Unable to deploy studio. " + e.getMessage());
      log(ERROR, e.getMessage());
    }
    return result;
  }
  private String doGetFileByRange(
      String urlPath,
      Object app,
      String instance,
      String filePath,
      int start,
      int end,
      String range) {

    boolean supportsRanges = false;
    try {
      supportsRanges =
          getRestTemplate()
              .execute(
                  getUrl(urlPath),
                  HttpMethod.HEAD,
                  new RequestCallback() {
                    public void doWithRequest(ClientHttpRequest request) throws IOException {
                      request.getHeaders().set("Range", "bytes=0-");
                    }
                  },
                  new ResponseExtractor<Boolean>() {
                    public Boolean extractData(ClientHttpResponse response) throws IOException {
                      if (response.getStatusCode().equals(HttpStatus.PARTIAL_CONTENT)) {
                        return true;
                      }
                      return false;
                    }
                  },
                  app,
                  instance,
                  filePath);
    } catch (CloudFoundryException e) {
      if (e.getStatusCode().equals(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE)) {
        // must be a 0 byte file
        return "";
      } else {
        throw e;
      }
    }
    HttpHeaders headers = new HttpHeaders();
    if (supportsRanges) {
      headers.set("Range", range);
    }
    HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
    ResponseEntity<String> responseEntity =
        getRestTemplate()
            .exchange(
                getUrl(urlPath),
                HttpMethod.GET,
                requestEntity,
                String.class,
                app,
                instance,
                filePath);
    String response = responseEntity.getBody();
    boolean partialFile = false;
    if (responseEntity.getStatusCode().equals(HttpStatus.PARTIAL_CONTENT)) {
      partialFile = true;
    }
    if (!partialFile && response != null) {
      if (start == -1) {
        return response.substring(response.length() - end);
      } else {
        if (start >= response.length()) {
          if (response.length() == 0) {
            return "";
          }
          throw new CloudFoundryException(
              HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE,
              "The starting position " + start + " is past the end of the file content.");
        }
        if (end != -1) {
          if (end >= response.length()) {
            end = response.length() - 1;
          }
          return response.substring(start, end + 1);
        } else {
          return response.substring(start);
        }
      }
    }
    return response;
  }