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