diff options
| author | jesus m. rodriguez <jesusr@redhat.com> | 2009-11-12 15:25:33 -0500 |
|---|---|---|
| committer | jesus m. rodriguez <jesusr@redhat.com> | 2009-11-12 15:25:33 -0500 |
| commit | f4feea49201468152cb19cc1a5013b6d2d7f7f8a (patch) | |
| tree | 98467f17ee49c886f1e8a444e7a7e362dc40d9d1 /proxy/code/src | |
| parent | 5f2c420567b94d6fc8949833d6d441c2bd1a291e (diff) | |
| download | candlepin-f4feea49201468152cb19cc1a5013b6d2d7f7f8a.tar.gz candlepin-f4feea49201468152cb19cc1a5013b6d2d7f7f8a.tar.xz candlepin-f4feea49201468152cb19cc1a5013b6d2d7f7f8a.zip | |
allow upload of cert and return list of available ent pools
Diffstat (limited to 'proxy/code/src')
7 files changed, 183 insertions, 27 deletions
diff --git a/proxy/code/src/com/redhat/rhn/common/cert/Certificate.java b/proxy/code/src/com/redhat/rhn/common/cert/Certificate.java index ddedfaf..5c94214 100644 --- a/proxy/code/src/com/redhat/rhn/common/cert/Certificate.java +++ b/proxy/code/src/com/redhat/rhn/common/cert/Certificate.java @@ -49,7 +49,7 @@ public class Certificate { private String satelliteVersion; private String generation; private String signature; - private List channelFamilies; + private List<ChannelFamilyDescriptor> channelFamilies; private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; @@ -57,7 +57,7 @@ public class Certificate { * Construct an empty certificate. */ Certificate() { - channelFamilies = new ArrayList(); + channelFamilies = new ArrayList<ChannelFamilyDescriptor>(); } /** @@ -76,14 +76,15 @@ public class Certificate { * * @return the certificate in the canonical form used for signing */ + @SuppressWarnings("unchecked") public String asChecksumString() { StringBuffer result = new StringBuffer(); // Fields must appear in the output in alphabetical order // The channelFamilies are sorted in their very own way // (see ChannelFamily.compareTo) Collections.sort(channelFamilies); - for (int i = 0; i < channelFamilies.size(); i++) { - ChannelFamilyDescriptor cf = (ChannelFamilyDescriptor) channelFamilies.get(i); + + for (ChannelFamilyDescriptor cf : channelFamilies) { result.append(cf.asChecksumString()).append("\n"); } appendField(result, "expires", getExpires()); @@ -128,7 +129,7 @@ public class Certificate { .getVirtualizationPlatformSlots()); for (int i = 0; i < channelFamilies.size(); i++) { - ChannelFamilyDescriptor cf = (ChannelFamilyDescriptor) channelFamilies.get(i); + ChannelFamilyDescriptor cf = channelFamilies.get(i); buf.append(" ").append(cf.asXmlString()).append("\n"); } @@ -287,7 +288,7 @@ public class Certificate { * Return an unmodifiable list of the channel families * @return an unmodifiable list of the channel families */ - public List getChannelFamilies() { + public List<ChannelFamilyDescriptor> getChannelFamilies() { return Collections.unmodifiableList(channelFamilies); } @@ -300,7 +301,7 @@ public class Certificate { */ public ChannelFamilyDescriptor getChannelFamily(String family) { for (int i = 0; i < channelFamilies.size(); i++) { - ChannelFamilyDescriptor f = (ChannelFamilyDescriptor) channelFamilies.get(i); + ChannelFamilyDescriptor f = channelFamilies.get(i); if (f.getFamily().equals(family)) { return f; } @@ -327,6 +328,18 @@ public class Certificate { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.parse(expires); } + + /** + * Convenience function, returns java.util.Date equivalent of + * <code>issued</code> + * @return Date obj equivalent of <code>issued</code> + * @throws ParseException format of <code>issued</code> not what we + * expected + */ + public Date getIssuedDate() throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return sdf.parse(issued); + } /** * Get the generation diff --git a/proxy/code/src/com/redhat/rhn/common/cert/CertificateFactory.java b/proxy/code/src/com/redhat/rhn/common/cert/CertificateFactory.java index 2601215..b5b1229 100644 --- a/proxy/code/src/com/redhat/rhn/common/cert/CertificateFactory.java +++ b/proxy/code/src/com/redhat/rhn/common/cert/CertificateFactory.java @@ -54,7 +54,8 @@ public class CertificateFactory { new SimpleExtractor("generation"), new ChannelFamilyExtractor("channel-families") }; - private static final HashMap FIELD_MAP = new HashMap(); + private static final HashMap<String,FieldExtractor> FIELD_MAP = + new HashMap<String,FieldExtractor>(); static { for (int i = 0; i < FIELD_EXTRACTORS.length; i++) { @@ -103,6 +104,7 @@ public class CertificateFactory { return readDocument(new SAXBuilder().build(url), url.toExternalForm()); } + @SuppressWarnings("unchecked") private static Certificate readDocument(Document doc, String source) throws JDOMException { Certificate result = new Certificate(); @@ -127,7 +129,7 @@ public class CertificateFactory { private static void extractField(Certificate result, Element child) throws JDOMException { String name = child.getAttributeValue("name"); - FieldExtractor e = (FieldExtractor) FIELD_MAP.get(name); + FieldExtractor e = FIELD_MAP.get(name); if (name == null) { throw new JDOMException("The field " + name + " is not one of the possible fields for " + ELEM_FIELD); diff --git a/proxy/code/src/com/redhat/rhn/common/cert/ChannelFamilyDescriptor.java b/proxy/code/src/com/redhat/rhn/common/cert/ChannelFamilyDescriptor.java index eed8e6f..f9024ea 100644 --- a/proxy/code/src/com/redhat/rhn/common/cert/ChannelFamilyDescriptor.java +++ b/proxy/code/src/com/redhat/rhn/common/cert/ChannelFamilyDescriptor.java @@ -22,6 +22,7 @@ import com.redhat.rhn.common.cert.XmlTag; * * Borrowed from project Spacewalk: http://spacewalk.redhat.com */ +@SuppressWarnings("unchecked") public class ChannelFamilyDescriptor implements Comparable { private String family; diff --git a/proxy/code/src/com/redhat/rhn/common/cert/XmlTag.java b/proxy/code/src/com/redhat/rhn/common/cert/XmlTag.java index ee4e0dc..e503462 100644 --- a/proxy/code/src/com/redhat/rhn/common/cert/XmlTag.java +++ b/proxy/code/src/com/redhat/rhn/common/cert/XmlTag.java @@ -16,7 +16,6 @@ package com.redhat.rhn.common.cert; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -28,10 +27,10 @@ import java.util.Map; public class XmlTag { private String tag; - private Map attribs; - private List body; + private Map<String, String> attribs; + private List<Object> body; private boolean spaceBeforeEndTag; - private List keys; + private List<String> keys; /** * Standard xml header with utf-8 encoding. Example usage:<br /> @@ -73,10 +72,10 @@ public class XmlTag { */ protected XmlTag(String tagIn, boolean spaceBefore) { - attribs = new HashMap(); + attribs = new HashMap<String, String>(); tag = tagIn; - body = new ArrayList(); - keys = new ArrayList(); + body = new ArrayList<Object>(); + keys = new ArrayList<String>(); spaceBeforeEndTag = spaceBefore; } @@ -146,13 +145,11 @@ public class XmlTag { StringBuffer ret = new StringBuffer("<"); ret.append(tag); - Iterator i = keys.iterator(); - while (i.hasNext()) { - String attrib = (String) i.next(); + for (String attrib : keys) { ret.append(" "); ret.append(attrib); ret.append("=\""); - ret.append((String) attribs.get(attrib)); + ret.append(attribs.get(attrib)); ret.append("\""); } ret.append(">"); @@ -166,9 +163,9 @@ public class XmlTag { */ public String renderBody() { StringBuffer buf = new StringBuffer(); - - for (Iterator itr = body.iterator(); itr.hasNext();) { - buf.append(convertToString(itr.next())); + + for (Object obj : body) { + buf.append(convertToString(obj)); } return buf.toString(); diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java index e6604e7..d4cf900 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java @@ -22,7 +22,8 @@ import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; /** - * Represents a collection of entitlements for a given order. + * Represents a pool of products eligible to be consumed (entitled). + * For every Product there will be a corresponding Pool. */ @XmlRootElement @XmlAccessorType(XmlAccessType.PROPERTY) diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Pinsetter.java b/proxy/code/src/org/fedoraproject/candlepin/model/Pinsetter.java new file mode 100644 index 0000000..f95b1cc --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Pinsetter.java @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2008 Red Hat, Inc. + * + * This software is licensed to you under the GNU General Public License, + * version 2 (GPLv2). There is NO WARRANTY for this software, express or + * implied, including the implied warranties of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 + * along with this software; if not, see + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. + * + * Red Hat trademarks are not licensed under GPLv2. No permission is + * granted to use or replicate Red Hat trademarks that are incorporated + * in this software or its documentation. + */ +package org.fedoraproject.candlepin.model; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + + +/** + * Pinsetter + * @version $Rev$ + */ +public class Pinsetter { + public static Pinsetter instance = new Pinsetter(); + private static List<EntitlementPool> pools = new ArrayList<EntitlementPool>(); + + private Pinsetter() { + // do nothing + } + + public static Pinsetter get() { + return instance; + } + + /** + * Add a new product to entitle. + * @param owner Owner of the product/entitlement. + * @param pname Product name + * @param maxmem Maximum members available. + * @param start start date of the product entitlement + * @param end end date of the product entitlement. + */ + public void addProduct(Owner owner, String pname, long maxmem, + Date start, Date end) { + + Product p = new Product(pname, pname); + + EntitlementPool ep = new EntitlementPool(); + ep.setOwner(owner); + ep.setProduct(p); + ep.setMaxMembers(maxmem); + ep.setStartDate(start); + ep.setEndDate(end); + ep.setName(pname); + + pools.add(ep); + ObjectFactory.get().store(ep); + } +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/resource/CertificateResource.java b/proxy/code/src/org/fedoraproject/candlepin/resource/CertificateResource.java index 7eae390..b0ae0c7 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/resource/CertificateResource.java +++ b/proxy/code/src/org/fedoraproject/candlepin/resource/CertificateResource.java @@ -14,21 +14,29 @@ */ package org.fedoraproject.candlepin.resource; +import org.fedoraproject.candlepin.model.ObjectFactory; +import org.fedoraproject.candlepin.model.Owner; +import org.fedoraproject.candlepin.model.Pinsetter; import org.fedoraproject.candlepin.model.User; import com.redhat.rhn.common.cert.Certificate; import com.redhat.rhn.common.cert.CertificateFactory; +import com.redhat.rhn.common.cert.ChannelFamilyDescriptor; import com.sun.jersey.core.util.Base64; import org.jdom.JDOMException; import java.io.IOException; +import java.text.ParseException; +import java.util.Date; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; /** @@ -54,14 +62,18 @@ public class CertificateResource extends BaseResource { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - public String create(String base64cert) { + public String upload(String base64cert) { try { - System.out.println("cert: [" + base64cert + "]"); + if (base64cert == null || "".equals(base64cert)) { + throw new WebApplicationException(Response.Status.BAD_REQUEST); + } + String decoded = Base64.base64Decode(base64cert); System.out.println(decoded); cert = CertificateFactory.read(decoded); - System.out.println(cert.getExpires()); + + addProducts(cert); } catch (JDOMException e) { // TODO Auto-generated catch block @@ -71,6 +83,10 @@ public class CertificateResource extends BaseResource { // TODO Auto-generated catch block e.printStackTrace(); } + catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } return "uuid"; } @@ -83,4 +99,68 @@ public class CertificateResource extends BaseResource { public static Certificate get() { return cert; } + + private void addProducts(Certificate cert) throws ParseException { + // look up the owner by the same name, if none found, create a new one. + Owner owner = (Owner) ObjectFactory.get().lookupByFieldName( + Owner.class, "name", cert.getOwner()); + + if (owner == null) { + owner = new Owner(); + owner.setName(cert.getOwner()); + owner = (Owner) ObjectFactory.get().store(owner); + System.out.println(owner.getUuid()); + } + + // get the product the cert is for (and the channel families + // which have the other products you can have) + Date issued = cert.getIssuedDate(); + Date expires = cert.getExpiresDate(); + + Pinsetter.get().addProduct(owner, cert.getProduct(), + new Long(cert.getSlots()).longValue(), + issued, expires); + + // create products for the channel families + for (ChannelFamilyDescriptor cfd : cert.getChannelFamilies()) { + Pinsetter.get().addProduct(owner, cfd.getFamily(), + new Long(cfd.getQuantity()).longValue(), + issued, expires); + } + + // create products for each of the add-on entitlements. + if (!isEmpty(cert.getMonitoringSlots())) { + Pinsetter.get().addProduct(owner, "monitoring", + new Long(cert.getMonitoringSlots()).longValue(), + issued, expires); + } + + if (!isEmpty(cert.getNonlinuxSlots())) { + Pinsetter.get().addProduct(owner, "nonlinux", + new Long(cert.getNonlinuxSlots()).longValue(), + issued, expires); + } + + if (!isEmpty(cert.getProvisioningSlots())) { + Pinsetter.get().addProduct(owner, "provisioning", + new Long(cert.getProvisioningSlots()).longValue(), + issued, expires); + } + + if (!isEmpty(cert.getVirtualizationSlots())) { + Pinsetter.get().addProduct(owner, "virtualization_host", + new Long(cert.getVirtualizationSlots()).longValue(), + issued, expires); + } + + if (!isEmpty(cert.getVirtualizationPlatformSlots())) { + Pinsetter.get().addProduct(owner, "virtualization_host_platform", + new Long(cert.getVirtualizationPlatformSlots()).longValue(), + issued, expires); + } + } + + private boolean isEmpty(String str) { + return str == null || "".equals(str); + } } |
