From bf527ec7aa5ca798d7c364a77347717525c1c016 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Sat, 12 Apr 2014 21:17:20 +0200 Subject: [PATCH] Refactored some code Replaced deprecated code --- .../certiorem/sub/Subscriptions.java | 19 +++++++++++-------- .../certiorem/web/AbstractSubServlet.java | 19 +++++++------------ .../certiorem/hub/data/AbstractDAOTest.java | 8 +++++--- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/rometools/certiorem/sub/Subscriptions.java b/src/main/java/org/rometools/certiorem/sub/Subscriptions.java index 87269b1..50646ac 100644 --- a/src/main/java/org/rometools/certiorem/sub/Subscriptions.java +++ b/src/main/java/org/rometools/certiorem/sub/Subscriptions.java @@ -49,6 +49,9 @@ import com.sun.syndication.io.SyndFeedInput; * @author robert.cooper */ public class Subscriptions { + + private static final Logger LOGGER = Logger.getLogger(Subscriptions.class.getName()); + // TODO unsubscribe. private FeedFetcherCache cache; private Requester requester; @@ -69,7 +72,7 @@ public class Subscriptions { try { this.callback(callbackPath, feed.getBytes("UTF-8")); } catch (final UnsupportedEncodingException ex) { - Logger.getLogger(Subscriptions.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); throw new HttpStatusCodeException(400, "Unable to parse feed.", ex); } } @@ -80,10 +83,10 @@ public class Subscriptions { try { this.callback(callbackPath, input.build(new InputStreamReader(feed))); } catch (final IllegalArgumentException ex) { - Logger.getLogger(Subscriptions.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); throw new HttpStatusCodeException(500, "Unable to parse feed.", ex); } catch (final FeedException ex) { - Logger.getLogger(Subscriptions.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); throw new HttpStatusCodeException(400, "Unable to parse feed.", ex); } } @@ -99,7 +102,7 @@ public class Subscriptions { } final String id = callbackPath.substring(callbackPrefix.length()); - Logger.getLogger(Subscriptions.class.getName()).log(Level.FINE, "Got callback for {0}", id); + LOGGER.log(Level.FINE, "Got callback for {0}", id); final Subscription s = dao.findById(id); if (s == null) { @@ -115,7 +118,7 @@ public class Subscriptions { url = new URL(s.getSourceUrl()); info = cache.getFeedInfo(url); } catch (final MalformedURLException ex) { - Logger.getLogger(Subscriptions.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } if (info == null) { @@ -193,7 +196,7 @@ public class Subscriptions { } final String id = callbackPath.substring(callbackPrefix.length()); - Logger.getLogger(Subscriptions.class.getName()).log(Level.FINE, "Handling validation request for id {0}", id); + LOGGER.log(Level.FINE, "Handling validation request for id {0}", id); final Subscription s = dao.findById(id); if (s == null) { throw new HttpStatusCodeException(404, "Not a valid subscription id", null); @@ -218,7 +221,7 @@ public class Subscriptions { } else { throw new HttpStatusCodeException(400, "Unsupported mode " + mode, null); } - Logger.getLogger(Subscriptions.class.getName()).log(Level.FINE, "Validated. Returning {0}", challenge); + LOGGER.log(Level.FINE, "Validated. Returning {0}", challenge); return challenge; } @@ -245,7 +248,7 @@ public class Subscriptions { break; } catch (final URISyntaxException ex) { - Logger.getLogger(Subscriptions.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } } diff --git a/src/main/java/org/rometools/certiorem/web/AbstractSubServlet.java b/src/main/java/org/rometools/certiorem/web/AbstractSubServlet.java index 3790667..6094731 100644 --- a/src/main/java/org/rometools/certiorem/web/AbstractSubServlet.java +++ b/src/main/java/org/rometools/certiorem/web/AbstractSubServlet.java @@ -21,10 +21,10 @@ package org.rometools.certiorem.web; import java.io.IOException; import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpUtils; import org.rometools.certiorem.HttpStatusCodeException; import org.rometools.certiorem.sub.Subscriptions; @@ -33,18 +33,14 @@ import org.rometools.certiorem.sub.Subscriptions; * * @author robert.cooper */ -public class AbstractSubServlet extends HttpServlet { +public abstract class AbstractSubServlet extends HttpServlet { - /** - * - */ private static final long serialVersionUID = 1L; + private final Subscriptions subscriptions; protected AbstractSubServlet(final Subscriptions subscriptions) { - super(); this.subscriptions = subscriptions; - } @Override @@ -55,23 +51,22 @@ public class AbstractSubServlet extends HttpServlet { final String leaseString = req.getParameter("hub.lease_seconds"); final String verifyToken = req.getParameter("hub.verify_token"); try { - final String result = subscriptions.validate(HttpUtils.getRequestURL(req).toString(), topic, mode, challenge, leaseString, verifyToken); + final String result = subscriptions.validate(req.getRequestURL().toString(), topic, mode, challenge, leaseString, verifyToken); resp.setStatus(200); resp.getWriter().print(result); - return; } catch (final HttpStatusCodeException e) { e.printStackTrace(); resp.setStatus(e.getStatus()); resp.getWriter().print(e.getMessage()); } - } @Override protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { try { - subscriptions.callback(HttpUtils.getRequestURL(req).toString(), req.getInputStream()); - return; + final String requestUrl = req.getRequestURL().toString(); + final ServletInputStream inputStream = req.getInputStream(); + subscriptions.callback(requestUrl, inputStream); } catch (final HttpStatusCodeException e) { e.printStackTrace(); resp.setStatus(e.getStatus()); diff --git a/src/test/java/org/rometools/certiorem/hub/data/AbstractDAOTest.java b/src/test/java/org/rometools/certiorem/hub/data/AbstractDAOTest.java index bd0fbd2..77836b3 100644 --- a/src/test/java/org/rometools/certiorem/hub/data/AbstractDAOTest.java +++ b/src/test/java/org/rometools/certiorem/hub/data/AbstractDAOTest.java @@ -30,12 +30,14 @@ import org.junit.Test; */ public abstract class AbstractDAOTest { + private static final Logger LOGGER = Logger.getLogger(AbstractDAOTest.class.getName()); + protected abstract HubDAO get(); @Test public void testSubscribe() { final HubDAO instance = get(); - Logger.getLogger(AbstractDAOTest.class.getName()).log(Level.INFO, "{0} testSubscribe", instance.getClass().getName()); + LOGGER.log(Level.INFO, "{0} testSubscribe", instance.getClass().getName()); final Subscriber subscriber = new Subscriber(); subscriber.setCallback("http://localhost:9797/noop"); subscriber.setTopic("http://feeds.feedburner.com/screaming-penguin"); @@ -54,7 +56,7 @@ public abstract class AbstractDAOTest { @Test public void testLeaseExpire() throws InterruptedException { final HubDAO instance = get(); - Logger.getLogger(AbstractDAOTest.class.getName()).log(Level.INFO, "{0} testLeaseExpire", instance.getClass().getName()); + LOGGER.log(Level.INFO, "{0} testLeaseExpire", instance.getClass().getName()); final Subscriber subscriber = new Subscriber(); subscriber.setCallback("http://localhost:9797/noop"); subscriber.setTopic("http://feeds.feedburner.com/screaming-penguin"); @@ -76,7 +78,7 @@ public abstract class AbstractDAOTest { @Test public void testUnsubscribe() throws InterruptedException { final HubDAO instance = get(); - Logger.getLogger(AbstractDAOTest.class.getName()).log(Level.INFO, "{0} testUnsubscribe", instance.getClass().getName()); + LOGGER.log(Level.INFO, "{0} testUnsubscribe", instance.getClass().getName()); final Subscriber subscriber = new Subscriber(); subscriber.setCallback("http://localhost:9797/noop"); subscriber.setTopic("http://feeds.feedburner.com/screaming-penguin");