Java Examples

Shared Example Classes

The examples on this page use some shared classes, mostly to simplify exception handling:

class ExampleException extends Exception {
  ExampleException(Throwable cause) {
    super(cause);
  }
}
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

class ExampleHelper {
  static String readEntityContent(final HttpEntity entity) throws ExampleException {
    try (final InputStream stream = entity.getContent()) {
      return readStreamContent(stream);
    } catch (IOException e) {
      throw new ExampleException(e);
    }
  }

  static HttpResponse executeRequest(final HttpUriRequest request) throws ExampleException {
    final HttpClient client = HttpClientBuilder.create().build();
    try {
      return client.execute(request);
    } catch (IOException e) {
      throw new ExampleException(e);
    }
  }

  static URI buildUri(final URIBuilder builder) throws ExampleException {
    try {
      return builder.build();
    } catch (URISyntaxException e) {
      throw new ExampleException(e);
    }
  }

  private static String readStreamContent(final InputStream stream) throws ExampleException {
    final StringBuilder builder = new StringBuilder();

    try (final InputStreamReader inner = new InputStreamReader(stream, "UTF-8")) {
      try (final BufferedReader reader = new BufferedReader(inner)) {
        while (true) {
          final String line = reader.readLine();
          if (line == null) {
            break;
          }
          builder.append(line);
        }
      }
    } catch (IOException e) {
      throw new ExampleException(e);
    }

    return builder.toString();
  }
}

Sample: Creating a Background Burn

Code

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;

import java.net.URI;

public class Create {
  public static void main(String[] args) throws ExampleException {
    final URIBuilder builder = new URIBuilder();
    builder.setScheme("https");
    builder.setHost("api.bonanza.com");
    builder.setPath("/api/background_burns");
    builder.setParameter("url", "https://bonanzapublic.s3.amazonaws.com/images/background_burner/colorful_bag.jpg");
    builder.setParameter("key", "gpVpnYoedr0U2NX");
    builder.setParameter("user_id", "1234");

    final URI uri = ExampleHelper.buildUri(builder);
    final HttpUriRequest request = new HttpPost(uri);
    final HttpResponse response = ExampleHelper.executeRequest(request);
    final HttpEntity entity = response.getEntity();
    final String content = ExampleHelper.readEntityContent(entity);

    System.out.println(content);
  }
}

Data

{
  "action": "create",
  "message": "Successfully queued",
  "user_id": "1234",
  "burn_user_id": 4413807,
  "access_token": "iPwHq4dTd0We",
  "id": 17008612,
  "url": "http://burner.bonanza.com/background_burns/17008612",
  "position_in_queue": 0,
  "complete": true,
  "selected_mask_id": 54974828,
  "final_result_url": "http://www.bonanza.com/background_masks/54974828.png?1479318331&composite=1&transparent=1&no_crop=0",
  "source_image_url": "https://images.bonanzastatic.com/uploads/burnees/1479318322-2212114-0002-4680.jpg",
  "masks": [
    {
      "id": 55024130,
      "url": "http://burner.bonanza.com/background_masks/55024130.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024130.png?1479318331&composite=true&no_crop=false&transparent=true",
      "variation": "flooded",
      "quality_score": 506
    },
    {
      "id": 55024131,
      "url": "http://burner.bonanza.com/background_masks/55024131.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024131.png?1479318331&composite=true&no_crop=false&transparent=true",
      "variation": "adjacent-probable",
      "quality_score": 495
    },
    {
      "id": 55024133,
      "url": "http://burner.bonanza.com/background_masks/55024133.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024133.png?1479318331&composite=true&no_crop=false&transparent=true",
      "variation": "raw-probable",
      "quality_score": 481
    },
    {
      "id": 55024135,
      "url": "http://burner.bonanza.com/background_masks/55024135.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024135.png?1479318331&composite=true&no_crop=false&transparent=true",
      "variation": "side_burn-probable",
      "quality_score": 435
    },
    {
      "id": 55024136,
      "url": "http://burner.bonanza.com/background_masks/55024136.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024136.png?1479318331&composite=true&no_crop=false&transparent=true",
      "variation": "bg-probable",
      "quality_score": 434
    }
  ],
  "review_complete": false
}

Sample: Showing a Background Burn

Code

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;

import java.net.URI;

