Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMengxin Zhu2012-01-16 22:30:54 -0500
committerMengxin Zhu2012-01-16 22:30:54 -0500
commit4529de7e84cc27de8774adf3ab8302cacbe7eb25 (patch)
tree44fefb7a24a8129da40cfbf259a7a674d9a21b3a /bundles/org.eclipse.equinox.p2.testserver
parent04549b1dd45c808514d61d55185d55a7a8b7fb63 (diff)
downloadrt.equinox.p2-4529de7e84cc27de8774adf3ab8302cacbe7eb25.tar.gz
rt.equinox.p2-4529de7e84cc27de8774adf3ab8302cacbe7eb25.tar.xz
rt.equinox.p2-4529de7e84cc27de8774adf3ab8302cacbe7eb25.zip
364929 support customizable download statisticsv20120117-1140v20120117-0330
Signed-off-by: Mengxin Zhu <kane.zhu@windriver.com>
Diffstat (limited to 'bundles/org.eclipse.equinox.p2.testserver')
-rw-r--r--bundles/org.eclipse.equinox.p2.testserver/src/org/eclipse/equinox/p2/testserver/Activator.java5
-rw-r--r--bundles/org.eclipse.equinox.p2.testserver/src/org/eclipse/equinox/p2/testserver/servlets/Stats.java113
2 files changed, 117 insertions, 1 deletions
diff --git a/bundles/org.eclipse.equinox.p2.testserver/src/org/eclipse/equinox/p2/testserver/Activator.java b/bundles/org.eclipse.equinox.p2.testserver/src/org/eclipse/equinox/p2/testserver/Activator.java
index 90cbd16ac..2a3b43c2d 100644
--- a/bundles/org.eclipse.equinox.p2.testserver/src/org/eclipse/equinox/p2/testserver/Activator.java
+++ b/bundles/org.eclipse.equinox.p2.testserver/src/org/eclipse/equinox/p2/testserver/Activator.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, Cloudsmith Inc and others.
+ * Copyright (c) 2009, 2012 Cloudsmith Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -20,6 +20,7 @@ import org.eclipse.equinox.p2.testserver.servlets.FileMolester;
import org.eclipse.equinox.p2.testserver.servlets.IntermittentTimeout;
import org.eclipse.equinox.p2.testserver.servlets.LastModifiedLier;
import org.eclipse.equinox.p2.testserver.servlets.Redirector;
+import org.eclipse.equinox.p2.testserver.servlets.Stats;
import org.eclipse.equinox.p2.testserver.servlets.StatusCodeResponse;
import org.eclipse.equinox.p2.testserver.servlets.TimeOut;
import org.eclipse.equinox.p2.testserver.servlets.Truncator;
@@ -88,6 +89,7 @@ public class Activator implements BundleActivator, ServiceTrackerCustomizer {
httpService.registerServlet("/proxy3/aprivate", new BasicResourceDelivery("/proxy3/aprivate", URI.create(SITE2)), null, artifactSecuredHttpContext); //$NON-NLS-1$//$NON-NLS-2$
httpService.registerServlet("/proxy4/aprivate", new BasicResourceDelivery("/proxy4/aprivate", URI.create(SITE3)), null, artifactSecuredHttpContext); //$NON-NLS-1$//$NON-NLS-2$
+ httpService.registerServlet("/stats", new Stats(), null, null); //$NON-NLS-1$
} catch (NamespaceException e) {
// TODO Auto-generated catch block
@@ -147,6 +149,7 @@ public class Activator implements BundleActivator, ServiceTrackerCustomizer {
httpService = (HttpService) service;
httpService.unregister("/public"); //$NON-NLS-1$
httpService.unregister("/private"); //$NON-NLS-1$
+ httpService.unregister("/stats"); //$NON-NLS-1$
}
}
diff --git a/bundles/org.eclipse.equinox.p2.testserver/src/org/eclipse/equinox/p2/testserver/servlets/Stats.java b/bundles/org.eclipse.equinox.p2.testserver/src/org/eclipse/equinox/p2/testserver/servlets/Stats.java
new file mode 100644
index 000000000..5d8f47316
--- /dev/null
+++ b/bundles/org.eclipse.equinox.p2.testserver/src/org/eclipse/equinox/p2/testserver/servlets/Stats.java
@@ -0,0 +1,113 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Wind River and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Wind River - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.equinox.p2.testserver.servlets;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Map;
+import java.util.TimeZone;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.eclipse.equinox.p2.testserver.HttpConstants;
+
+public class Stats extends HttpServlet {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -3209768955270841029L;
+ private static final String UnkownPackage = "unkown"; //$NON-NLS-1$
+ private static final String PACKAGE = "package"; //$NON-NLS-1$
+ private Map downloadStats = new HashMap();
+
+ public Stats() {
+ downloadStats.put(UnkownPackage, new Integer(0));
+ }
+
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+ responseHeaderAndStatus(response);
+
+ final String result = getStatsResult();
+ response.setContentLength(result.length());
+ response.setContentType("text/plain"); //$NON-NLS-1$
+
+ BufferedReader reader = new BufferedReader(new StringReader(result));
+ PrintWriter writer = response.getWriter();
+ try {
+ writer.flush(); /* write the headers and unbuffer the output */
+ char buffer[] = new char[4096];
+ int read;
+ while ((read = reader.read(buffer, 0, buffer.length)) != -1) {
+ writer.write(buffer, 0, read);
+ }
+ } finally {
+ reader.close();
+ writer.close();
+ }
+ }
+
+ private void responseHeaderAndStatus(HttpServletResponse response) {
+ // set when the resource was modified
+ DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); //$NON-NLS-1$
+ // must always be GMT
+ df.setTimeZone(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$
+ response.setHeader(HttpConstants.LAST_MODIFIED, df.format(new Date()));
+ response.setStatus(HttpServletResponse.SC_OK);
+ }
+
+ private String getStatsResult() {
+ StringBuffer sb = new StringBuffer();
+ sb.append("It's a page to count the downloading times when heading this page with query 'package=<package name>'.\n"); //$NON-NLS-1$
+ for (Iterator iter = downloadStats.keySet().iterator(); iter.hasNext();) {
+ String packageName = (String) iter.next();
+ sb.append(packageName).append(" download number: ").append(downloadStats.get(packageName)).append("\n"); //$NON-NLS-1$//$NON-NLS-2$
+ }
+ return sb.toString();
+ }
+
+ protected void doHead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ String queryString = request.getQueryString();
+
+ responseHeaderAndStatus(response);
+
+ boolean found = false;
+ if (queryString != null) {
+ String[] parameters = queryString.split("&"); //$NON-NLS-1$
+ for (int i = 0; i < parameters.length; i++) {
+ String[] paraPair = parameters[i].split("=", 2); //$NON-NLS-1$
+ if (paraPair.length == 2 && PACKAGE.equals(paraPair[0])) {
+ found = true;
+ Integer count = (Integer) downloadStats.get(paraPair[1]);
+ if (count == null) {
+ count = new Integer(1);
+ } else
+ count = new Integer(1 + count.intValue());
+ downloadStats.put(paraPair[1], count);
+ break;
+ }
+ }
+ }
+ if (!found) {
+ Integer count = (Integer) downloadStats.get(UnkownPackage);
+ downloadStats.put(UnkownPackage, new Integer(1 + count.intValue()));
+ }
+ }
+}

Back to the top