From d6fb4288688294d2f76325f14410db3091fea0fa Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 11 Jul 2016 12:25:03 +0300 Subject: logging --- .../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 +++-- 6 files changed, 71 insertions(+), 46 deletions(-) (limited to 'juick-www/src/main/java/com/juick/www') 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; } } -- cgit v1.2.3