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                LOGGER.log(Level.WARNING, "Retrying request due to transient error status=%d body=%s",
361                        new Object[] {apiException.getResponseCode(), apiException.getResponse()});
362
363                try {
364                    this.resetBody();
365                } catch (IOException ioException) {
366                    throw apiException;
367                }
368
369                try {
370                    this.backoffCounter.waitBackoff();
371                } catch (InterruptedException interruptedException) {
372                    Thread.currentThread().interrupt();
373                    throw apiException;
374                }
375            }
376        }
377
378        throw new RuntimeException();
379    }
380
381    /**
382     * Returns a String containing the URL, HTTP method, headers and body of this request.
383     * @return a String containing information about this request.
384     */
385    @Override
386    public String toString() {
387        String lineSeparator = System.getProperty("line.separator");
388        StringBuilder builder = new StringBuilder();
389        builder.append("Request");
390        builder.append(lineSeparator);
391        builder.append(this.method);
392        builder.append(' ');
393        builder.append(this.url.toString());
394        builder.append(lineSeparator);
395
396        if (this.requestProperties != null) {
397
398            for (Map.Entry<String, List<String>> entry : this.requestProperties.entrySet()) {
399                List<String> nonEmptyValues = new ArrayList<String>();
400                for (String value : entry.getValue()) {
401                    if (value != null && value.trim().length() != 0) {
402                        nonEmptyValues.add(value);
403                    }
404                }
405
406                if (nonEmptyValues.size() == 0) {
407                    continue;
408                }
409
410                builder.append(entry.getKey());
411                builder.append(": ");
412                for (String value : nonEmptyValues) {
413                    builder.append(value);
414                    builder.append(", ");
415                }
416
417                builder.delete(builder.length() - 2, builder.length());
418                builder.append(lineSeparator);
419            }
420        }
421
422        String bodyString = this.bodyToString();
423        if (bodyString != null) {
424            builder.append(lineSeparator);
425            builder.append(bodyString);
426        }
427
428        return builder.toString().trim();
429    }
430
431    /**
432     * Returns a String representation of this request's body used in {@link #toString}. This method returns
433     * null by default.
434     *
435     * <p>A subclass may want override this method if the body can be converted to a String for logging or debugging
436     * purposes.</p>
437     *
438     * @return a String representation of this request's body.
439     */
440    protected String bodyToString() {
441        return null;
442    }
443
444    /**
445     * Writes the body of this request to an HttpURLConnection.
446     *
447     * <p>Subclasses overriding this method must remember to close the connection's OutputStream after writing.</p>
448     *
449     * @param connection the connection to which the body should be written.
450     * @param listener   an optional listener for monitoring the write progress.
451     * @throws BoxAPIException if an error occurs while writing to the connection.
452     */
453    protected void writeBody(HttpURLConnection connection, ProgressListener listener) {
454        if (this.body == null) {
455            return;
456        }
457
458        connection.setDoOutput(true);
459        try {
460            OutputStream output = connection.getOutputStream();
461            if (listener != null) {
462                output = new ProgressOutputStream(output, listener, this.bodyLength);
463            }
464            int b = this.body.read();
465            while (b != -1) {
466                output.write(b);
467                b = this.body.read();
468            }
469            output.close();
470        } catch (IOException e) {
471            throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
472        }
473    }
474
475    /**
476     * Resets the InputStream containing this request's body.
477     *
478     * <p>This method will be called before each attempt to resend the request, giving subclasses an opportunity to
479     * reset any streams that need to be read when sending the body.</p>
480     *
481     * @throws IOException if the stream cannot be reset.
482     */
483    protected void resetBody() throws IOException {
484        if (this.body != null) {
485            this.body.reset();
486        }
487    }
488
489    void setBackoffCounter(BackoffCounter counter) {
490        this.backoffCounter = counter;
491    }
492
493    private BoxAPIResponse trySend(ProgressListener listener) {
494        if (this.api != null) {
495            RequestInterceptor interceptor = this.api.getRequestInterceptor();
496            if (interceptor != null) {
497                BoxAPIResponse response = interceptor.onRequest(this);
498                if (response != null) {
499                    return response;
500                }
501            }
502        }
503
504        HttpURLConnection connection = this.createConnection();
505
506        if (connection instanceof HttpsURLConnection) {
507            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
508
509            if (sslSocketFactory != null) {
510                httpsConnection.setSSLSocketFactory(sslSocketFactory);
511            }
512        }
513
514        if (this.bodyLength > 0) {
515            connection.setFixedLengthStreamingMode((int) this.bodyLength);
516            connection.setDoOutput(true);
517        }
518
519        if (this.api != null) {
520            if (this.shouldAuthenticate) {
521                connection.addRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + this.api.lockAccessToken());
522            }
523            connection.setRequestProperty("User-Agent", this.api.getUserAgent());
524            if (this.api.getProxy() != null) {
525                if (this.api.getProxyUsername() != null && this.api.getProxyPassword() != null) {
526                    String usernameAndPassword = this.api.getProxyUsername() + ":" + this.api.getProxyPassword();
527                    String encoded = new String(Base64.encode(usernameAndPassword.getBytes()));
528                    connection.addRequestProperty("Proxy-Authorization", "Basic " + encoded);
529                }
530            }
531
532            if (this.api instanceof SharedLinkAPIConnection) {
533                SharedLinkAPIConnection sharedItemAPI = (SharedLinkAPIConnection) this.api;
534                String sharedLink = sharedItemAPI.getSharedLink();
535                String boxAPIValue = "shared_link=" + sharedLink;
536                String sharedLinkPassword = sharedItemAPI.getSharedLinkPassword();
537                if (sharedLinkPassword != null) {
538                    boxAPIValue += "&shared_link_password=" + sharedLinkPassword;
539                }
540                connection.addRequestProperty("BoxApi", boxAPIValue);
541            }
542        }
543
544        this.requestProperties = connection.getRequestProperties();
545
546        int responseCode;
547        try {
548            this.writeBody(connection, listener);
549
550            // Ensure that we're connected in case writeBody() didn't write anything.
551            try {
552                connection.connect();
553            } catch (IOException e) {
554                throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
555            }
556
557            this.logRequest(connection);
558
559            // We need to manually handle redirects by creating a new HttpURLConnection so that connection pooling
560            // happens correctly. There seems to be a bug in Oracle's Java implementation where automatically handled
561            // redirects will not keep the connection alive.
562            try {
563                responseCode = connection.getResponseCode();
564            } catch (IOException e) {
565                throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
566            }
567        } finally {
568            if (this.api != null && this.shouldAuthenticate) {
569                this.api.unlockAccessToken();
570            }
571        }
572
573        if (isResponseRedirect(responseCode)) {
574            return this.handleRedirect(connection, listener);
575        }
576
577        String contentType = connection.getContentType();
578        BoxAPIResponse response;
579        if (contentType == null) {
580            response = new BoxAPIResponse(connection);
581        } else if (contentType.contains("application/json")) {
582            response = new BoxJSONResponse(connection);
583        } else {
584            response = new BoxAPIResponse(connection);
585        }
586
587        return response;
588    }
589
590    private BoxAPIResponse handleRedirect(HttpURLConnection connection, ProgressListener listener) {
591        if (this.numRedirects >= MAX_REDIRECTS) {
592            throw new BoxAPIException("The Box API responded with too many redirects.");
593        }
594        this.numRedirects++;
595
596        // Even though the redirect response won't have a body, we need to read the InputStream so that Java will put
597        // the connection back in the connection pool.
598        try {
599            InputStream stream = connection.getInputStream();
600            byte[] buffer = new byte[8192];
601            int n = stream.read(buffer);
602            while (n != -1) {
603                n = stream.read(buffer);
604            }
605            stream.close();
606        } catch (IOException e) {
607            throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
608        }
609
610        String redirect = connection.getHeaderField("Location");
611        try {
612            this.url = new URL(redirect);
613        } catch (MalformedURLException e) {
614            throw new BoxAPIException("The Box API responded with an invalid redirect.", e);
615        }
616
617        if (this.followRedirects) {
618            return this.trySend(listener);
619        } else {
620            BoxRedirectResponse redirectResponse = new BoxRedirectResponse();
621            redirectResponse.setRedirectURL(this.url);
622            return redirectResponse;
623        }
624    }
625
626    private void logRequest(HttpURLConnection connection) {
627        if (LOGGER.isLoggable(Level.FINE)) {
628            LOGGER.log(Level.FINE, this.toString());
629        }
630    }
631
632    private HttpURLConnection createConnection() {
633        HttpURLConnection connection = null;
634
635        try {
636            if (this.api == null || this.api.getProxy() == null) {
637                connection = (HttpURLConnection) this.url.openConnection();
638            } else {
639                connection = (HttpURLConnection) this.url.openConnection(this.api.getProxy());
640            }
641        } catch (IOException e) {
642            throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
643        }
644
645        try {
646            connection.setRequestMethod(this.method);
647        } catch (ProtocolException e) {
648            throw new BoxAPIException("Couldn't connect to the Box API because the request's method was invalid.", e);
649        }
650
651        connection.setConnectTimeout(this.connectTimeout);
652        connection.setReadTimeout(this.readTimeout);
653
654        // Don't allow HttpURLConnection to automatically redirect because it messes up the connection pool. See the
655        // trySend(ProgressListener) method for how we handle redirects.
656        connection.setInstanceFollowRedirects(false);
657
658        for (RequestHeader header : this.headers) {
659            connection.addRequestProperty(header.getKey(), header.getValue());
660        }
661
662        return connection;
663    }
664
665    void shouldAuthenticate(boolean shouldAuthenticate) {
666        this.shouldAuthenticate = shouldAuthenticate;
667    }
668
669    private static boolean isResponseRetryable(int responseCode) {
670        return (responseCode >= 500 || responseCode == 429);
671    }
672    private static boolean isResponseRedirect(int responseCode) {
673        return (responseCode == 301 || responseCode == 302);
674    }
675
676    /**
677     * Class for mapping a request header and value.
678     */
679    public final class RequestHeader {
680        private final String key;
681        private final String value;
682
683        /**
684         * Construct a request header from header key and value.
685         * @param key header name
686         * @param value header value
687         */
688        public RequestHeader(String key, String value) {
689            this.key = key;
690            this.value = value;
691        }
692
693        /**
694         * Get header key.
695         * @return http header name
696         */
697        public String getKey() {
698            return this.key;
699        }
700
701        /**
702         * Get header value.
703         * @return http header value
704         */
705        public String getValue() {
706            return this.value;
707        }
708    }
709}