Skip to content

Commit

Permalink
Merge pull request #352 from elandau/admin_jersey_props
Browse files Browse the repository at this point in the history
Properties to configure jersey under admin resources
  • Loading branch information
elandau authored May 19, 2017
2 parents 531dcab + fdf2871 commit aad08b3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 52 deletions.
40 changes: 30 additions & 10 deletions karyon2-admin/src/main/java/netflix/admin/AdminConfigImpl.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package netflix.admin;

import com.google.inject.Injector;
import com.netflix.config.ConfigurationManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.Filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Injector;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.util.ConfigurationUtils;

@Singleton
public class AdminConfigImpl implements AdminContainerConfig {
private static final Logger logger = LoggerFactory.getLogger(AdminConfigImpl.class);

public static final String NETFLIX_ADMIN_TEMPLATE_CONTEXT = "netflix.admin.template.context";
public static final String ADMIN_PREFIX = "netflix.admin.";

public static final String NETFLIX_ADMIN_TEMPLATE_CONTEXT = ADMIN_PREFIX + "template.context";
public static final String TEMPLATE_CONTEXT_DEFAULT = "/admin";

public static final String NETFLIX_ADMIN_RESOURCE_CONTEXT = "netflix.admin.resource.context";
public static final String NETFLIX_ADMIN_RESOURCE_CONTEXT = ADMIN_PREFIX + "resource.context";
public static final String RESOURCE_CONTEXT_DEFAULT = "/webadmin";

private static final String JERSEY_CORE_RESOURCES = "netflix.platform.admin.resources.core.packages";
Expand All @@ -36,12 +43,15 @@ public class AdminConfigImpl implements AdminContainerConfig {
public static final String SERVER_ENABLE_PROP_NAME = "netflix.platform.admin.resources.enable";
public static final boolean SERVER_ENABLE_DEFAULT = true;

public static final String NETFLIX_ADMIN_RESOURCES_ISOLATE = "netflix.admin.resources.isolate";
public static final String NETFLIX_ADMIN_RESOURCES_ISOLATE = ADMIN_PREFIX + "resources.isolate";
public static final boolean ISOLATE_RESOURCES_DEFAULT = false;

public static final String NETFLIX_ADMIN_CTX_FILTERS = "netflix.admin.additional.filters";
public static final String NETFLIX_ADMIN_CTX_FILTERS = ADMIN_PREFIX + "additional.filters";
public static final String DEFAULT_CONTEXT_FILTERS = "";

private static final String JERSEY_PROPERTY_PREFIX = "com.sun.jersey.config";
private static final String ADMIN_JERSEY_PROPERTY_PREFIX = ADMIN_PREFIX + JERSEY_PROPERTY_PREFIX;

private final Injector injector;

public AdminConfigImpl() {
Expand Down Expand Up @@ -93,6 +103,16 @@ public int listenPort() {
return ConfigurationManager.getConfigInstance().getInt(CONTAINER_LISTEN_PORT, LISTEN_PORT_DEFAULT);
}

@Override
public Map<String, Object> getJerseyConfigProperties() {
Map<String, Object> result = new HashMap<>();
Properties props = ConfigurationUtils.getProperties(ConfigurationManager.getConfigInstance().subset(ADMIN_JERSEY_PROPERTY_PREFIX));
for (Entry<Object, Object> prop : props.entrySet()) {
result.put(JERSEY_PROPERTY_PREFIX + "." + prop.getKey().toString(), prop.getValue().toString());
}
return result;
}

@Override
public List<Filter> additionalFilters() {
String rootContextFilters = ConfigurationManager.getConfigInstance().getString(NETFLIX_ADMIN_CTX_FILTERS, DEFAULT_CONTEXT_FILTERS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package netflix.admin;

import com.google.inject.ImplementedBy;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import java.util.List;

import com.google.inject.ImplementedBy;

@ImplementedBy(AdminConfigImpl.class)
public interface AdminContainerConfig {
Expand All @@ -16,5 +18,6 @@ public interface AdminContainerConfig {
boolean shouldScanClassPathForPluginDiscovery();
int listenPort();
List<Filter> additionalFilters();
Map<String, Object> getJerseyConfigProperties();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

package netflix.adminresources;

import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Singleton;
import com.google.inject.Stage;
import com.netflix.governator.guice.LifecycleInjector;
import com.netflix.governator.lifecycle.LifecycleManager;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.Filter;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
Expand All @@ -40,15 +44,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.Filter;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Singleton;
import com.google.inject.Stage;
import com.netflix.governator.guice.LifecycleInjector;
import com.netflix.governator.lifecycle.LifecycleManager;
import com.sun.jersey.api.core.PackagesResourceConfig;

import netflix.admin.AdminConfigImpl;
import netflix.admin.AdminContainerConfig;
Expand Down Expand Up @@ -139,7 +143,11 @@ public void init() throws Exception {

// admin page template resources
AdminResourcesFilter arfTemplatesResources = adminResourceInjector.getInstance(AdminResourcesFilter.class);
arfTemplatesResources.setPackages(adminContainerConfig.jerseyViewableResourcePkgList());
Map<String, Object> props = new HashMap<>(adminContainerConfig.getJerseyConfigProperties());
props.put(PackagesResourceConfig.PROPERTY_PACKAGES,
adminContainerConfig.jerseyViewableResourcePkgList() + ";" +
Objects.toString(props.get(PackagesResourceConfig.PROPERTY_PACKAGES)));
arfTemplatesResources.setProperties(props);

logger.info("Admin templates context : {}", adminContainerConfig.templateResourceContext());
final Context adminTemplatesResHandler = new Context();
Expand All @@ -152,9 +160,12 @@ public void init() throws Exception {
adminTemplatesResHandler.addServlet(new ServletHolder(new DefaultServlet()), "/*");

// admin page data resources
final String jerseyPkgListForAjaxResources = appendCoreJerseyPackages(adminPageRegistry.buildJerseyResourcePkgListForAdminPages());
AdminResourcesFilter arfDataResources = adminResourceInjector.getInstance(AdminResourcesFilter.class);
arfDataResources.setPackages(jerseyPkgListForAjaxResources);
props = new HashMap<>(adminContainerConfig.getJerseyConfigProperties());
props.put(PackagesResourceConfig.PROPERTY_PACKAGES,
appendCoreJerseyPackages(adminPageRegistry.buildJerseyResourcePkgListForAdminPages()) + ";" +
Objects.toString(props.get(PackagesResourceConfig.PROPERTY_PACKAGES)));
arfDataResources.setProperties(props);

logger.info("Admin resources context : {}", adminContainerConfig.ajaxDataResourceContext());
final Context adminDataResHandler = new Context();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package netflix.adminresources;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;
import javax.servlet.ServletException;

import com.google.inject.Injector;
import com.netflix.explorers.providers.FreemarkerTemplateProvider;
import com.netflix.explorers.providers.WebApplicationExceptionMapper;
Expand All @@ -8,12 +16,6 @@
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
import com.sun.jersey.spi.container.servlet.WebConfig;

import java.util.Map;
import java.util.Set;

import javax.inject.Inject;
import javax.servlet.ServletException;

import netflix.admin.AdminFreemarkerTemplateProvider;

/**
Expand All @@ -22,29 +24,19 @@
* The AdminResources app needs minimal features and this class provides those.
*/
class AdminResourcesFilter extends GuiceContainer {
private volatile String packages;
private Map<String, Object> props = Collections.emptyMap();

@Inject
AdminResourcesFilter(Injector injector) {
super(injector);
}

/**
* Set the packages for Jersey to scan for resources
*
* @param packages packages to scan
*/
void setPackages(String packages) {
this.packages = packages;
}

@Override
protected ResourceConfig getDefaultResourceConfig(Map<String, Object> props,
WebConfig webConfig) throws ServletException {
props.put(PackagesResourceConfig.PROPERTY_PACKAGES, packages);
props.put(ResourceConfig.FEATURE_DISABLE_WADL, "false");

return new PackagesResourceConfig(props) {
HashMap<String, Object> mergedProps = new HashMap<>(props);
mergedProps.putAll(this.props);
return new PackagesResourceConfig(mergedProps) {
@Override
public Set<Class<?>> getProviderClasses() {
Set<Class<?>> providers = super.getProviderClasses();
Expand All @@ -56,4 +48,8 @@ public Set<Class<?>> getProviderClasses() {
}
};
}

public void setProperties(Map<String, Object> props) {
this.props = new HashMap<>(props);
}
}

0 comments on commit aad08b3

Please sign in to comment.