Skip to content

Commit

Permalink
FISH-785 Setting default-web-module disables /metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jGauravGupta committed Nov 27, 2020
1 parent 9758ea0 commit f8e09ad
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
*/
package fish.payara.microprofile.metrics.rest;

import com.sun.enterprise.config.serverbeans.Configs;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.VirtualServer;
import fish.payara.microprofile.metrics.admin.MetricsServiceConfiguration;
import static java.util.Arrays.asList;
import java.util.Map;
Expand All @@ -52,15 +55,24 @@
import static javax.servlet.annotation.ServletSecurity.TransportGuarantee.CONFIDENTIAL;
import static org.glassfish.common.util.StringHelper.isEmpty;
import org.glassfish.internal.api.Globals;
import java.util.HashSet;
import static org.glassfish.api.web.Constants.ADMIN_VS;

public class MetricsServletContainerInitializer implements ServletContainerInitializer {

private static final String DAS_CONFIG_NAME = "server-config";

@Override
public void onStartup(Set<Class<?>> set, ServletContext ctx) throws ServletException {

MetricsServiceConfiguration configuration = Globals.getDefaultBaseServiceLocator()
.getService(MetricsServiceConfiguration.class);
if (!"".equals(ctx.getContextPath())) {
String contextPath = ctx.getContextPath();
contextPath = contextPath.substring(
0 + (contextPath.startsWith("/") ? 1 : 0),
contextPath.length() - (contextPath.endsWith("/") ? 1 : 0)
);
if (!getWebModulesPath().contains(contextPath)) {
return;
}

Expand All @@ -87,4 +99,26 @@ public void onStartup(Set<Class<?>> set, ServletContext ctx) throws ServletExcep
ctx.declareRoles(roles);
}
}

private Set<String> getWebModulesPath() {
Set<String> paths = new HashSet<>();
Configs configs = Globals.getDefaultBaseServiceLocator().getService(Configs.class);
Config serverConfig = configs.getConfigByName(DAS_CONFIG_NAME);
for (VirtualServer virtualServer : serverConfig.getHttpService().getVirtualServer()) {
if (ADMIN_VS.equals(virtualServer.getId())) {
continue;
}
String path = virtualServer.getDefaultWebModule();
if (path != null) {
path = path.substring(
0 + (path.startsWith("/") ? 1 : 0),
path.length() - (path.endsWith("/") ? 1 : 0)
);
} else {
path = "";
}
paths.add(path);
}
return paths;
}
}

0 comments on commit f8e09ad

Please sign in to comment.