Refactored some code

Replaced deprecated code
This commit is contained in:
Patrick Gotthard 2014-04-12 21:17:20 +02:00
parent 372e2581ce
commit bf527ec7aa
3 changed files with 23 additions and 23 deletions

View file

@ -49,6 +49,9 @@ import com.sun.syndication.io.SyndFeedInput;
* @author robert.cooper * @author robert.cooper
*/ */
public class Subscriptions { public class Subscriptions {
private static final Logger LOGGER = Logger.getLogger(Subscriptions.class.getName());
// TODO unsubscribe. // TODO unsubscribe.
private FeedFetcherCache cache; private FeedFetcherCache cache;
private Requester requester; private Requester requester;
@ -69,7 +72,7 @@ public class Subscriptions {
try { try {
this.callback(callbackPath, feed.getBytes("UTF-8")); this.callback(callbackPath, feed.getBytes("UTF-8"));
} catch (final UnsupportedEncodingException ex) { } 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); throw new HttpStatusCodeException(400, "Unable to parse feed.", ex);
} }
} }
@ -80,10 +83,10 @@ public class Subscriptions {
try { try {
this.callback(callbackPath, input.build(new InputStreamReader(feed))); this.callback(callbackPath, input.build(new InputStreamReader(feed)));
} catch (final IllegalArgumentException ex) { } 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); throw new HttpStatusCodeException(500, "Unable to parse feed.", ex);
} catch (final FeedException 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); throw new HttpStatusCodeException(400, "Unable to parse feed.", ex);
} }
} }
@ -99,7 +102,7 @@ public class Subscriptions {
} }
final String id = callbackPath.substring(callbackPrefix.length()); 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); final Subscription s = dao.findById(id);
if (s == null) { if (s == null) {
@ -115,7 +118,7 @@ public class Subscriptions {
url = new URL(s.getSourceUrl()); url = new URL(s.getSourceUrl());
info = cache.getFeedInfo(url); info = cache.getFeedInfo(url);
} catch (final MalformedURLException ex) { } catch (final MalformedURLException ex) {
Logger.getLogger(Subscriptions.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} }
if (info == null) { if (info == null) {
@ -193,7 +196,7 @@ public class Subscriptions {
} }
final String id = callbackPath.substring(callbackPrefix.length()); 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); final Subscription s = dao.findById(id);
if (s == null) { if (s == null) {
throw new HttpStatusCodeException(404, "Not a valid subscription id", null); throw new HttpStatusCodeException(404, "Not a valid subscription id", null);
@ -218,7 +221,7 @@ public class Subscriptions {
} else { } else {
throw new HttpStatusCodeException(400, "Unsupported mode " + mode, null); 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; return challenge;
} }
@ -245,7 +248,7 @@ public class Subscriptions {
break; break;
} catch (final URISyntaxException ex) { } catch (final URISyntaxException ex) {
Logger.getLogger(Subscriptions.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} }
} }
} }

View file

@ -21,10 +21,10 @@ package org.rometools.certiorem.web;
import java.io.IOException; import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpUtils;
import org.rometools.certiorem.HttpStatusCodeException; import org.rometools.certiorem.HttpStatusCodeException;
import org.rometools.certiorem.sub.Subscriptions; import org.rometools.certiorem.sub.Subscriptions;
@ -33,18 +33,14 @@ import org.rometools.certiorem.sub.Subscriptions;
* *
* @author robert.cooper * @author robert.cooper
*/ */
public class AbstractSubServlet extends HttpServlet { public abstract class AbstractSubServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final Subscriptions subscriptions; private final Subscriptions subscriptions;
protected AbstractSubServlet(final Subscriptions subscriptions) { protected AbstractSubServlet(final Subscriptions subscriptions) {
super();
this.subscriptions = subscriptions; this.subscriptions = subscriptions;
} }
@Override @Override
@ -55,23 +51,22 @@ public class AbstractSubServlet extends HttpServlet {
final String leaseString = req.getParameter("hub.lease_seconds"); final String leaseString = req.getParameter("hub.lease_seconds");
final String verifyToken = req.getParameter("hub.verify_token"); final String verifyToken = req.getParameter("hub.verify_token");
try { 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.setStatus(200);
resp.getWriter().print(result); resp.getWriter().print(result);
return;
} catch (final HttpStatusCodeException e) { } catch (final HttpStatusCodeException e) {
e.printStackTrace(); e.printStackTrace();
resp.setStatus(e.getStatus()); resp.setStatus(e.getStatus());
resp.getWriter().print(e.getMessage()); resp.getWriter().print(e.getMessage());
} }
} }
@Override @Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
try { try {
subscriptions.callback(HttpUtils.getRequestURL(req).toString(), req.getInputStream()); final String requestUrl = req.getRequestURL().toString();
return; final ServletInputStream inputStream = req.getInputStream();
subscriptions.callback(requestUrl, inputStream);
} catch (final HttpStatusCodeException e) { } catch (final HttpStatusCodeException e) {
e.printStackTrace(); e.printStackTrace();
resp.setStatus(e.getStatus()); resp.setStatus(e.getStatus());

View file

@ -30,12 +30,14 @@ import org.junit.Test;
*/ */
public abstract class AbstractDAOTest { public abstract class AbstractDAOTest {
private static final Logger LOGGER = Logger.getLogger(AbstractDAOTest.class.getName());
protected abstract HubDAO get(); protected abstract HubDAO get();
@Test @Test
public void testSubscribe() { public void testSubscribe() {
final HubDAO instance = get(); 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(); final Subscriber subscriber = new Subscriber();
subscriber.setCallback("http://localhost:9797/noop"); subscriber.setCallback("http://localhost:9797/noop");
subscriber.setTopic("http://feeds.feedburner.com/screaming-penguin"); subscriber.setTopic("http://feeds.feedburner.com/screaming-penguin");
@ -54,7 +56,7 @@ public abstract class AbstractDAOTest {
@Test @Test
public void testLeaseExpire() throws InterruptedException { public void testLeaseExpire() throws InterruptedException {
final HubDAO instance = get(); 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(); final Subscriber subscriber = new Subscriber();
subscriber.setCallback("http://localhost:9797/noop"); subscriber.setCallback("http://localhost:9797/noop");
subscriber.setTopic("http://feeds.feedburner.com/screaming-penguin"); subscriber.setTopic("http://feeds.feedburner.com/screaming-penguin");
@ -76,7 +78,7 @@ public abstract class AbstractDAOTest {
@Test @Test
public void testUnsubscribe() throws InterruptedException { public void testUnsubscribe() throws InterruptedException {
final HubDAO instance = get(); 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(); final Subscriber subscriber = new Subscriber();
subscriber.setCallback("http://localhost:9797/noop"); subscriber.setCallback("http://localhost:9797/noop");
subscriber.setTopic("http://feeds.feedburner.com/screaming-penguin"); subscriber.setTopic("http://feeds.feedburner.com/screaming-penguin");