Beispiel #1
0
  public static DocumentListEntry executePost(
      HttpRequestFactory requestFactory, DocsUrl url, AtomContent content) throws IOException {

    HttpRequest request = requestFactory.buildPostRequest(url, content);
    HttpResponse response = requestExecutor.executeSensitive(request);
    return response.parseAs(DocumentListEntry.class);
  }
Beispiel #2
0
  public static Entry executeGet(
      HttpRequestFactory requestFactory, DocsUrl url, Class<Entry> feedClass) throws IOException {

    HttpRequest request = requestFactory.buildGetRequest(url);
    HttpResponse response = requestExecutor.executeNonSensitive(request);
    return response.parseAs(feedClass);
  }
  private Entry uploadLastChunk(
      byte[] bytes,
      int position,
      int length,
      String fileType,
      HttpRequestFactory requestFactory,
      long size,
      String location) {

    try {

      HttpResponse response =
          putRequest(bytes, position, length, fileType, requestFactory, size, location);
      if (!response.isSuccessStatusCode()) {
        String message =
            "upload finished, but there was an error during upload, "
                + response.getStatusCode()
                + ": "
                + response.getStatusMessage();
        logger.error(message);
        throw new CoreException(message);
      }

      return response.parseAs(Entry.class);
    } catch (IOException e) {
      String message = "unable to parse response " + e.getMessage();
      logger.error(message);
      throw new CoreException(message);
    }
  }
  @Override
  public GameState call() throws Exception {
    HttpContent content;
    HttpRequest request;
    HttpResponse response;
    GameState gameState = null;
    AdvancedGameState advancedGameState;

    try {
      // Initial request
      logger.info("Sending initial request...");
      content = new UrlEncodedContent(apiKey);
      request = REQUEST_FACTORY.buildPostRequest(gameUrl, content);
      request.setReadTimeout(0); // Wait forever to be assigned to a game
      response = request.execute();
      gameState = response.parseAs(GameState.class);
      logger.info("Game URL: {}", gameState.getViewUrl());

      advancedGameState = new AdvancedGameState(gameState);

      // Game loop
      while (!gameState.getGame().isFinished() && !gameState.getHero().isCrashed()) {
        logger.info("Taking turn " + gameState.getGame().getTurn());
        BotMove direction = bot.move(advancedGameState);
        Move move = new Move(apiKey.getKey(), direction.toString());

        HttpContent turn = new UrlEncodedContent(move);
        HttpRequest turnRequest =
            REQUEST_FACTORY.buildPostRequest(new GenericUrl(gameState.getPlayUrl()), turn);
        HttpResponse turnResponse = turnRequest.execute();

        gameState = turnResponse.parseAs(GameState.class);
        advancedGameState = new AdvancedGameState(advancedGameState, gameState);
      }

    } catch (Exception e) {
      logger.error("Error during game play", e);
    }

    logger.info("Game over");
    return gameState;
  }
Beispiel #5
0
 public String shortUrl(String long_url) {
   GenericData data = new GenericData();
   data.put("longUrl", long_url);
   JsonHttpContent content = new JsonHttpContent();
   content.data = data;
   request.content = content;
   HttpResponse response;
   try {
     response = request.execute();
     Result result = response.parseAs(Result.class);
     return result.shortUrl;
   } catch (IOException e) {
     return null;
   }
 }
Beispiel #6
0
  public MobileDeviceMatch findMobileDevice(String serialNumber) {
    // TODO: urlencode string
    String matchUrlString = jssServerUrl + MOBILE_DEVICES_API_PATH + "/match/" + serialNumber;
    GenericUrl matchUrl = new GenericUrl(matchUrlString);
    int status = 500;
    try {
      // JSS REST API - use GET method to query existing objects
      // (read-only)
      HttpRequest matchRequest = factory.buildGetRequest(matchUrl);
      HttpResponse matchResponse = matchRequest.execute();
      status = matchResponse.getStatusCode();
      if (status < 400) {
        MobileDeviceList mobileDevices = matchResponse.parseAs(MobileDeviceList.class);
        if (mobileDevices.size == 1) {
          MobileDeviceMatch device = mobileDevices.first();
          if (debug) {
            System.out.println(
                "match "
                    + serialNumber
                    + ", status "
                    + String.valueOf(status)
                    + ", id "
                    + device.id);
          }
          if (device.id != null && !device.id.isEmpty()) {
            return device;
          }
        } else if (debug) {
          System.out.println(
              "match " + serialNumber + ", got " + String.valueOf(mobileDevices.size) + " devices");
        }
      }
    } catch (IndexOutOfBoundsException ex0) {
      System.out.println("Didn't parse any devices. ");
    } catch (Exception ex1) {
      System.out.println("Exception! " + ex1.getMessage());
    }

    if (debug) {
      // Not found or internal error
      System.out.println("match " + serialNumber + ", status " + String.valueOf(status));
    }
    return null;
  }
Beispiel #7
0
  public MobileDevice getMobileDevice(String id) {
    String getUrlString = jssServerUrl + MOBILE_DEVICES_API_PATH + "/id/" + id;
    GenericUrl getUrl = new GenericUrl(getUrlString);
    int status = 500;
    try {
      // JSS REST API - use GET method to query existing objects
      // (read-only)
      HttpRequest getRequest = factory.buildGetRequest(getUrl);
      HttpResponse getResponse = getRequest.execute();
      status = getResponse.getStatusCode();
      if (status < 400) {
        MobileDevice device = getResponse.parseAs(MobileDevice.class);
        if (device != null) {
          return device;
        }
      }
    } catch (Exception ex1) {
      System.out.println("Exception! " + ex1.getMessage());
    }

    return null;
  }
Beispiel #8
0
 public static Feed first100Entries(HttpRequestFactory requestFactory, DocsUrl url)
     throws IOException {
   HttpRequest request = requestFactory.buildGetRequest(url);
   HttpResponse response = requestExecutor.executeNonSensitive(request);
   return response.parseAs(Feed.class);
 }