public class Show {
  public static void main(String[] args) throws ExampleException {
    final int burn = 17008612;

    final URIBuilder builder = new URIBuilder();
    builder.setScheme("https");
    builder.setHost("api.bonanza.com");
    builder.setPath("/api/background_burns/" + String.valueOf(burn));
    builder.setParameter("key", "gpVpnYoedr0U2NX");
    builder.setParameter("user_id", "1234");

    final URI uri = ExampleHelper.buildUri(builder);
    final HttpUriRequest request = new HttpGet(uri);
    final HttpResponse response = ExampleHelper.executeRequest(request);
    final HttpEntity entity = response.getEntity();
    final String content = ExampleHelper.readEntityContent(entity);

    System.out.println(content);
  }
}

Data

{
  "action": "show",
  "message": "Found existing burn for user 1234 with id: 17008612",
  "user_id": "1234",
  "burn_user_id": 4413807,
  "access_token": "iPwHq4dTd0We",
  "id": 17008612,
  "url": "http://burner.bonanza.com/background_burns/17008612",
  "position_in_queue": 0,
  "complete": true,
  "selected_mask_id": 55024130,
  "final_result_url": "http://www.bonanza.com/background_masks/54974828.png?1479318741&composite=1&transparent=1&no_crop=0",
  "source_image_url": "https://images.bonanzastatic.com/uploads/burnees/1479318322-2212114-0002-4680.jpg",
  "masks": [
    {
      "id": 55024130,
      "url": "http://burner.bonanza.com/background_masks/55024130.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024130.png?1479318741&composite=true&no_crop=false&transparent=true",
      "variation": "flooded",
      "quality_score": 506
    },
    {
      "id": 55024131,
      "url": "http://burner.bonanza.com/background_masks/55024131.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024131.png?1479318741&composite=true&no_crop=false&transparent=true",
      "variation": "adjacent-probable",
      "quality_score": 495
    },
    {
      "id": 55024133,
      "url": "http://burner.bonanza.com/background_masks/55024133.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024133.png?1479318741&composite=true&no_crop=false&transparent=true",
      "variation": "raw-probable",
      "quality_score": 481
    },
    {
      "id": 55024135,
      "url": "http://burner.bonanza.com/background_masks/55024135.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024135.png?1479318741&composite=true&no_crop=false&transparent=true",
      "variation": "side_burn-probable",
      "quality_score": 435
    },
    {
      "id": 55024136,
      "url": "http://burner.bonanza.com/background_masks/55024136.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024136.png?1479318741&composite=true&no_crop=false&transparent=true",
      "variation": "bg-probable",
      "quality_score": 434
    }
  ],
  "review_complete": false
}

Sample: Updating a Background Burn

Code

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;

import java.net.URI;

public class Update {
  public static void main(String[] args) throws ExampleException {
    final int burn = 17008612;
    final int mask = 55024130;

    final URIBuilder builder = new URIBuilder();
    builder.setScheme("https");
    builder.setHost("api.bonanza.com");
    builder.setPath("/api/background_burns/" + String.valueOf(burn));
    builder.setParameter("key", "gpVpnYoedr0U2NX");
    builder.setParameter("user_id", "1234");
    builder.setParameter("selected_mask_id", String.valueOf(mask));

    final URI uri = ExampleHelper.buildUri(builder);
    final HttpUriRequest request = new HttpPut(uri);
    final HttpResponse response = ExampleHelper.executeRequest(request);
    final HttpEntity entity = response.getEntity();
    final String content = ExampleHelper.readEntityContent(entity);

    System.out.println(content);
  }
}

Data

