diff options
author | Endi S. Dewata <edewata@redhat.com> | 2013-10-24 09:17:37 -0400 |
---|---|---|
committer | Endi S. Dewata <edewata@redhat.com> | 2013-11-04 11:37:06 -0500 |
commit | 89d871642969572a0a64f2fa9d9455e0753fcf1c (patch) | |
tree | 2da14ad12a4a3388a3233bb5ce22a4f96af84171 /base/common/src/com/netscape/certsrv/tps | |
parent | 481ee45823a6dd1d3d151f407eb78e142e7149fa (diff) | |
download | pki-89d871642969572a0a64f2fa9d9455e0753fcf1c.tar.gz pki-89d871642969572a0a64f2fa9d9455e0753fcf1c.tar.xz pki-89d871642969572a0a64f2fa9d9455e0753fcf1c.zip |
Added TPS profile resource.
A new REST service and clients have been added to manage the profiles
in the TPS configuration file.
Ticket #652
Diffstat (limited to 'base/common/src/com/netscape/certsrv/tps')
5 files changed, 438 insertions, 0 deletions
diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/tps/TPSClient.java index 40820d84d..7f65784c9 100644 --- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java +++ b/base/common/src/com/netscape/certsrv/tps/TPSClient.java @@ -29,6 +29,7 @@ import com.netscape.certsrv.tps.authenticator.AuthenticatorClient; import com.netscape.certsrv.tps.cert.TPSCertClient; import com.netscape.certsrv.tps.config.ConfigClient; import com.netscape.certsrv.tps.connection.ConnectionClient; +import com.netscape.certsrv.tps.profile.ProfileClient; import com.netscape.certsrv.tps.profile.ProfileMappingClient; import com.netscape.certsrv.tps.token.TokenClient; import com.netscape.certsrv.user.UserClient; @@ -51,6 +52,7 @@ public class TPSClient extends SubsystemClient { addClient(new ConfigClient(client, name)); addClient(new ConnectionClient(client, name)); addClient(new GroupClient(client, name)); + addClient(new ProfileClient(client, name)); addClient(new ProfileMappingClient(client, name)); addClient(new SelfTestClient(client, name)); addClient(new TokenClient(client, name)); diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileClient.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileClient.java new file mode 100644 index 000000000..6f262e95a --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileClient.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.profile; + +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 ProfileClient extends Client { + + public ProfileResource resource; + + public ProfileClient(PKIClient client) throws URISyntaxException { + this(client, client.getSubsystem()); + } + + public ProfileClient(PKIClient client, String subsystem) throws URISyntaxException { + super(client, subsystem, "profile"); + init(); + } + + public void init() throws URISyntaxException { + resource = createProxy(ProfileResource.class); + } + + public ProfileCollection findProfiles(Integer start, Integer size) { + return resource.findProfiles(start, size); + } + + public ProfileData getProfile(String profileID) { + return resource.getProfile(profileID); + } + + public ProfileData addProfile(ProfileData profileData) { + @SuppressWarnings("unchecked") + ClientResponse<ProfileData> response = (ClientResponse<ProfileData>)resource.addProfile(profileData); + return client.getEntity(response); + } + + public ProfileData updateProfile(String profileID, ProfileData profileData) { + @SuppressWarnings("unchecked") + ClientResponse<ProfileData> response = (ClientResponse<ProfileData>)resource.updateProfile(profileID, profileData); + return client.getEntity(response); + } + + public ProfileData changeProfileStatus(String profileID, String action) { + @SuppressWarnings("unchecked") + ClientResponse<ProfileData> response = (ClientResponse<ProfileData>)resource.changeProfileStatus(profileID, action); + return client.getEntity(response); + } + + public void removeProfile(String profileID) { + resource.removeProfile(profileID); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileCollection.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileCollection.java new file mode 100644 index 000000000..a802cb680 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileCollection.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.profile; + +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="Profiles") +public class ProfileCollection extends DataCollection<ProfileData> { + + @XmlElementRef + public Collection<ProfileData> getEntries() { + return super.getEntries(); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileData.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileData.java new file mode 100644 index 000000000..c4bbc037e --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileData.java @@ -0,0 +1,232 @@ +// --- 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.profile; + +import java.io.StringReader; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +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 javax.xml.bind.annotation.XmlValue; +import javax.xml.bind.annotation.adapters.XmlAdapter; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +/** + * @author Endi S. Dewata + */ +@XmlRootElement(name="Profile") +public class ProfileData { + + public static Marshaller marshaller; + public static Unmarshaller unmarshaller; + + static { + try { + marshaller = JAXBContext.newInstance(ProfileData.class).createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + unmarshaller = JAXBContext.newInstance(ProfileData.class).createUnmarshaller(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + String id; + String status; + Map<String, String> properties = new LinkedHashMap<String, String>(); + + 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="Properties") + @XmlJavaTypeAdapter(MapAdapter.class) + public Map<String, String> getProperties() { + return properties; + } + + public void setProperties(Map<String, String> properties) { + this.properties.clear(); + this.properties.putAll(properties); + } + + public Collection<String> getPropertyNames() { + return properties.keySet(); + } + + public String getProperty(String name) { + return properties.get(name); + } + + public void setProperty(String name, String value) { + properties.put(name, value); + } + + public String removeProperty(String name) { + return properties.remove(name); + } + + public static class MapAdapter extends XmlAdapter<PropertyList, Map<String, String>> { + + public PropertyList marshal(Map<String, String> map) { + PropertyList list = new PropertyList(); + for (Map.Entry<String, String> entry : map.entrySet()) { + Property property = new Property(); + property.name = entry.getKey(); + property.value = entry.getValue(); + list.properties.add(property); + } + return list; + } + + public Map<String, String> unmarshal(PropertyList list) { + Map<String, String> map = new LinkedHashMap<String, String>(); + for (Property property : list.properties) { + map.put(property.name, property.value); + } + return map; + } + } + + public static class PropertyList { + @XmlElement(name="Property") + public List<Property> properties = new ArrayList<Property>(); + } + + public static class Property { + + @XmlAttribute + public String name; + + @XmlValue + public String value; + } + + @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 + ((properties == null) ? 0 : properties.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; + ProfileData other = (ProfileData) 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 (properties == null) { + if (other.properties != null) + return false; + } else if (!properties.equals(other.properties)) + 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 ProfileData valueOf(String string) throws Exception { + try { + return (ProfileData)unmarshaller.unmarshal(new StringReader(string)); + } catch (Exception e) { + return null; + } + } + + public static void main(String args[]) throws Exception { + + ProfileData before = new ProfileData(); + before.setID("profile1"); + before.setStatus("ENABLED"); + before.setProperty("param1", "value1"); + before.setProperty("param2", "value2"); + + String string = before.toString(); + System.out.println(string); + + ProfileData after = ProfileData.valueOf(string); + System.out.println(before.equals(after)); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileResource.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileResource.java new file mode 100644 index 000000000..d86a4d809 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileResource.java @@ -0,0 +1,90 @@ +// --- 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.profile; + +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; + +import com.netscape.certsrv.acls.ACLMapping; +import com.netscape.certsrv.authentication.AuthMethodMapping; + + +/** + * @author Endi S. Dewata + */ +@Path("profiles") +@AuthMethodMapping("profiles") +@ACLMapping("profiles.read") +public interface ProfileResource { + + @GET + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public ProfileCollection findProfiles( + @QueryParam("start") Integer start, + @QueryParam("size") Integer size); + + @GET + @Path("{profileID}") + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public ProfileData getProfile(@PathParam("profileID") String profileID); + + @POST + @ACLMapping("profiles.add") + @ClientResponseType(entityType=ProfileData.class) + @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public Response addProfile(ProfileData profileData); + + @PUT + @Path("{profileID}") + @ACLMapping("profiles.modify") + @ClientResponseType(entityType=ProfileData.class) + @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public Response updateProfile( + @PathParam("profileID") String profileID, + ProfileData profileData); + + @POST + @Path("{profileID}") + @ACLMapping("profiles.approve") + @ClientResponseType(entityType=ProfileData.class) + @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public Response changeProfileStatus( + @PathParam("profileID") String profileID, + @QueryParam("action") String action); + + + @DELETE + @Path("{profileID}") + @ACLMapping("profiles.remove") + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public void removeProfile(@PathParam("profileID") String profileID); +} |