Skip to content

Commit

Permalink
fixed handling of malformed URL and unreachable hosts when creating s…
Browse files Browse the repository at this point in the history
…ource from URL
  • Loading branch information
lfoster committed Jul 12, 2013
1 parent f0b5659 commit 9808199
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions juxta-ws/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.4.0</version>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -25,6 +26,7 @@
import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.validator.routines.UrlValidator;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
Expand Down Expand Up @@ -211,10 +213,15 @@ private Representation handleJsonPost(Representation entity) {
String data = jsonObj.get("data").getAsString();

try {
if (type.equalsIgnoreCase("url")) {
// pull content from the URL. Type will be determined from
// the HTTP response
ids.add( scrapeExternalUrl(name, data) );
if (type.equalsIgnoreCase("url")) {
if ( UrlValidator.getInstance().isValid(data)) {
// pull content from the URL. Type will be determined from
// the HTTP response
ids.add( scrapeExternalUrl(name, data) );
} else {
setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return toTextRepresentation("Malformed source URL");
}
} else if (type.equalsIgnoreCase("raw")) {
if (jsonObj.has("contentType") == false) {
setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
Expand All @@ -233,6 +240,9 @@ private Representation handleJsonPost(Representation entity) {
} else {
return toTextRepresentation("Link to "+data+" failed: "+e.getMessage());
}
} catch (UnknownHostException e) {
setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return toTextRepresentation("The URL contains an unknown host");
} catch (IOException e) {
setStatus(Status.SERVER_ERROR_INTERNAL);
return toTextRepresentation("Unable to create source "+name+": "+e.toString());
Expand Down

0 comments on commit 9808199

Please sign in to comment.