diff options
| author | Mike McCune <mmccune@gibson.pdx.redhat.com> | 2009-07-09 15:11:39 -0700 |
|---|---|---|
| committer | Mike McCune <mmccune@gibson.pdx.redhat.com> | 2009-07-09 15:12:40 -0700 |
| commit | cd305d823c9c45448b7b8e9b2ca5b515004ae217 (patch) | |
| tree | b3050c38a8e77866f666163bbb30a50089394480 /proxy/code/src | |
| parent | a402c6d2142dd7f7e3a7ef5210d32ecc3e4fb642 (diff) | |
adding some basic API stuff for our model classes
Added a BaseApi class that allows some free basic code to other classes
that want to be able to be manipulated with REST.
Diffstat (limited to 'proxy/code/src')
14 files changed, 417 insertions, 35 deletions
diff --git a/proxy/code/src/log4j.properties b/proxy/code/src/log4j.properties new file mode 100644 index 0000000..d6c2560 --- /dev/null +++ b/proxy/code/src/log4j.properties @@ -0,0 +1,10 @@ +log4j.appender.RootAppender=org.apache.log4j.ConsoleAppender +log4j.appender.RootAppender.layout=org.apache.log4j.PatternLayout +#log4j.appender.RootAppender.layout.ConversionPattern=%d [%t] %-5p %c - %m%n +log4j.appender.RootAppender.layout.ConversionPattern=%m%n +log4j.rootLogger=WARN,RootAppender + +# General debug for everything. Very noisy +#log4j.logger.org.fedoraproject.candlepin=DEBUG + +log4j.logger.org.fedoraproject.candlepin=DEBUG diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/AuthHandler.java b/proxy/code/src/org/fedoraproject/candlepin/api/AuthHandler.java new file mode 100644 index 0000000..a3f3e33 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/api/AuthHandler.java @@ -0,0 +1,5 @@ +package org.fedoraproject.candlepin.api; + +public class AuthHandler { + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/BaseApi.java b/proxy/code/src/org/fedoraproject/candlepin/api/BaseApi.java new file mode 100644 index 0000000..1e6f17b --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/api/BaseApi.java @@ -0,0 +1,93 @@ +/** + * 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.api; + +import org.apache.log4j.Logger; + +import com.sun.jersey.api.representation.Form; + +import org.fedoraproject.candlepin.model.BaseModel; +import org.fedoraproject.candlepin.model.ObjectFactory; +import org.fedoraproject.candlepin.util.MethodUtil; + +import java.util.Iterator; +import java.util.List; + +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; + + +public abstract class BaseApi { + + /** + * Logger for this class + */ + private static final Logger log = Logger.getLogger(BaseApi.class); + + @GET @Path("/{uuid}") + @Produces("text/plain") + public Object get(@PathParam("uuid") String uuid) { + Object o = ObjectFactory.get(). + lookupByUUID(getApiClass(), uuid); + return o; + } + + @GET @Path("/list") + @Produces("text/plain") + public String list() { + StringBuffer retval = new StringBuffer(); + List objects = ObjectFactory.get().listObjectsByClass(getApiClass()); + for (int i = 0; i < objects.size(); i++) { + retval.append(objects.get(i).toString()); + retval.append("\n"); + } + + return retval.toString(); + } + + @POST + @Produces("text/plain") + public void post(Form form) { + String name = form.getFirst("name"); + if (name == null) { + throw new RuntimeException("Missing name parameter."); + } + String newuuid = BaseModel.generateUUID(); + Object args[] = new Object[1]; + args[0] = newuuid; + BaseModel newobject = (BaseModel) + MethodUtil.callNewMethod(getApiClass().getName(), args); + // newobject.setName(name); + // newobject.setUuid(newuuid); + Iterator i = form.keySet().iterator(); + while (i.hasNext()) { + String key = (String) i.next(); + String value = form.getFirst(key); + log.debug("value : " + value); + MethodUtil.callSetter(newobject, key, value); + } + if (log.isDebugEnabled()) { + log.debug("before store name: " + newobject.getName()); + log.debug("before store uuid: " + newobject.getUuid()); + } + ObjectFactory.get().store(newobject); + } + + protected abstract Class getApiClass(); + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/ConsumerApi.java b/proxy/code/src/org/fedoraproject/candlepin/api/ConsumerApi.java new file mode 100644 index 0000000..4afd1b1 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/api/ConsumerApi.java @@ -0,0 +1,30 @@ +/** + * 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.api; + +import org.fedoraproject.candlepin.model.Consumer; + +import javax.ws.rs.Path; + +@Path("/consumer") +public class ConsumerApi extends BaseApi { + + @Override + protected Class getApiClass() { + return Consumer.class; + } + + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/OrgApi.java b/proxy/code/src/org/fedoraproject/candlepin/api/OrgApi.java new file mode 100644 index 0000000..d33c2ea --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/api/OrgApi.java @@ -0,0 +1,30 @@ +/** + * 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.api; + +import org.fedoraproject.candlepin.model.Organization; + +import javax.ws.rs.Path; + +@Path("/org") +public class OrgApi extends BaseApi { + + @Override + protected Class getApiClass() { + return Organization.class; + } + + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/test/ApiHandlerTest.java b/proxy/code/src/org/fedoraproject/candlepin/api/test/ApiTest.java index 5587a04..c05a697 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/api/test/ApiHandlerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/api/test/ApiTest.java @@ -3,11 +3,17 @@ */ package org.fedoraproject.candlepin.api.test; +import com.sun.jersey.api.representation.Form; + import org.fedoraproject.candlepin.api.ApiHandler; +import org.fedoraproject.candlepin.api.ConsumerApi; +import org.fedoraproject.candlepin.api.OrgApi; import org.fedoraproject.candlepin.model.BaseModel; +import org.fedoraproject.candlepin.model.Consumer; import org.fedoraproject.candlepin.model.ObjectFactory; import org.fedoraproject.candlepin.model.Organization; import org.fedoraproject.candlepin.model.User; +import org.fedoraproject.candlepin.model.test.OrganizationTest; import junit.framework.TestCase; @@ -15,7 +21,7 @@ import junit.framework.TestCase; * @author mmccune * */ -public class ApiHandlerTest extends TestCase { +public class ApiTest extends TestCase { public void testAuthentication() throws Exception { User u = new User(); @@ -42,7 +48,8 @@ public class ApiHandlerTest extends TestCase { String token = ApiHandler.get().login(u.getLogin(), u.getPassword()); - Organization lookedup = ApiHandler.get().getOrg(token, "BAD-UUID-NOTFOUND"); + OrgApi oapi = new OrgApi(); + Organization lookedup = (Organization) oapi.get("BAD-UUID-NOTFOUND"); assertNull(lookedup); lookedup = ApiHandler.get().getOrg(token, o.getUuid()); assertNotNull(lookedup); @@ -57,4 +64,18 @@ public class ApiHandlerTest extends TestCase { assertTrue(failed); } + + public void testCreateConsumer() throws Exception { + String newname = "test-consumer-" + System.currentTimeMillis(); + Organization o = OrganizationTest.createOrg(); + ConsumerApi capi = new ConsumerApi(); + Form f = new Form(); + f.add("name", newname); + f.add("type", "standard-system"); + capi.post(f); + assertNotNull(ObjectFactory.get().lookupByFieldName(Consumer.class, + "name", newname)); + + + } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index 473474b..216ff77 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -17,69 +17,85 @@ package org.fedoraproject.candlepin.model; import java.util.LinkedList; import java.util.List; - public class Consumer extends BaseModel { - - private String type; + private Organization organization; private Consumer parent; private List<Product> consumedProducts; - + private ConsumerInfo info; + /** * @param uuid */ public Consumer(String uuid) { super(uuid); } + /** * @return the type */ public String getType() { - return type; + if (this.info == null) { + return null; + } + else { + return info.getType(); + } } + /** - * @param type the type to set + * Set the type of this Consumer. + * @param typeIn to set */ - public void setType(String type) { - this.type = type; + public void setType(String typeIn) { + if (this.info == null) { + this.info = new ConsumerInfo(); + } + this.info.setType(typeIn); } + /** * @return the parent */ public Consumer getParent() { return parent; } + /** * @param parent the parent to set */ public void setParent(Consumer parent) { this.parent = parent; } + /** * @return the consumedProducts */ public List<Product> getConsumedProducts() { return consumedProducts; } + /** * @param consumedProducts the consumedProducts to set */ public void setConsumedProducts(List<Product> consumedProducts) { this.consumedProducts = consumedProducts; } + /** * @return the organization */ public Organization getOrganization() { return organization; } + /** * @param organization the organization to set */ public void setOrganization(Organization organization) { this.organization = organization; } - + /** * Add a Product to this Consumer. * @@ -89,8 +105,32 @@ public class Consumer extends BaseModel { this.consumedProducts = new LinkedList<Product>(); } this.consumedProducts.add(p); - + + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return "Consumer [type=" + this.getType() + ", getName()=" + getName() + + ", getUuid()=" + getUuid() + "]"; } + + /** + * @return Returns the info. + */ + public ConsumerInfo getInfo() { + return info; + } + + /** + * @param infoIn The info to set. + */ + public void setInfo(ConsumerInfo infoIn) { + info = infoIn; + } + } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java new file mode 100644 index 0000000..bd056cf --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java @@ -0,0 +1,67 @@ +/** + * 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.Map; + + +public class ConsumerInfo { + + private Consumer parent; + private String type; + private Map<String, String> properties; + + /** + * @return Returns the parent. + */ + public Consumer getParent() { + return parent; + } + + /** + * @param parentIn The parent to set. + */ + public void setParent(Consumer parentIn) { + parent = parentIn; + } + + /** + * @return Returns the properties. + */ + public Map<String, String> getProperties() { + return properties; + } + + /** + * @param propertiesIn The properties to set. + */ + public void setProperties(Map<String, String> propertiesIn) { + properties = propertiesIn; + } + + /** + * @return Returns the type. + */ + public String getType() { + return type; + } + + /** + * @param typeIn The type to set. + */ + public void setType(String typeIn) { + type = typeIn; + } +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java index 2899ac1..1da671a 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java @@ -16,85 +16,93 @@ package org.fedoraproject.candlepin.model; import java.util.Date; - public class EntitlementPool extends BaseModel { - + private Organization organization; private Product product; private long maxMembers; private long currentMembers; - + private Date startDate; private Date endDate; - + /** * @param uuid */ public EntitlementPool(String uuid) { super(uuid); } - + /** * Default const */ public EntitlementPool() { - + } - + /** * @return the product */ public Product getProduct() { return product; } + /** * @param product the product to set */ public void setProduct(Product product) { this.product = product; } + /** * @return the startDate */ public Date getStartDate() { return startDate; } + /** * @param startDate the startDate to set */ public void setStartDate(Date startDate) { this.startDate = startDate; } + /** * @return the endDate */ public Date getEndDate() { return endDate; } + /** * @param endDate the endDate to set */ public void setEndDate(Date endDate) { this.endDate = endDate; } + /** * @return the maxMembers */ public long getMaxMembers() { return maxMembers; } + /** * @param maxMembers the maxMembers to set */ public void setMaxMembers(long maxMembers) { this.maxMembers = maxMembers; } + /** * @return the currentMembers */ public long getCurrentMembers() { return currentMembers; } + /** * @param currentMembers the currentMembers to set */ @@ -115,6 +123,5 @@ public class EntitlementPool extends BaseModel { public void setOrganization(Organization organization) { this.organization = organization; } - } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java index 94f1b29..b96f8eb 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java @@ -14,6 +14,9 @@ */ package org.fedoraproject.candlepin.model; +import org.apache.log4j.Logger; +import org.fedoraproject.candlepin.util.MethodUtil; + import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; @@ -21,25 +24,69 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import org.fedoraproject.candlepin.util.MethodUtil; - /** * @author mmccune * */ public class ObjectFactory { + /** + * Logger for this class + */ + private static final Logger logger = Logger.getLogger(ObjectFactory.class); + private static ObjectFactory instance = new ObjectFactory(); private Map objects; private ObjectFactory() { objects = new HashMap(); + initMockObjects(); + } + + private void initMockObjects() { + Organization org = new Organization(BaseModel.generateUUID()); + org.setName("test-org"); + // Product + Product rhel = new Product(BaseModel.generateUUID()); + rhel.setName("Red Hat Enterprise Linux"); + + // User + User u = new User(); + u.setLogin("test-login"); + u.setPassword("redhat"); + org.addUser(u); + + // Consumer + Consumer c = new Consumer(BaseModel.generateUUID()); + c.setName("fake-consumer-i386"); + c.setOrganization(org); + org.addConsumer(c); + c.addConsumedProduct(rhel); + + // EntitlementPool + EntitlementPool pool = new EntitlementPool(BaseModel.generateUUID()); + org.addEntitlementPool(pool); + pool.setProduct(rhel); + + this.store(org); + this.store(u); + this.store(c); + this.store(pool); } public static ObjectFactory get() { return instance; } + + /** + * Get a List of objects by type + * @param clazz + * @return List if found. null if not. + */ + public List listObjectsByClass(Class<?> clazz) { + return (List) objects.get(clazz.getName()); + } /** * Lookup an Organization by UUID @@ -64,13 +111,13 @@ public class ObjectFactory { List typelist = (List) objects.get(key); for (int i = 0; i < typelist.size(); i++) { Object o = typelist.get(i); - System.out.println("O: " + o); + logger.debug("O: " + o); String getter = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); - System.out.println("getter: " + getter); + logger.debug("getter: " + getter); Object v = MethodUtil.callMethod(o, getter, new Object[0]); - System.out.println("v: " + v); - if (v.equals(value)) { + logger.debug("v: " + v); + if (v != null && v.equals(value)) { return o; } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Organization.java b/proxy/code/src/org/fedoraproject/candlepin/model/Organization.java index 6beaa68..f9623cc 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Organization.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Organization.java @@ -108,4 +108,13 @@ public class Organization extends BaseModel { } this.entitlementPools.add(pool); } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return "Organization [getName()=" + getName() + ", getUuid()=" + + getUuid() + "]"; + } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Product.java b/proxy/code/src/org/fedoraproject/candlepin/model/Product.java index a4876cc..5dfe7fc 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Product.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Product.java @@ -17,9 +17,8 @@ package org.fedoraproject.candlepin.model; import java.util.LinkedList; import java.util.List; - public class Product extends BaseModel { - + private List<Product> childProducts; /** @@ -29,7 +28,7 @@ public class Product extends BaseModel { public Product(String uuid) { super(uuid); } - + /** * Default constructor */ @@ -49,7 +48,7 @@ public class Product extends BaseModel { public void setChildProducts(List<Product> childProducts) { this.childProducts = childProducts; } - + /** * Add a child of this Product. * @param p to add diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/OrganizationTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/OrganizationTest.java index df15d18..4dad8e8 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/OrganizationTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/OrganizationTest.java @@ -24,28 +24,46 @@ import org.fedoraproject.candlepin.model.Organization; import org.fedoraproject.candlepin.model.Product; import org.fedoraproject.candlepin.model.User; +import java.util.List; + /** * * */ public class OrganizationTest extends TestCase { + public static Organization createOrg() { + String lookedUp = BaseModel.generateUUID(); + Organization o = new Organization(); + o.setUuid(lookedUp); + ObjectFactory.get().store(o); + return o; + } + public void testOrg() throws Exception { Organization o = new Organization(BaseModel.generateUUID()); assertNotNull(o); } public void testLookup() throws Exception { - String lookedUp = BaseModel.generateUUID(); - Organization o = new Organization(); - o.setUuid(lookedUp); - ObjectFactory.get().store(o); + Organization o = createOrg(); + String lookedUp = o.getUuid(); o = (Organization) ObjectFactory.get(). lookupByUUID(Organization.class, lookedUp); assertNotNull(o); } + public void testList() throws Exception { + for (int i = 0; i < 10; i++) { + createOrg(); + } + + List orgs = ObjectFactory.get().listObjectsByClass(Organization.class); + assertNotNull(orgs); + assertTrue(orgs.size() >= 10); + } + public void testObjectRelationships() throws Exception { Organization org = new Organization(BaseModel.generateUUID()); org.setName("test-org"); diff --git a/proxy/code/src/org/fedoraproject/candlepin/util/MethodUtil.java b/proxy/code/src/org/fedoraproject/candlepin/util/MethodUtil.java index 2884c70..882bb9a 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/util/MethodUtil.java +++ b/proxy/code/src/org/fedoraproject/candlepin/util/MethodUtil.java @@ -56,6 +56,11 @@ public class MethodUtil { return true; } + public static void callSetter(Object o, String fieldName, Object param) { + String setter = "set" + fieldName.substring(0, 1).toUpperCase() + + fieldName.substring(1); + MethodUtil.callMethod(o, setter, param); + } /** * Call the specified method with the specified arguments, converting @@ -178,12 +183,13 @@ public class MethodUtil { * @param className * @return instance of class passed in. */ - private static Object callNewMethod(String className, Object... args) { + public static Object callNewMethod(String className, Object... args) { Object retval = null; try { Class clazz = Thread.currentThread(). getContextClassLoader().loadClass(className); + System.out.println("cz: " + clazz.getName()); if (args == null || args.length == 0) { retval = clazz.newInstance(); } |
