001package com.box.sdk;
002
003import java.net.URL;
004
005import com.box.sdk.http.HttpMethod;
006
007/**
008 * Used to make HTTP requests containing JSON to the Box API.
009 *
010 * <p>This request type extends BoxAPIRequest to provide additional functionality for handling JSON strings. It
011 * automatically sets the appropriate "Content-Type" HTTP headers and allows the JSON in the request to be logged.</p>
012 */
013public class BoxJSONRequest extends BoxAPIRequest {
014    private String json;
015
016    /**
017     * Constructs an authenticated BoxJSONRequest using a provided BoxAPIConnection.
018     * @param  api    an API connection for authenticating the request.
019     * @param  url    the URL of the request.
020     * @param  method the HTTP method of the request.
021     */
022    public BoxJSONRequest(BoxAPIConnection api, URL url, String method) {
023        super(api, url, method);
024
025        this.addHeader("Content-Type", "application/json");
026    }
027
028    /**
029     * Constructs an authenticated BoxJSONRequest using a provided BoxAPIConnection.
030     * @param  api    an API connection for authenticating the request.
031     * @param  url    the URL of the request.
032     * @param  method the HTTP method of the request.
033     */
034    public BoxJSONRequest(BoxAPIConnection api, URL url, HttpMethod method) {
035        super(api, url, method);
036
037        this.addHeader("Content-Type", "application/json");
038    }
039
040    /**
041     * Sets the body of this request to a given JSON string.
042     * @param body the JSON string to use as the body.
043     */
044    @Override
045    public void setBody(String body) {
046        super.setBody(body);
047        this.json = body;
048    }
049
050    @Override
051    protected String bodyToString() {
052        return this.json;
053    }
054}