append http protocol if missing

This commit is contained in:
Athou
2013-04-08 15:13:32 +02:00
parent 22408abc50
commit 546562c175

View File

@@ -39,11 +39,13 @@ public class SubscriptionsREST extends AbstractREST {
@Path("fetch") @Path("fetch")
public Feed fetchFeed(@QueryParam("url") String url) { public Feed fetchFeed(@QueryParam("url") String url) {
Preconditions.checkNotNull(url); Preconditions.checkNotNull(url);
url = prependHttp(url);
Feed feed = null; Feed feed = null;
try { try {
feed = feedFetcher.fetch(url); feed = feedFetcher.fetch(url);
} catch (FeedException e) { } catch (FeedException e) {
throw new WebApplicationException(Response throw new WebApplicationException(e, Response
.status(Status.INTERNAL_SERVER_ERROR) .status(Status.INTERNAL_SERVER_ERROR)
.entity(e.getMessage()).build()); .entity(e.getMessage()).build());
} }
@@ -57,7 +59,9 @@ public class SubscriptionsREST extends AbstractREST {
Preconditions.checkNotNull(req.getTitle()); Preconditions.checkNotNull(req.getTitle());
Preconditions.checkNotNull(req.getUrl()); Preconditions.checkNotNull(req.getUrl());
Feed fetchedFeed = fetchFeed(req.getUrl()); String url = prependHttp(req.getUrl());
Feed fetchedFeed = fetchFeed(url);
Feed feed = feedService.findByUrl(fetchedFeed.getUrl()); Feed feed = feedService.findByUrl(fetchedFeed.getUrl());
if (feed == null) { if (feed == null) {
feed = fetchedFeed; feed = fetchedFeed;
@@ -74,6 +78,13 @@ public class SubscriptionsREST extends AbstractREST {
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
private String prependHttp(String url) {
if (!url.startsWith("http")) {
url = "http://" + url;
}
return url;
}
@GET @GET
@Path("unsubscribe") @Path("unsubscribe")
public Response unsubscribe(@QueryParam("id") Long subscriptionId) { public Response unsubscribe(@QueryParam("id") Long subscriptionId) {