001package com.box.sdk;
002
003import static com.box.sdk.http.ContentType.APPLICATION_JSON;
004
005import com.box.sdk.http.HttpMethod;
006import com.eclipsesource.json.Json;
007import com.eclipsesource.json.JsonObject;
008import com.eclipsesource.json.JsonValue;
009import java.net.URL;
010
011/**
012 * Used to make HTTP requests containing JSON to the Box API.
013 *
014 * <p>This request type extends BoxAPIRequest to provide additional functionality for handling JSON strings. It
015 * automatically sets the appropriate "Content-Type" HTTP headers and allows the JSON in the request to be logged.</p>
016 */
017public class BoxJSONRequest extends BoxAPIRequest {
018    private JsonValue jsonValue;
019
020    protected BoxJSONRequest(BoxAPIConnection api, URL url, String method, String mediaType) {
021        super(api, url, method, mediaType);
022    }
023
024    /**
025     * Constructs an authenticated BoxJSONRequest using a provided BoxAPIConnection.
026     *
027     * @param api    an API connection for authenticating the request.
028     * @param url    the URL of the request.
029     * @param method the HTTP method of the request.
030     */
031    public BoxJSONRequest(BoxAPIConnection api, URL url, String method) {
032        this(api, url, method, APPLICATION_JSON);
033    }
034
035    /**
036     * Constructs an authenticated BoxJSONRequest using a provided BoxAPIConnection.
037     *
038     * @param api    an API connection for authenticating the request.
039     * @param url    the URL of the request.
040     * @param method the HTTP method of the request.
041     */
042    public BoxJSONRequest(BoxAPIConnection api, URL url, HttpMethod method) {
043        this(api, url, method.name());
044    }
045
046    /**
047     * Constructs an authenticated BoxJSONRequest.
048     *
049     * @param url    the URL of the request.
050     * @param method the HTTP method of the request.
051     */
052    public BoxJSONRequest(URL url, HttpMethod method) {
053        this(null, url, method);
054    }
055
056    /**
057     * Sets the body of this request to a given JSON string.
058     *
059     * @param body the JSON string to use as the body.
060     */
061    @Override
062    public void setBody(String body) {
063        super.setBody(body);
064        this.jsonValue = Json.parse(body);
065    }
066
067    /**
068     * Sets the body of this request to a given JsonObject.
069     *
070     * @param body the JsonObject to use as the body.
071     */
072    public void setBody(JsonObject body) {
073        super.setBody(body.toString());
074        this.jsonValue = body;
075    }
076
077    /**
078     * Gets the body of this request as a JsonObject.
079     *
080     * @return body represented as JsonObject.
081     */
082    public JsonObject getBodyAsJsonObject() {
083        if (this.jsonValue.isObject()) {
084            return this.jsonValue.asObject();
085        }
086
087        return null;
088    }
089
090    /**
091     * Gets the body of this request as a {@link JsonValue}.
092     *
093     * @return body represented as JsonValue
094     */
095    public JsonValue getBodyAsJsonValue() {
096        return this.jsonValue;
097    }
098
099    @Override
100    public BoxJSONResponse send() {
101        return convert(super.send());
102    }
103
104    @Override
105    public BoxJSONResponse send(ProgressListener listener) {
106        return convert(super.send(listener));
107    }
108
109    private BoxJSONResponse convert(BoxAPIResponse response) {
110        if (response instanceof BoxJSONResponse) {
111            return (BoxJSONResponse) response;
112        } else {
113            return new BoxJSONResponse(response);
114        }
115    }
116
117    @Override
118    protected String bodyToString() {
119        return this.jsonValue != null ? this.jsonValue.toString() : null;
120    }
121}