Skip to content

Commit

Permalink
ISPN-7593 Actually use the user-specified context path for the REST e…
Browse files Browse the repository at this point in the history
…ndpoint
  • Loading branch information
tristantarrant authored and ryanemerson committed Mar 13, 2017
1 parent 75431f7 commit 84daa79
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
Expand Up @@ -19,6 +19,7 @@
package org.infinispan.server.endpoint.subsystem; package org.infinispan.server.endpoint.subsystem;


import org.infinispan.rest.configuration.ExtendedHeaders; import org.infinispan.rest.configuration.ExtendedHeaders;
import org.infinispan.rest.configuration.RestServerConfigurationBuilder;
import org.jboss.as.controller.AttributeDefinition; import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.OperationStepHandler; import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.PathElement; import org.jboss.as.controller.PathElement;
Expand Down Expand Up @@ -49,10 +50,11 @@ public class RestConnectorResource extends CommonConnectorResource {


static final SimpleAttributeDefinition CONTEXT_PATH = static final SimpleAttributeDefinition CONTEXT_PATH =
new SimpleAttributeDefinitionBuilder(ModelKeys.CONTEXT_PATH, ModelType.STRING, true) new SimpleAttributeDefinitionBuilder(ModelKeys.CONTEXT_PATH, ModelType.STRING, true)
.setAllowExpression(true) .setAllowExpression(true)
.setXmlName(ModelKeys.CONTEXT_PATH) .setXmlName(ModelKeys.CONTEXT_PATH)
.setRestartAllServices() .setRestartAllServices()
.build(); .setDefaultValue(new ModelNode().set(RestServerConfigurationBuilder.DEFAULT_CONTEXT_PATH))
.build();


static final SimpleAttributeDefinition EXTENDED_HEADERS = static final SimpleAttributeDefinition EXTENDED_HEADERS =
new SimpleAttributeDefinitionBuilder(ModelKeys.EXTENDED_HEADERS, ModelType.STRING, true) new SimpleAttributeDefinitionBuilder(ModelKeys.EXTENDED_HEADERS, ModelType.STRING, true)
Expand Down
Expand Up @@ -54,7 +54,6 @@
* @since 6.0 * @since 6.0
*/ */
public class RestService implements Service<NettyRestServer>, EncryptableService { public class RestService implements Service<NettyRestServer>, EncryptableService {
private static final String DEFAULT_CONTEXT_PATH = "";
private final InjectedValue<PathManager> pathManagerInjector = new InjectedValue<>(); private final InjectedValue<PathManager> pathManagerInjector = new InjectedValue<>();
private final InjectedValue<EmbeddedCacheManager> cacheManagerInjector = new InjectedValue<>(); private final InjectedValue<EmbeddedCacheManager> cacheManagerInjector = new InjectedValue<>();
private final InjectedValue<SocketBinding> socketBinding = new InjectedValue<>(); private final InjectedValue<SocketBinding> socketBinding = new InjectedValue<>();
Expand All @@ -63,40 +62,26 @@ public class RestService implements Service<NettyRestServer>, EncryptableService
private final Map<String, InjectedValue<SecurityRealm>> sniDomains = new HashMap<>(); private final Map<String, InjectedValue<SecurityRealm>> sniDomains = new HashMap<>();


private final RestAuthMethod authMethod; private final RestAuthMethod authMethod;
private final ModelNode config;
private final String serverName; private final String serverName;
private final String contextPath;
private final ExtendedHeaders extendedHeaders;
private final Set<String> ignoredCaches;
private NettyRestServer restServer; private NettyRestServer restServer;
private boolean clientAuth; private boolean clientAuth;


public RestService(String serverName, ModelNode config, RestAuthMethod authMethod) { public RestService(String serverName, RestAuthMethod authMethod, String contextPath, ExtendedHeaders extendedHeaders, Set<String> ignoredCaches) {
this.serverName = serverName; this.serverName = serverName;
this.config = config.clone();
this.authMethod = authMethod; this.authMethod = authMethod;
} this.contextPath = contextPath;

this.extendedHeaders = extendedHeaders;
private String cleanContextPath(String s) { this.ignoredCaches = ignoredCaches;
if (s.endsWith("/")) {
return s.substring(0, s.length() - 1);
} else {
return s;
}
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public synchronized void start(StartContext startContext) throws StartException { public synchronized void start(StartContext startContext) throws StartException {
String path = this.config.hasDefined(ModelKeys.CONTEXT_PATH) ? cleanContextPath(this.config.get(ModelKeys.CONTEXT_PATH).asString()) : DEFAULT_CONTEXT_PATH;

RestServerConfigurationBuilder builder = new RestServerConfigurationBuilder(); RestServerConfigurationBuilder builder = new RestServerConfigurationBuilder();
builder.name(serverName); builder.name(serverName).extendedHeaders(extendedHeaders).ignoredCaches(ignoredCaches).contextPath(contextPath);
if (config.hasDefined(ModelKeys.IGNORED_CACHES)) {
Set<String> ignoredCaches = config.get(ModelKeys.IGNORED_CACHES).asList()
.stream().map(ModelNode::asString).collect(Collectors.toSet());
builder.ignoredCaches(ignoredCaches);
}
builder.extendedHeaders(config.hasDefined(ModelKeys.EXTENDED_HEADERS)
? ExtendedHeaders.valueOf(config.get(ModelKeys.EXTENDED_HEADERS).asString())
: ExtendedHeaders.ON_DEMAND);


EncryptableServiceHelper.fillSecurityConfiguration(this, builder.ssl()); EncryptableServiceHelper.fillSecurityConfiguration(this, builder.ssl());


Expand Down Expand Up @@ -146,7 +131,7 @@ public synchronized void start(StartContext startContext) throws StartException


try { try {
restServer.start(); restServer.start();
ROOT_LOGGER.httpEndpointStarted(protocolName, path, "rest"); ROOT_LOGGER.httpEndpointStarted(protocolName, contextPath, "rest");
} catch (Exception e) { } catch (Exception e) {
throw ROOT_LOGGER.restContextStartFailed(e); throw ROOT_LOGGER.restContextStartFailed(e);
} }
Expand Down
Expand Up @@ -18,6 +18,11 @@
*/ */
package org.infinispan.server.endpoint.subsystem; package org.infinispan.server.endpoint.subsystem;


import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

import org.infinispan.rest.configuration.ExtendedHeaders;
import org.jboss.as.controller.AbstractAddStepHandler; import org.jboss.as.controller.AbstractAddStepHandler;
import org.jboss.as.controller.AttributeDefinition; import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.OperationContext; import org.jboss.as.controller.OperationContext;
Expand Down Expand Up @@ -51,9 +56,16 @@ protected void performRuntime(OperationContext context, ModelNode operation, Mod
authConfig = config.get(ModelKeys.AUTHENTICATION, ModelKeys.AUTHENTICATION_NAME); authConfig = config.get(ModelKeys.AUTHENTICATION, ModelKeys.AUTHENTICATION_NAME);
restAuthMethod = RestAuthMethod.valueOf(RestAuthenticationResource.AUTH_METHOD.resolveModelAttribute(context, authConfig).asString()); restAuthMethod = RestAuthMethod.valueOf(RestAuthenticationResource.AUTH_METHOD.resolveModelAttribute(context, authConfig).asString());
} }
String contextPath = RestConnectorResource.CONTEXT_PATH.resolveModelAttribute(context, config).asString();
ExtendedHeaders extendedHeaders = ExtendedHeaders.valueOf(RestConnectorResource.EXTENDED_HEADERS.resolveModelAttribute(context, config).asString());


Set<String> ignoredCaches = Collections.emptySet();
if (config.hasDefined(ModelKeys.IGNORED_CACHES)) {
ignoredCaches = config.get(ModelKeys.IGNORED_CACHES).asList()
.stream().map(ModelNode::asString).collect(Collectors.toSet());
}
// Create the service // Create the service
final RestService service = new RestService(getServiceName(config), config, restAuthMethod); final RestService service = new RestService(getServiceName(config), restAuthMethod, cleanContextPath(contextPath), extendedHeaders, ignoredCaches);


// Setup the various dependencies with injectors and install the service // Setup the various dependencies with injectors and install the service
ServiceBuilder<?> builder = context.getServiceTarget().addService(EndpointUtils.getServiceName(operation, "rest"), service); ServiceBuilder<?> builder = context.getServiceTarget().addService(EndpointUtils.getServiceName(operation, "rest"), service);
Expand All @@ -75,6 +87,14 @@ protected void performRuntime(OperationContext context, ModelNode operation, Mod
builder.install(); builder.install();
} }


private static String cleanContextPath(String s) {
if (s.endsWith("/")) {
return s.substring(0, s.length() - 1);
} else {
return s;
}
}

protected String getSocketBindingName(ModelNode config) { protected String getSocketBindingName(ModelNode config) {
return config.hasDefined(ModelKeys.SOCKET_BINDING) ? config.get(ModelKeys.SOCKET_BINDING).asString() : null; return config.hasDefined(ModelKeys.SOCKET_BINDING) ? config.get(ModelKeys.SOCKET_BINDING).asString() : null;
} }
Expand Down
Expand Up @@ -10,11 +10,13 @@
public class RestServerConfiguration extends ProtocolServerConfiguration { public class RestServerConfiguration extends ProtocolServerConfiguration {
private final ExtendedHeaders extendedHeaders; private final ExtendedHeaders extendedHeaders;
private final boolean startTransport; private final boolean startTransport;
private final String contextPath;


RestServerConfiguration(ExtendedHeaders extendedHeaders, String host, int port, Set<String> ignoredCaches, SslConfiguration ssl, boolean startTransport) { RestServerConfiguration(ExtendedHeaders extendedHeaders, String host, int port, Set<String> ignoredCaches, SslConfiguration ssl, boolean startTransport, String contextPath) {
super(null, null, host, port, -1, -1, -1, ssl, false, -1, ignoredCaches, startTransport); super(null, null, host, port, -1, -1, -1, ssl, false, -1, ignoredCaches, startTransport);
this.extendedHeaders = extendedHeaders; this.extendedHeaders = extendedHeaders;
this.startTransport = startTransport; this.startTransport = startTransport;
this.contextPath = contextPath;
} }


public ExtendedHeaders extendedHeaders() { public ExtendedHeaders extendedHeaders() {
Expand All @@ -32,4 +34,8 @@ public Set<String> getIgnoredCaches() {
public boolean startTransport() { public boolean startTransport() {
return startTransport; return startTransport;
} }

public String contextPath() {
return contextPath;
}
} }
Expand Up @@ -15,11 +15,13 @@ public class RestServerConfigurationBuilder extends ProtocolServerConfigurationB
Builder<RestServerConfiguration> { Builder<RestServerConfiguration> {


private final static Log logger = LogFactory.getLog(RestServerConfigurationBuilder.class, Log.class); private final static Log logger = LogFactory.getLog(RestServerConfigurationBuilder.class, Log.class);
public static final String DEFAULT_CONTEXT_PATH = "rest";
private boolean startTransport = true; private boolean startTransport = true;


private static final int DEFAULT_PORT = 8080; private static final int DEFAULT_PORT = 8080;


private ExtendedHeaders extendedHeaders = ExtendedHeaders.ON_DEMAND; private ExtendedHeaders extendedHeaders = ExtendedHeaders.ON_DEMAND;
private String contextPath = DEFAULT_CONTEXT_PATH;


public RestServerConfigurationBuilder() { public RestServerConfigurationBuilder() {
super(DEFAULT_PORT); super(DEFAULT_PORT);
Expand All @@ -35,14 +37,20 @@ public RestServerConfigurationBuilder startTransport(boolean startTransport) {
return this; return this;
} }



public RestServerConfigurationBuilder contextPath(String contextPath) {
this.contextPath = contextPath;
return this;
}

@Override @Override
public void validate() { public void validate() {
// Nothing to do // Nothing to do
} }


@Override @Override
public RestServerConfiguration create() { public RestServerConfiguration create() {
return new RestServerConfiguration(extendedHeaders, host, port, ignoredCaches, ssl.create(), startTransport); return new RestServerConfiguration(extendedHeaders, host, port, ignoredCaches, ssl.create(), startTransport, contextPath);
} }


@Override @Override
Expand Down
Expand Up @@ -28,8 +28,6 @@
public final class NettyRestServer extends AbstractCacheIgnoreAware implements Lifecycle { public final class NettyRestServer extends AbstractCacheIgnoreAware implements Lifecycle {
private final static Log log = LogFactory.getLog(NettyRestServer.class, Log.class); private final static Log log = LogFactory.getLog(NettyRestServer.class, Log.class);


private static final String DEFAULT_REST_PATH = "rest";

private final EmbeddedCacheManager cacheManager; private final EmbeddedCacheManager cacheManager;
private final RestServerConfiguration configuration; private final RestServerConfiguration configuration;
private final Authenticator authenticator; private final Authenticator authenticator;
Expand Down Expand Up @@ -94,7 +92,7 @@ public void start() {
addEncryption(configuration, nettyServer); addEncryption(configuration, nettyServer);
nettyServer.start(); nettyServer.start();
ResteasyDeployment deployment = nettyServer.getDeployment(); ResteasyDeployment deployment = nettyServer.getDeployment();
deployment.getRegistry().addSingletonResource(server, DEFAULT_REST_PATH); deployment.getRegistry().addSingletonResource(server, configuration.contextPath());
deployment.getProviderFactory().register(new RestAccessLoggingHandler(), ContainerRequestFilter.class, deployment.getProviderFactory().register(new RestAccessLoggingHandler(), ContainerRequestFilter.class,
ContainerResponseFilter.class); ContainerResponseFilter.class);
log.startRestServer(configuration.host(), configuration.port()); log.startRestServer(configuration.host(), configuration.port());
Expand Down

0 comments on commit 84daa79

Please sign in to comment.