diff options
Diffstat (limited to 'base/tps-tomcat/src/org/dogtagpki/tps/token')
3 files changed, 509 insertions, 0 deletions
diff --git a/base/tps-tomcat/src/org/dogtagpki/tps/token/TokenDatabase.java b/base/tps-tomcat/src/org/dogtagpki/tps/token/TokenDatabase.java new file mode 100644 index 000000000..3db76649f --- /dev/null +++ b/base/tps-tomcat/src/org/dogtagpki/tps/token/TokenDatabase.java @@ -0,0 +1,76 @@ +// --- 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) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package org.dogtagpki.tps.token; + +import java.util.Collection; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * This class implements in-memory token database. In the future this + * will be replaced with LDAP database. + * + * @author Endi S. Dewata + */ +public class TokenDatabase { + + public final static int DEFAULT_SIZE = 20; + + Map<String, TokenRecord> tokens = new LinkedHashMap<String, TokenRecord>(); + + public Collection<TokenRecord> getTokens() throws Exception { + return tokens.values(); + } + + public TokenRecord getToken(String tokenID) throws Exception { + if (!tokens.containsKey(tokenID)) { + throw new Exception("Token "+ tokenID + " does not exist."); + } + return tokens.get(tokenID); + } + + public void addToken(TokenRecord tokenRecord) throws Exception { + if (tokens.containsKey(tokenRecord.getID())) { + throw new Exception("Token "+ tokenRecord.getID() + " already exists."); + } + + tokenRecord.setStatus("ENABLED"); + tokenRecord.setCreateTimestamp(new Date()); + + tokens.put(tokenRecord.getID(), tokenRecord); + } + + public void updateToken(String tokenID, TokenRecord tokenRecord) throws Exception { + if (!tokens.containsKey(tokenRecord.getID())) { + throw new Exception("Token "+ tokenRecord.getID() + " does not exist."); + } + + tokenRecord.setModifyTimestamp(new Date()); + + tokens.put(tokenRecord.getID(), tokenRecord); + } + + public void removeToken(String tokenID) throws Exception { + if (!tokens.containsKey(tokenID)) { + throw new Exception("Token "+ tokenID + " does not exist."); + } + tokens.remove(tokenID); + } +} diff --git a/base/tps-tomcat/src/org/dogtagpki/tps/token/TokenRecord.java b/base/tps-tomcat/src/org/dogtagpki/tps/token/TokenRecord.java new file mode 100644 index 000000000..1f9d9caf5 --- /dev/null +++ b/base/tps-tomcat/src/org/dogtagpki/tps/token/TokenRecord.java @@ -0,0 +1,188 @@ +// --- 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) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package org.dogtagpki.tps.token; + +import java.util.Date; + +import com.netscape.certsrv.token.TokenData; + +/** + * @author Endi S. Dewata + */ +public class TokenRecord { + + String id; + String userID; + String status; + String reason; + String appletID; + String keyInfo; + Date createTimestamp; + Date modifyTimestamp; + + public String getID() { + return id; + } + + public void setID(String id) { + this.id = id; + } + + public String getUserID() { + return userID; + } + + public void setUserID(String userID) { + this.userID = userID; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getReason() { + return reason; + } + + public void setReason(String reason) { + this.reason = reason; + } + + public String getAppletID() { + return appletID; + } + + public void setAppletID(String appletID) { + this.appletID = appletID; + } + + public String getKeyInfo() { + return keyInfo; + } + + public void setKeyInfo(String keyInfo) { + this.keyInfo = keyInfo; + } + + public Date getCreateTimestamp() { + return createTimestamp; + } + + public void setCreateTimestamp(Date createTimestamp) { + this.createTimestamp = createTimestamp; + } + + public Date getModifyTimestamp() { + return modifyTimestamp; + } + + public void setModifyTimestamp(Date modifyTimestamp) { + this.modifyTimestamp = modifyTimestamp; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((appletID == null) ? 0 : appletID.hashCode()); + result = prime * result + ((createTimestamp == null) ? 0 : createTimestamp.hashCode()); + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((keyInfo == null) ? 0 : keyInfo.hashCode()); + result = prime * result + ((modifyTimestamp == null) ? 0 : modifyTimestamp.hashCode()); + result = prime * result + ((reason == null) ? 0 : reason.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + result = prime * result + ((userID == null) ? 0 : userID.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TokenRecord other = (TokenRecord) obj; + if (appletID == null) { + if (other.appletID != null) + return false; + } else if (!appletID.equals(other.appletID)) + return false; + if (createTimestamp == null) { + if (other.createTimestamp != null) + return false; + } else if (!createTimestamp.equals(other.createTimestamp)) + return false; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (keyInfo == null) { + if (other.keyInfo != null) + return false; + } else if (!keyInfo.equals(other.keyInfo)) + return false; + if (modifyTimestamp == null) { + if (other.modifyTimestamp != null) + return false; + } else if (!modifyTimestamp.equals(other.modifyTimestamp)) + return false; + if (reason == null) { + if (other.reason != null) + return false; + } else if (!reason.equals(other.reason)) + return false; + if (status == null) { + if (other.status != null) + return false; + } else if (!status.equals(other.status)) + return false; + if (userID == null) { + if (other.userID != null) + return false; + } else if (!userID.equals(other.userID)) + return false; + return true; + } + + public static void main(String args[]) throws Exception { + + TokenData before = new TokenData(); + before.setID("token1"); + before.setUserID("user1"); + before.setStatus("revoked"); + before.setReason("lost"); + before.setAppletID("APPLET1234"); + before.setKeyInfo("key info"); + before.setCreateTimestamp(new Date()); + before.setModifyTimestamp(new Date()); + + String string = before.toString(); + System.out.println(string); + + TokenData after = TokenData.valueOf(string); + System.out.println(before.equals(after)); + } +} diff --git a/base/tps-tomcat/src/org/dogtagpki/tps/token/TokenService.java b/base/tps-tomcat/src/org/dogtagpki/tps/token/TokenService.java new file mode 100644 index 000000000..bc8b35d59 --- /dev/null +++ b/base/tps-tomcat/src/org/dogtagpki/tps/token/TokenService.java @@ -0,0 +1,245 @@ +// --- 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) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package org.dogtagpki.tps.token; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URLEncoder; +import java.util.Iterator; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.dogtagpki.tps.server.TPSSubsystem; +import org.jboss.resteasy.plugins.providers.atom.Link; + +import com.netscape.certsrv.base.PKIException; +import com.netscape.certsrv.token.TokenCollection; +import com.netscape.certsrv.token.TokenData; +import com.netscape.certsrv.token.TokenModifyRequest; +import com.netscape.certsrv.token.TokenResource; +import com.netscape.cms.servlet.base.PKIService; + +/** + * @author Endi S. Dewata + */ +public class TokenService extends PKIService implements TokenResource { + + public final static int DEFAULT_SIZE = 20; + + public TokenService() { + System.out.println("TokenService.<init>()"); + } + + public TokenData createTokenData(TokenRecord tokenRecord) { + + TokenData tokenData = new TokenData(); + tokenData.setID(tokenRecord.getID()); + tokenData.setUserID(tokenRecord.getUserID()); + tokenData.setStatus(tokenRecord.getStatus()); + tokenData.setReason(tokenRecord.getReason()); + tokenData.setAppletID(tokenRecord.getAppletID()); + tokenData.setKeyInfo(tokenRecord.getKeyInfo()); + tokenData.setCreateTimestamp(tokenRecord.getCreateTimestamp()); + tokenData.setModifyTimestamp(tokenRecord.getModifyTimestamp()); + + String tokenID = tokenRecord.getID(); + try { + tokenID = URLEncoder.encode(tokenID, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + + URI uri = uriInfo.getBaseUriBuilder().path(TokenResource.class).path("{tokenID}").build(tokenID); + tokenData.setLink(new Link("self", uri)); + + return tokenData; + } + + public TokenRecord createTokenRecord(TokenData tokenData) { + + TokenRecord tokenRecord = new TokenRecord(); + tokenRecord.setID(tokenData.getID()); + tokenRecord.setUserID(tokenData.getUserID()); + tokenRecord.setStatus(tokenData.getStatus()); + tokenRecord.setReason(tokenData.getReason()); + tokenRecord.setAppletID(tokenData.getAppletID()); + tokenRecord.setKeyInfo(tokenData.getKeyInfo()); + tokenRecord.setCreateTimestamp(tokenData.getCreateTimestamp()); + tokenRecord.setModifyTimestamp(tokenData.getModifyTimestamp()); + + return tokenRecord; + } + + @Override + public TokenCollection findTokens(Integer start, Integer size) { + + System.out.println("TokenService.findTokens()"); + + try { + start = start == null ? 0 : start; + size = size == null ? DEFAULT_SIZE : size; + + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + TokenDatabase database = subsystem.getTokenDatabase(); + + Iterator<TokenRecord> tokens = database.getTokens().iterator(); + + TokenCollection response = new TokenCollection(); + + int i = 0; + + // skip to the start of the page + for ( ; i<start && tokens.hasNext(); i++) tokens.next(); + + // return entries up to the page size + for ( ; i<start+size && tokens.hasNext(); i++) { + response.addToken(createTokenData(tokens.next())); + } + + // count the total entries + for ( ; tokens.hasNext(); i++) tokens.next(); + + if (start > 0) { + URI uri = uriInfo.getRequestUriBuilder().replaceQueryParam("start", Math.max(start-size, 0)).build(); + response.addLink(new Link("prev", uri)); + } + + if (start+size < i) { + URI uri = uriInfo.getRequestUriBuilder().replaceQueryParam("start", start+size).build(); + response.addLink(new Link("next", uri)); + } + + return response; + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public TokenData getToken(String tokenID) { + + System.out.println("TokenService.getToken(\"" + tokenID + "\")"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + TokenDatabase database = subsystem.getTokenDatabase(); + + return createTokenData(database.getToken(tokenID)); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public Response addToken(TokenData tokenData) { + + System.out.println("TokenService.addToken(\"" + tokenData.getID() + "\")"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + TokenDatabase database = subsystem.getTokenDatabase(); + + database.addToken(createTokenRecord(tokenData)); + tokenData = createTokenData(database.getToken(tokenData.getID())); + + return Response + .created(tokenData.getLink().getHref()) + .entity(tokenData) + .type(MediaType.APPLICATION_XML) + .build(); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public Response updateToken(String tokenID, TokenData tokenData) { + + System.out.println("TokenService.updateToken(\"" + tokenID + "\")"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + TokenDatabase database = subsystem.getTokenDatabase(); + + TokenRecord tokenRecord = database.getToken(tokenID); + tokenRecord.setUserID(tokenData.getUserID()); + database.updateToken(tokenData.getID(), tokenRecord); + + tokenData = createTokenData(database.getToken(tokenID)); + + return Response + .ok(tokenData) + .type(MediaType.APPLICATION_XML) + .build(); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public Response modifyToken(String tokenID, TokenModifyRequest request) { + + System.out.println("TokenService.modifyToken(\"" + tokenID + "\", request"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + TokenDatabase database = subsystem.getTokenDatabase(); + + TokenRecord tokenRecord = database.getToken(tokenID); + // TODO: perform modification + + TokenData tokenData = createTokenData(tokenRecord); + + return Response + .ok(tokenData) + .type(MediaType.APPLICATION_XML) + .build(); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public void removeToken(String tokenID) { + + System.out.println("TokenService.removeToken(\"" + tokenID + "\")"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + TokenDatabase database = subsystem.getTokenDatabase(); + database.removeToken(tokenID); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } +} |