1 2 package de.jwi.jgallery.servlets; 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.sql.SQLException ; 7 import java.text.DateFormat ; 8 import java.util.Date ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.Properties ; 12 13 import javax.naming.Context ; 14 import javax.naming.InitialContext ; 15 import javax.naming.NamingException ; 16 import javax.servlet.RequestDispatcher ; 17 import javax.servlet.ServletException ; 18 import javax.servlet.http.HttpServlet ; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import javax.sql.DataSource ; 22 23 import de.jwi.jgallery.db.Statistics; 24 25 43 44 48 public class StatisticsController extends HttpServlet 49 { 50 51 private Statistics statistics; 52 53 private static String version = "unknown"; 54 55 public void init() throws ServletException 56 { 57 String dataSource = (String ) getServletContext().getInitParameter( 58 "dataSource"); 59 60 Context context; 61 try 62 { 63 context = new InitialContext (); 64 if (context == null) 65 { 66 throw new ServletException ("Boom - No Context"); 67 } 68 DataSource ds = (DataSource ) context.lookup(dataSource); 69 70 statistics = new Statistics(ds); 71 } 72 catch (NamingException e) 73 { 74 throw new ServletException (e.getMessage(), e); 75 } 76 77 try 78 { 79 InputStream is = getServletContext().getResourceAsStream( 80 "/WEB-INF/" + Controller.VERSIONCONFIGFILE); 81 82 Properties versionProperties = new Properties (); 83 versionProperties.load(is); 84 85 String s = versionProperties.getProperty("version"); 86 if (null != s) 87 { 88 version = s; 89 } 90 } 91 catch (Exception e) 92 { 93 e.printStackTrace(System.err); } 95 96 } 97 98 public void doGet(HttpServletRequest request, HttpServletResponse response) 99 throws IOException , ServletException 100 { 101 102 String self = null; 103 String contextPath = null; 104 105 contextPath = request.getContextPath(); 106 String servletPath = request.getServletPath(); 107 108 String pathInfo = request.getPathInfo(); 109 110 self = contextPath + servletPath; 111 112 113 if (pathInfo != null 114 && (pathInfo.startsWith("/to/") || pathInfo.startsWith("/to/"))) 115 { 116 doGetDetail(request,response, self); 117 return; 118 } 119 120 121 try 122 { 123 List l = statistics.getStatistics(request, self); 124 request.setAttribute("statistics", l); 125 } 126 catch (SQLException e) 127 { 128 throw new ServletException (e.getMessage(), e); 129 } 130 131 132 request.setAttribute("self", self); 133 134 request.setAttribute("context", contextPath); 135 136 Date d = new Date (); 137 request.setAttribute("date", DateFormat 138 .getDateInstance(DateFormat.FULL).format(d)); 139 request.setAttribute("time", DateFormat.getTimeInstance().format(d)); 140 141 request.setAttribute("version", version); 142 143 request.setAttribute("serverInfo", getServletContext().getServerInfo()); 144 145 String forward = "/WEB-INF/statistics.jsp"; 146 147 RequestDispatcher requestDispatcher = getServletContext() 148 .getRequestDispatcher(forward); 149 150 requestDispatcher.forward(request, response); 151 152 } 153 154 public void doGetDetail(HttpServletRequest request, 155 HttpServletResponse response,String self) throws IOException , ServletException 156 { 157 boolean withThumbs = false; 158 String pathInfo = request.getPathInfo(); 159 withThumbs = pathInfo.startsWith("/tn/"); 160 161 String id = pathInfo.substring(4); 162 163 try 164 { 165 Map m = statistics.getFolderStatistics(id, request, self); 166 request.setAttribute("statistics", m.get("images")); 167 request.setAttribute("folder", m.get("folder")); 168 } 169 catch (SQLException e) 170 { 171 throw new ServletException (e.getMessage(), e); 172 } 173 174 Date d = new Date (); 175 request.setAttribute("date", DateFormat 176 .getDateInstance(DateFormat.FULL).format(d)); 177 request.setAttribute("time", DateFormat.getTimeInstance().format(d)); 178 179 request.setAttribute("self", self); 180 181 request.setAttribute("context", request.getContextPath()); 182 183 String forward = "/WEB-INF/statisticsdet.jsp"; 184 185 RequestDispatcher requestDispatcher = getServletContext() 186 .getRequestDispatcher(forward); 187 188 requestDispatcher.forward(request, response); 189 190 } 191 } | Popular Tags |