{
  "action": "update",
  "message": "Saved final mask for burn with id: 17008612",
  "user_id": "1234",
  "burn_user_id": 4413807,
  "access_token": "iPwHq4dTd0We",
  "id": 17008612,
  "url": "http://burner.bonanza.com/background_burns/17008612",
  "position_in_queue": 0,
  "complete": true,
  "selected_mask_id": 55024130,
  "final_result_url": "http://www.bonanza.com/background_masks/54974828.png?1479318695&composite=1&transparent=1&no_crop=0",
  "source_image_url": "https://images.bonanzastatic.com/uploads/burnees/1479318322-2212114-0002-4680.jpg",
  "masks": [
    {
      "id": 55024130,
      "url": "http://burner.bonanza.com/background_masks/55024130.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024130.png?1479318695&composite=true&no_crop=false&transparent=true",
      "variation": "flooded",
      "quality_score": 506
    },
    {
      "id": 55024131,
      "url": "http://burner.bonanza.com/background_masks/55024131.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024131.png?1479318695&composite=true&no_crop=false&transparent=true",
      "variation": "adjacent-probable",
      "quality_score": 495
    },
    {
      "id": 55024133,
      "url": "http://burner.bonanza.com/background_masks/55024133.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024133.png?1479318695&composite=true&no_crop=false&transparent=true",
      "variation": "raw-probable",
      "quality_score": 481
    },
    {
      "id": 55024135,
      "url": "http://burner.bonanza.com/background_masks/55024135.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024135.png?1479318695&composite=true&no_crop=false&transparent=true",
      "variation": "side_burn-probable",
      "quality_score": 435
    },
    {
      "id": 55024136,
      "url": "http://burner.bonanza.com/background_masks/55024136.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024136.png?1479318695&composite=true&no_crop=false&transparent=true",
      "variation": "bg-probable",
      "quality_score": 434
    }
  ],
  "review_complete": false
}

Sample: Deleting a Background Burn

Code

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;

import java.net.URI;

public class Delete {
  public static void main(String[] args) throws ExampleException {
    final int burn = 17008612;

    final URIBuilder builder = new URIBuilder();
    builder.setScheme("https");
    builder.setHost("api.bonanza.com");
    builder.setPath("/api/background_burns/" + String.valueOf(burn));
    builder.setParameter("key", "gpVpnYoedr0U2NX");
    builder.setParameter("user_id", "1234");

    final URI uri = ExampleHelper.buildUri(builder);
    final HttpUriRequest request = new HttpDelete(uri);
    final HttpResponse response = ExampleHelper.executeRequest(request);
    final HttpEntity entity = response.getEntity();
    final String content = ExampleHelper.readEntityContent(entity);

    System.out.println(content);
  }
}

Data

{
  "action": "destroy",
  "message": "Archived burn with id: 17008612",
  "user_id": "1234",
  "burn_user_id": 4413807,
  "access_token": "iPwHq4dTd0We",
  "id": 17008612,
  "url": "http://burner.bonanza.com/background_burns/17008612",
  "position_in_queue": 0,
  "complete": true,
  "selected_mask_id": 55024130,
  "final_result_url": "http://www.bonanza.com/background_masks/54974828.png?1479318795&composite=1&transparent=1&no_crop=0",
  "source_image_url": "https://images.bonanzastatic.com/uploads/burnees/1479318322-2212114-0002-4680.jpg",
  "masks": [
    {
      "id": 55024130,
      "url": "http://burner.bonanza.com/background_masks/55024130.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024130.png?1479318795&composite=true&no_crop=false&transparent=true",
      "variation": "flooded",
      "quality_score": 506
    },
    {
      "id": 55024131,
      "url": "http://burner.bonanza.com/background_masks/55024131.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024131.png?1479318795&composite=true&no_crop=false&transparent=true",
      "variation": "adjacent-probable",
      "quality_score": 495
    },
    {
      "id": 55024133,
      "url": "http://burner.bonanza.com/background_masks/55024133.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024133.png?1479318795&composite=true&no_crop=false&transparent=true",
      "variation": "raw-probable",
      "quality_score": 481
    },
    {
      "id": 55024135,
      "url": "http://burner.bonanza.com/background_masks/55024135.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024135.png?1479318795&composite=true&no_crop=false&transparent=true",
      "variation": "side_burn-probable",
      "quality_score": 435
    },
    {
      "id": 55024136,
      "url": "http://burner.bonanza.com/background_masks/55024136.png?",
      "composite_url": "http://burner.bonanza.com/background_masks/55024136.png?1479318795&composite=true&no_crop=false&transparent=true",
      "variation": "bg-probable",
      "quality_score": 434
    }
  ],
  "review_complete": false
}

Sample: Listing Background Burns

Code

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;

import java.net.URI;

public class Index {
  public static void main(String[] args) throws ExampleException {
    final URIBuilder builder = new URIBuilder();
    builder.setScheme("https");
    builder.setHost("api.bonanza.com");
    builder.setPath("/api/background_burns");
    builder.setParameter("key", "gpVpnYoedr0U2NX");
    builder.setParameter("user_id", "1234");

    final URI uri = ExampleHelper.buildUri(builder);
    final HttpUriRequest request = new HttpGet(uri);
    final HttpResponse response = ExampleHelper.executeRequest(request);
    final HttpEntity entity = response.getEntity();
    final String content = ExampleHelper.readEntityContent(entity);

    System.out.println(content);
  }
}

