modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java
| 49 +++++++---
modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ContentTest.java
| 8 +
2 files changed, 45 insertions(+), 12 deletions(-)
New commits:
commit 1ceae7f8fc049bd036d03e4c84fadb5a8a057563
Author: Heiko W. Rupp <hwr(a)redhat.com>
Date: Wed Jul 24 11:54:53 2013 +0200
BZ 976786 Add a bit more wait time and an additional check if SUCCESS really means it.
Return IN_PROGRESS otherwise.
diff --git
a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java
b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java
index 4bfbb7c..7a6fb33 100644
---
a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java
+++
b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java
@@ -721,6 +721,9 @@ public class ResourceHandlerBean extends AbstractRestBean {
@POST
@Path("/")
+ @ApiErrors({
+ @ApiError(code = 302, reason = "Creation is still happening. Check back with
a GET on the Location.")
+ })
@ApiOperation(value = "Create a new resource as a child of an existing resource.
",
notes= "If a handle is given, a content based resource is created; the
content identified by the handle is not removed from the content store." +
"If no handle is given, a resource is created from the data of the
passed 'resource' object.")
@@ -824,6 +827,11 @@ public class ResourceHandlerBean extends AbstractRestBean {
CreateResourceStatus status = history.getStatus();
+ try {
+ Thread.sleep(2000L); // give the agent time to do the work
+ } catch (InterruptedException e) {
+ ; // nothing
+ }
MediaType mediaType = headers.getAcceptableMediaTypes().get(0);
@@ -832,11 +840,16 @@ public class ResourceHandlerBean extends AbstractRestBean {
if ( status == CreateResourceStatus.SUCCESS) {
ResourceWithType rwt =
findCreatedResource(history.getParentResource().getId(),history.getCreatedResourceName(),uriInfo);
-
- builder = Response.ok();
- builder.entity(rwt);
+ if (rwt!=null) {
+ builder = Response.ok();
+ builder.entity(rwt);
+ } else {
+ // History says we had success but due to internal timing
+ // the resource is not yet visible, so switch to in_progress
+ status = CreateResourceStatus.IN_PROGRESS;
+ }
}
- else if (status==CreateResourceStatus.IN_PROGRESS) {
+ if (status==CreateResourceStatus.IN_PROGRESS) {
try {
Thread.sleep(2000L); // give the agent time to do the work
@@ -865,6 +878,7 @@ public class ResourceHandlerBean extends AbstractRestBean {
@GET
@Path("/creationStatus/{id}")
@ApiOperation("Get the status of a resource creation for content based
resources.")
+ @ApiError(code = 302, reason = "Creation is still going on. Check back later
with the same URL.")
public Response getHistoryItem(@PathParam("id") int historyId, @Context
HttpHeaders headers, @Context UriInfo uriInfo) {
CreateResourceHistory history;
@@ -888,13 +902,17 @@ public class ResourceHandlerBean extends AbstractRestBean {
if (status== CreateResourceStatus.SUCCESS) {
ResourceWithType rwt =
findCreatedResource(history.getParentResource().getId(),history.getCreatedResourceName(),uriInfo);
-
- builder = Response.ok();
- setCachingHeader(builder, 600);
- builder.entity(rwt);
-
+ if (rwt!=null) {
+ builder = Response.ok();
+ setCachingHeader(builder, 600);
+ builder.entity(rwt);
+ } else {
+ // History says we had success but due to internal timing
+ // the resource is not yet visible, so switch to in_progress
+ status = CreateResourceStatus.IN_PROGRESS;
+ }
}
- else if (status==CreateResourceStatus.IN_PROGRESS) {
+ if (status==CreateResourceStatus.IN_PROGRESS) {
UriBuilder uriBuilder = uriInfo.getRequestUriBuilder();
@@ -913,6 +931,14 @@ public class ResourceHandlerBean extends AbstractRestBean {
}
+ /**
+ * Find the created resource by its name and parent. Will only return it
+ * if the resource is already committed.
+ * @param parentId Id of the parent
+ * @param name Name of the resource to find
+ * @param uriInfo UriInfo object to fill links in the returned resource
+ * @return A ResourceWithType if found, null otherwise.
+ */
private ResourceWithType findCreatedResource(int parentId, String name, UriInfo
uriInfo) {
ResourceCriteria criteria = new ResourceCriteria();
criteria.setStrict(true);
@@ -920,6 +946,9 @@ public class ResourceHandlerBean extends AbstractRestBean {
criteria.addFilterName(name);
criteria.addFilterInventoryStatus(InventoryStatus.COMMITTED);
List<Resource> resources =
resMgr.findResourcesByCriteria(caller,criteria);
+ if (resources.size()==0) {
+ return null;
+ }
Resource res = resources.get(0);
return fillRWT(res,uriInfo);
}
diff --git
a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ContentTest.java
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ContentTest.java
index 8303513..b12eea3 100644
---
a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ContentTest.java
+++
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ContentTest.java
@@ -222,8 +222,10 @@ public class ContentTest extends AbstractBase {
int status = response.getStatusCode();
String location = response.getHeader("Location");
- System.out.println("\nLocation " + location + "\n\n");
- assert location!=null;
+ if (status!=200) {
+ System.out.println("\nLocation " + location +
"\n\n");
+ assert location!=null;
+ }
// We need to check what we got. A 302 means the deploy is still
// in progress, so we need to wait a little longer
@@ -244,6 +246,7 @@ public class ContentTest extends AbstractBase {
createdResourceId = response.jsonPath().getInt("resourceId");
+ System.out.flush();
System.out.println("\n Deploy is done, resource Id = " +
createdResourceId + " \n");
System.out.flush();
@@ -254,6 +257,7 @@ public class ContentTest extends AbstractBase {
// Remove the uploaded content
removeContent(handle, false);
+ System.out.flush();
System.out.println("\n Content removed \n");
System.out.flush();