Skip to content

Commit

Permalink
Merge branch 'release/0.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Dunn committed Mar 31, 2017
2 parents b6b8d3e + 333d443 commit b1297b2
Show file tree
Hide file tree
Showing 23 changed files with 813 additions and 34 deletions.
3 changes: 3 additions & 0 deletions rcloud-gist-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ dependencies {
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-cache'
compile 'org.springframework.cloud:spring-cloud-starter'
compile 'org.springframework.hateoas:spring-hateoas'
compile 'commons-io:commons-io'
compile 'org.codehaus.groovy:groovy-all'
compile 'com.hazelcast:hazelcast'
compile 'com.hazelcast:hazelcast-spring'

compile 'joda-time:joda-time'
compile 'com.fasterxml.jackson.datatype:jackson-datatype-joda'
compile 'org.apache.commons:commons-collections4:4.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class Application {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2017 AT&T Intellectual Property, [http://www.att.com]
*
* SPDX-License-Identifier: MIT
*
*******************************************************************************/
package com.mangosolutions.rcloud.rawgist;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import com.mangosolutions.rcloud.rawgist.CacheConfigurationProperties.GistCacheConfiguration;

@Configuration
@EnableConfigurationProperties(CacheConfigurationProperties.class)
public class CacheConfiguration {

private final Logger logger = LoggerFactory.getLogger(CacheConfiguration.class);

@Autowired
private CacheConfigurationProperties cacheConfigurationProperties;

@Autowired
private HazelcastInstance hazelcastInstance;

@Bean
public HazelcastCacheManager cacheManager() {
HazelcastCacheManager cacheManager = new HazelcastCacheManager(hazelcastInstance);
configureCaches(cacheManager);
return cacheManager;
}

public void configureCaches(HazelcastCacheManager hazelcastCacheManager) {
HazelcastInstance hazelcastInstance = hazelcastCacheManager.getHazelcastInstance();
for(GistCacheConfiguration cacheConfig: cacheConfigurationProperties.getCaches()) {
String name = cacheConfig.getName();
if(!StringUtils.isEmpty(name)) {
createAndApplyCacheConfiguration(hazelcastInstance, cacheConfig);
}
}
}

private void createAndApplyCacheConfiguration(HazelcastInstance hazelcastInstance, GistCacheConfiguration cacheConfig) {
Config config = hazelcastInstance.getConfig();
Map<String, MapConfig> mapConfigs = config.getMapConfigs();
String cacheName = cacheConfig.getName();
if(mapConfigs.containsKey(cacheName)) {
logger.warn("Altering existing configuration of cache config {}", mapConfigs.get(cacheName));
}
MapConfig mapConfig = new MapConfig(cacheName);//config.getMapConfig(cacheConfig.getName());
mapConfig.setEvictionPercentage(cacheConfig.getEvictionPercentage());
mapConfig.setEvictionPolicy(cacheConfig.getEvictionPolicy());
mapConfig.setTimeToLiveSeconds(cacheConfig.getTtl());
mapConfig.setMinEvictionCheckMillis(cacheConfig.getEvictionCheck());
config.addMapConfig(mapConfig);
logger.info("Configured cache {} with with settings: {}", cacheName, mapConfig);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2017 AT&T Intellectual Property, [http://www.att.com]
*
* SPDX-License-Identifier: MIT
*
*******************************************************************************/
package com.mangosolutions.rcloud.rawgist;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

import com.hazelcast.config.EvictionPolicy;

@ConfigurationProperties()
public class CacheConfigurationProperties {

private List<GistCacheConfiguration> caches;

public List<GistCacheConfiguration> getCaches() {
return caches;
}

public void setCaches(List<GistCacheConfiguration> caches) {
this.caches = caches;
}

public static class GistCacheConfiguration {

private String name;
private int evictionPercentage = 25;
private EvictionPolicy evictionPolicy = EvictionPolicy.LFU;
private int ttl = 300;
private int evictionCheck = 500;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEvictionPercentage() {
return evictionPercentage;
}
public void setEvictionPercentage(int evictionPercentage) {
this.evictionPercentage = evictionPercentage;
}
public EvictionPolicy getEvictionPolicy() {
return evictionPolicy;
}
public void setEvictionPolicy(EvictionPolicy evictionPolicy) {
this.evictionPolicy = evictionPolicy;
}
public int getTtl() {
return ttl;
}
public void setTtl(int ttl) {
this.ttl = ttl;
}
public int getEvictionCheck() {
return evictionCheck;
}
public void setEvictionCheck(int evictionCheck) {
this.evictionCheck = evictionCheck;
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import com.mangosolutions.rcloud.rawgist.repository.GistIdGenerator;
import com.mangosolutions.rcloud.rawgist.repository.GistRepositoryService;
import com.mangosolutions.rcloud.rawgist.repository.GitGistRepositoryService;
Expand Down Expand Up @@ -57,4 +59,9 @@ public Config getHazelCastConfig() {
return config;
}

@Bean
public CacheManager cacheManager() {
return new HazelcastCacheManager(hazelcastInstance); // (3)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter;
Expand All @@ -40,6 +41,8 @@ public class SessionKeyServerSecurityConfiguration extends WebSecurityConfigurer
@Autowired
private SessionKeyServerProperties keyserverProperties;



@Override
protected void configure(HttpSecurity http) throws Exception {
http
Expand All @@ -56,12 +59,9 @@ protected void configure(HttpSecurity http) throws Exception {
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(preauthAuthProvider());
}

@Bean
public UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> userDetailsServiceWrapper() {
UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> wrapper =
new UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken>();


@Bean
public UserDetailsService getSessionKeyServerUserDetailsService() {
SessionKeyServerUserDetailsService service = new SessionKeyServerUserDetailsService();
String serverUrl = keyserverProperties.getUrl();
if(!StringUtils.isEmpty(serverUrl)) {
Expand All @@ -73,8 +73,15 @@ public UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> user
logger.info("Setting the session key URL to {}", serverUrl);
service.setRealm(realm.trim());
}
return service;
}

@Bean
public UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> userDetailsServiceWrapper() {
UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> wrapper =
new UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken>();

wrapper.setUserDetailsService(service);
wrapper.setUserDetailsService(this.getSessionKeyServerUserDetailsService());
return wrapper;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,17 @@ public String getCommentUrl(String gistId, Long commentId, User activeUser) {
}
return url;
}

public String getForksUrl(String gistId, User activeUser) {
String url = null;
if(gistId != null) {
url = linkTo(
methodOn(GistRestController.class)
.forkGist(gistId, activeUser))
.withSelfRel()
.getHref();
}
return url;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -27,6 +32,7 @@

@RestController()
@RequestMapping(value = "/gists/{gistId}/comments", produces={ MediaType.APPLICATION_JSON_VALUE })
@CacheConfig(cacheNames="comments")
public class GistCommentRestController {

@Autowired
Expand All @@ -36,13 +42,15 @@ public class GistCommentRestController {
private ControllerUrlResolver resolver;

@RequestMapping(method=RequestMethod.GET)
@Cacheable(key="#gistId")
public List<GistCommentResponse> getComments(@PathVariable("gistId") String gistId, @AuthenticationPrincipal User activeUser) {
List<GistCommentResponse> comments = repository.getComments(gistId, activeUser);
this.decorateUrls(comments, gistId, activeUser);
return comments;
}

@RequestMapping(value="/{commentId}", method=RequestMethod.GET)
@Cacheable(key="{#gistId, #commentId}")
public GistCommentResponse getComment(@PathVariable("gistId") String gistId, @PathVariable("commentId") long commentId, @AuthenticationPrincipal User activeUser) {
GistCommentResponse response = repository.getComment(gistId, commentId, activeUser);
this.decorateUrls(response, gistId, activeUser);
Expand All @@ -51,13 +59,16 @@ public GistCommentResponse getComment(@PathVariable("gistId") String gistId, @Pa

@RequestMapping(method=RequestMethod.POST)
@ResponseStatus( HttpStatus.CREATED )
@CacheEvict(key="#gistId")
public GistCommentResponse createComment(@PathVariable("gistId") String gistId, @RequestBody GistComment comment, @AuthenticationPrincipal User activeUser) {
GistCommentResponse response = repository.createComment(gistId, comment, activeUser);
this.decorateUrls(response, gistId, activeUser);
return response;
}

@RequestMapping(value="/{commentId}", method=RequestMethod.PATCH)
@CachePut(key="{#gistId, #commentId}")
@Caching(evict = @CacheEvict(key="#gistId"), put = @CachePut(key="{#gistId, #commentId}"))
public GistCommentResponse editComment(@PathVariable("gistId") String gistId, @PathVariable("commentId") long commentId, @RequestBody GistComment comment, @AuthenticationPrincipal User activeUser) {
GistCommentResponse response = repository.editComment(gistId, commentId, comment, activeUser);
this.decorateUrls(response, gistId, activeUser);
Expand All @@ -66,6 +77,7 @@ public GistCommentResponse editComment(@PathVariable("gistId") String gistId, @P

@RequestMapping(value="/{commentId}", method=RequestMethod.DELETE)
@ResponseStatus( HttpStatus.NO_CONTENT )
@Caching(evict = { @CacheEvict(key="#gistId"), @CacheEvict(key="{#gistId, #commentId}") })
public void deleteComment(@PathVariable("gistId") String gistId, @PathVariable("commentId") long commentId, @AuthenticationPrincipal User activeUser) {
repository.deleteComment(gistId, commentId, activeUser);
}
Expand Down
Loading

0 comments on commit b1297b2

Please sign in to comment.