From 621d9e5c413e561293d7484b93882d985b3fe15f Mon Sep 17 00:00:00 2001 From: Endi Sukma Dewata Date: Sat, 24 Mar 2012 02:27:47 -0500 Subject: Removed unnecessary pki folder. Previously the source code was located inside a pki folder. This folder was created during svn migration and is no longer needed. This folder has now been removed and the contents have been moved up one level. Ticket #131 --- .../src/com/netscape/certsrv/util/HttpInput.java | 258 +++++++++++++++++++++ .../com/netscape/certsrv/util/IStatsSubsystem.java | 61 +++++ .../src/com/netscape/certsrv/util/StatsEvent.java | 175 ++++++++++++++ 3 files changed, 494 insertions(+) create mode 100644 base/common/src/com/netscape/certsrv/util/HttpInput.java create mode 100644 base/common/src/com/netscape/certsrv/util/IStatsSubsystem.java create mode 100644 base/common/src/com/netscape/certsrv/util/StatsEvent.java (limited to 'base/common/src/com/netscape/certsrv/util') diff --git a/base/common/src/com/netscape/certsrv/util/HttpInput.java b/base/common/src/com/netscape/certsrv/util/HttpInput.java new file mode 100644 index 000000000..7e7fe7c4a --- /dev/null +++ b/base/common/src/com/netscape/certsrv/util/HttpInput.java @@ -0,0 +1,258 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package com.netscape.certsrv.util; + +import java.io.IOException; +import java.net.URL; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.servlet.http.HttpServletRequest; + +import netscape.ldap.LDAPDN; + +public class HttpInput { + public static int getPortNumberInInt(HttpServletRequest request, String name) + throws IOException { + String val = request.getParameter(name); + int p = Integer.parseInt(val); + return p; + } + + public static String getBoolean(HttpServletRequest request, String name) + throws IOException { + String val = request.getParameter(name); + if (val.equals("true") || val.equals("false")) { + return val; + } + throw new IOException("Invalid boolean value '" + val + "'"); + } + + public static String getCheckbox(HttpServletRequest request, String name) + throws IOException { + String val = request.getParameter(name); + if (val == null || val.equals("")) { + return "off"; + } else if (val.equals("on") || val.equals("off")) { + return val; + } + throw new IOException("Invalid checkbox value '" + val + "'"); + } + + public static String getInteger(HttpServletRequest request, String name) + throws IOException { + String val = request.getParameter(name); + int p = 0; + try { + p = Integer.parseInt(val); + } catch (NumberFormatException e) { + throw new IOException("Input '" + val + "' is not an integer"); + } + + if (!val.equals(Integer.toString(p))) { + throw new IOException("Input '" + val + "' is not an integer"); + } + return val; + } + + public static String getInteger(HttpServletRequest request, String name, + int min, int max) throws IOException { + String val = getInteger(request, name); + int p = Integer.parseInt(val); + if (p < min || p > max) { + throw new IOException("Input '" + val + "' is out of range"); + } + return val; + } + + public static String getPortNumber(HttpServletRequest request, String name) + throws IOException { + String v = getInteger(request, name); + return v; + } + + public static String getString(HttpServletRequest request, String name) { + String val = request.getParameter(name); + return val; + } + + public static String getString(HttpServletRequest request, String name, + int minlen, int maxlen) throws IOException { + String val = request.getParameter(name); + if (val.length() < minlen || val.length() > maxlen) { + throw new IOException("String length of '" + val + + "' is out of range"); + } + return val; + } + + public static String getLdapDatabase(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getURL(HttpServletRequest request, String name) + throws IOException { + String v = getString(request, name); + try { + new URL(v); // throw exception on error + } catch (Exception e) { + throw new IOException("Invalid URL " + v); + } + return v; + } + + public static String getUID(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getPassword(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getKeyType(HttpServletRequest request, String name) + throws IOException { + String v = getString(request, name); + if (v.equals("rsa")) { + return v; + } + if (v.equals("ecc")) { + return v; + } + throw new IOException("Invalid key type '" + v + "' not supported."); + } + + public static String getKeySize(HttpServletRequest request, String name) + throws IOException { + String i = getInteger(request, name); + if (i.equals("256") || i.equals("512") || i.equals("1024") || + i.equals("2048") || i.equals("4096")) { + return i; + } + throw new IOException("Invalid key length '" + + i + "'. Currently supported key lengths are 256, 512, 1024, 2048, 4096."); + } + + public static String getKeySize(HttpServletRequest request, String name, String keyType) + throws IOException { + String i = getInteger(request, name); + if (keyType.equals("rsa")) { + if (i.equals("256") || i.equals("512") || i.equals("1024") || + i.equals("2048") || i.equals("4096")) { + return i; + } else { + throw new IOException("Invalid key length '" + + i + "'. Currently supported RSA key lengths are 256, 512, 1024, 2048, 4096."); + } + } + if (keyType.equals("ecc")) { + int p = 0; + try { + p = Integer.parseInt(i); + } catch (NumberFormatException e) { + throw new IOException("Input '" + i + "' is not an integer"); + } + if ((p >= 112) && (p <= 571)) + return i; + else { + throw new IOException( + "Invalid key length '" + i + + "'. Please consult your security officer for a proper length, or take the default value. Here are examples of some commonly used key lengths: 256, 384, 521."); + } + /* + + if (i.equals("256") || i.equals("384") || i.equals("521")) { + return i; + } else { + throw new IOException("Invalid key length '" + i + "'. Currently supported ECC key lengths are 256, 384, 521."); + } + */ + } + throw new IOException("Invalid key type '" + keyType + "'"); + } + + public static String getDN(HttpServletRequest request, String name) + throws IOException { + String v = getString(request, name); + String dn[] = LDAPDN.explodeDN(v, true); + if (dn == null || dn.length <= 0) { + throw new IOException("Invalid DN " + v + " in " + name); + } + return v; + } + + public static String getID(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getName(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getCertRequest(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getCertChain(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getCert(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getNickname(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getHostname(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getTokenName(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getReplicationAgreementName(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getEmail(HttpServletRequest request, String name) + throws IOException { + String v = getString(request, name); + if (v.indexOf('@') == -1) { + throw new IOException("Invalid email " + v); + } + return v; + } + + public static String getDomainName(HttpServletRequest request, String name) { + return getString(request, name); + } + + public static String getSecurityDomainName(HttpServletRequest request, String name) + throws IOException { + String v = getName(request, name); + Pattern p = Pattern.compile("[A-Za-z0-9]+[A-Za-z0-9 -]*"); + Matcher m = p.matcher(v); + if (!m.matches()) { + throw new IOException("Invalid characters found in Security Domain Name " + + v + ". Valid characters are A-Z, a-z, 0-9, dash and space"); + } + return v; + } +} diff --git a/base/common/src/com/netscape/certsrv/util/IStatsSubsystem.java b/base/common/src/com/netscape/certsrv/util/IStatsSubsystem.java new file mode 100644 index 000000000..989d7a4a1 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/util/IStatsSubsystem.java @@ -0,0 +1,61 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package com.netscape.certsrv.util; + +import java.util.Date; + +import com.netscape.certsrv.base.ISubsystem; + +/** + * A class represents a internal subsystem. This subsystem + * can be loaded into cert server kernel to perform + * statistics collection. + *

+ * + * @author thomask + * @version $Revision$, $Date$ + */ +public interface IStatsSubsystem extends ISubsystem { + /** + * Retrieves the start time since startup or + * clearing of statistics. + */ + public Date getStartTime(); + + /** + * Starts timing of a operation. + */ + public void startTiming(String id); + + public void startTiming(String id, boolean main); + + /** + * Stops timing of a operation. + */ + public void endTiming(String id); + + /** + * Resets counters. + */ + public void resetCounters(); + + /** + * Resets all internal counters. + */ + public StatsEvent getMainStatsEvent(); +} diff --git a/base/common/src/com/netscape/certsrv/util/StatsEvent.java b/base/common/src/com/netscape/certsrv/util/StatsEvent.java new file mode 100644 index 000000000..eafd90d05 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/util/StatsEvent.java @@ -0,0 +1,175 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package com.netscape.certsrv.util; + +import java.util.Enumeration; +import java.util.Vector; + +/** + * A statistics transaction. + *

+ * + * @author thomask + * @version $Revision$, $Date$ + */ +public class StatsEvent { + private String mName = null; + private long mMin = -1; + private long mMax = -1; + private long mTimeTaken = 0; + private long mTimeTakenSqSum = 0; + private long mNoOfOperations = 0; + private Vector mSubEvents = new Vector(); + private StatsEvent mParent = null; + + public StatsEvent(StatsEvent parent) { + mParent = parent; + } + + public void setName(String name) { + mName = name; + } + + /** + * Retrieves Transaction name. + */ + public String getName() { + return mName; + } + + public void addSubEvent(StatsEvent st) { + mSubEvents.addElement(st); + } + + /** + * Retrieves a list of sub transaction names. + */ + public Enumeration getSubEventNames() { + Vector names = new Vector(); + Enumeration e = mSubEvents.elements(); + while (e.hasMoreElements()) { + StatsEvent st = e.nextElement(); + names.addElement(st.getName()); + } + return names.elements(); + } + + /** + * Retrieves a sub transaction. + */ + public StatsEvent getSubEvent(String name) { + Enumeration e = mSubEvents.elements(); + while (e.hasMoreElements()) { + StatsEvent st = e.nextElement(); + if (st.getName().equals(name)) { + return st; + } + } + return null; + } + + public void resetCounters() { + mMin = -1; + mMax = -1; + mNoOfOperations = 0; + mTimeTaken = 0; + mTimeTakenSqSum = 0; + Enumeration e = getSubEventNames(); + while (e.hasMoreElements()) { + String n = e.nextElement(); + StatsEvent c = getSubEvent(n); + c.resetCounters(); + } + } + + public long getMax() { + return mMax; + } + + public long getMin() { + return mMin; + } + + public void incNoOfOperations(long c) { + mNoOfOperations += c; + } + + public long getTimeTakenSqSum() { + return mTimeTakenSqSum; + } + + public long getPercentage() { + if (mParent == null || mParent.getTimeTaken() == 0) { + return 100; + } else { + return (mTimeTaken * 100 / mParent.getTimeTaken()); + } + } + + public long getStdDev() { + if (getNoOfOperations() == 0) { + return 0; + } else { + long a = getTimeTakenSqSum(); + long b = (-2 * getAvg() * getTimeTaken()); + long c = getAvg() * getAvg() * getNoOfOperations(); + return (long) Math.sqrt((a + b + c) / getNoOfOperations()); + } + } + + public long getAvg() { + if (mNoOfOperations == 0) { + return -1; + } else { + return mTimeTaken / mNoOfOperations; + } + } + + /** + * Retrieves number of operations performed. + */ + public long getNoOfOperations() { + return mNoOfOperations; + } + + public void incTimeTaken(long c) { + if (mMin == -1) { + mMin = c; + } else { + if (c < mMin) { + mMin = c; + } + } + if (mMax == -1) { + mMax = c; + } else { + if (c > mMax) { + mMax = c; + } + } + mTimeTaken += c; + mTimeTakenSqSum += (c * c); + } + + /** + * Retrieves total time token in msec. + */ + public long getTimeTaken() { + return mTimeTaken; + } +} -- cgit