001package com.box.sdk;
002
003import java.io.ByteArrayInputStream;
004import java.io.IOException;
005import java.io.InputStream;
006import java.io.OutputStream;
007import java.net.HttpURLConnection;
008import java.net.MalformedURLException;
009import java.net.ProtocolException;
010import java.net.URL;
011import java.security.KeyManagementException;
012import java.security.NoSuchAlgorithmException;
013import java.util.ArrayList;
014import java.util.List;
015import java.util.Map;
016import java.util.logging.Level;
017import java.util.logging.Logger;
018import javax.net.ssl.HttpsURLConnection;
019import javax.net.ssl.SSLContext;
020import javax.net.ssl.SSLParameters;
021
022import com.box.sdk.http.HttpHeaders;
023import com.box.sdk.http.HttpMethod;
024
025
026/**
027 * Used to make HTTP requests to the Box API.
028 *
029 * <p>All requests to the REST API are sent using this class or one of its subclasses. This class wraps {@link
030 * HttpURLConnection} in order to provide a simpler interface that can automatically handle various conditions specific
031 * to Box's API. Requests will be authenticated using a {@link BoxAPIConnection} (if one is provided), so it isn't
032 * necessary to add authorization headers. Requests can also be sent more than once, unlike with HttpURLConnection. If
033 * an error occurs while sending a request, it will be automatically retried (with a back off delay) up to the maximum
034 * number of times set in the BoxAPIConnection.</p>
035 *
036 * <p>Specifying a body for a BoxAPIRequest is done differently than it is with HttpURLConnection. Instead of writing to
037 * an OutputStream, the request is provided an {@link InputStream} which will be read when the {@link #send} method is
038 * called. This makes it easy to retry requests since the stream can automatically reset and reread with each attempt.
039 * If the stream cannot be reset, then a new stream will need to be provided before each call to send. There is also a
040 * convenience method for specifying the body as a String, which simply wraps the String with an InputStream.</p>
041 */
042public class BoxAPIRequest {
043    private static final Logger LOGGER = Logger.getLogger(BoxAPIRequest.class.getName());
044    private static final int BUFFER_SIZE = 8192;
045    private static final int MAX_REDIRECTS = 3;
046
047    private final BoxAPIConnection api;
048    private final List<RequestHeader> headers;
049    private final String method;
050
051    private URL url;
052    private BackoffCounter backoffCounter;
053    private int connectTimeout;
054    private int readTimeout;
055    private InputStream body;
056    private long bodyLength;
057    private Map<String, List<String>> requestProperties;
058    private int numRedirects;
059    private boolean followRedirects = true;
060    private boolean shouldAuthenticate;
061    private SSLContext sslContext;
062
063    /**
064     * Constructs an unauthenticated BoxAPIRequest.
065     * @param  url    the URL of the request.
066     * @param  method the HTTP method of the request.
067     */
068    public BoxAPIRequest(URL url, String method) {
069        this(null, url, method);
070    }
071
072    /**
073     * Constructs an authenticated BoxAPIRequest using a provided BoxAPIConnection.
074     * @param  api    an API connection for authenticating the request.
075     * @param  url    the URL of the request.
076     * @param  method the HTTP method of the request.
077     */
078    public BoxAPIRequest(BoxAPIConnection api, URL url, String method) {
079        this.api = api;
080        this.url = url;
081        this.method = method;
082        this.headers = new ArrayList<RequestHeader>();
083        if (api != null) {
084            Map<String, String> customHeaders = api.getHeaders();
085            if (customHeaders != null) {
086                for (String header : customHeaders.keySet()) {
087                    this.addHeader(header, customHeaders.get(header));
088                }
089            }
090            this.headers.add(new RequestHeader("X-Box-UA", api.getBoxUAHeader()));
091        }
092        this.backoffCounter = new BackoffCounter(new Time());
093        this.shouldAuthenticate = true;
094        this.connectTimeout = BoxGlobalSettings.getConnectTimeout();
095        this.readTimeout = BoxGlobalSettings.getReadTimeout();
096
097        this.addHeader("Accept-Encoding", "gzip");
098        this.addHeader("Accept-Charset", "utf-8");
099
100        // Setup the SSL context manually to force newer TLS version on legacy Java environments
101        // This is necessary because Java 7 uses TLSv1.0 by default, but the Box API will need
102        // to deprecate this protocol in the future.  To prevent clients from breaking, we must
103        // ensure that they are using TLSv1.1 or greater!
104        SSLContext sc = null;
105        try {
106            sc = SSLContext.getDefault();
107            SSLParameters params = sc.getDefaultSSLParameters();
108            boolean supportsNewTLS = false;
109            for (String protocol : params.getProtocols()) {
110                if (protocol.compareTo("TLSv1") > 0) {
111                    supportsNewTLS = true;
112                    break;
113                }
114            }
115            if (!supportsNewTLS) {
116                // Try to upgrade to a higher TLS version
117                sc = null;
118                sc = SSLContext.getInstance("TLSv1.1");
119                sc.init(null, null, new java.security.SecureRandom());
120                sc = SSLContext.getInstance("TLSv1.2");
121                sc.init(null, null, new java.security.SecureRandom());
122            }
123        } catch (NoSuchAlgorithmException ex) {
124            if (sc == null) {
125                LOGGER.warning("Unable to set up SSL context for HTTPS!  This may result in the inability "
126                        + " to connect to the Box API.");
127            }
128            if (sc != null && sc.getProtocol().equals("TLSv1")) {
129                // Could not find a good version of TLS
130                LOGGER.warning("Using deprecated TLSv1 protocol, which will be deprecated by the Box API!  Upgrade "
131                        + "to a newer version of Java as soon as possible.");
132            }
133        } catch (KeyManagementException ex) {
134
135            LOGGER.warning("Exception when initializing SSL Context!  This may result in the inabilty to connect to "
136                    + "the Box API");
137            sc = null;
138        }
139        this.sslContext = sc;
140    }
141
142    /**
143     * Constructs an authenticated BoxAPIRequest using a provided BoxAPIConnection.
144     * @param  api    an API connection for authenticating the request.
145     * @param  url the URL of the request.
146     * @param  method the HTTP method of the request.
147     */
148    public BoxAPIRequest(BoxAPIConnection api, URL url, HttpMethod method) {
149        this(api, url, method.name());
150    }
151
152    /**
153     * Constructs an request, using URL and HttpMethod.
154     * @param  url the URL of the request.
155     * @param  method the HTTP method of the request.
156     */
157    public BoxAPIRequest(URL url, HttpMethod method) {
158        this(url, method.name());
159    }
160
161    /**
162     * Adds an HTTP header to this request.
163     * @param key   the header key.
164     * @param value the header value.
165     */
166    public void addHeader(String key, String value) {
167        if (key.equals("As-User")) {
168            for (int i = 0; i < this.headers.size(); i++) {
169                if (this.headers.get(i).getKey().equals("As-User")) {
170                    this.headers.remove(i);
171                }
172            }
173        }
174        if (key.equals("X-Box-UA")) {
175            throw new IllegalArgumentException("Altering the X-Box-UA header is not permitted");
176        }
177        this.headers.add(new RequestHeader(key, value));
178    }
179
180    /**
181     * Sets a Connect timeout for this request in milliseconds.
182     * @param timeout the timeout in milliseconds.
183     */
184    public void setConnectTimeout(int timeout) {
185        this.connectTimeout = timeout;
186    }
187
188    /**
189     * Sets a read timeout for this request in milliseconds.
190     * @param timeout the timeout in milliseconds.
191     */
192    public void setReadTimeout(int timeout) {
193        this.readTimeout = timeout;
194    }
195
196  /**
197     * Sets whether or not to follow redirects (i.e. Location header)
198     * @param followRedirects true to follow, false to not follow
199     */
200    public void setFollowRedirects(boolean followRedirects) {
201        this.followRedirects = followRedirects;
202    }
203
204    /**
205     * Gets the stream containing contents of this request's body.
206     *
207     * <p>Note that any bytes that read from the returned stream won't be sent unless the stream is reset back to its
208     * initial position.</p>
209     *
210     * @return an InputStream containing the contents of this request's body.
211     */
212    public InputStream getBody() {
213        return this.body;
214    }
215
216    /**
217     * Sets the request body to the contents of an InputStream.
218     *
219     * <p>The stream must support the {@link InputStream#reset} method if auto-retry is used or if the request needs to
220     * be resent. Otherwise, the body must be manually set before each call to {@link #send}.</p>
221     *
222     * @param stream an InputStream containing the contents of the body.
223     */
224    public void setBody(InputStream stream) {
225        this.body = stream;
226    }
227
228    /**
229     * Sets the request body to the contents of an InputStream.
230     *
231     * <p>Providing the length of the InputStream allows for the progress of the request to be monitored when calling
232     * {@link #send(ProgressListener)}.</p>
233     *
234     * <p> See {@link #setBody(InputStream)} for more information on setting the body of the request.</p>
235     *
236     * @param stream an InputStream containing the contents of the body.
237     * @param length the expected length of the stream.
238     */
239    public void setBody(InputStream stream, long length) {
240        this.bodyLength = length;
241        this.body = stream;
242    }
243
244    /**
245     * Sets the request body to the contents of a String.
246     *
247     * <p>If the contents of the body are large, then it may be more efficient to use an {@link InputStream} instead of
248     * a String. Using a String requires that the entire body be in memory before sending the request.</p>
249     *
250     * @param body a String containing the contents of the body.
251     */
252    public void setBody(String body) {
253        byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
254        this.bodyLength = bytes.length;
255        this.body = new ByteArrayInputStream(bytes);
256    }
257
258    /**
259     * Gets the URL from the request.
260     *
261     * @return a URL containing the URL of the request.
262     */
263    public URL getUrl() {
264        return this.url;
265    }
266
267    /**
268     * Gets the http method from the request.
269     *
270     * @return http method
271     */
272    public String getMethod() {
273        return this.method;
274    }
275
276    /**
277     * Get headers as list of RequestHeader objects.
278     * @return headers as list of RequestHeader objects
279     */
280    protected List<RequestHeader> getHeaders() {
281        return this.headers;
282    }
283
284    /**
285     * Sends this request and returns a BoxAPIResponse containing the server's response.
286     *
287     * <p>The type of the returned BoxAPIResponse will be based on the content type returned by the server, allowing it
288     * to be cast to a more specific type. For example, if it's known that the API call will return a JSON response,
289     * then it can be cast to a {@link BoxJSONResponse} like so:</p>
290     *
291     * <pre>BoxJSONResponse response = (BoxJSONResponse) request.send();</pre>
292     *
293     * <p>If the server returns an error code or if a network error occurs, then the request will be automatically
294     * retried. If the maximum number of retries is reached and an error still occurs, then a {@link BoxAPIException}
295     * will be thrown.</p>
296     *
297     * @throws BoxAPIException if the server returns an error code or if a network error occurs.
298     * @return a {@link BoxAPIResponse} containing the server's response.
299     */
300    public BoxAPIResponse send() {
301        return this.send(null);
302    }
303
304    /**
305     * Sends this request while monitoring its progress and returns a BoxAPIResponse containing the server's response.
306     *
307     * <p>A ProgressListener is generally only useful when the size of the request is known beforehand. If the size is
308     * unknown, then the ProgressListener will be updated for each byte sent, but the total number of bytes will be
309     * reported as 0.</p>
310     *
311     * <p> See {@link #send} for more information on sending requests.</p>
312     *
313     * @param  listener a listener for monitoring the progress of the request.
314     * @throws BoxAPIException if the server returns an error code or if a network error occurs.
315     * @return a {@link BoxAPIResponse} containing the server's response.
316     */
317    public BoxAPIResponse send(ProgressListener listener) {
318        if (this.api == null) {
319            this.backoffCounter.reset(BoxAPIConnection.DEFAULT_MAX_ATTEMPTS);
320        } else {
321            this.backoffCounter.reset(this.api.getMaxRequestAttempts());
322        }
323
324        while (this.backoffCounter.getAttemptsRemaining() > 0) {
325            try {
326                return this.trySend(listener);
327            } catch (BoxAPIException apiException) {
328                if (!this.backoffCounter.decrement() || !isResponseRetryable(apiException.getResponseCode())) {
329                    throw apiException;
330                }
331
332                try {
333                    this.resetBody();
334                } catch (IOException ioException) {
335                    throw apiException;
336                }
337
338                try {
339                    this.backoffCounter.waitBackoff();
340                } catch (InterruptedException interruptedException) {
341                    Thread.currentThread().interrupt();
342                    throw apiException;
343                }
344            }
345        }
346
347        throw new RuntimeException();
348    }
349
350    /**
351     * Returns a String containing the URL, HTTP method, headers and body of this request.
352     * @return a String containing information about this request.
353     */
354    @Override
355    public String toString() {
356        String lineSeparator = System.getProperty("line.separator");
357        StringBuilder builder = new StringBuilder();
358        builder.append("Request");
359        builder.append(lineSeparator);
360        builder.append(this.method);
361        builder.append(' ');
362        builder.append(this.url.toString());
363        builder.append(lineSeparator);
364
365        if (this.requestProperties != null) {
366
367            for (Map.Entry<String, List<String>> entry : this.requestProperties.entrySet()) {
368                List<String> nonEmptyValues = new ArrayList<String>();
369                for (String value : entry.getValue()) {
370                    if (value != null && value.trim().length() != 0) {
371                        nonEmptyValues.add(value);
372                    }
373                }
374
375                if (nonEmptyValues.size() == 0) {
376                    continue;
377                }
378
379                builder.append(entry.getKey());
380                builder.append(": ");
381                for (String value : nonEmptyValues) {
382                    builder.append(value);
383                    builder.append(", ");
384                }
385
386                builder.delete(builder.length() - 2, builder.length());
387                builder.append(lineSeparator);
388            }
389        }
390
391        String bodyString = this.bodyToString();
392        if (bodyString != null) {
393            builder.append(lineSeparator);
394            builder.append(bodyString);
395        }
396
397        return builder.toString().trim();
398    }
399
400    /**
401     * Returns a String representation of this request's body used in {@link #toString}. This method returns
402     * null by default.
403     *
404     * <p>A subclass may want override this method if the body can be converted to a String for logging or debugging
405     * purposes.</p>
406     *
407     * @return a String representation of this request's body.
408     */
409    protected String bodyToString() {
410        return null;
411    }
412
413    /**
414     * Writes the body of this request to an HttpURLConnection.
415     *
416     * <p>Subclasses overriding this method must remember to close the connection's OutputStream after writing.</p>
417     *
418     * @param connection the connection to which the body should be written.
419     * @param listener   an optional listener for monitoring the write progress.
420     * @throws BoxAPIException if an error occurs while writing to the connection.
421     */
422    protected void writeBody(HttpURLConnection connection, ProgressListener listener) {
423        if (this.body == null) {
424            return;
425        }
426
427        connection.setDoOutput(true);
428        try {
429            OutputStream output = connection.getOutputStream();
430            if (listener != null) {
431                output = new ProgressOutputStream(output, listener, this.bodyLength);
432            }
433            int b = this.body.read();
434            while (b != -1) {
435                output.write(b);
436                b = this.body.read();
437            }
438            output.close();
439        } catch (IOException e) {
440            throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
441        }
442    }
443
444    /**
445     * Resets the InputStream containing this request's body.
446     *
447     * <p>This method will be called before each attempt to resend the request, giving subclasses an opportunity to
448     * reset any streams that need to be read when sending the body.</p>
449     *
450     * @throws IOException if the stream cannot be reset.
451     */
452    protected void resetBody() throws IOException {
453        if (this.body != null) {
454            this.body.reset();
455        }
456    }
457
458    void setBackoffCounter(BackoffCounter counter) {
459        this.backoffCounter = counter;
460    }
461
462    private BoxAPIResponse trySend(ProgressListener listener) {
463        if (this.api != null) {
464            RequestInterceptor interceptor = this.api.getRequestInterceptor();
465            if (interceptor != null) {
466                BoxAPIResponse response = interceptor.onRequest(this);
467                if (response != null) {
468                    return response;
469                }
470            }
471        }
472
473        HttpURLConnection connection = this.createConnection();
474
475        if (connection instanceof HttpsURLConnection) {
476            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
477
478            if (this.sslContext != null) {
479                httpsConnection.setSSLSocketFactory(this.sslContext.getSocketFactory());
480            }
481        }
482
483        if (this.bodyLength > 0) {
484            connection.setFixedLengthStreamingMode((int) this.bodyLength);
485            connection.setDoOutput(true);
486        }
487
488        if (this.api != null) {
489            if (this.shouldAuthenticate) {
490                connection.addRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + this.api.lockAccessToken());
491            }
492            connection.setRequestProperty("User-Agent", this.api.getUserAgent());
493            if (this.api.getProxy() != null) {
494                if (this.api.getProxyUsername() != null && this.api.getProxyPassword() != null) {
495                    String usernameAndPassword = this.api.getProxyUsername() + ":" + this.api.getProxyPassword();
496                    String encoded = new String(Base64.encode(usernameAndPassword.getBytes()));
497                    connection.addRequestProperty("Proxy-Authorization", "Basic " + encoded);
498                }
499            }
500
501            if (this.api instanceof SharedLinkAPIConnection) {
502                SharedLinkAPIConnection sharedItemAPI = (SharedLinkAPIConnection) this.api;
503                String sharedLink = sharedItemAPI.getSharedLink();
504                String boxAPIValue = "shared_link=" + sharedLink;
505                String sharedLinkPassword = sharedItemAPI.getSharedLinkPassword();
506                if (sharedLinkPassword != null) {
507                    boxAPIValue += "&shared_link_password=" + sharedLinkPassword;
508                }
509                connection.addRequestProperty("BoxApi", boxAPIValue);
510            }
511        }
512
513        this.requestProperties = connection.getRequestProperties();
514
515        int responseCode;
516        try {
517            this.writeBody(connection, listener);
518
519            // Ensure that we're connected in case writeBody() didn't write anything.
520            try {
521                connection.connect();
522            } catch (IOException e) {
523                throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
524            }
525
526            this.logRequest(connection);
527
528            // We need to manually handle redirects by creating a new HttpURLConnection so that connection pooling
529            // happens correctly. There seems to be a bug in Oracle's Java implementation where automatically handled
530            // redirects will not keep the connection alive.
531            try {
532                responseCode = connection.getResponseCode();
533            } catch (IOException e) {
534                throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
535            }
536        } finally {
537            if (this.api != null && this.shouldAuthenticate) {
538                this.api.unlockAccessToken();
539            }
540        }
541
542        if (isResponseRedirect(responseCode)) {
543            return this.handleRedirect(connection, listener);
544        }
545
546        String contentType = connection.getContentType();
547        BoxAPIResponse response;
548        if (contentType == null) {
549            response = new BoxAPIResponse(connection);
550        } else if (contentType.contains("application/json")) {
551            response = new BoxJSONResponse(connection);
552        } else {
553            response = new BoxAPIResponse(connection);
554        }
555
556        return response;
557    }
558
559    private BoxAPIResponse handleRedirect(HttpURLConnection connection, ProgressListener listener) {
560        if (this.numRedirects >= MAX_REDIRECTS) {
561            throw new BoxAPIException("The Box API responded with too many redirects.");
562        }
563        this.numRedirects++;
564
565        // Even though the redirect response won't have a body, we need to read the InputStream so that Java will put
566        // the connection back in the connection pool.
567        try {
568            InputStream stream = connection.getInputStream();
569            byte[] buffer = new byte[8192];
570            int n = stream.read(buffer);
571            while (n != -1) {
572                n = stream.read(buffer);
573            }
574            stream.close();
575        } catch (IOException e) {
576            throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
577        }
578
579        String redirect = connection.getHeaderField("Location");
580        try {
581            this.url = new URL(redirect);
582        } catch (MalformedURLException e) {
583            throw new BoxAPIException("The Box API responded with an invalid redirect.", e);
584        }
585
586        if (this.followRedirects) {
587            return this.trySend(listener);
588        } else {
589            BoxRedirectResponse redirectResponse = new BoxRedirectResponse();
590            redirectResponse.setRedirectURL(this.url);
591            return redirectResponse;
592        }
593    }
594
595    private void logRequest(HttpURLConnection connection) {
596        if (LOGGER.isLoggable(Level.FINE)) {
597            LOGGER.log(Level.FINE, this.toString());
598        }
599    }
600
601    private HttpURLConnection createConnection() {
602        HttpURLConnection connection = null;
603
604        try {
605            if (this.api == null || this.api.getProxy() == null) {
606                connection = (HttpURLConnection) this.url.openConnection();
607            } else {
608                connection = (HttpURLConnection) this.url.openConnection(this.api.getProxy());
609            }
610        } catch (IOException e) {
611            throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
612        }
613
614        try {
615            connection.setRequestMethod(this.method);
616        } catch (ProtocolException e) {
617            throw new BoxAPIException("Couldn't connect to the Box API because the request's method was invalid.", e);
618        }
619
620        connection.setConnectTimeout(this.connectTimeout);
621        connection.setReadTimeout(this.readTimeout);
622
623        // Don't allow HttpURLConnection to automatically redirect because it messes up the connection pool. See the
624        // trySend(ProgressListener) method for how we handle redirects.
625        connection.setInstanceFollowRedirects(false);
626
627        for (RequestHeader header : this.headers) {
628            connection.addRequestProperty(header.getKey(), header.getValue());
629        }
630
631        return connection;
632    }
633
634    void shouldAuthenticate(boolean shouldAuthenticate) {
635        this.shouldAuthenticate = shouldAuthenticate;
636    }
637
638    private static boolean isResponseRetryable(int responseCode) {
639        return (responseCode >= 500 || responseCode == 429);
640    }
641    private static boolean isResponseRedirect(int responseCode) {
642        return (responseCode == 301 || responseCode == 302);
643    }
644
645    /**
646     * Class for mapping a request header and value.
647     */
648    public final class RequestHeader {
649        private final String key;
650        private final String value;
651
652        /**
653         * Construct a request header from header key and value.
654         * @param key header name
655         * @param value header value
656         */
657        public RequestHeader(String key, String value) {
658            this.key = key;
659            this.value = value;
660        }
661
662        /**
663         * Get header key.
664         * @return http header name
665         */
666        public String getKey() {
667            return this.key;
668        }
669
670        /**
671         * Get header value.
672         * @return http header value
673         */
674        public String getValue() {
675            return this.value;
676        }
677    }
678}