Commit 51e7bc07 by Jason Song Committed by GitHub

Merge pull request #362 from nobodyiam/client-no-appid

Client no appid
parents 0a5424ed e540d883
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-biz</artifactId>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -7,12 +7,13 @@ import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.MetaDomainConsts;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.enums.EnvUtils;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.foundation.Foundation;
import com.dianping.cat.Cat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Named;
import org.unidal.net.Networks;
import java.util.concurrent.TimeUnit;
......@@ -41,12 +42,15 @@ public class ConfigUtil {
/**
* Get the app id for the current application.
*
* @return the app id
* @throws IllegalStateException if app id is not set
* @return the app id or ConfigConsts.NO_APPID_PLACEHOLDER if app id is not available
*/
public String getAppId() {
String appId = Foundation.app().getAppId();
Preconditions.checkState(appId != null, "app.id is not set");
if (Strings.isNullOrEmpty(appId)) {
appId = ConfigConsts.NO_APPID_PLACEHOLDER;
logger.error("app.id is not set, apollo will only load public namespace configurations!");
Cat.logError(new ApolloConfigException("app.id is not set"));
}
return appId;
}
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -59,20 +59,21 @@ public class ConfigController {
@RequestParam(value = "ip", required = false) String clientIp,
HttpServletResponse response) throws IOException {
String originalNamespace = namespace;
//strip out .properties suffix
namespace = namespaceUtil.filterNamespaceName(namespace);
List<Release> releases = Lists.newLinkedList();
Release currentAppRelease = loadConfig(appId, clusterName, namespace, dataCenter);
String appClusterNameLoaded = clusterName;
if (!ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
Release currentAppRelease = loadConfig(appId, clusterName, namespace, dataCenter);
if (currentAppRelease != null) {
releases.add(currentAppRelease);
//we have cluster search process, so the cluster name might be overridden
appClusterNameLoaded = currentAppRelease.getClusterName();
}
}
//if namespace does not belong to this appId, should check if there is a public configuration
if (!namespaceBelongsToAppId(appId, namespace)) {
......@@ -116,6 +117,11 @@ public class ConfigController {
return true;
}
//if no appId is present, then no other namespace belongs to it
if (ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
return false;
}
AppNamespace appNamespace = appNamespaceService.findOne(appId, namespaceName);
return appNamespace != null;
......
......@@ -40,6 +40,7 @@ public class WatchKeysUtil {
/**
* Assemble watch keys for the given appId, cluster, namespaces, dataCenter combination
*
* @return a multimap with namespace as the key and watch keys as the value
*/
public Multimap<String, String> assembleAllWatchKeys(String appId, String clusterName,
......@@ -91,7 +92,9 @@ public class WatchKeysUtil {
private Set<String> assembleWatchKeys(String appId, String clusterName, String namespace,
String dataCenter) {
if (ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
return Collections.emptySet();
}
Set<String> watchedKeys = Sets.newHashSet();
//watch specified cluster config change
......@@ -124,6 +127,9 @@ public class WatchKeysUtil {
}
private Set<String> namespacesBelongToAppId(String appId, Set<String> namespaces) {
if (ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
return Collections.emptySet();
}
List<AppNamespace> appNamespaces =
appNamespaceService.findByAppIdAndNamespaces(appId, namespaces);
......
......@@ -30,6 +30,7 @@ import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -431,6 +432,50 @@ public class ConfigControllerTest {
configController.mergeReleaseConfigurations(Lists.newArrayList(someRelease));
}
@Test
public void testQueryConfigForNoAppIdPlaceHolder() throws Exception {
String someClientSideReleaseKey = "1";
HttpServletResponse someResponse = mock(HttpServletResponse.class);
String appId = ConfigConsts.NO_APPID_PLACEHOLDER;
ApolloConfig result = configController.queryConfig(appId, someClusterName,
defaultNamespaceName, someDataCenter, someClientSideReleaseKey,
someClientIp, someResponse);
verify(releaseService, never()).findLatestActiveRelease(appId, someClusterName, defaultNamespaceName);
verify(appNamespaceService, never()).findPublicNamespaceByName(defaultNamespaceName);
assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
}
@Test
public void testQueryConfigForNoAppIdPlaceHolderWithPublicNamespace() throws Exception {
String someClientSideReleaseKey = "1";
String someServerSideReleaseKey = "2";
HttpServletResponse someResponse = mock(HttpServletResponse.class);
String somePublicAppId = "somePublicAppId";
AppNamespace somePublicAppNamespace =
assemblePublicAppNamespace(somePublicAppId, somePublicNamespaceName);
String appId = ConfigConsts.NO_APPID_PLACEHOLDER;
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName))
.thenReturn(somePublicAppNamespace);
when(releaseService.findLatestActiveRelease(somePublicAppId, someDataCenter, somePublicNamespaceName))
.thenReturn(somePublicRelease);
when(somePublicRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey);
ApolloConfig result = configController.queryConfig(appId, someClusterName,
somePublicNamespaceName, someDataCenter, someClientSideReleaseKey,
someClientIp, someResponse);
verify(releaseService, never()).findLatestActiveRelease(appId, someClusterName, somePublicNamespaceName);
assertEquals(someServerSideReleaseKey, result.getReleaseKey());
assertEquals(appId, result.getAppId());
assertEquals(someClusterName, result.getCluster());
assertEquals(somePublicNamespaceName, result.getNamespaceName());
assertEquals("foo", result.getConfigurations().get("apollo.public.bar"));
}
private AppNamespace assemblePublicAppNamespace(String appId, String namespace) {
return assembleAppNamespace(appId, namespace, true);
}
......
......@@ -217,4 +217,37 @@ public class ConfigControllerIntegrationTest extends AbstractBaseIntegrationTest
assertEquals("v1-file", result.getConfigurations().get("k1"));
assertEquals(null, result.getConfigurations().get("k2"));
}
@Test
public void testQueryConfigForNoAppIdPlaceHolderWithPrivateNamespace() throws Exception {
HttpStatusCodeException httpException = null;
try {
ResponseEntity<ApolloConfig> response = restTemplate
.getForEntity("{baseurl}/configs/{appId}/{clusterName}/{namespace}", ApolloConfig.class,
getHostUrl(), ConfigConsts.NO_APPID_PLACEHOLDER, someCluster, ConfigConsts.NAMESPACE_APPLICATION);
} catch (HttpStatusCodeException ex) {
httpException = ex;
}
assertEquals(HttpStatus.NOT_FOUND, httpException.getStatusCode());
}
@Test
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryPublicConfigForNoAppIdPlaceHolder() throws Exception {
ResponseEntity<ApolloConfig> response = restTemplate
.getForEntity("{baseurl}/configs/{appId}/{clusterName}/{namespace}?dataCenter={dateCenter}",
ApolloConfig.class,
getHostUrl(), ConfigConsts.NO_APPID_PLACEHOLDER, someCluster, somePublicNamespace, someDC);
ApolloConfig result = response.getBody();
assertEquals("TEST-RELEASE-KEY4", result.getReleaseKey());
assertEquals(ConfigConsts.NO_APPID_PLACEHOLDER, result.getAppId());
assertEquals(someCluster, result.getCluster());
assertEquals(somePublicNamespace, result.getNamespaceName());
assertEquals("someDC-v1", result.getConfigurations().get("k1"));
assertEquals("someDC-v2", result.getConfigurations().get("k2"));
}
}
......@@ -75,6 +75,8 @@ public class WatchKeysUtilTest {
when(somePublicAppNamespace.getName()).thenReturn(somePublicNamespace);
when(appNamespaceService.findPublicNamespacesByNames(Sets.newHashSet(somePublicNamespace)))
.thenReturn(Lists.newArrayList(somePublicAppNamespace));
when(appNamespaceService.findPublicNamespacesByNames(Sets.newHashSet(someNamespace, somePublicNamespace)))
.thenReturn(Lists.newArrayList(somePublicAppNamespace));
ReflectionTestUtils.setField(watchKeysUtil, "appNamespaceService", appNamespaceService);
}
......@@ -141,6 +143,28 @@ public class WatchKeysUtilTest {
assertWatchKeys(somePublicAppId, clusters, somePublicNamespace, watchKeysMap.get(somePublicNamespace));
}
@Test
public void testAssembleWatchKeysForNoAppIdPlaceHolder() throws Exception {
Multimap<String, String> watchKeysMap =
watchKeysUtil.assembleAllWatchKeys(ConfigConsts.NO_APPID_PLACEHOLDER, someCluster,
Sets.newHashSet(someNamespace, anotherNamespace), someDC);
assertTrue(watchKeysMap.isEmpty());
}
@Test
public void testAssembleWatchKeysForNoAppIdPlaceHolderAndPublicNamespace() throws Exception {
Multimap<String, String> watchKeysMap =
watchKeysUtil.assembleAllWatchKeys(ConfigConsts.NO_APPID_PLACEHOLDER, someCluster,
Sets.newHashSet(someNamespace, somePublicNamespace), someDC);
Set<String> clusters = Sets.newHashSet(defaultCluster, someCluster, someDC);
assertEquals(clusters.size(), watchKeysMap.size());
assertWatchKeys(somePublicAppId, clusters, somePublicNamespace, watchKeysMap.get(somePublicNamespace));
}
private void assertWatchKeys(String appId, Set<String> clusters, String namespaceName,
Collection<String> watchedKeys) {
for (String cluster : clusters) {
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -6,4 +6,5 @@ public interface ConfigConsts {
String CLUSTER_NAMESPACE_SEPARATOR = "+";
String APOLLO_CLUSTER_KEY = "apollo.cluster";
String CONFIG_FILE_CONTENT_KEY = "content";
String NO_APPID_PLACEHOLDER = "ApolloNoAppIdPlaceHolder";
}
......@@ -4,7 +4,7 @@
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-demo</artifactId>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.7</version>
<version>0.0.8-SNAPSHOT</version>
<name>Apollo</name>
<packaging>pom</packaging>
<description>Ctrip Configuration Center</description>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment