summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/servlet/key
diff options
context:
space:
mode:
authorAde Lee <alee@redhat.com>2012-01-12 12:24:24 -0500
committerAde Lee <alee@redhat.com>2012-01-16 13:04:26 -0500
commit8ff3cae8509bf5527d49166f818776cfc618555c (patch)
treea7cf488310c08e68daa529f4f016dbae2592d4b8 /pki/base/common/src/com/netscape/cms/servlet/key
parent0491736b2570447391835bc2e5282d809f0de4f1 (diff)
downloadpki-8ff3cae8509bf5527d49166f818776cfc618555c.tar.gz
pki-8ff3cae8509bf5527d49166f818776cfc618555c.tar.xz
pki-8ff3cae8509bf5527d49166f818776cfc618555c.zip
Enhanced new REST search interface for keys and key requests
Defined parameters that can be searched for in key and keyrequest searches. Searches for KeyRequests and Keys will perform VLV searches if those searches are defined. The results will include links to next and previous pages in the results. Also added maxTime and maxResults parameters for regular searches. These will be operational unless they exceed server defined limits - which are enforced at the repo level. Modified link URL from "link" to "Link"
Diffstat (limited to 'pki/base/common/src/com/netscape/cms/servlet/key')
-rw-r--r--pki/base/common/src/com/netscape/cms/servlet/key/KeysResource.java55
-rw-r--r--pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDAO.java32
-rw-r--r--pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDataInfos.java87
3 files changed, 153 insertions, 21 deletions
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/KeysResource.java b/pki/base/common/src/com/netscape/cms/servlet/key/KeysResource.java
index 98f12ae5a..4cf5db3f4 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/KeysResource.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/KeysResource.java
@@ -20,21 +20,22 @@
*/
package com.netscape.cms.servlet.key;
+import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
-import java.util.List;
-
+import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.cms.servlet.base.CMSResource;
import com.netscape.cms.servlet.key.model.KeyDAO;
-import com.netscape.cms.servlet.key.model.KeyDataInfo;
+import com.netscape.cms.servlet.key.model.KeyDataInfos;
/**
* @author alee
@@ -43,6 +44,9 @@ import com.netscape.cms.servlet.key.model.KeyDataInfo;
@Path("/keys")
public class KeysResource extends CMSResource {
+ private static final String DEFAULT_MAXTIME = "10";
+ private static final String DEFAULT_MAXRESULTS = "100";
+
@Context
UriInfo uriInfo;
@@ -51,20 +55,51 @@ public class KeysResource extends CMSResource {
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
- public List<KeyDataInfo> listKeys() {
+ public KeyDataInfos listKeys(@QueryParam("clientID") String clientID,
+ @QueryParam("status") String status,
+ @DefaultValue(DEFAULT_MAXRESULTS) @QueryParam("maxResults") int maxResults,
+ @DefaultValue(DEFAULT_MAXTIME) @QueryParam("maxTime") int maxTime) {
// auth and authz
- // parse search parameters from uriInfo and create search filter
- // String clientID = uriInfo.getQueryParameters().getFirst(CLIENT_ID);
- String filter = "objectClass=keyRecord";
+
+ // get ldap filter
+ String filter = createSearchFilter(status, clientID);
+ CMS.debug("listKeys: filter is " + filter);
+
KeyDAO dao = new KeyDAO();
- List<KeyDataInfo> info;
+ KeyDataInfos infos;
try {
- info = dao.listKeys(filter, uriInfo);
+ infos = dao.listKeys(filter, maxResults, maxTime, uriInfo);
} catch (EBaseException e) {
e.printStackTrace();
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
- return info;
+ return infos;
+ }
+
+ private String createSearchFilter(String status, String clientID) {
+ String filter = "";
+ int matches = 0;
+
+ if ((status == null) && (clientID == null)) {
+ filter = "(serialno=*)";
+ return filter;
+ }
+
+ if (status != null) {
+ filter += "(status=" + status + ")";
+ matches ++;
+ }
+
+ if (clientID != null) {
+ filter += "(clientID=" + clientID + ")";
+ matches ++;
+ }
+
+ if (matches > 1) {
+ filter = "(&" + filter + ")";
+ }
+
+ return filter;
}
}
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDAO.java b/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDAO.java
index b7a2d8ed1..5fd17a333 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDAO.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDAO.java
@@ -38,8 +38,6 @@ import com.netscape.cms.servlet.request.model.RecoveryRequestData;
public class KeyDAO {
private IKeyRepository repo;
- private int maxSize = 100;
- private int maxTime = 20;
public KeyDAO() {
IKeyRecoveryAuthority kra = null;
@@ -47,25 +45,37 @@ public class KeyDAO {
repo = kra.getKeyRepository();
}
/**
- * This will find the keys in the database matching the specified search parameters
- * Needs input validation and probably paging, maybe using the vlv functions
- * @throws EBaseException
+ * Returns list of keys meeting specified search filter.
+ * Currently, vlv searches are not used for keys.
+ *
+ * @param filter
+ * @param maxResults
+ * @param maxTime
+ * @param uriInfo
+ * @return
+ * @throws EBaseException
*/
- public List<KeyDataInfo> listKeys(String filter, UriInfo uriInfo) throws EBaseException {
- List <KeyDataInfo> list = new ArrayList<KeyDataInfo>();
+ public KeyDataInfos listKeys(String filter, int maxResults, int maxTime, UriInfo uriInfo)
+ throws EBaseException {
+ List <KeyDataInfo> list = new ArrayList<KeyDataInfo>();
Enumeration<IKeyRecord> e = null;
- e = repo.searchKeys(filter, maxSize, maxTime);
-
+ e = repo.searchKeys(filter, maxResults, maxTime);
if (e == null) {
throw new EBaseException("search results are null");
}
while (e.hasMoreElements()) {
IKeyRecord rec = e.nextElement();
- list.add(createKeyDataInfo(rec, uriInfo));
+ if (rec != null) {
+ list.add(createKeyDataInfo(rec, uriInfo));
+ }
}
- return list;
+
+ KeyDataInfos ret = new KeyDataInfos();
+ ret.setKeyInfos(list);
+
+ return ret;
}
public KeyData getKey(String keyId, RecoveryRequestData data) throws EBaseException {
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDataInfos.java b/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDataInfos.java
new file mode 100644
index 000000000..b01184708
--- /dev/null
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDataInfos.java
@@ -0,0 +1,87 @@
+// --- 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) 2012 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.cms.servlet.key.model;
+
+import java.util.Collection;
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlElementRef;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
+
+import com.netscape.cms.servlet.base.model.Link;
+
+@XmlRootElement(name = "SecurityDataInfos")
+public class KeyDataInfos {
+
+ protected Collection<KeyDataInfo> keyInfos;
+ protected List<Link> links;
+
+ /**
+ * @return the keyInfos
+ */
+ @XmlElementRef
+ public Collection<KeyDataInfo> getKeyInfos() {
+ return keyInfos;
+ }
+ /**
+ * @param keyInfos the keyInfos to set
+ */
+ public void setKeyInfos(Collection<KeyDataInfo> keyInfos) {
+ this.keyInfos = keyInfos;
+ }
+ /**
+ * @return the links
+ */
+ @XmlElementRef
+ public List<Link> getLinks() {
+ return links;
+ }
+ /**
+ * @param links the links to set
+ */
+ public void setLinks(List<Link> links) {
+ this.links = links;
+ }
+
+ @XmlTransient
+ public String getNext() {
+ if (links == null) {
+ return null;
+ }
+ for (Link link : links) {
+ if ("next".equals(link.getRelationship())) {
+ return link.getHref();
+ }
+ }
+ return null;
+ }
+
+ @XmlTransient
+ public String getPrevious() {
+ if (links == null) {
+ return null;
+ }
+ for (Link link : links) {
+ if ("previous".equals(link.getRelationship())) {
+ return link.getHref();
+ }
+ }
+ return null;
+ }
+}