Skip to content

Commit

Permalink
GEODE-7107: Introduce HttpService interface (apache#3963)
Browse files Browse the repository at this point in the history
- This allows for Jetty to be optional on the classpath.
  • Loading branch information
jdeppe-pivotal authored Aug 26, 2019
1 parent 083f1ba commit feb7567
Show file tree
Hide file tree
Showing 21 changed files with 174 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
*/
package org.apache.geode.management.internal;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.net.URI;

import org.hamcrest.text.IsEqualIgnoringCase;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -44,28 +44,27 @@ public void setUp() throws IOException {

@Test
public void testRESTApiExists() {
String gemFireWarLocation = agentUtil.findWarLocation("geode-web-api");
URI gemFireWarLocation = agentUtil.findWarLocation("geode-web-api");
assertNotNull("GemFire REST API WAR File was not found", gemFireWarLocation);
}

@Test
public void testPulseWarExists() {
String gemFireWarLocation = agentUtil.findWarLocation("geode-pulse");
URI gemFireWarLocation = agentUtil.findWarLocation("geode-pulse");
assertNotNull("Pulse WAR File was not found", gemFireWarLocation);
}

@Test
public void testLookupOfWarFileOnClassPath() {
String classpath = System.getProperty("java.class.path");
String gemFireWarLocation = agentUtil.findWarLocation("testWarFile");
URI gemFireWarLocation = agentUtil.findWarLocation("testWarFile");
assertNull(gemFireWarLocation);

classpath =
classpath + System.getProperty("path.separator") + "somelocation/testWarFile.war";
System.setProperty("java.class.path", classpath);
gemFireWarLocation = agentUtil.findWarLocation("testWarFile");
assertNotNull(gemFireWarLocation);
assertThat(gemFireWarLocation,
IsEqualIgnoringCase.equalToIgnoringCase("somelocation/testWarFile.war"));
assertThat(gemFireWarLocation.toString()).endsWith("somelocation/testWarFile.war");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.geode.cache.internal;

import java.nio.file.Path;
import java.util.Map;

import org.apache.geode.internal.cache.InternalCache;

/**
* This interface provides access to the simple http service exposed on the Geode
* {@link InternalCache}. An instance can be retrieved by calling
* {@code InternalCache.getHttpService()}.
*/
public interface HttpService {

/**
* Stop the service. This should also stop any transitively managed components. Starting the
* service is implicitly handled by {@link #addWebApplication(String, Path, Map)}
*/
void stop();

/**
* Add a new web application in the form of a war file. This method is also implicitly
* responsible for starting the container if necessary.
*
* @param webAppContext the context path to be exposed for the web application
* @param warFilePath the absolute path to the war file
* @param attributeNameValuePairs attributes to be set on the servlet context
*/
void addWebApplication(String webAppContext, Path warFilePath,
Map<String, Object> attributeNameValuePairs) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Callable;
Expand All @@ -39,8 +42,6 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.Logger;

import org.apache.geode.CancelException;
Expand Down Expand Up @@ -68,9 +69,9 @@
import org.apache.geode.internal.GemFireVersion;
import org.apache.geode.internal.admin.remote.DistributionLocatorId;
import org.apache.geode.internal.cache.GemFireCacheImpl;
import org.apache.geode.internal.cache.HttpService;
import org.apache.geode.internal.cache.InternalCache;
import org.apache.geode.internal.cache.InternalCacheBuilder;
import org.apache.geode.internal.cache.InternalHttpService;
import org.apache.geode.internal.cache.tier.sockets.TcpServerFactory;
import org.apache.geode.internal.cache.wan.WANServiceProvider;
import org.apache.geode.internal.config.JAXBService;
Expand Down Expand Up @@ -746,27 +747,25 @@ private void startClusterManagementService() throws IOException {
AgentUtil agentUtil = new AgentUtil(GemFireVersion.getGemFireVersion());

// Find the V2 Management rest WAR file
String gemfireManagementWar = agentUtil.findWarLocation("geode-web-management");
URI gemfireManagementWar = agentUtil.findWarLocation("geode-web-management");
if (gemfireManagementWar == null) {
logger.info(
"Unable to find GemFire V2 Management REST API WAR file; the Management REST Interface for Geode will not be accessible.");
return;
}

Pair<String, Object> securityServiceAttribute =
new ImmutablePair<>(HttpService.SECURITY_SERVICE_SERVLET_CONTEXT_PARAM,
internalCache.getSecurityService());
Pair<String, Object> clusterManagementServiceAttribute =
new ImmutablePair<>(HttpService.CLUSTER_MANAGEMENT_SERVICE_CONTEXT_PARAM,
clusterManagementService);
Map<String, Object> serviceAttributes = new HashMap<>();
serviceAttributes.put(InternalHttpService.SECURITY_SERVICE_SERVLET_CONTEXT_PARAM,
internalCache.getSecurityService());
serviceAttributes.put(InternalHttpService.CLUSTER_MANAGEMENT_SERVICE_CONTEXT_PARAM,
clusterManagementService);

if (distributionConfig.getEnableManagementRestService()) {
internalCache.getHttpService().ifPresent(x -> {
try {
logger.info("Geode Property {}=true Geode Management Rest Service is enabled.",
ConfigurationProperties.ENABLE_MANAGEMENT_REST_SERVICE);
x.addWebApplication("/management", gemfireManagementWar, securityServiceAttribute,
clusterManagementServiceAttribute);
x.addWebApplication("/management", Paths.get(gemfireManagementWar), serviceAttributes);
} catch (Throwable e) {
logger.warn("Unable to start management service: {}", e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
import org.apache.geode.cache.client.internal.PoolImpl;
import org.apache.geode.cache.control.ResourceManager;
import org.apache.geode.cache.execute.FunctionService;
import org.apache.geode.cache.internal.HttpService;
import org.apache.geode.cache.query.QueryService;
import org.apache.geode.cache.query.internal.DefaultQueryService;
import org.apache.geode.cache.query.internal.InternalQueryService;
Expand Down Expand Up @@ -939,9 +940,11 @@ public static GemFireCacheImpl getForPdx(String reason) {
httpService = Optional.empty();
} else {
try {
httpService = Optional.of(new HttpService(systemConfig.getHttpServiceBindAddress(),
systemConfig.getHttpServicePort(), SSLConfigurationFactory
.getSSLConfigForComponent(systemConfig, SecurableCommunicationChannel.WEB)));
httpService =
Optional.of(new InternalHttpService(systemConfig.getHttpServiceBindAddress(),
systemConfig.getHttpServicePort(), SSLConfigurationFactory
.getSSLConfigForComponent(systemConfig,
SecurableCommunicationChannel.WEB)));
} catch (Throwable ex) {
logger.warn("Could not enable HttpService: {}", ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.geode.cache.asyncqueue.AsyncEventQueue;
import org.apache.geode.cache.asyncqueue.internal.AsyncEventQueueImpl;
import org.apache.geode.cache.client.internal.ClientMetadataService;
import org.apache.geode.cache.internal.HttpService;
import org.apache.geode.cache.query.QueryService;
import org.apache.geode.cache.query.internal.InternalQueryService;
import org.apache.geode.cache.query.internal.QueryMonitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.geode.cache.asyncqueue.internal.AsyncEventQueueImpl;
import org.apache.geode.cache.client.internal.ClientMetadataService;
import org.apache.geode.cache.control.ResourceManager;
import org.apache.geode.cache.internal.HttpService;
import org.apache.geode.cache.query.QueryService;
import org.apache.geode.cache.query.internal.InternalQueryService;
import org.apache.geode.cache.query.internal.QueryMonitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
package org.apache.geode.internal.cache;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.Connector;
Expand All @@ -36,17 +36,17 @@
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.webapp.WebAppContext;

import org.apache.geode.cache.internal.HttpService;
import org.apache.geode.internal.admin.SSLConfig;
import org.apache.geode.internal.logging.LogService;
import org.apache.geode.management.internal.SSLUtil;

public class HttpService {
public class InternalHttpService implements HttpService {

private static final Logger logger = LogService.getLogger();
private Server httpServer;
private String bindAddress = "0.0.0.0";
private int port;
private SSLConfig sslConfig;
private static final String FILE_PATH_SEPARATOR = System.getProperty("file.separator");
private static final String USER_DIR = System.getProperty("user.dir");
private static final String USER_NAME = System.getProperty("user.name");
Expand All @@ -62,11 +62,10 @@ public class HttpService {

private List<WebAppContext> webApps = new ArrayList<>();

public HttpService(String bindAddress, int port, SSLConfig sslConfig) {
public InternalHttpService(String bindAddress, int port, SSLConfig sslConfig) {
if (port == 0) {
return;
}
this.sslConfig = sslConfig;

this.httpServer = new Server();

Expand Down Expand Up @@ -125,15 +124,16 @@ public HttpService(String bindAddress, int port, SSLConfig sslConfig) {
}
this.port = port;

logger.info("Enabled HttpService on port {}", port);
logger.info("Enabled InternalHttpService on port {}", port);
}

public Server getHttpServer() {
return httpServer;
}

public synchronized void addWebApplication(String webAppContext, String warFilePath,
Pair<String, Object>... attributeNameValuePairs)
@Override
public synchronized void addWebApplication(String webAppContext, Path warFilePath,
Map<String, Object> attributeNameValuePairs)
throws Exception {
if (httpServer == null) {
logger.info(
Expand All @@ -144,14 +144,13 @@ public synchronized void addWebApplication(String webAppContext, String warFileP

WebAppContext webapp = new WebAppContext();
webapp.setContextPath(webAppContext);
webapp.setWar(warFilePath);
webapp.setWar(warFilePath.toString());
webapp.setParentLoaderPriority(false);
webapp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
webapp.addAliasCheck(new AllowSymLinkAliasChecker());

if (attributeNameValuePairs != null) {
Arrays.stream(attributeNameValuePairs)
.forEach(p -> webapp.setAttribute(p.getKey(), p.getValue()));
attributeNameValuePairs.forEach((key, value) -> webapp.setAttribute(key, value));
}

File tmpPath = new File(getWebAppBaseDirectory(webAppContext));
Expand Down Expand Up @@ -183,6 +182,7 @@ private String getWebAppBaseDirectory(final String context) {
return workingDirectory;
}

@Override
public void stop() {
if (this.httpServer == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.apache.geode.cache.client.PoolManager;
import org.apache.geode.cache.client.internal.ClientMetadataService;
import org.apache.geode.cache.client.internal.PoolImpl;
import org.apache.geode.cache.internal.HttpService;
import org.apache.geode.cache.query.CqAttributes;
import org.apache.geode.cache.query.CqQuery;
import org.apache.geode.cache.query.CqServiceStatistics;
Expand Down Expand Up @@ -122,7 +123,6 @@
import org.apache.geode.internal.cache.ExpirationScheduler;
import org.apache.geode.internal.cache.FilterProfile;
import org.apache.geode.internal.cache.GemFireCacheImpl;
import org.apache.geode.internal.cache.HttpService;
import org.apache.geode.internal.cache.InitialImageOperation;
import org.apache.geode.internal.cache.InternalCache;
import org.apache.geode.internal.cache.InternalCacheForClientAccess;
Expand Down
Loading

0 comments on commit feb7567

Please sign in to comment.