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