Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Fix #89 - Handle HTTP 301 redirects (#97)
Browse files Browse the repository at this point in the history
* Handle HTTP 301 redirects and log warning
* If redirect to HTTPS fails, catch the SSLHandshakeException and tell the user to install the JCE Extension
* Add documentation to README for installing JCE Extension
  • Loading branch information
barbeau committed Mar 28, 2017
1 parent 58906a7 commit 180785d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -19,6 +19,7 @@ Following are the requirements to get the project up and running.
* JDK installed on the system
* Maven installed on the system
* (optional) git installed on the system to clone the repository
* [Java Cryptography Extension (JCE)](http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html) - If you're downloading GTFS or GTFS-rt from secure HTTPS URLs, you may need to install the JCE Extension. You will need to replace the `US_export_policy.jar` and `local_policy.jar` files in your JVM `/security` directory, such as `C:\Program Files\Java\jdk1.8.0_73\jre\lib\security`, with the JAR files in the JCE Extension download.

### 1. Download the code

Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.onebusaway.gtfs.serialization.GtfsReader;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLHandshakeException;
import javax.ws.rs.*;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -313,8 +314,21 @@ private GtfsDaoImpl saveGtfsFeed(GtfsFeedModel gtfsFeed) {

private Response.Status downloadGtfsFeed(String saveFilePath, HttpURLConnection connection) {
try {
// opens input stream from the HTTP connection
InputStream inputStream = connection.getInputStream();
// Check for HTTP 301 redirect
String redirect = connection.getHeaderField("Location");
if (redirect != null) {
_log.warn("Redirecting to " + redirect);
connection = (HttpURLConnection) new URL(redirect).openConnection();
}

// Opens input stream from the HTTP(S) connection
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (SSLHandshakeException sslEx) {
_log.error("SSL handshake failed. Try installing the JCE Extension - see https://github.com/CUTR-at-USF/gtfs-realtime-validator#prerequisites", sslEx);
return null;
}

// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
Expand Down

0 comments on commit 180785d

Please sign in to comment.