Index: trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/ResourceUtil.java =================================================================== --- trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/ResourceUtil.java (revision 46274) +++ trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/ResourceUtil.java (revision 46275) @@ -41,6 +41,7 @@ package org.glassfish.admin.rest; +import com.sun.enterprise.v3.common.ActionReporter; import java.util.Locale; import org.glassfish.admin.rest.generator.CommandResourceMetaData; import java.lang.reflect.Method; @@ -77,7 +78,6 @@ import org.jvnet.hk2.config.DomDocument; import com.sun.enterprise.config.serverbeans.Config; import com.sun.enterprise.config.serverbeans.Domain; -import org.glassfish.admin.rest.generator.ResourcesGeneratorBase; import org.glassfish.admin.rest.provider.MethodMetaData; import org.glassfish.admin.rest.provider.ParameterMetaData; import org.glassfish.admin.rest.provider.ProviderUtil; @@ -552,13 +552,21 @@ return Response.status(status).entity(message).build(); } - // FIXME: This should take ActionReport as a param public static ActionReportResult getActionReportResult(int status, String message, HttpHeaders requestHeaders, UriInfo uriInfo) { + return getActionReportResult(status, null, message, requestHeaders, uriInfo); + } + + public static ActionReportResult getActionReportResult(int status, ActionReport parentActionReport, String message, HttpHeaders requestHeaders, UriInfo uriInfo) { if (isBrowser(requestHeaders)) { message = getHtml(message, uriInfo, false); } RestActionReporter ar = new RestActionReporter(); + if (parentActionReport != null) { + ar.getSubActionsReport().addAll(((ActionReporter)parentActionReport).getSubActionsReport()); + } + ActionReportResult result = new ActionReportResult(ar); + if ((status >= 200) && (status <= 299)) { ar.setSuccess(); } else { Index: trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/resources/TemplateRestResource.java =================================================================== --- trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/resources/TemplateRestResource.java (revision 46274) +++ trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/resources/TemplateRestResource.java (revision 46275) @@ -141,12 +141,12 @@ ActionReporter ar = Util.applyChanges(data, uriInfo, habitat); if(ar.getActionExitCode() != ActionReport.ExitCode.SUCCESS) { //TODO better error handling. - return Response.status(400).entity(ResourceUtil.getActionReportResult(400, "Could not apply changes" + ar.getMessage(), requestHeaders, uriInfo)).build(); + return Response.status(400).entity(ResourceUtil.getActionReportResult(400, ar, "Could not apply changes" + ar.getMessage(), requestHeaders, uriInfo)).build(); } String successMessage = localStrings.getLocalString("rest.resource.update.message", "\"{0}\" updated successfully.", uriInfo.getAbsolutePath()); - return Response.ok(ResourceUtil.getActionReportResult(200, successMessage, requestHeaders, uriInfo)).build(); + return Response.ok(ResourceUtil.getActionReportResult(200, ar, successMessage, requestHeaders, uriInfo)).build(); } catch (Exception ex) { if (ex.getCause() instanceof ValidationException) { return Response.status(400).entity(ResourceUtil.getActionReportResult(400, ex.getMessage(), requestHeaders, uriInfo)).build(); @@ -215,17 +215,17 @@ if (exitCode != ActionReport.ExitCode.FAILURE) { String successMessage = localStrings.getLocalString("rest.resource.delete.message", "\"{0}\" deleted successfully.", new Object[]{uriInfo.getAbsolutePath()}); - return Response.ok(ResourceUtil.getActionReportResult(200, successMessage, requestHeaders, uriInfo)).build(); //200 - ok + return Response.ok(ResourceUtil.getActionReportResult(200, actionReport, successMessage, requestHeaders, uriInfo)).build(); //200 - ok } String errorMessage = actionReport.getMessage(); - return Response.status(400).entity(ResourceUtil.getActionReportResult(400, errorMessage, requestHeaders, uriInfo)).build(); //400 - bad request + return Response.status(400).entity(ResourceUtil.getActionReportResult(400, actionReport, errorMessage, requestHeaders, uriInfo)).build(); //400 - bad request } String message = localStrings.getLocalString("rest.resource.delete.forbidden", "DELETE on \"{0}\" is forbidden.", new Object[]{uriInfo.getAbsolutePath()}); - return Response.status(400).entity(ResourceUtil.getActionReportResult(403, message, requestHeaders, uriInfo)).build(); //403 - forbidden + return Response.status(400).entity(ResourceUtil.getActionReportResult(403, actionReport, message, requestHeaders, uriInfo)).build(); //403 - forbidden } catch (Exception e) { throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR); } Index: trunk/v3/admin/rest/src/test/java/org/glassfish/admin/rest/ApplicationTest.java =================================================================== --- trunk/v3/admin/rest/src/test/java/org/glassfish/admin/rest/ApplicationTest.java (revision 46285) +++ trunk/v3/admin/rest/src/test/java/org/glassfish/admin/rest/ApplicationTest.java (revision 46286) @@ -53,6 +53,7 @@ import java.util.Map; import org.junit.Test; import static org.junit.Assert.*; +import static org.junit.Assert.*; /** * @@ -60,6 +61,7 @@ */ public class ApplicationTest extends RestTestBase { public static final String URL_APPLICATION_DEPLOY = "/domain/applications/application"; + public static final String URL_CODI_SAMPLE = "http://java.net/jira/secure/attachment/44850/GlassfishIssues.war"; public static final String URL_CREATE_INSTANCE = "/domain/create-instance"; public static final String URL_SUB_COMPONENTS = "/domain/applications/application/list-sub-components"; @@ -71,36 +73,43 @@ put("contextroot", appName); put("name", appName); }}; + try { + Map deployedApp = deployApp(newApp); + assertEquals(appName, deployedApp.get("name")); - Map deployedApp = deployApp(newApp); - assertEquals(appName, deployedApp.get("name")); - - assertEquals("/" + appName, deployedApp.get("contextRoot")); - - undeployApp(newApp); + assertEquals("/" + appName, deployedApp.get("contextRoot")); + } finally { + undeployApp(newApp); + } } -// @Test - // Disabled until GLASSFISH-15905 is fixed + @Test public void deployCodiApp() throws URISyntaxException, MalformedURLException, IOException { -// http://java.net/jira/secure/attachment/44850/GlassfishIssues.war - final String appName = "testApp" + generateRandomString(); - final File file = -// new File (new File(System.getProperty("java.io.tmpdir")), "GlassfishIssues.war"); - downloadFile(new URL("http://java.net/jira/secure/attachment/44850/GlassfishIssues.war")); - Map newApp = new HashMap() {{ - put("id", file); - put("contextroot", appName); - put("name", appName); - }}; + try { + final String appName = "testApp" + generateRandomString(); - Map deployedApp = deployApp(newApp); - assertEquals(appName, deployedApp.get("name")); + Map newApp = new HashMap() {{ + put("id", downloadFile(new URL(URL_CODI_SAMPLE))); + put("contextroot", appName); + put("name", appName); + }}; - assertEquals("/" + appName, deployedApp.get("contextRoot")); + Map params = new HashMap(); + params.put("name", "CloudBeesDS"); + params.put("poolName", "DerbyPool"); - undeployApp(newApp); - file.delete(); + ClientResponse response = post (JdbcTest.BASE_JDBC_RESOURCE_URL, params); + assertTrue(isSuccess(response)); + + Map deployedApp = deployApp(newApp); + assertEquals(appName, deployedApp.get("name")); + + assertEquals("/" + appName, deployedApp.get("contextRoot")); + + undeployApp(newApp); + } finally { + delete(JdbcTest.BASE_JDBC_RESOURCE_URL + "/CloudBeesDS"); + } } @Test @@ -165,28 +174,6 @@ } } - protected File getFile(String fileName) throws URISyntaxException { - final URL resource = getClass().getResource("/" + fileName); - return new File(resource.toURI()); - } - - protected File downloadFile(URL url) throws IOException { - File file = File.createTempFile("test", ""); - file.deleteOnExit(); - BufferedInputStream in = new BufferedInputStream(url.openStream()); - FileOutputStream fos = new FileOutputStream(file); - BufferedOutputStream bout = new BufferedOutputStream(fos, 1024); - byte data[] = new byte[8192]; - while (in.read(data, 0, 8192) >= 0) { - bout.write(data); - data = new byte[8192]; - } - bout.close(); - in.close(); - - return file; - } - @Test public void testCreatingAndDeletingApplicationRefs() throws URISyntaxException { final String instanceName = "instance_" + generateRandomString(); @@ -253,17 +240,88 @@ undeployApp(newApp); } } + + @Test + public void testUndeploySubActionWarnings() throws URISyntaxException { + final String appName = "testApp" + generateRandomString(); + final String serverName = "in" + generateRandomNumber(); + Map newApp = new HashMap() {{ + put("id", getFile("test.war")); + put("contextroot", appName); + put("name", appName); + }}; + try { + ClientResponse response = post ("/domain/create-instance", new HashMap() {{ + put("id", serverName); + put("node", "localhost-domain1"); + }}); + checkStatusForSuccess(response); + response = post("/domain/servers/server/" + serverName + "/start-instance"); + checkStatusForSuccess(response); + + deployApp(newApp); + addAppRef(appName, serverName); + + response = post("/domain/servers/server/" + serverName + "/stop-instance"); + checkStatusForSuccess(response); + + response = delete ("/domain/applications/application/"+appName, new HashMap() {{ + put("target", "domain"); + }}); + assertTrue(response.getEntity(String.class).contains("WARNING: Instance " + serverName + " seems to be offline")); + } finally { + delete ("/domain/applications/application/" + appName, new HashMap() {{ + put("target", "domain"); + }}); + } + } + + protected File getFile(String fileName) throws URISyntaxException { + final URL resource = getClass().getResource("/" + fileName); + return new File(resource.toURI()); + } + + protected File downloadFile(URL url) throws IOException { + String urlText = url.getFile(); + String fileName = urlText.substring(urlText.lastIndexOf("/")+1); + File file = new File(fileName); + file.deleteOnExit(); + BufferedInputStream in = new BufferedInputStream(url.openStream()); + FileOutputStream fos = new FileOutputStream(file); + BufferedOutputStream bout = new BufferedOutputStream(fos, 1024); + byte data[] = new byte[8192]; + int read = in.read(data, 0, 8192); + while (read >= 0) { + bout.write(data, 0, read); + data = new byte[8192]; + read = in.read(data, 0, 8192); + } + bout.close(); + in.close(); + + return file; + } + protected Map deployApp(Map app) { ClientResponse response = postWithUpload(URL_APPLICATION_DEPLOY, app); checkStatusForSuccess(response); return getEntityValues(get(URL_APPLICATION_DEPLOY + "/" + app.get("name"))); } - - protected void undeployApp(Map app) { + + protected void addAppRef(final String applicationName, final String targetName){ + ClientResponse cr = post("/domain/servers/server/" + targetName + "/application-ref", new HashMap() {{ + put("id", applicationName); + put("target", targetName); + }}); + checkStatusForSuccess(cr); + } + + protected ClientResponse undeployApp(Map app) { ClientResponse response = delete(URL_APPLICATION_DEPLOY + "/" + app.get("name")); checkStatusForSuccess(response); - response = delete(URL_APPLICATION_DEPLOY + "/stateles-simple"); + + return response; } } Index: trunk/v3/admin/rest/src/test/java/org/glassfish/admin/rest/RestTestBase.java =================================================================== --- trunk/v3/admin/rest/src/test/java/org/glassfish/admin/rest/RestTestBase.java (revision 46285) +++ trunk/v3/admin/rest/src/test/java/org/glassfish/admin/rest/RestTestBase.java (revision 46286) @@ -267,7 +267,9 @@ protected void checkStatusForSuccess(ClientResponse cr) { int status = cr.getStatus(); if ((status < 200) || (status > 299)) { - fail("Expected a status between 200 and 299 (inclusive). Found " + status); + String message = getErrorMessage(cr); + fail("Expected a status between 200 and 299 (inclusive). Found " + status + + ((message != null) ? ": " + message : "")); } } @@ -277,6 +279,16 @@ fail("Expected a status less than 200 or greater than 299 (inclusive). Found " + status); } } + + protected String getErrorMessage(ClientResponse cr) { + String message = null; + Map map = MarshallingUtils.buildMapFromDocument(cr.getEntity(String.class)); + if (map != null) { + message = (String)map.get("message"); + } + + return message; + } protected static String getParameter(String paramName, String defaultValue) { String value = System.getenv(paramName); Index: trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/ResourceUtil.java =================================================================== --- trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/ResourceUtil.java (revision 46285) +++ trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/ResourceUtil.java (revision 46286) @@ -552,6 +552,10 @@ return Response.status(status).entity(message).build(); } + public static ActionReportResult getActionReportResult(Response.Status status, String message, HttpHeaders requestHeaders, UriInfo uriInfo) { + return getActionReportResult(status.getStatusCode(), null, message, requestHeaders, uriInfo); + } + public static ActionReportResult getActionReportResult(int status, String message, HttpHeaders requestHeaders, UriInfo uriInfo) { return getActionReportResult(status, null, message, requestHeaders, uriInfo); } Index: trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/resources/TemplateListOfResource.java =================================================================== --- trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/resources/TemplateListOfResource.java (revision 46285) +++ trunk/v3/admin/rest/src/main/java/org/glassfish/admin/rest/resources/TemplateListOfResource.java (revision 46286) @@ -127,17 +127,17 @@ ActionReport.ExitCode exitCode = actionReport.getActionExitCode(); if (exitCode != ActionReport.ExitCode.FAILURE) { String successMessage = - localStrings.getLocalString("rest.resource.create.message", - "\"{0}\" created successfully.", resourceToCreate); + localStrings.getLocalString("rest.resource.create.message", + "\"{0}\" created successfully.", resourceToCreate); ActionReportResult arr = ResourceUtil.getActionReportResult(201, successMessage, requestHeaders, uriInfo); return Response.ok(arr).build(); } String errorMessage = getErrorMessage(data, actionReport); - ActionReportResult arr = ResourceUtil.getActionReportResult(400, errorMessage, requestHeaders, uriInfo); - return Response.status(400).entity(arr).build(); + ActionReportResult arr = ResourceUtil.getActionReportResult(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, requestHeaders, uriInfo); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(arr).build(); } else { - ActionReportResult arr = ResourceUtil.getActionReportResult(400, "No CRUD Create possible.", requestHeaders, uriInfo); + ActionReportResult arr = ResourceUtil.getActionReportResult(Response.Status.INTERNAL_SERVER_ERROR, "No CRUD Create possible.", requestHeaders, uriInfo); return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(arr).build(); } } catch (Exception e) {