diff options
author | Endi S. Dewata <edewata@redhat.com> | 2013-08-26 13:45:55 -0400 |
---|---|---|
committer | Endi Sukma Dewata <edewata@redhat.com> | 2013-09-01 01:04:57 -0500 |
commit | 15e029e97aea4a23f065e6bc7fbfa16cdae8c02d (patch) | |
tree | f0b9f036f1ccd26ba4202af30e36957fec520a37 | |
parent | a847bcb7c71836f7c0498163e31238f118740339 (diff) | |
download | pki-15e029e97aea4a23f065e6bc7fbfa16cdae8c02d.tar.gz pki-15e029e97aea4a23f065e6bc7fbfa16cdae8c02d.tar.xz pki-15e029e97aea4a23f065e6bc7fbfa16cdae8c02d.zip |
Added TPS connection resource.
A skeleton for TPS connection services and the clients have been added.
The service implementation will be added later.
Ticket #652
22 files changed, 1698 insertions, 10 deletions
diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/tps/TPSClient.java index 94a906cdf..00da49b20 100644 --- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java +++ b/base/common/src/com/netscape/certsrv/tps/TPSClient.java @@ -24,6 +24,7 @@ import com.netscape.certsrv.client.SubsystemClient; import com.netscape.certsrv.group.GroupClient; import com.netscape.certsrv.logging.ActivityClient; import com.netscape.certsrv.tps.cert.TPSCertClient; +import com.netscape.certsrv.tps.connection.ConnectionClient; import com.netscape.certsrv.tps.token.TokenClient; import com.netscape.certsrv.user.UserClient; @@ -39,6 +40,7 @@ public class TPSClient extends SubsystemClient { public void init() throws URISyntaxException { addClient(new ActivityClient(client, name)); + addClient(new ConnectionClient(client, name)); addClient(new GroupClient(client, name)); addClient(new TokenClient(client, name)); addClient(new TPSCertClient(client, name)); diff --git a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionClient.java b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionClient.java new file mode 100644 index 000000000..74ec14616 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionClient.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 com.netscape.certsrv.tps.connection; + +import java.net.URISyntaxException; + +import org.jboss.resteasy.client.ClientResponse; + +import com.netscape.certsrv.client.Client; +import com.netscape.certsrv.client.PKIClient; + +/** + * @author Endi S. Dewata + */ +public class ConnectionClient extends Client { + + public ConnectionResource resource; + + public ConnectionClient(PKIClient client) throws URISyntaxException { + this(client, client.getSubsystem()); + } + + public ConnectionClient(PKIClient client, String subsystem) throws URISyntaxException { + super(client, subsystem, "connection"); + init(); + } + + public void init() throws URISyntaxException { + resource = createProxy(ConnectionResource.class); + } + + public ConnectionCollection findConnections(Integer start, Integer size) { + return resource.findConnections(start, size); + } + + public ConnectionData getConnection(String connectionID) { + return resource.getConnection(connectionID); + } + + public ConnectionData addConnection(ConnectionData connectionData) { + @SuppressWarnings("unchecked") + ClientResponse<ConnectionData> response = (ClientResponse<ConnectionData>)resource.addConnection(connectionData); + return client.getEntity(response); + } + + public ConnectionData updateConnection(String connectionID, ConnectionData connectionData) { + @SuppressWarnings("unchecked") + ClientResponse<ConnectionData> response = (ClientResponse<ConnectionData>)resource.updateConnection(connectionID, connectionData); + return client.getEntity(response); + } + + public ConnectionData modifyConnection(String connectionID, ConnectionModification connectionModification) { + @SuppressWarnings("unchecked") + ClientResponse<ConnectionData> response = (ClientResponse<ConnectionData>)resource.modifyConnection(connectionID, connectionModification); + return client.getEntity(response); + } + + public void removeConnection(String connectionID) { + resource.removeConnection(connectionID); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionCollection.java b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionCollection.java new file mode 100644 index 000000000..b26a7308d --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionCollection.java @@ -0,0 +1,38 @@ +// --- 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 com.netscape.certsrv.tps.connection; + +import java.util.Collection; + +import javax.xml.bind.annotation.XmlElementRef; +import javax.xml.bind.annotation.XmlRootElement; + +import com.netscape.certsrv.base.DataCollection; + +/** + * @author Endi S. Dewata + */ +@XmlRootElement(name="Connections") +public class ConnectionCollection extends DataCollection<ConnectionInfo> { + + @XmlElementRef + public Collection<ConnectionInfo> getEntries() { + return super.getEntries(); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionData.java b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionData.java new file mode 100644 index 000000000..573bb9543 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionData.java @@ -0,0 +1,169 @@ +// --- 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 com.netscape.certsrv.tps.connection; + +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +/** + * @author Endi S. Dewata + */ +@XmlRootElement(name="Connection") +public class ConnectionData { + + public static Marshaller marshaller; + public static Unmarshaller unmarshaller; + + static { + try { + marshaller = JAXBContext.newInstance(ConnectionData.class).createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + unmarshaller = JAXBContext.newInstance(ConnectionData.class).createUnmarshaller(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + String id; + String status; + String contents; + + Link link; + + @XmlAttribute(name="id") + public String getID() { + return id; + } + + public void setID(String id) { + this.id = id; + } + + @XmlElement(name="Status") + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + @XmlElement(name="Contents") + public String getContents() { + return contents; + } + + public void setContents(String contents) { + this.contents = contents; + } + + @XmlElement(name="Link") + public Link getLink() { + return link; + } + + public void setLink(Link link) { + this.link = link; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((contents == null) ? 0 : contents.hashCode()); + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((link == null) ? 0 : link.hashCode()); + result = prime * result + ((status == null) ? 0 : status.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; + ConnectionData other = (ConnectionData) obj; + if (contents == null) { + if (other.contents != null) + return false; + } else if (!contents.equals(other.contents)) + return false; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (link == null) { + if (other.link != null) + return false; + } else if (!link.equals(other.link)) + return false; + if (status == null) { + if (other.status != null) + return false; + } else if (!status.equals(other.status)) + return false; + return true; + } + + public String toString() { + try { + StringWriter sw = new StringWriter(); + marshaller.marshal(this, sw); + return sw.toString(); + + } catch (Exception e) { + return super.toString(); + } + } + + public static ConnectionData valueOf(String string) throws Exception { + try { + return (ConnectionData)unmarshaller.unmarshal(new StringReader(string)); + } catch (Exception e) { + return null; + } + } + + public static void main(String args[]) throws Exception { + + ConnectionData before = new ConnectionData(); + before.setID("connection1"); + before.setStatus("ENABLED"); + before.setContents("name=connection1\nparam=value"); + + String string = before.toString(); + System.out.println(string); + + ConnectionData after = ConnectionData.valueOf(string); + System.out.println(before.equals(after)); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionInfo.java b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionInfo.java new file mode 100644 index 000000000..d9d93517e --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionInfo.java @@ -0,0 +1,152 @@ +// --- 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 com.netscape.certsrv.tps.connection; + +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +/** + * @author Endi S. Dewata + */ +@XmlRootElement(name="Connection") +public class ConnectionInfo { + + public static Marshaller marshaller; + public static Unmarshaller unmarshaller; + + static { + try { + marshaller = JAXBContext.newInstance(ConnectionInfo.class).createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + unmarshaller = JAXBContext.newInstance(ConnectionInfo.class).createUnmarshaller(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + String id; + String status; + + Link link; + + @XmlAttribute(name="id") + public String getID() { + return id; + } + + public void setID(String id) { + this.id = id; + } + + @XmlElement(name="Status") + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + @XmlElement(name="Link") + public Link getLink() { + return link; + } + + public void setLink(Link link) { + this.link = link; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((link == null) ? 0 : link.hashCode()); + result = prime * result + ((status == null) ? 0 : status.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; + ConnectionInfo other = (ConnectionInfo) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (link == null) { + if (other.link != null) + return false; + } else if (!link.equals(other.link)) + return false; + if (status == null) { + if (other.status != null) + return false; + } else if (!status.equals(other.status)) + return false; + return true; + } + + public String toString() { + try { + StringWriter sw = new StringWriter(); + marshaller.marshal(this, sw); + return sw.toString(); + + } catch (Exception e) { + return super.toString(); + } + } + + public static ConnectionInfo valueOf(String string) throws Exception { + try { + return (ConnectionInfo)unmarshaller.unmarshal(new StringReader(string)); + } catch (Exception e) { + return null; + } + } + + public static void main(String args[]) throws Exception { + + ConnectionInfo before = new ConnectionInfo(); + before.setID("connection1"); + before.setStatus("ENABLED"); + + String string = before.toString(); + System.out.println(string); + + ConnectionInfo after = ConnectionInfo.valueOf(string); + System.out.println(before.equals(after)); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionModification.java b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionModification.java new file mode 100644 index 000000000..7e48074b2 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionModification.java @@ -0,0 +1,169 @@ +// --- 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 com.netscape.certsrv.tps.connection; + +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +/** + * @author Endi S. Dewata + */ +@XmlRootElement(name="ConnectionModifyRequest") +public class ConnectionModification { + + public static Marshaller marshaller; + public static Unmarshaller unmarshaller; + + static { + try { + marshaller = JAXBContext.newInstance(ConnectionModification.class).createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + unmarshaller = JAXBContext.newInstance(ConnectionModification.class).createUnmarshaller(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + String id; + String status; + String contents; + + Link link; + + @XmlAttribute(name="id") + public String getID() { + return id; + } + + public void setID(String id) { + this.id = id; + } + + @XmlElement(name="Status") + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + @XmlElement(name="Contents") + public String getContents() { + return contents; + } + + public void setContents(String contents) { + this.contents = contents; + } + + @XmlElement(name="Link") + public Link getLink() { + return link; + } + + public void setLink(Link link) { + this.link = link; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((contents == null) ? 0 : contents.hashCode()); + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((link == null) ? 0 : link.hashCode()); + result = prime * result + ((status == null) ? 0 : status.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; + ConnectionModification other = (ConnectionModification) obj; + if (contents == null) { + if (other.contents != null) + return false; + } else if (!contents.equals(other.contents)) + return false; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (link == null) { + if (other.link != null) + return false; + } else if (!link.equals(other.link)) + return false; + if (status == null) { + if (other.status != null) + return false; + } else if (!status.equals(other.status)) + return false; + return true; + } + + public String toString() { + try { + StringWriter sw = new StringWriter(); + marshaller.marshal(this, sw); + return sw.toString(); + + } catch (Exception e) { + return super.toString(); + } + } + + public static ConnectionModification valueOf(String string) throws Exception { + try { + return (ConnectionModification)unmarshaller.unmarshal(new StringReader(string)); + } catch (Exception e) { + return null; + } + } + + public static void main(String args[]) throws Exception { + + ConnectionModification before = new ConnectionModification(); + before.setID("connection1"); + before.setStatus("ENABLED"); + before.setContents("name=connection1\nparam=value"); + + String string = before.toString(); + System.out.println(string); + + ConnectionModification after = ConnectionModification.valueOf(string); + System.out.println(before.equals(after)); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionResource.java b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionResource.java new file mode 100644 index 000000000..ee971b052 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionResource.java @@ -0,0 +1,80 @@ +// --- 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 com.netscape.certsrv.tps.connection; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.jboss.resteasy.annotations.ClientResponseType; + + +/** + * @author Endi S. Dewata + */ +@Path("connections") +public interface ConnectionResource { + + @GET + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public ConnectionCollection findConnections( + @QueryParam("start") Integer start, + @QueryParam("size") Integer size); + + @GET + @Path("{connectionID}") + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public ConnectionData getConnection(@PathParam("connectionID") String connectionID); + + @POST + @ClientResponseType(entityType=ConnectionData.class) + @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public Response addConnection(ConnectionData connectionData); + + @PUT + @Path("{connectionID}") + @ClientResponseType(entityType=ConnectionData.class) + @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public Response updateConnection( + @PathParam("connectionID") String connectionID, + ConnectionData connectionData); + + @POST + @Path("{connectionID}") + @ClientResponseType(entityType=ConnectionData.class) + @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public Response modifyConnection( + @PathParam("connectionID") String connectionID, + ConnectionModification request); + + @DELETE + @Path("{connectionID}") + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public void removeConnection(@PathParam("connectionID") String connectionID); +} diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java index d1d6132ae..a048ebff3 100644 --- a/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java @@ -83,9 +83,9 @@ public class CertShowCLI extends CLI { String encoded = certData.getEncoded(); if (encoded != null && file != null) { // store cert to file - PrintWriter out = new PrintWriter(new FileWriter(file)); - out.print(encoded); - out.close(); + try (PrintWriter out = new PrintWriter(new FileWriter(file))) { + out.print(encoded); + } } MainCLI.printMessage("Certificate \"" + certID.toHexString() + "\""); diff --git a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java index 2f28ada7d..7a14b59f6 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java @@ -23,6 +23,7 @@ import com.netscape.certsrv.tps.TPSClient; import com.netscape.cmstools.group.GroupCLI; import com.netscape.cmstools.logging.ActivityCLI; import com.netscape.cmstools.tps.cert.TPSCertCLI; +import com.netscape.cmstools.tps.connection.ConnectionCLI; import com.netscape.cmstools.tps.token.TokenCLI; import com.netscape.cmstools.user.UserCLI; @@ -37,6 +38,7 @@ public class TPSCLI extends SubsystemCLI { super("tps", "TPS management commands", mainCLI); addModule(new ActivityCLI(this)); + addModule(new ConnectionCLI(this)); addModule(new GroupCLI(this)); addModule(new TokenCLI(this)); addModule(new TPSCertCLI(this)); diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionAddCLI.java new file mode 100644 index 000000000..bb2beda13 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionAddCLI.java @@ -0,0 +1,107 @@ +// --- 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 com.netscape.cmstools.tps.connection; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.tps.connection.ConnectionData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionAddCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionAddCLI(ConnectionCLI connectionCLI) { + super("add", "Add connection", connectionCLI); + this.connectionCLI = connectionCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Connection ID> [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "status", true, "Status: ENABLED, DISABLED."); + option.setArgName("status"); + option.setRequired(true); + options.addOption(option); + + option = new Option(null, "contents", true, "Input file containing connection attributes."); + option.setArgName("file"); + option.setRequired(true); + options.addOption(option); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 1) { + printHelp(); + System.exit(1); + } + + String connectionID = cmdArgs[0]; + String status = cmd.getOptionValue("status"); + String contents = cmd.getOptionValue("contents"); + + ConnectionData connectionData = new ConnectionData(); + connectionData.setID(connectionID); + connectionData.setStatus(status); + + if (contents != null) { + try (BufferedReader in = new BufferedReader(new FileReader(contents)); + StringWriter sw = new StringWriter(); + PrintWriter out = new PrintWriter(sw, true)) { + + String line; + while ((line = in.readLine()) != null) { + out.println(line); + } + + connectionData.setContents(sw.toString()); + } + } + + connectionData = connectionCLI.connectionClient.addConnection(connectionData); + + MainCLI.printMessage("Added connection \"" + connectionID + "\""); + + ConnectionCLI.printConnectionData(connectionData); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionCLI.java new file mode 100644 index 000000000..5371dbe9d --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionCLI.java @@ -0,0 +1,108 @@ +// --- 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 com.netscape.cmstools.tps.connection; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.Arrays; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +import com.netscape.certsrv.tps.connection.ConnectionClient; +import com.netscape.certsrv.tps.connection.ConnectionData; +import com.netscape.certsrv.tps.connection.ConnectionInfo; +import com.netscape.cmstools.cli.CLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionCLI extends CLI { + + public ConnectionClient connectionClient; + + public ConnectionCLI(CLI parent) { + super("connection", "Connection management commands", parent); + + addModule(new ConnectionAddCLI(this)); + addModule(new ConnectionFindCLI(this)); + addModule(new ConnectionModifyCLI(this)); + addModule(new ConnectionRemoveCLI(this)); + addModule(new ConnectionShowCLI(this)); + } + + public void execute(String[] args) throws Exception { + + client = parent.getClient(); + connectionClient = (ConnectionClient)parent.getClient("connection"); + + if (args.length == 0) { + printHelp(); + System.exit(1); + } + + String command = args[0]; + String[] commandArgs = Arrays.copyOfRange(args, 1, args.length); + + if (command == null) { + printHelp(); + System.exit(1); + } + + CLI module = getModule(command); + if (module != null) { + module.execute(commandArgs); + + } else { + System.err.println("Error: Invalid command \"" + command + "\""); + printHelp(); + System.exit(1); + } + } + + public static void printConnectionInfo(ConnectionInfo connectionInfo) { + System.out.println(" Connection ID: " + connectionInfo.getID()); + if (connectionInfo.getStatus() != null) System.out.println(" Status: " + connectionInfo.getStatus()); + + Link link = connectionInfo.getLink(); + if (verbose && link != null) { + System.out.println(" Link: " + link.getHref()); + } + } + + public static void printConnectionData(ConnectionData connectionData) throws IOException { + System.out.println(" Connection ID: " + connectionData.getID()); + if (connectionData.getStatus() != null) System.out.println(" Status: " + connectionData.getStatus()); + + System.out.println(" Contents:"); + String contents = connectionData.getContents(); + if (contents != null) { + BufferedReader in = new BufferedReader(new StringReader(contents)); + String line; + while ((line = in.readLine()) != null) { + System.out.println(" " + line); + } + } + + Link link = connectionData.getLink(); + if (verbose && link != null) { + System.out.println(" Link: " + link.getHref()); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionFindCLI.java new file mode 100644 index 000000000..9218edee4 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionFindCLI.java @@ -0,0 +1,94 @@ +// --- 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 com.netscape.cmstools.tps.connection; + +import java.util.Collection; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.tps.connection.ConnectionCollection; +import com.netscape.certsrv.tps.connection.ConnectionInfo; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionFindCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionFindCLI(ConnectionCLI tokenCLI) { + super("find", "Find connections", tokenCLI); + this.connectionCLI = tokenCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "start", true, "Page start"); + option.setArgName("start"); + options.addOption(option); + + option = new Option(null, "size", true, "Page size"); + option.setArgName("size"); + options.addOption(option); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String s = cmd.getOptionValue("start"); + Integer start = s == null ? null : Integer.valueOf(s); + + s = cmd.getOptionValue("size"); + Integer size = s == null ? null : Integer.valueOf(s); + + ConnectionCollection result = connectionCLI.connectionClient.findConnections(start, size); + Collection<ConnectionInfo> connections = result.getEntries(); + + MainCLI.printMessage(connections.size() + " connection(s) matched"); + + boolean first = true; + + for (ConnectionInfo connectionInfo : connections) { + + if (first) { + first = false; + } else { + System.out.println(); + } + + ConnectionCLI.printConnectionInfo(connectionInfo); + } + + MainCLI.printMessage("Number of entries returned " + connections.size()); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionModifyCLI.java new file mode 100644 index 000000000..f3e449132 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionModifyCLI.java @@ -0,0 +1,107 @@ +// --- 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 com.netscape.cmstools.tps.connection; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.tps.connection.ConnectionData; +import com.netscape.certsrv.tps.connection.ConnectionModification; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionModifyCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionModifyCLI(ConnectionCLI connectionCLI) { + super("mod", "Modify connection", connectionCLI); + this.connectionCLI = connectionCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Connection ID> [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "status", true, "Status: ENABLED, DISABLED."); + option.setArgName("status"); + options.addOption(option); + + option = new Option(null, "contents", true, "Input file containing connection attributes."); + option.setArgName("file"); + options.addOption(option); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 1) { + printHelp(); + System.exit(1); + } + + String connectionID = cmdArgs[0]; + String status = cmd.getOptionValue("status"); + String contents = cmd.getOptionValue("contents"); + + ConnectionModification connectionModification = new ConnectionModification(); + connectionModification.setID(connectionID); + connectionModification.setStatus(status); + + if (contents != null) { + // load contents from file + StringWriter sw = new StringWriter(); + try (BufferedReader in = new BufferedReader(new FileReader(contents)); + PrintWriter out = new PrintWriter(sw, true)) { + + String line; + while ((line = in.readLine()) != null) { + out.println(line); + } + + connectionModification.setContents(sw.toString()); + } + } + + ConnectionData connectionData = connectionCLI.connectionClient.modifyConnection(connectionID, connectionModification); + + MainCLI.printMessage("Modified connection \"" + connectionID + "\""); + + ConnectionCLI.printConnectionData(connectionData); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionRemoveCLI.java new file mode 100644 index 000000000..5169f23d9 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionRemoveCLI.java @@ -0,0 +1,53 @@ +// --- 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 com.netscape.cmstools.tps.connection; + +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionRemoveCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionRemoveCLI(ConnectionCLI connectionCLI) { + super("del", "Remove connection", connectionCLI); + this.connectionCLI = connectionCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Connection ID>", options); + } + + public void execute(String[] args) throws Exception { + + if (args.length != 1) { + printHelp(); + System.exit(1); + } + + String connectionID = args[0]; + + connectionCLI.connectionClient.removeConnection(connectionID); + + MainCLI.printMessage("Deleted connection \"" + connectionID + "\""); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionShowCLI.java new file mode 100644 index 000000000..7e660d394 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionShowCLI.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) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.tps.connection; + +import java.io.FileWriter; +import java.io.PrintWriter; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.tps.connection.ConnectionData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionShowCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionShowCLI(ConnectionCLI connectionCLI) { + super("show", "Show connection", connectionCLI); + this.connectionCLI = connectionCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Connection ID>", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "contents", true, "Output file to store connection attributes."); + option.setArgName("file"); + options.addOption(option); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 1) { + printHelp(); + System.exit(1); + } + + String connectionID = args[0]; + String file = cmd.getOptionValue("contents"); + + ConnectionData connectionData = connectionCLI.connectionClient.getConnection(connectionID); + + MainCLI.printMessage("Connection \"" + connectionID + "\""); + ConnectionCLI.printConnectionData(connectionData); + + if (file != null) { + // store contents to file + PrintWriter out = new PrintWriter(new FileWriter(file)); + String contents = connectionData.getContents(); + if (contents != null) out.print(contents); + out.close(); + } + } +} diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java index b0eac50f8..a7722720a 100644 --- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java +++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java @@ -23,6 +23,7 @@ import java.util.Set; import javax.ws.rs.core.Application; import org.dogtagpki.server.tps.cert.TPSCertService; +import org.dogtagpki.server.tps.connection.ConnectionService; import org.dogtagpki.server.tps.logging.ActivityService; import org.dogtagpki.server.tps.token.TokenService; @@ -67,6 +68,9 @@ public class TPSApplication extends Application { // activities classes.add(ActivityService.class); + // connections + classes.add(ConnectionService.class); + // certificates classes.add(TPSCertService.class); diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java index 5c27c1233..13b90fe05 100644 --- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java +++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java @@ -18,6 +18,7 @@ package org.dogtagpki.server.tps; import org.dogtagpki.server.tps.cert.TPSCertDatabase; +import org.dogtagpki.server.tps.connection.ConnectionDatabase; import org.dogtagpki.server.tps.logging.ActivityDatabase; import org.dogtagpki.server.tps.token.TokenDatabase; import org.mozilla.jss.CryptoManager; @@ -49,6 +50,7 @@ public class TPSSubsystem implements IAuthority, ISubsystem { public IConfigStore config; public ActivityDatabase activityDatabase = new ActivityDatabase(); + public ConnectionDatabase connectionDatabase = new ConnectionDatabase(); public TPSCertDatabase certDatabase = new TPSCertDatabase(); public TokenDatabase tokenDatabase = new TokenDatabase(); @@ -121,6 +123,10 @@ public class TPSSubsystem implements IAuthority, ISubsystem { return activityDatabase; } + public ConnectionDatabase getConnectionDatabase() { + return connectionDatabase; + } + public TPSCertDatabase getCertDatabase() { return certDatabase; } diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionDatabase.java new file mode 100644 index 000000000..cb7fb981f --- /dev/null +++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionDatabase.java @@ -0,0 +1,60 @@ +// --- 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.server.tps.connection; + +import com.netscape.cmscore.dbs.Database; + +/** + * This class implements in-memory connection database. In the future this + * will be replaced with LDAP database. + * + * @author Endi S. Dewata + */ +public class ConnectionDatabase extends Database<ConnectionRecord> { + + public ConnectionDatabase() { + super("Connection"); + + // add sample records + try { + ConnectionRecord record1 = new ConnectionRecord(); + record1.setID("connection1"); + record1.setStatus("ENABLED"); + record1.setContents("name=connection1\nparam=value"); + addRecord(record1); + + ConnectionRecord record2 = new ConnectionRecord(); + record2.setID("connection2"); + record2.setStatus("DISABLED"); + record2.setContents("name=connection2\nparam=value"); + addRecord(record2); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void addRecord(ConnectionRecord connectionRecord) throws Exception { + addRecord(connectionRecord.getID(), connectionRecord); + } + + public void updateRecord(ConnectionRecord connectionRecord) throws Exception { + updateRecord(connectionRecord.getID(), connectionRecord); + } +} diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionRecord.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionRecord.java new file mode 100644 index 000000000..c4bcc1ded --- /dev/null +++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionRecord.java @@ -0,0 +1,91 @@ +// --- 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.server.tps.connection; + + +/** + * @author Endi S. Dewata + */ +public class ConnectionRecord { + + String id; + String status; + String contents; + + public String getID() { + return id; + } + + public void setID(String id) { + this.id = id; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getContents() { + return contents; + } + + public void setContents(String contents) { + this.contents = contents; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((contents == null) ? 0 : contents.hashCode()); + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((status == null) ? 0 : status.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; + ConnectionRecord other = (ConnectionRecord) obj; + if (contents == null) { + if (other.contents != null) + return false; + } else if (!contents.equals(other.contents)) + return false; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (status == null) { + if (other.status != null) + return false; + } else if (!status.equals(other.status)) + return false; + return true; + } +} diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionService.java new file mode 100644 index 000000000..a6ecbbadd --- /dev/null +++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionService.java @@ -0,0 +1,264 @@ +// --- 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.server.tps.connection; + +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.server.tps.TPSSubsystem; +import org.jboss.resteasy.plugins.providers.atom.Link; + +import com.netscape.certsrv.apps.CMS; +import com.netscape.certsrv.base.PKIException; +import com.netscape.certsrv.tps.connection.ConnectionCollection; +import com.netscape.certsrv.tps.connection.ConnectionData; +import com.netscape.certsrv.tps.connection.ConnectionInfo; +import com.netscape.certsrv.tps.connection.ConnectionModification; +import com.netscape.certsrv.tps.connection.ConnectionResource; +import com.netscape.cms.servlet.base.PKIService; + +/** + * @author Endi S. Dewata + */ +public class ConnectionService extends PKIService implements ConnectionResource { + + public final static int DEFAULT_SIZE = 20; + + public ConnectionService() { + CMS.debug("ConnectionService.<init>()"); + } + + public ConnectionInfo createConnectionInfo(ConnectionRecord connectionRecord) { + + ConnectionInfo connectionInfo = new ConnectionInfo(); + connectionInfo.setID(connectionRecord.getID()); + connectionInfo.setStatus(connectionRecord.getStatus()); + + String connectionID = connectionRecord.getID(); + try { + connectionID = URLEncoder.encode(connectionID, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + + URI uri = uriInfo.getBaseUriBuilder().path(ConnectionResource.class).path("{connectionID}").build(connectionID); + connectionInfo.setLink(new Link("self", uri)); + + return connectionInfo; + } + + public ConnectionData createConnectionData(ConnectionRecord connectionRecord) { + + ConnectionData connectionData = new ConnectionData(); + connectionData.setID(connectionRecord.getID()); + connectionData.setStatus(connectionRecord.getStatus()); + connectionData.setContents(connectionRecord.getContents()); + + String connectionID = connectionRecord.getID(); + try { + connectionID = URLEncoder.encode(connectionID, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + + URI uri = uriInfo.getBaseUriBuilder().path(ConnectionResource.class).path("{connectionID}").build(connectionID); + connectionData.setLink(new Link("self", uri)); + + return connectionData; + } + + public ConnectionRecord createConnectionRecord(ConnectionData connectionData) { + + ConnectionRecord connectionRecord = new ConnectionRecord(); + connectionRecord.setID(connectionData.getID()); + connectionRecord.setStatus(connectionData.getStatus()); + connectionRecord.setContents(connectionData.getContents()); + + return connectionRecord; + } + + @Override + public ConnectionCollection findConnections(Integer start, Integer size) { + + CMS.debug("ConnectionService.findConnections()"); + + try { + start = start == null ? 0 : start; + size = size == null ? DEFAULT_SIZE : size; + + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + ConnectionDatabase database = subsystem.getConnectionDatabase(); + + Iterator<ConnectionRecord> connections = database.getRecords().iterator(); + + ConnectionCollection response = new ConnectionCollection(); + + int i = 0; + + // skip to the start of the page + for ( ; i<start && connections.hasNext(); i++) connections.next(); + + // return entries up to the page size + for ( ; i<start+size && connections.hasNext(); i++) { + response.addEntry(createConnectionInfo(connections.next())); + } + + // count the total entries + for ( ; connections.hasNext(); i++) connections.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 ConnectionData getConnection(String connectionID) { + + CMS.debug("ConnectionService.getConnection(\"" + connectionID + "\")"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + ConnectionDatabase database = subsystem.getConnectionDatabase(); + + return createConnectionData(database.getRecord(connectionID)); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public Response addConnection(ConnectionData connectionData) { + + CMS.debug("ConnectionService.addConnection(\"" + connectionData.getID() + "\")"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + ConnectionDatabase database = subsystem.getConnectionDatabase(); + + database.addRecord(createConnectionRecord(connectionData)); + connectionData = createConnectionData(database.getRecord(connectionData.getID())); + + return Response + .created(connectionData.getLink().getHref()) + .entity(connectionData) + .type(MediaType.APPLICATION_XML) + .build(); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public Response updateConnection(String connectionID, ConnectionData connectionData) { + + CMS.debug("ConnectionService.updateConnection(\"" + connectionID + "\")"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + ConnectionDatabase database = subsystem.getConnectionDatabase(); + + database.updateRecord(createConnectionRecord(connectionData)); + connectionData = createConnectionData(database.getRecord(connectionID)); + + return Response + .ok(connectionData) + .type(MediaType.APPLICATION_XML) + .build(); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public Response modifyConnection(String connectionID, ConnectionModification request) { + + CMS.debug("ConnectionService.modifyConnection(\"" + connectionID + "\", request"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + ConnectionDatabase database = subsystem.getConnectionDatabase(); + + ConnectionRecord connectionRecord = database.getRecord(connectionID); + + String status = request.getStatus(); + if (status != null) { + connectionRecord.setStatus(status); + } + + String contents = request.getContents(); + if (contents != null) { + connectionRecord.setContents(contents); + } + + database.updateRecord(connectionRecord); + ConnectionData connectionData = createConnectionData(database.getRecord(connectionID)); + + return Response + .ok(connectionData) + .type(MediaType.APPLICATION_XML) + .build(); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public void removeConnection(String connectionID) { + + CMS.debug("ConnectionService.removeConnection(\"" + connectionID + "\")"); + + try { + TPSSubsystem subsystem = TPSSubsystem.getInstance(); + ConnectionDatabase database = subsystem.getConnectionDatabase(); + database.removeRecord(connectionID); + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } +} diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java index 87d4cbc5b..a4c8e34c5 100644 --- a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java +++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java @@ -32,6 +32,24 @@ public class TokenDatabase extends Database<TokenRecord> { public TokenDatabase() { super("Token"); + + // add sample records + try { + TokenRecord record1 = new TokenRecord(); + record1.setID("token1"); + record1.setUserID("user1"); + record1.setStatus("ENABLED"); + addRecord(record1); + + TokenRecord record2 = new TokenRecord(); + record2.setID("token2"); + record2.setUserID("user2"); + record2.setStatus("DISABLED"); + addRecord(record2); + + } catch (Exception e) { + e.printStackTrace(); + } } public void addRecord(TokenRecord tokenRecord) throws Exception { diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java index 66f96f932..06c92b7ff 100644 --- a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java +++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java @@ -29,6 +29,7 @@ import javax.ws.rs.core.Response; import org.dogtagpki.server.tps.TPSSubsystem; import org.jboss.resteasy.plugins.providers.atom.Link; +import com.netscape.certsrv.apps.CMS; import com.netscape.certsrv.base.PKIException; import com.netscape.certsrv.tps.token.TokenCollection; import com.netscape.certsrv.tps.token.TokenData; @@ -44,7 +45,7 @@ public class TokenService extends PKIService implements TokenResource { public final static int DEFAULT_SIZE = 20; public TokenService() { - System.out.println("TokenService.<init>()"); + CMS.debug("TokenService.<init>()"); } public TokenData createTokenData(TokenRecord tokenRecord) { @@ -91,7 +92,7 @@ public class TokenService extends PKIService implements TokenResource { @Override public TokenCollection findTokens(Integer start, Integer size) { - System.out.println("TokenService.findTokens()"); + CMS.debug("TokenService.findTokens()"); try { start = start == null ? 0 : start; @@ -138,7 +139,7 @@ public class TokenService extends PKIService implements TokenResource { @Override public TokenData getToken(String tokenID) { - System.out.println("TokenService.getToken(\"" + tokenID + "\")"); + CMS.debug("TokenService.getToken(\"" + tokenID + "\")"); try { TPSSubsystem subsystem = TPSSubsystem.getInstance(); @@ -155,7 +156,7 @@ public class TokenService extends PKIService implements TokenResource { @Override public Response addToken(TokenData tokenData) { - System.out.println("TokenService.addToken(\"" + tokenData.getID() + "\")"); + CMS.debug("TokenService.addToken(\"" + tokenData.getID() + "\")"); try { TPSSubsystem subsystem = TPSSubsystem.getInstance(); @@ -179,7 +180,7 @@ public class TokenService extends PKIService implements TokenResource { @Override public Response updateToken(String tokenID, TokenData tokenData) { - System.out.println("TokenService.updateToken(\"" + tokenID + "\")"); + CMS.debug("TokenService.updateToken(\"" + tokenID + "\")"); try { TPSSubsystem subsystem = TPSSubsystem.getInstance(); @@ -205,7 +206,7 @@ public class TokenService extends PKIService implements TokenResource { @Override public Response modifyToken(String tokenID, TokenModifyRequest request) { - System.out.println("TokenService.modifyToken(\"" + tokenID + "\", request"); + CMS.debug("TokenService.modifyToken(\"" + tokenID + "\", request"); try { TPSSubsystem subsystem = TPSSubsystem.getInstance(); @@ -230,7 +231,7 @@ public class TokenService extends PKIService implements TokenResource { @Override public void removeToken(String tokenID) { - System.out.println("TokenService.removeToken(\"" + tokenID + "\")"); + CMS.debug("TokenService.removeToken(\"" + tokenID + "\")"); try { TPSSubsystem subsystem = TPSSubsystem.getInstance(); |