Data

{
  "action": "index",
  "message": "Success",
  "user_id": "1234",
  "burn_user_id": 4413807,
  "access_token": "iPwHq4dTd0We",
  "count": 3,
  "background_burns": [
    {
      "id": 17008612,
      "url": "http://burner.bonanza.com/background_burns/17008612",
      "position_in_queue": 0,
      "complete": true,
      "selected_mask_id": 54974828,
      "final_result_url": "http://www.bonanza.com/background_masks/54974828.png?1479318438&composite=1&transparent=1&no_crop=0",
      "source_image_url": "https://images.bonanzastatic.com/uploads/burnees/1479318322-2212114-0002-4680.jpg",
      "masks": [
        {
          "id": 55024130,
          "url": "http://burner.bonanza.com/background_masks/55024130.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/55024130.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "flooded",
          "quality_score": 506
        },
        {
          "id": 55024131,
          "url": "http://burner.bonanza.com/background_masks/55024131.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/55024131.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "adjacent-probable",
          "quality_score": 495
        },
        {
          "id": 55024133,
          "url": "http://burner.bonanza.com/background_masks/55024133.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/55024133.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "raw-probable",
          "quality_score": 481
        },
        {
          "id": 55024135,
          "url": "http://burner.bonanza.com/background_masks/55024135.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/55024135.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "side_burn-probable",
          "quality_score": 435
        },
        {
          "id": 55024136,
          "url": "http://burner.bonanza.com/background_masks/55024136.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/55024136.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "bg-probable",
          "quality_score": 434
        }
      ],
      "review_complete": false
    },
    {
      "id": 16996045,
      "url": "http://burner.bonanza.com/background_burns/16996045",
      "position_in_queue": 0,
      "complete": true,
      "selected_mask_id": 54974828,
      "final_result_url": "https://images.bonanzastatic.com/uploads/burns/16996045.png?1479259344",
      "source_image_url": "https://images.bonanzastatic.com/uploads/burnees/1479258971-259360-0001-7566.jpg",
      "masks": [
        {
          "id": 54974828,
          "url": "http://burner.bonanza.com/background_masks/54974828.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974828.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "flooded",
          "quality_score": 506
        },
        {
          "id": 54974829,
          "url": "http://burner.bonanza.com/background_masks/54974829.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974829.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "adjacent-probable",
          "quality_score": 495
        },
        {
          "id": 54974830,
          "url": "http://burner.bonanza.com/background_masks/54974830.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974830.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "raw-probable",
          "quality_score": 481
        },
        {
          "id": 54974831,
          "url": "http://burner.bonanza.com/background_masks/54974831.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974831.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "side_burn-probable",
          "quality_score": 435
        },
        {
          "id": 54974833,
          "url": "http://burner.bonanza.com/background_masks/54974833.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974833.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "bg-probable",
          "quality_score": 434
        }
      ],
      "review_complete": false
    },
    {
      "id": 16995904,
      "url": "http://burner.bonanza.com/background_burns/16995904",
      "position_in_queue": 0,
      "complete": true,
      "selected_mask_id": null,
      "final_result_url": null,
      "source_image_url": "https://images.bonanzastatic.com/uploads/burnees/1479258335-291027-0001-4313.jpg",
      "masks": [
        {
          "id": 54974270,
          "url": "http://burner.bonanza.com/background_masks/54974270.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974270.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "flooded",
          "quality_score": 506
        },
        {
          "id": 54974271,
          "url": "http://burner.bonanza.com/background_masks/54974271.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974271.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "adjacent-probable",
          "quality_score": 495
        },
        {
          "id": 54974273,
          "url": "http://burner.bonanza.com/background_masks/54974273.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974273.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "raw-probable",
          "quality_score": 481
        },
        {
          "id": 54974276,
          "url": "http://burner.bonanza.com/background_masks/54974276.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974276.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "side_burn-probable",
          "quality_score": 435
        },
        {
          "id": 54974277,
          "url": "http://burner.bonanza.com/background_masks/54974277.png?",
          "composite_url": "http://burner.bonanza.com/background_masks/54974277.png?1479318438&composite=true&no_crop=false&transparent=true",
          "variation": "bg-probable",
          "quality_score": 434
        }
      ],
      "review_complete": false
    }
  ]
}