Skip to content

Commit

Permalink
Fix of default context root with a generated suffix.
Browse files Browse the repository at this point in the history
This is caused by commit 8262ac4 which saves the uploaded deployment file to a temporary file, which automatically generates a suffix.
Signed-off-by:Ondrej Mihalyi <ondrej.mihalyi@payara.fish>
  • Loading branch information
Ondrej Mihalyi committed May 25, 2022
1 parent b403f7c commit 59f862b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.glassfish.main.admin.test.rest;

import jakarta.ws.rs.core.Response;
import java.io.File;

import java.net.URISyntaxException;
import java.util.Map;
Expand Down Expand Up @@ -66,6 +67,26 @@ public void testApplicationDeployment() throws URISyntaxException {
}
}

@Test
public void testApplicationDeploymentWithDefaultContextRoot() throws URISyntaxException {
try {
final File war = getWar("test");
String expectedContextRoot = getFileNameWithoutSuffix(war.getName());
Map<String, String> deployedApp = deployApp(war, null, appName);
assertEquals(appName, deployedApp.get("name"));
assertEquals("/" + expectedContextRoot, deployedApp.get("contextRoot"));
} finally {
undeployApp(appName);
}
}

private static String getFileNameWithoutSuffix(final String filename) {
if (filename.contains(".")) {
return filename.substring(0, filename.lastIndexOf("."));
} else {
return filename;
}
}

@Test
public void testApplicationDisableEnable() throws URISyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,26 @@ public void deleteCluster(String clusterName) {
assertEquals(404, response.getStatus());
}

/*
* Arguments contextRoot and name can be null, then they are not set in the deploy command
*/
public Map<String, String> deployApp(final File archive, final String contextRoot, final String name) {
Map<String, Object> app = Map.of(
"id", archive,
"contextroot", contextRoot,
"name", name
);
Map<String, Object> app = new HashMap<>();
app.put("id", archive);
putIfNotNull("contextroot", contextRoot, app);
putIfNotNull("name", name, app);

Response response = managementClient.postWithUpload(URL_APPLICATION_DEPLOY, app);
assertThat(response.getStatus(), equalTo(200));
return getEntityValues(managementClient.get(URL_APPLICATION_DEPLOY + "/" + app.get("name")));
}

private void putIfNotNull(final String key, Object value, Map<String, Object> app) {
if (value != null) {
app.put(key, value);
}
}

public void addAppRef(final String applicationName, final String targetName){
Response response = managementClient.post("/domain/servers/server/" + targetName + "/application-ref",
Map.of("id", applicationName, "target", targetName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import jakarta.ws.rs.core.PathSegment;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.HttpHeaders;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.glassfish.admin.rest.Constants;
import org.glassfish.admin.rest.RestLogging;
import org.glassfish.admin.rest.utils.xml.RestActionReporter;
Expand Down Expand Up @@ -468,8 +470,7 @@ public static String getMethodParameterList(CommandModel cm, boolean withType, b
public static File saveTemporaryFile(String fileName, InputStream fileStream) {
File file;
try {
String[] parts = getNameAndSuffix(fileName);
file = File.createTempFile(parts[0], parts[1]);
file = Files.createTempDirectory("gf_uploads").resolve(fileName).toFile();
} catch (IOException e) {
throw new IllegalStateException("Could not create a temp file for " + fileName, e);
}
Expand All @@ -479,6 +480,7 @@ public static File saveTemporaryFile(String fileName, InputStream fileStream) {
while ((bytesRead = fileStream.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
file.deleteOnExit();
return file;
} catch (IOException e) {
throw new IllegalStateException("Could not write to a temp file " + file, e);
Expand Down

0 comments on commit 59f862b

Please sign in to comment.