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