From d6fb4288688294d2f76325f14410db3091fea0fa Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 11 Jul 2016 12:25:03 +0300 Subject: logging --- juick-api/src/main/java/com/juick/api/Main.java | 8 +-- .../src/main/java/com/juick/api/Messages.java | 6 +- .../src/main/java/com/juick/www/FacebookLogin.java | 2 +- juick-www/src/main/java/com/juick/www/Main.java | 77 +++++++++++++--------- .../src/main/java/com/juick/www/NewMessage.java | 12 ++-- .../src/main/java/com/juick/www/PageTemplates.java | 5 +- juick-www/src/main/java/com/juick/www/Utils.java | 6 +- .../main/java/com/juick/www/VKontakteLogin.java | 15 +++-- .../main/java/com/juick/xmpp/s2s/Connection.java | 8 +-- .../java/com/juick/xmpp/s2s/ConnectionOut.java | 26 ++++---- .../src/main/java/ru/sape/SapeConnection.java | 17 +++-- 11 files changed, 105 insertions(+), 77 deletions(-) diff --git a/juick-api/src/main/java/com/juick/api/Main.java b/juick-api/src/main/java/com/juick/api/Main.java index afd7c11c..03c97435 100644 --- a/juick-api/src/main/java/com/juick/api/Main.java +++ b/juick-api/src/main/java/com/juick/api/Main.java @@ -253,7 +253,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { try { attachmentFName = Utils.receiveMultiPartFile(request, "attach"); } catch (Exception e) { - System.out.println("MULTIPART ERROR: " + e.toString()); + log("MULTIPART ERROR", e); response.sendError(400); return; } @@ -264,7 +264,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { URL imgUrl = new URL(paramImg); attachmentFName = Utils.downloadImage(imgUrl); } catch (Exception e) { - System.out.println("DOWNLOAD ERROR: " + e.toString()); + log("DOWNLOAD ERROR", e); response.sendError(500); return; } @@ -369,7 +369,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { try { attachmentFName = Utils.receiveMultiPartFile(request, "attach"); } catch (Exception e) { - System.out.println("MULTIPART ERROR: " + e.toString()); + log("MULTIPART ERROR", e); response.sendError(400); return; } @@ -379,7 +379,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { try { attachmentFName = Utils.downloadImage(new URL(paramImg)); } catch (Exception e) { - System.out.println("DOWNLOAD ERROR: " + e.toString()); + log("DOWNLOAD ERROR", e); response.sendError(500); return; } diff --git a/juick-api/src/main/java/com/juick/api/Messages.java b/juick-api/src/main/java/com/juick/api/Messages.java index b7131c31..ad8d82e0 100644 --- a/juick-api/src/main/java/com/juick/api/Messages.java +++ b/juick-api/src/main/java/com/juick/api/Messages.java @@ -18,6 +18,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -25,6 +27,8 @@ import java.util.List; */ public class Messages { + private static final Logger logger = Logger.getLogger(Messages.class.getName()); + JdbcTemplate sql; MessageSerializer messageSerializer = new MessageSerializer(); @@ -178,7 +182,7 @@ public class Messages { throw new Exception("Message not found"); } } catch (Exception e) { - System.err.println("SETPOPULAR ERROR: " + e.toString()); + logger.log(Level.SEVERE, "SETPOPULAR ERROR", e); } } diff --git a/juick-www/src/main/java/com/juick/www/FacebookLogin.java b/juick-www/src/main/java/com/juick/www/FacebookLogin.java index 22b081a5..a448ea27 100644 --- a/juick-www/src/main/java/com/juick/www/FacebookLogin.java +++ b/juick-www/src/main/java/com/juick/www/FacebookLogin.java @@ -83,7 +83,7 @@ public class FacebookLogin { String graph = Utils.fetchURL("https://graph.facebook.com/me?access_token=" + token); if (graph == null || graph.isEmpty()) { - System.err.println("FACEBOOK GRAPH ERROR"); + logger.log(Level.SEVERE, "FACEBOOK GRAPH ERROR"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } diff --git a/juick-www/src/main/java/com/juick/www/Main.java b/juick-www/src/main/java/com/juick/www/Main.java index e69d3c02..c39c152c 100644 --- a/juick-www/src/main/java/com/juick/www/Main.java +++ b/juick-www/src/main/java/com/juick/www/Main.java @@ -269,42 +269,55 @@ public class Main extends HttpServlet implements Stream.StreamListener { } String uri = request.getRequestURI(); - if (uri.equals("/post")) { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUID() > 0 && !visitor.Banned) { - pagesNewMessage.doPostMessage(sql, request, response, xmpp, visitor); - } else { - response.sendError(403); + switch (uri) { + case "/post": { + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + if (visitor.getUID() > 0 && !visitor.Banned) { + pagesNewMessage.doPostMessage(sql, request, response, xmpp, visitor); + } else { + response.sendError(403); + } + break; } - } else if (uri.equals("/comment")) { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUID() > 0 && !visitor.Banned) { - pagesNewMessage.doPostComment(sql, request, response, xmpp, visitor); - } else { - response.sendError(403); + case "/comment": { + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + if (visitor.getUID() > 0 && !visitor.Banned) { + pagesNewMessage.doPostComment(sql, request, response, xmpp, visitor); + } else { + response.sendError(403); + } + break; } - } else if (uri.equals("/like")) { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUID() > 0 && !visitor.Banned) { - pagesNewMessage.doPostRecomm(sql, request, response, xmpp, visitor); - } else { - response.sendError(403); + case "/like": { + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + if (visitor.getUID() > 0 && !visitor.Banned) { + pagesNewMessage.doPostRecomm(sql, request, response, xmpp, visitor); + } else { + response.sendError(403); + } + break; } - } else if (uri.equals("/pm/send")) { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUID() > 0 && !visitor.Banned) { - pm.doPostPM(sql, request, response, xmpp, visitor); - } else { - response.sendError(403); + case "/pm/send": { + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + if (visitor.getUID() > 0 && !visitor.Banned) { + pm.doPostPM(sql, request, response, xmpp, visitor); + } else { + response.sendError(403); + } + break; } - } else if (uri.equals("/login")) { - login.doPostLogin(sql, request, response); - } else if (uri.equals("/signup")) { - signup.doPost(sql, request, response); - } else if (uri.equals("/settings")) { - settings.doPost(sql, request, response); - } else { - response.sendError(405); + case "/login": + login.doPostLogin(sql, request, response); + break; + case "/signup": + signup.doPost(sql, request, response); + break; + case "/settings": + settings.doPost(sql, request, response); + break; + default: + response.sendError(405); + break; } } } diff --git a/juick-www/src/main/java/com/juick/www/NewMessage.java b/juick-www/src/main/java/com/juick/www/NewMessage.java index dc1b25f7..397badbb 100644 --- a/juick-www/src/main/java/com/juick/www/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/NewMessage.java @@ -38,6 +38,8 @@ import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -45,6 +47,8 @@ import java.util.List; */ public class NewMessage { + private static final Logger logger = Logger.getLogger(NewMessage.class.getName()); + protected void doGetNewMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { @@ -159,7 +163,7 @@ public class NewMessage { try { attachmentFName = Utils.receiveMultiPartFile(request, "attach"); } catch (Exception e) { - System.out.println("MULTIPART ERROR: " + e.toString()); + logger.log(Level.SEVERE, "MULTIPART ERROR", e); response.sendError(400); return; } @@ -170,7 +174,7 @@ public class NewMessage { URL imgUrl = new URL(paramImg); attachmentFName = Utils.downloadImage(imgUrl); } catch (Exception e) { - System.out.println("DOWNLOAD ERROR: " + e.toString()); + logger.log(Level.SEVERE, "DOWNLOAD ERROR", e); response.sendError(500); return; } @@ -314,7 +318,7 @@ public class NewMessage { try { attachmentFName = Utils.receiveMultiPartFile(request, "attach"); } catch (Exception e) { - System.out.println("MULTIPART ERROR: " + e.toString()); + logger.log(Level.SEVERE, "MULTIPART ERROR", e); response.sendError(400); return; } @@ -324,7 +328,7 @@ public class NewMessage { try { attachmentFName = Utils.downloadImage(new URL(paramImg)); } catch (Exception e) { - System.out.println("DOWNLOAD ERROR: " + e.toString()); + logger.log(Level.SEVERE, "DOWNLOAD ERROR", e); response.sendError(500); return; } diff --git a/juick-www/src/main/java/com/juick/www/PageTemplates.java b/juick-www/src/main/java/com/juick/www/PageTemplates.java index b7883a79..0bb17ff0 100644 --- a/juick-www/src/main/java/com/juick/www/PageTemplates.java +++ b/juick-www/src/main/java/com/juick/www/PageTemplates.java @@ -35,6 +35,8 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -45,6 +47,7 @@ import java.util.stream.Collectors; */ public class PageTemplates { + private static final Logger logger = Logger.getLogger(PageTemplates.class.getName()); public static Sape sape = null; protected static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SimpleDateFormat sdfSimple = new SimpleDateFormat("d MMM"); @@ -273,7 +276,7 @@ public class PageTemplates { ret = sdfFull.format(fulldate); } } catch (Exception e) { - System.err.println("PARSE EXCEPTION: " + fulldate); + logger.log(Level.SEVERE, "PARSE EXCEPTION: " + fulldate); } } return ret; diff --git a/juick-www/src/main/java/com/juick/www/Utils.java b/juick-www/src/main/java/com/juick/www/Utils.java index ccb7cdd2..240b6be4 100644 --- a/juick-www/src/main/java/com/juick/www/Utils.java +++ b/juick-www/src/main/java/com/juick/www/Utils.java @@ -28,6 +28,8 @@ import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -35,6 +37,8 @@ import java.util.UUID; */ public class Utils { + private static final Logger logger = Logger.getLogger(Utils.class.getName()); + public static String getCookie(HttpServletRequest request, String name) { Cookie cookies[] = request.getCookies(); if (cookies != null) { @@ -157,7 +161,7 @@ public class Utils { in.close(); return b.toString(); } catch (Exception e) { - System.err.println("fetchURL: "+e.toString()); + logger.log(Level.SEVERE, "fetchURL" , e); return null; } } diff --git a/juick-www/src/main/java/com/juick/www/VKontakteLogin.java b/juick-www/src/main/java/com/juick/www/VKontakteLogin.java index 509187a9..7d718c9b 100644 --- a/juick-www/src/main/java/com/juick/www/VKontakteLogin.java +++ b/juick-www/src/main/java/com/juick/www/VKontakteLogin.java @@ -30,13 +30,15 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; /** * * @author Ugnich Anton */ public class VKontakteLogin { - + private static final Logger logger = Logger.getLogger(VKontakteLogin.class.getName()); private static final String VK_APPID = "3544101"; private static final String VK_SECRET = "z2afNI8jA5lIpZ2jsTm1"; private static final String VK_REDIRECT = "http://juick.com/_vklogin"; @@ -52,7 +54,7 @@ public class VKontakteLogin { String tokenjson = Utils.fetchURL("https://oauth.vk.com/access_token?client_id=" + VK_APPID + "&redirect_uri=" + URLEncoder.encode(VK_REDIRECT, "utf-8") + "&client_secret=" + VK_SECRET + "&code=" + URLEncoder.encode(code, "utf-8")); if (tokenjson == null || tokenjson.isEmpty()) { - System.err.println("VK TOKEN EMPTY"); + logger.log(Level.SEVERE, "VK TOKEN EMPTY"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } @@ -63,12 +65,12 @@ public class VKontakteLogin { token = json.getString("access_token"); vkID = json.getLong("user_id"); } catch (JSONException e) { - System.err.println("VK TOKEN EXCEPTION: " + e); + logger.log(Level.SEVERE, "VK TOKEN EXCEPTION: ", e); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } if (token == null || vkID == 0) { - System.err.println("VK TOKEN EMPTY: " + tokenjson); + logger.log(Level.SEVERE, "VK TOKEN EMPTY: " + tokenjson); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } @@ -77,7 +79,7 @@ public class VKontakteLogin { String graph = Utils.fetchURL("https://api.vk.com/method/users.get?uids=" + vkID + "&fields=screen_name&access_token=" + token); if (graph == null || graph.isEmpty()) { - System.err.println("VK GRAPH ERROR"); + logger.log(Level.SEVERE, "VK GRAPH ERROR"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } @@ -107,9 +109,8 @@ public class VKontakteLogin { response.setHeader("Location", "/signup?type=vk&hash=" + loginhash); } } catch (Exception e) { - System.err.println("JSON ERROR: " + e); + logger.log(Level.SEVERE, "JSON ERROR", e); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - return; } } diff --git a/juick-www/src/main/java/com/juick/xmpp/s2s/Connection.java b/juick-www/src/main/java/com/juick/xmpp/s2s/Connection.java index eae6efaa..b7837b06 100644 --- a/juick-www/src/main/java/com/juick/xmpp/s2s/Connection.java +++ b/juick-www/src/main/java/com/juick/xmpp/s2s/Connection.java @@ -23,7 +23,7 @@ import java.util.logging.Logger; */ public class Connection { - protected static final Logger LOGGER = Logger.getLogger(Connection.class.getName()); + protected static final Logger logger = Logger.getLogger(Connection.class.getName()); public String streamID; public long tsCreated = 0; @@ -80,12 +80,12 @@ public class Connection { tag += " " + parser.getAttributeName(i) + "=\"" + parser.getAttributeValue(i) + "\""; } tag += ">...\n"; - LOGGER.fine(tag); + logger.fine(tag); } public void sendStanza(String xml) throws IOException { if (streamID != null) { - LOGGER.fine("OUT: " + xml + "\n"); + logger.fine("OUT: " + xml + "\n"); } writer.write(xml); writer.flush(); @@ -96,7 +96,7 @@ public class Connection { void closeConnection() { if (streamID != null) { - LOGGER.info(String.format("CLOSING STREAM %s", streamID)); + logger.info(String.format("CLOSING STREAM %s", streamID)); } try { diff --git a/juick-www/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java b/juick-www/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java index 2552bfcd..24ed3363 100644 --- a/juick-www/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java +++ b/juick-www/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java @@ -58,7 +58,7 @@ public class ConnectionOut extends Connection implements Runnable { @Override public void run() { - LOGGER.info("STREAM TO " + to + " START"); + logger.info("STREAM TO " + to + " START"); try { socket = new Socket(); socket.connect(DNSQueries.getServerAddress(to)); @@ -72,7 +72,7 @@ public class ConnectionOut extends Connection implements Runnable { throw new Exception("STREAM TO " + to + " INVALID FIRST PACKET"); } - LOGGER.info("STREAM TO " + to + " " + streamID + " OPEN"); + logger.info("STREAM TO " + to + " " + streamID + " OPEN"); xmpp.addConnectionOut(ConnectionOut.this); boolean xmppversionnew = parser.getAttributeValue(null, "version") != null; if (!xmppversionnew) { @@ -90,16 +90,16 @@ public class ConnectionOut extends Connection implements Runnable { String type = parser.getAttributeValue(null, "type"); if (type != null && type.equals("valid")) { streamReady = true; - LOGGER.info("STREAM TO " + to + " " + streamID + " READY"); + logger.info("STREAM TO " + to + " " + streamID + " READY"); String cache = xmpp.getFromCache(to); if (cache != null) { - LOGGER.info("STREAM TO " + to + " " + streamID + " SENDING CACHE"); + logger.info("STREAM TO " + to + " " + streamID + " SENDING CACHE"); sendStanza(cache); } } else { - LOGGER.info("STREAM TO " + to + " " + streamID + " DIALBACK FAIL"); + logger.info("STREAM TO " + to + " " + streamID + " DIALBACK FAIL"); } XmlUtils.skip(parser); } else if (tag.equals("verify") && parser.getNamespace().equals(NS_DB)) { @@ -116,7 +116,7 @@ public class ConnectionOut extends Connection implements Runnable { } else if (tag.equals("features") && parser.getNamespace().equals(NS_STREAM)) { StreamFeatures features = StreamFeatures.parse(parser); if (!isSecured() && features.STARTTLS >= 0 && !xmpp.brokenSSLhosts.contains(to)) { - System.out.println("STREAM TO " + to + " " + streamID + " SECURING"); + logger.info("STREAM TO " + to + " " + streamID + " SECURING"); sendStanza(""); } else { processDialback(); @@ -127,11 +127,11 @@ public class ConnectionOut extends Connection implements Runnable { socket.getPort(), true); ((SSLSocket) socket).startHandshake(); setSecured(true); - System.out.println("STREAM " + streamID + " SECURED"); + logger.info("STREAM " + streamID + " SECURED"); restartParser(); sendOpenStream(); } catch (SSLException sex) { - LOGGER.log(Level.SEVERE, String.format("s2s ssl error: %s %s", to, streamID), sex); + logger.log(Level.SEVERE, String.format("s2s ssl error: %s %s", to, streamID), sex); sendStanza(""); xmpp.removeConnectionOut(this); closeConnection(); @@ -139,19 +139,19 @@ public class ConnectionOut extends Connection implements Runnable { } else if (isSecured() && tag.equals("stream") && parser.getNamespace().equals(NS_STREAM)) { streamID = parser.getAttributeValue(null, "id"); } else { - LOGGER.info("STREAM TO " + to + " " + streamID + ": " + XmlUtils.parseToString(parser, true)); + logger.info("STREAM TO " + to + " " + streamID + ": " + XmlUtils.parseToString(parser, true)); } } - LOGGER.warning("STREAM TO " + to + " " + streamID + " FINISHED"); + logger.warning("STREAM TO " + to + " " + streamID + " FINISHED"); xmpp.removeConnectionOut(ConnectionOut.this); closeConnection(); } catch (EOFException | SocketException eofex) { - LOGGER.info(String.format("STREAM %s %s CLOSED (dirty)", to, streamID)); + logger.info(String.format("STREAM %s %s CLOSED (dirty)", to, streamID)); xmpp.removeConnectionOut(ConnectionOut.this); closeConnection(); } catch (Exception e) { - LOGGER.log(Level.SEVERE, String.format("s2s out exception: %s %s", to, streamID), e); + logger.log(Level.SEVERE, String.format("s2s out exception: %s %s", to, streamID), e); xmpp.removeConnectionOut(ConnectionOut.this); closeConnection(); } @@ -161,7 +161,7 @@ public class ConnectionOut extends Connection implements Runnable { try { sendStanza("" + key + ""); } catch (IOException e) { - LOGGER.log(Level.WARNING, "STREAM TO " + to + " " + streamID + " ERROR", e); + logger.log(Level.WARNING, "STREAM TO " + to + " " + streamID + " ERROR", e); } } } diff --git a/juick-www/src/main/java/ru/sape/SapeConnection.java b/juick-www/src/main/java/ru/sape/SapeConnection.java index 8c794b08..dcaf629f 100644 --- a/juick-www/src/main/java/ru/sape/SapeConnection.java +++ b/juick-www/src/main/java/ru/sape/SapeConnection.java @@ -7,13 +7,12 @@ import java.io.Reader; import java.io.StringWriter; import java.net.HttpURLConnection; import java.net.URL; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; public class SapeConnection { - + private static final Logger logger = Logger.getLogger(SapeConnection.class.getName()); private final String version = "1.0.3"; private final List serverList = Arrays.asList("dispenser-01.sape.ru", "dispenser-02.sape.ru"); private final String dispenserPath; @@ -80,7 +79,7 @@ public class SapeConnection { } if (data.startsWith("FATAL ERROR:")) { - System.err.println("Sape responded with error: " + data); + logger.log(Level.SEVERE, "Sape responded with error: " + data); continue; } @@ -88,7 +87,7 @@ public class SapeConnection { try { cached = (Map) new SerializedPhpParser(data).parse(); } catch (Exception e) { - System.err.println("Can't parse Sape data: " + e); + logger.log(Level.SEVERE, "Can't parse Sape data", e); continue; } @@ -97,9 +96,9 @@ public class SapeConnection { return cached; } - System.err.println("Unable to fetch Sape data"); + logger.log(Level.SEVERE, "Unable to fetch Sape data"); - return new HashMap(); + return Collections.emptyMap(); } return cached; -- cgit v1.2.3