|
View:
New views
1 Messages
—
Rating Filter:
Alert me
|
|
|
[HippoCMS-svn] [20630] ecm/site-toolkit: HSTTWO-882: Completing all jax-rs operations with unit test cases:Revision: 20630
Author: wko Date: 2009-11-11 17:11:58 +0100 (Wed, 11 Nov 2009) Log Message: ----------- HSTTWO-882: Completing all jax-rs operations with unit test cases: - GET to retrieve document, folder, node and property - POST to create document, folder and node - DELETE to delete node - PUT to update properties on a node Modified Paths: -------------- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/pom.xml ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/BaseHstContentService.java ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/ContentService.java ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoBeanContent.java ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoDocumentBeanContent.java ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoFolderBeanContent.java ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/ItemContent.java ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/NodeContent.java ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/PropertyContent.java ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/test/java/org/hippoecm/hst/services/support/jaxrs/content/TestContentService.java Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/pom.xml =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/pom.xml 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/pom.xml 2009-11-11 16:11:58 UTC (rev 20630) @@ -53,6 +53,11 @@ <artifactId>slf4j-api</artifactId> </dependency> + <dependency> + <groupId>org.apache.jackrabbit</groupId> + <artifactId>jackrabbit-jcr-commons</artifactId> + </dependency> + <!-- JAX-RS API --> <dependency> <groupId>javax.ws.rs</groupId> Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/BaseHstContentService.java =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/BaseHstContentService.java 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/BaseHstContentService.java 2009-11-11 16:11:58 UTC (rev 20630) @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import javax.jcr.LoginException; import javax.jcr.RepositoryException; @@ -54,6 +55,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * BaseHstContentService + * + * @version $Id$ + */ public class BaseHstContentService { public static final String SITE_CONTENT_PATH = "org.hippoecm.hst.services.support.site.content.path"; @@ -78,25 +84,33 @@ return objectConverter; } - protected HippoBeanContent createHippoBeanContent(HippoBean bean) throws RepositoryException { + protected HippoBeanContent createHippoBeanContent(HippoBean bean, final Set<String> propertyNamesFilledWithValues) throws RepositoryException { HippoBeanContent beanContent = null; if (bean instanceof HippoFolderBean) { - beanContent = new HippoFolderBeanContent((HippoFolderBean) bean); + beanContent = new HippoFolderBeanContent((HippoFolderBean) bean, propertyNamesFilledWithValues); } else if (bean instanceof HippoDocumentBean) { - beanContent = new HippoDocumentBeanContent((HippoDocumentBean) bean); + beanContent = new HippoDocumentBeanContent((HippoDocumentBean) bean, propertyNamesFilledWithValues); } else { - beanContent = new HippoBeanContent(bean); + beanContent = new HippoBeanContent(bean, propertyNamesFilledWithValues); } return beanContent; } protected String getContentItemPath(final HttpServletRequest servletRequest, final List<PathSegment> pathSegments) { - StringBuilder pathBuilder = new StringBuilder(80).append(getSiteContentPath(servletRequest)); + StringBuilder pathBuilder = new StringBuilder(80); - for (PathSegment pathSegment : pathSegments) { - pathBuilder.append('/').append(pathSegment.getPath()); + if (pathSegments.size() > 0 && "jcr:root".equals(pathSegments.get(0).getPath())) { + for (int i = 1; i < pathSegments.size(); i++) { + pathBuilder.append('/').append(pathSegments.get(i).getPath()); + } + } else { + pathBuilder.append(getSiteContentPath(servletRequest)); + + for (PathSegment pathSegment : pathSegments) { + pathBuilder.append('/').append(pathSegment.getPath()); + } } String path = pathBuilder.toString(); Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/ContentService.java =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/ContentService.java 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/ContentService.java 2009-11-11 16:11:58 UTC (rev 20630) @@ -18,6 +18,7 @@ import java.util.Calendar; import java.util.Collection; import java.util.List; +import java.util.Set; import javax.jcr.Item; import javax.jcr.Node; @@ -30,6 +31,7 @@ import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.PathSegment; @@ -43,6 +45,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * ContentService + * + * @version $Id$ + */ @Path("/contentservice/") public class ContentService extends BaseHstContentService { @@ -62,7 +69,7 @@ @GET @Path("/uuid/{uuid}/") - public HippoBeanContent getContentNodeByUUID(@PathParam("uuid") String uuid) { + public HippoBeanContent getContentNodeByUUID(@PathParam("uuid") String uuid, @QueryParam("pv") Set<String> propertyNamesFilledWithValues) { HippoBeanContent beanContent = new HippoBeanContent(); try { @@ -70,7 +77,7 @@ HippoBean bean = (HippoBean) cpm.getObjectByUuid(uuid); if (bean != null) { - beanContent = createHippoBeanContent(bean); + beanContent = createHippoBeanContent(bean, propertyNamesFilledWithValues); String encoding = servletRequest.getCharacterEncoding(); beanContent.buildUri(getRequestURIBase(uriInfo) + SERVICE_PATH, getSiteContentPath(servletRequest), encoding); beanContent.buildChildUris(getRequestURIBase(uriInfo) + SERVICE_PATH, getSiteContentPath(servletRequest), encoding); @@ -90,7 +97,7 @@ @GET @Path("/{path:.*}") - public ItemContent getContentItem(@PathParam("path") List<PathSegment> pathSegments) { + public ItemContent getContentItem(@PathParam("path") List<PathSegment> pathSegments, @QueryParam("pv") Set<String> propertyNamesFilledWithValues) { String itemPath = getContentItemPath(servletRequest, pathSegments); ItemContent itemContent = new ItemContent(); @@ -102,7 +109,7 @@ HippoBean bean = (HippoBean) cpm.getObject(itemPath); if (bean != null) { - HippoBeanContent beanContent = createHippoBeanContent(bean); + HippoBeanContent beanContent = createHippoBeanContent(bean, propertyNamesFilledWithValues); String encoding = servletRequest.getCharacterEncoding(); beanContent.buildUri(getRequestURIBase(uriInfo) + SERVICE_PATH, getSiteContentPath(servletRequest), encoding); beanContent.buildChildUris(getRequestURIBase(uriInfo) + SERVICE_PATH, getSiteContentPath(servletRequest), encoding); @@ -137,13 +144,14 @@ HippoBean bean = (HippoBean) cpm.getObject(itemPath); if (bean == null) { - return Response.serverError().status(Response.Status.NOT_FOUND).build(); - } else { - HippoBeanContent beanContent = createHippoBeanContent(bean); - cpm.remove(bean); - cpm.save(); - return Response.ok(beanContent).build(); + throw new WebApplicationException(new IllegalArgumentException("Invalid item path: " + itemPath), Response.Status.NOT_FOUND); } + + HippoBeanContent beanContent = createHippoBeanContent(bean, null); + cpm.remove(bean); + cpm.save(); + + return Response.ok().build(); } catch (Exception e) { if (log.isDebugEnabled()) { log.warn("Failed to retrieve content bean.", e); @@ -158,40 +166,28 @@ @POST @Path("/{path:.*}") public Response createContentDocument(@PathParam("path") List<PathSegment> pathSegments, HippoDocumentBeanContent documentBeanContent) { - String itemPath = getContentItemPath(servletRequest, pathSegments); + String parentPath = getContentItemPath(servletRequest, pathSegments); + String itemPath = parentPath + "/" + documentBeanContent.getName(); try { - String primaryType = null; - Collection<PropertyContent> propertyContents = documentBeanContent.getPropertyContents(); + ContentPersistenceManager cpm = getContentPersistenceManager(servletRequest); + PropertyContent primaryTypePropertyContent = documentBeanContent.getPropertyContent("jcr:primaryType"); - if (propertyContents != null) { - for (PropertyContent propertyContent : propertyContents) { - if ("jcr:primaryType".equals(propertyContent.getName())) { - primaryType = propertyContent.getValues()[0].toString(); - break; - } - } + if (primaryTypePropertyContent == null) { + throw new WebApplicationException(new IllegalArgumentException("primary node type name not found."), Response.Status.BAD_REQUEST); } - if (primaryType == null) { - return Response.serverError().status(Response.Status.BAD_REQUEST).build(); - } - - ContentPersistenceManager cpm = getContentPersistenceManager(servletRequest); - int offset = itemPath.lastIndexOf('/'); - String folderPath = itemPath.substring(0, offset); - - cpm.create(folderPath, primaryType, documentBeanContent.getName(), true); + cpm.create(parentPath, (String) primaryTypePropertyContent.getValue(), documentBeanContent.getName(), true); cpm.save(); HippoBean bean = (HippoBean) cpm.getObject(itemPath); if (bean == null) { - return Response.serverError().status(Response.Status.NOT_FOUND).build(); - } else { - documentBeanContent = (HippoDocumentBeanContent) createHippoBeanContent(bean); - return Response.status(Response.Status.CREATED).entity(documentBeanContent).build(); + throw new WebApplicationException(new IllegalArgumentException("Invalid item path: " + itemPath), Response.Status.NOT_FOUND); } + + documentBeanContent = (HippoDocumentBeanContent) createHippoBeanContent(bean, null); + return Response.status(Response.Status.CREATED).entity(documentBeanContent).build(); } catch (Exception e) { if (log.isDebugEnabled()) { log.warn("Failed to save document.", e); @@ -206,23 +202,29 @@ @POST @Path("/node/{path:.*}") public Response createContentNode(@PathParam("path") List<PathSegment> pathSegments, NodeContent nodeContent) { - String itemPath = getContentItemPath(servletRequest, pathSegments); + String parentPath = getContentItemPath(servletRequest, pathSegments); try { ContentPersistenceManager cpm = getContentPersistenceManager(servletRequest); - int offset = itemPath.lastIndexOf('/'); - String parentPath = itemPath.substring(0, offset); - String nodeName = itemPath.substring(offset + 1); - HippoBean bean = (HippoBean) cpm.getObject(parentPath); - HippoBeanContent beanContent = createHippoBeanContent(bean); if (bean == null) { - return Response.serverError().status(Response.Status.NOT_FOUND).build(); - } else { - Node canonicalParentNode = beanContent.getCanonicalNode(); - Node node = canonicalParentNode.addNode(nodeName); + throw new WebApplicationException(new IllegalArgumentException("Invalid item path: " + parentPath), Response.Status.NOT_FOUND); + } + + HippoBeanContent beanContent = createHippoBeanContent(bean, null); + Node canonicalParentNode = beanContent.getCanonicalNode(); + Node node = null; + + if (nodeContent != null) { + String primaryNodeTypeName = nodeContent.getPrimaryNodeTypeName(); + if (primaryNodeTypeName != null) { + node = canonicalParentNode.addNode(nodeContent.getName()); + } else { + node = canonicalParentNode.addNode(nodeContent.getName(), primaryNodeTypeName); + } + Collection<PropertyContent> propertyContents = nodeContent.getPropertyContents(); if (propertyContents != null) { @@ -230,14 +232,13 @@ setPropertyValue(node, propertyContent); } } - - node.getSession().save(); - - bean = (HippoBean) cpm.getObject(parentPath); - beanContent = createHippoBeanContent(bean); - - return Response.status(Response.Status.CREATED).entity(beanContent).build(); } + + node.getSession().save(); + bean = (HippoBean) cpm.getObject(parentPath); + beanContent = createHippoBeanContent(bean, null); + + return Response.status(Response.Status.CREATED).entity(beanContent).build(); } catch (Exception e) { if (log.isDebugEnabled()) { log.warn("Failed to save document.", e); @@ -251,58 +252,26 @@ @PUT @Path("/{path:.*}") - public Response updateContentItem(@PathParam("path") List<PathSegment> pathSegments, ItemContent itemContent) { + public Response updateContentProperty(@PathParam("path") List<PathSegment> pathSegments, PropertyContent propertyContent) { String itemPath = getContentItemPath(servletRequest, pathSegments); try { ContentPersistenceManager cpm = getContentPersistenceManager(servletRequest); - if (itemContent instanceof PropertyContent) { - PropertyContent propertyContent = (PropertyContent) itemContent; - int offset = itemPath.lastIndexOf('/'); - String nodePath = itemPath.substring(0, offset); - - HippoBean bean = (HippoBean) cpm.getObject(nodePath); - - if (bean == null) { - return Response.serverError().status(Response.Status.NOT_FOUND).build(); - } else { - HippoBeanContent beanContent = createHippoBeanContent(bean); - setPropertyValue(beanContent.getCanonicalNode(), propertyContent); - cpm.update(bean); - cpm.save(); - - bean = (HippoBean) cpm.getObject(nodePath); - beanContent = (HippoBeanContent) createHippoBeanContent(bean); - - return Response.status(Response.Status.ACCEPTED).entity(beanContent).build(); - } - } else if (itemContent instanceof HippoBeanContent) { - HippoBean bean = (HippoBean) cpm.getObject(itemPath); - - if (bean == null) { - return Response.serverError().status(Response.Status.NOT_FOUND).build(); - } else { - HippoBeanContent beanContent = createHippoBeanContent(bean); - Collection<PropertyContent> propertyContents = ((HippoBeanContent) itemContent).getPropertyContents(); - - if (propertyContents != null) { - for (PropertyContent propertyContent : propertyContents) { - setPropertyValue(beanContent.getCanonicalNode(), propertyContent); - } - } - - cpm.update(bean); - cpm.save(); - - bean = (HippoBean) cpm.getObject(itemPath); - beanContent = (HippoBeanContent) createHippoBeanContent(bean); - - return Response.status(Response.Status.ACCEPTED).entity(beanContent).build(); - } + HippoBean bean = (HippoBean) cpm.getObject(itemPath); + + if (bean == null) { + throw new WebApplicationException(new IllegalArgumentException("Invalid item path: " + itemPath), Response.Status.NOT_FOUND); } - return Response.status(Response.Status.SERVICE_UNAVAILABLE).build(); + HippoBeanContent beanContent = createHippoBeanContent(bean, null); + + setPropertyValue(beanContent.getCanonicalNode(), propertyContent); + + cpm.update(bean); + cpm.save(); + + return Response.ok().build(); } catch (Exception e) { if (log.isDebugEnabled()) { log.warn("Failed to retrieve save bean.", e); @@ -315,46 +284,53 @@ } private void setPropertyValue(Node canonicalNode, PropertyContent propertyContent) throws Exception { + int type = propertyContent.getType(); boolean isMultiple = Boolean.parseBoolean(propertyContent.getMultiple()); - int propertyType = PropertyType.valueFromName(propertyContent.getType()); Object [] values = propertyContent.getValues(); + Object [] args = null; - switch (propertyType) { - case PropertyType.BOOLEAN: + if (!isMultiple) { + args = new Object [] { propertyContent.getName(), values[0] }; + } else { + args = new Object [] { propertyContent.getName(), propertyContent.getValuesAsString(), new Integer(type) }; + } + + switch (type) { + case PropertyType.BOOLEAN: if (isMultiple) { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { boolean [].class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, String [].class, int.class }); } else { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { boolean.class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, boolean.class }); } break; case PropertyType.NAME: case PropertyType.REFERENCE: case PropertyType.STRING: if (isMultiple) { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { String [].class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, String [].class }); } else { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { String.class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, String.class }); } break; case PropertyType.LONG : if (isMultiple) { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { long [].class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, String [].class, int.class }); } else { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { long.class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, long.class }); } break; case PropertyType.DOUBLE : if (isMultiple) { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { double [].class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, String [].class, int.class }); } else { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { double.class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, double.class }); } break; case PropertyType.DATE : if (isMultiple) { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { Calendar [].class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, String [].class, int.class }); } else { - MethodUtils.invokeExactMethod(canonicalNode, "setProperty", values, new Class [] { Calendar.class }); + MethodUtils.invokeExactMethod(canonicalNode, "setProperty", args, new Class [] { String.class, Calendar.class }); } break; } Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoBeanContent.java =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoBeanContent.java 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoBeanContent.java 2009-11-11 16:11:58 UTC (rev 20630) @@ -15,16 +15,22 @@ */ package org.hippoecm.hst.services.support.jaxrs.content; +import java.util.Set; + import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import org.hippoecm.hst.content.beans.standard.HippoBean; -import org.hippoecm.hst.persistence.ContentPersistenceException; import org.hippoecm.repository.api.HippoNode; -@XmlRootElement(name = "content") +/** + * HippoBeanContent + * + * @version $Id$ + */ +@XmlRootElement(name = "node") public class HippoBeanContent extends NodeContent { private HippoBean bean; @@ -43,7 +49,11 @@ } public HippoBeanContent(HippoBean bean) throws RepositoryException { - super(bean.getNode()); + this(bean, null); + } + + public HippoBeanContent(HippoBean bean, final Set<String> propertyNamesFilledWithValues) throws RepositoryException { + super(bean.getNode(), propertyNamesFilledWithValues); this.bean = bean; Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoDocumentBeanContent.java =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoDocumentBeanContent.java 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoDocumentBeanContent.java 2009-11-11 16:11:58 UTC (rev 20630) @@ -15,12 +15,19 @@ */ package org.hippoecm.hst.services.support.jaxrs.content; +import java.util.Set; + import javax.jcr.RepositoryException; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import org.hippoecm.hst.content.beans.standard.HippoDocumentBean; +/** + * HippoDocumentBeanContent + * + * @version $Id$ + */ @XmlRootElement(name = "document") public class HippoDocumentBeanContent extends HippoBeanContent { @@ -39,7 +46,11 @@ } public HippoDocumentBeanContent(HippoDocumentBean bean) throws RepositoryException { - super(bean); + this(bean, null); + } + + public HippoDocumentBeanContent(HippoDocumentBean bean, final Set<String> propertyNamesFilledWithValues) throws RepositoryException { + super(bean, propertyNamesFilledWithValues); this.canonicalHandleUuid = bean.getCanonicalHandleUUID(); } Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoFolderBeanContent.java =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoFolderBeanContent.java 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/HippoFolderBeanContent.java 2009-11-11 16:11:58 UTC (rev 20630) @@ -32,6 +32,11 @@ import org.hippoecm.hst.content.beans.standard.HippoFolderBean; import org.hippoecm.repository.api.HippoNodeType; +/** + * HippoFolderBeanContent + * + * @version $Id$ + */ @XmlRootElement(name = "folder") public class HippoFolderBeanContent extends HippoBeanContent { @@ -52,7 +57,11 @@ } public HippoFolderBeanContent(HippoFolderBean bean) throws RepositoryException { - super(bean); + this(bean, null); + } + + public HippoFolderBeanContent(HippoFolderBean bean, final Set<String> propertyNamesFilledWithValues) throws RepositoryException { + super(bean, propertyNamesFilledWithValues); Set<String> childNodePathSet = new HashSet<String>(); Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/ItemContent.java =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/ItemContent.java 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/ItemContent.java 2009-11-11 16:11:58 UTC (rev 20630) @@ -26,6 +26,11 @@ import org.apache.commons.lang.StringUtils; +/** + * ItemContent + * + * @version $Id$ + */ @XmlRootElement(name = "item") public class ItemContent { Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/NodeContent.java =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/NodeContent.java 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/NodeContent.java 2009-11-11 16:11:58 UTC (rev 20630) @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Set; import javax.jcr.Node; import javax.jcr.NodeIterator; @@ -30,9 +31,15 @@ import javax.xml.bind.annotation.XmlElements; import javax.xml.bind.annotation.XmlRootElement; +/** + * NodeContent + * + * @version $Id$ + */ @XmlRootElement(name = "node") public class NodeContent extends ItemContent { + private String primaryNodeTypeName; private String uuid; private Collection<PropertyContent> propertyContents; private Collection<NodeContent> childNodeContents; @@ -50,8 +57,14 @@ } public NodeContent(Node node) throws RepositoryException { + this(node, null); + } + + public NodeContent(Node node, final Set<String> propertyNamesFilledWithValues) throws RepositoryException { super(node); + primaryNodeTypeName = node.getPrimaryNodeType().getName(); + if (node.isNodeType("mix:referenceable")) { this.uuid = node.getUUID(); } @@ -60,7 +73,12 @@ for (PropertyIterator it = node.getProperties(); it.hasNext(); ) { Property prop = it.nextProperty(); - propertyContents.add(new PropertyContent(prop.getName(), prop.getPath())); + + if (propertyNamesFilledWithValues != null && propertyNamesFilledWithValues.contains(prop.getName())) { + propertyContents.add(new PropertyContent(prop)); + } else { + propertyContents.add(new PropertyContent(prop.getName(), prop.getPath())); + } } childNodeContents = new ArrayList<NodeContent>(); @@ -75,6 +93,15 @@ } @XmlAttribute + public String getPrimaryNodeTypeName() { + return primaryNodeTypeName; + } + + public void setPrimaryNodeTypeName(String primaryNodeTypeName) { + this.primaryNodeTypeName = primaryNodeTypeName; + } + + @XmlAttribute public String getUuid() { return uuid; } @@ -92,6 +119,18 @@ this.propertyContents = propertyContents; } + public PropertyContent getPropertyContent(String propertyName) { + if (propertyContents != null) { + for (PropertyContent propertyContent : propertyContents) { + if (propertyContent.getName().equals(propertyName)) { + return propertyContent; + } + } + } + + return null; + } + @XmlElements(@XmlElement(name="node")) public Collection<NodeContent> getChildNodeContents() { return childNodeContents; Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/PropertyContent.java =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/PropertyContent.java 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/main/java/org/hippoecm/hst/services/support/jaxrs/content/PropertyContent.java 2009-11-11 16:11:58 UTC (rev 20630) @@ -26,9 +26,16 @@ import javax.xml.bind.annotation.XmlElements; import javax.xml.bind.annotation.XmlRootElement; +import org.apache.jackrabbit.util.ISO8601; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.Element; +/** + * PropertyContent + * + * @version $Id$ + */ @XmlRootElement(name = "property") public class PropertyContent extends ItemContent { @@ -60,11 +67,21 @@ } @XmlAttribute - public String getType() { + public int getType() { + return type; + } + + public void setType(int type) { + this.typeName = PropertyType.nameFromValue(type); + this.type = type; + } + + @XmlAttribute + public String getTypeName() { return typeName; } - public void setType(String typeName) { + public void setTypeName(String typeName) { this.type = PropertyType.valueFromName(typeName); this.typeName = typeName; } @@ -85,8 +102,86 @@ public void setValues(Object [] values) { this.values = values; + + if (this.values != null) { + for (int i = 0; i < this.values.length; i++) { + if (this.values[i] instanceof Element) { + String textContent = ((Element) this.values[i]).getTextContent(); + + if (textContent != null) { + this.values[i] = convertPropertyValue(textContent, type); + } + } + } + } } + public Object getValue() { + if (values != null && values.length > 0) { + return values[0]; + } + + return null; + } + + public String [] getValuesAsString() { + if (values == null) { + return null; + } + + String [] stringValues = new String[values.length]; + + for (int i = 0; i < values.length; i++) { + if (values[i] instanceof Calendar) { + stringValues[i] = ISO8601.format((Calendar) values[i]); + } else { + stringValues[i] = values[i].toString(); + } + } + + return stringValues; + } + + public String getValueAsString() { + Object value = getValue(); + + if (value == null) { + return null; + } + + if (value instanceof Calendar) { + return ISO8601.format((Calendar) value); + } else { + return value.toString(); + } + } + + private Object convertPropertyValue(String textContent, int type) { + Object value = null; + + switch (type) { + case PropertyType.BOOLEAN: + value = Boolean.valueOf(textContent); + break; + case PropertyType.NAME: + case PropertyType.REFERENCE: + case PropertyType.STRING: + value = textContent; + break; + case PropertyType.LONG : + value = Long.valueOf(textContent); + break; + case PropertyType.DOUBLE : + value = Double.valueOf(textContent); + break; + case PropertyType.DATE : + value = ISO8601.parse(textContent); + break; + } + + return value; + } + private void loadPropertyValues(Property p) { try { boolean isMultiple = p.getDefinition().isMultiple(); Modified: ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/test/java/org/hippoecm/hst/services/support/jaxrs/content/TestContentService.java =================================================================== --- ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/test/java/org/hippoecm/hst/services/support/jaxrs/content/TestContentService.java 2009-11-11 12:59:11 UTC (rev 20629) +++ ecm/site-toolkit/trunk/testsuite/sandbox/jaxrs/src/test/java/org/hippoecm/hst/services/support/jaxrs/content/TestContentService.java 2009-11-11 16:11:58 UTC (rev 20630) @@ -16,7 +16,6 @@ package org.hippoecm.hst.services.support.jaxrs.content; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -47,6 +46,11 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; +/** + * TestContentService + * + * @version $Id$ + */ public class TestContentService extends AbstractHstTestCase { private static final String PREVIEW_SITE_CONTENT_PATH = "/testpreview/testproject/hst:content"; @@ -105,7 +109,9 @@ @Test public void testDemo() throws Exception { - // retrieves customer json data... + /* + * retrieves customer json data... + */ MockHttpServletRequest request = new MockHttpServletRequest(servletContext); request.setProtocol("http"); request.setServerName("localhost"); @@ -124,7 +130,9 @@ assertNotNull(response.getContentAsString()); assertEquals("{\"Customer\":{\"id\":123,\"name\":\"John\"}}", response.getContentAsString()); - // updating the existing customer... + /* + * updating the existing customer... + */ request = new MockHttpServletRequest(servletContext); request.setProtocol("http"); request.setServerName("localhost"); @@ -143,7 +151,9 @@ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - // adding a new customer... + /* + * adding a new customer... + */ request = new MockHttpServletRequest(servletContext); request.setProtocol("http"); request.setServerName("localhost"); @@ -165,7 +175,9 @@ assertTrue(response.getContentAsString().startsWith("{\"Customer\":")); assertTrue(response.getContentAsString().contains("\"name\":\"Jisung Park\"")); - // deleting a new customer... + /* + * deleting a new customer... + */ request = new MockHttpServletRequest(servletContext); request.setProtocol("http"); request.setServerName("localhost"); @@ -185,6 +197,9 @@ @Test public void testGetContentNodeByUUID() throws Exception { + /* + * Retrieves folder xml by uuid + */ MockHttpServletRequest request = new MockHttpServletRequest(servletContext); request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); @@ -212,6 +227,9 @@ @Test public void testGetContentItem() throws Exception { + /* + * Retrieves document xml by path + */ MockHttpServletRequest request = new MockHttpServletRequest(servletContext); request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); @@ -242,6 +260,9 @@ assertEquals("http://localhost:8085/testapp/preview/services/contentservice/Products/HippoCMS/HippoCMS/testproject:title", expr.evaluate(document)); + /* + * Retrieves property xml by path + */ request = new MockHttpServletRequest(servletContext); request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); @@ -266,6 +287,9 @@ expr = xpath.compile("string(/property/value)"); assertEquals("Hippo CMS", expr.evaluate(document)); + /* + * Retrieves child node xml of a document by path + */ request = new MockHttpServletRequest(servletContext); request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); @@ -286,11 +310,14 @@ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(response.getContentAsByteArray())); root = document.getDocumentElement(); - assertEquals("content", root.getNodeName()); + assertEquals("node", root.getNodeName()); } @Test public void testDeleteContentNode() throws Exception { + /* + * Delete a document by path + */ MockHttpServletRequest request = new MockHttpServletRequest(servletContext); request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); @@ -310,6 +337,9 @@ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + /* + * Check if the deleted document is not found. + */ request = new MockHttpServletRequest(servletContext); request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); @@ -327,22 +357,226 @@ jaxrsServlet.service(request, response); - assertFalse(Response.Status.OK.getStatusCode() == response.getStatus()); + assertTrue(Response.Status.OK.getStatusCode() != response.getStatus()); } @Test - public void testCreateContentDocument() throws Exception { - // TODO: - } - - @Test public void testCreateContentNode() throws Exception { - // TODO: + /* + * Create a textpage document + */ + MockHttpServletRequest request = new MockHttpServletRequest(servletContext); + request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); + request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); + request.setProtocol("http"); + request.setServerName("localhost"); + request.setServerPort(8085); + request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + request.setMethod("POST"); + request.setRequestURI("/testapp/preview/services/contentservice/Solutions"); + request.setContextPath("/testapp"); + request.setServletPath("/preview/services"); + request.setPathInfo("/contentservice/Solutions"); + request.setContentType("application/xml"); + String content = + "<document name=\"SolutionsPage2\">" + + "<property name=\"jcr:primaryType\" typeName=\"String\">" + + "<value>testproject:textpage</value>" + + "</property>" + + "</document>"; + request.setContent(content.getBytes()); + + MockHttpServletResponse response = new MockHttpServletResponse(); + + jaxrsServlet.service(request, response); + + assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); + + /* + * Check if the created document is found. + */ + request = new MockHttpServletRequest(servletContext); + request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); + request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); + request.setProtocol("http"); + request.setServerName("localhost"); + request.setServerPort(8085); + request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + request.setMethod("GET"); + request.setRequestURI("/testapp/preview/services/contentservice/Solutions/SolutionsPage2"); + request.setContextPath("/testapp"); + request.setServletPath("/preview/services"); + request.setPathInfo("/contentservice/Solutions/SolutionsPage2"); + + response = new MockHttpServletResponse(); + + jaxrsServlet.service(request, response); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + /* + * Check if the created document has body child node. + */ + request = new MockHttpServletRequest(servletContext); + request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); + request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); + request.setProtocol("http"); + request.setServerName("localhost"); + request.setServerPort(8085); + request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + request.setMethod("GET"); + request.setRequestURI("/testapp/preview/services/contentservice/Solutions/SolutionsPage2/SolutionsPage2/testproject:body"); + request.setContextPath("/testapp"); + request.setServletPath("/preview/services"); + request.setPathInfo("/contentservice/Solutions/SolutionsPage2/SolutionsPage2/testproject:body"); + + response = new MockHttpServletResponse(); + + jaxrsServlet.service(request, response); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + /* + * Create a node under the context relative root node. + */ + request = new MockHttpServletRequest(servletContext); + request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); + request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); + request.setProtocol("http"); + request.setServerName("localhost"); + request.setServerPort(8085); + request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + request.setMethod("POST"); + request.setRequestURI("/testapp/preview/services/contentservice/node/jcr:root/testcontent/documents/testproject"); + request.setContextPath("/testapp"); + request.setServletPath("/preview/services"); + request.setPathInfo("/contentservice/node/jcr:root/testcontent/documents/testproject"); + request.setContentType("application/xml"); + content = "<node primaryNodeTypeName=\"hippostd:folder\" name=\"afolder\"/>"; + request.setContent(content.getBytes()); + + response = new MockHttpServletResponse(); + + jaxrsServlet.service(request, response); + + assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); + + /* + * Check if the created node + */ + request = new MockHttpServletRequest(servletContext); + request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); + request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); + request.setProtocol("http"); + request.setServerName("localhost"); + request.setServerPort(8085); + request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + request.setMethod("GET"); + request.setRequestURI("/testapp/preview/services/contentservice/afolder"); + request.setContextPath("/testapp"); + request.setServletPath("/preview/services"); + request.setPathInfo("/contentservice/afolder"); + + response = new MockHttpServletResponse(); + + jaxrsServlet.service(request, response); + + assertTrue(Response.Status.OK.getStatusCode() == response.getStatus()); + Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(response.getContentAsByteArray())); + Element root = document.getDocumentElement(); + assertEquals("folder", root.getNodeName()); } @Test public void testUpdateContentItem() throws Exception { - // TODO: + /* + * Retrieves document xml by path + */ + MockHttpServletRequest request = new MockHttpServletRequest(servletContext); + request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); + request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); + request.setProtocol("http"); + request.setServerName("localhost"); + request.setServerPort(8085); + request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + request.setMethod("GET"); + request.setRequestURI("/testapp/preview/services/contentservice/News/News1/News1"); + request.setContextPath("/testapp"); + request.setServletPath("/preview/services"); + request.setPathInfo("/contentservice/News/News1/News1"); + request.setQueryString("pv=testproject:title&pv=testproject:summary"); + + MockHttpServletResponse response = new MockHttpServletResponse(); + + jaxrsServlet.service(request, response); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(response.getContentAsByteArray())); + Element root = document.getDocumentElement(); + XPath xpath = XPathFactory.newInstance().newXPath(); + XPathExpression expr = xpath.compile("string(/document/property[@name='testproject:title']/value)"); + assertEquals("News Item 1", expr.evaluate(document).trim()); + expr = xpath.compile("string(/document/property[@name='testproject:summary']/value)"); + assertEquals("Summary about news item 1", expr.evaluate(document)); + + /* + * Update properties + */ + request = new MockHttpServletRequest(servletContext); + request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); + request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); + request.setProtocol("http"); + request.setServerName("localhost"); + request.setServerPort(8085); + request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + request.setMethod("PUT"); + request.setRequestURI("/testapp/preview/services/contentservice/News/News1/News1"); + request.setContextPath("/testapp"); + request.setServletPath("/preview/services"); + request.setPathInfo("/contentservice/News/News1/News1"); + request.setQueryString(null); + request.setContentType("application/xml"); + String content = + "<property name=\"testproject:title\" typeName=\"String\">" + + "<value>News Item 1 - updated</value>" + + "</property>"; + request.setContent(content.getBytes()); + + response = new MockHttpServletResponse(); + + jaxrsServlet.service(request, response); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + /* + * Check the changes again. + */ + request = new MockHttpServletRequest(servletContext); + request.setAttribute(ContainerConstants.HST_REQUEST_CONTEXT, hstRequestContext); + request.setAttribute(BaseHstContentService.SITE_CONTENT_PATH, PREVIEW_SITE_CONTENT_PATH); + request.setProtocol("http"); + request.setServerName("localhost"); + request.setServerPort(8085); + request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + request.setMethod("GET"); + request.setRequestURI("/testapp/preview/services/contentservice/News/News1/News1"); + request.setContextPath("/testapp"); + request.setServletPath("/preview/services"); + request.setPathInfo("/contentservice/News/News1/News1"); + request.setQueryString("pv=testproject:title&pv=testproject:summary"); + + response = new MockHttpServletResponse(); + + jaxrsServlet.service(request, response); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(response.getContentAsByteArray())); + root = document.getDocumentElement(); + xpath = XPathFactory.newInstance().newXPath(); + expr = xpath.compile("string(/document/property[@name='testproject:title']/value)"); + assertEquals("News Item 1 - updated", expr.evaluate(document)); + expr = xpath.compile("string(/document/property[@name='testproject:summary']/value)"); + assertEquals("Summary about news item 1", expr.evaluate(document)); } } _______________________________________________ Hippocms-svn mailing list Hippocms-svn@... http://lists.hippo.nl/mailman/listinfo/hippocms-svn |
| Free embeddable forum powered by Nabble | Forum Help |