Skip to content
Merged

Sb2 #17

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ The project supports only Couchbase 4 and higher versions. For more information
- remove `@EnableCouchbaseHttpSession` annotation
- replace `session-couchbase.persistent.couchbase` properties with `spring.couchbase` in the _application.yml_ file

## Migrating from 2.x.x to 3.x.x

- `session-couchbase.timeout-in-seconds` `int` property in the _application.yml_ file is replaced by `session-couchbase.timeout` `Duration` property

## Installing

```gradle
repositories {
mavenCentral()
}
dependencies {
compile 'com.github.mkopylec:session-couchbase-spring-boot-starter:2.2.0'
compile group: 'com.github.mkopylec', name: 'session-couchbase-spring-boot-starter', version: '3.0.0'
}
```

Expand All @@ -44,7 +48,7 @@ Simply use `HttpSession` interface to control HTTP session. For example:
@Controller
public class SessionController {

@GetMapping("uri")
@GetMapping("/uri")
public void doSomething(HttpSession session) {
...
}
Expand Down Expand Up @@ -93,17 +97,16 @@ The session is visible only within a single web application instance and will be
The mode is useful for integration tests when you don't want to communicate with the real Couchbase server instance.

## Namespaces
The starter supports HTTP session namespaces.
The starter supports HTTP session namespaces to prevent session attribute's names conflicts in a distributed systems like platforms composed with micro-services.
The name of the namespace can be set in _application.yml_ file:

```yaml
session-couchbase:
application-namespace: <application_namespace>
```

Each web application in a distributed system has one application namespace under which the session attributes are stored.
Each web application in a distributed system can have one application namespace under which the application's session attributes are stored.
Every web application can also access global session attributes which are visible across the whole distributed system.
Namespaces prevent conflicts in attributes names between different web applications in the system.
Two web applications can have the same namespace and therefore access the same session attributes.
If two web applications have different namespaces they cannot access each others session attributes.

Expand Down Expand Up @@ -143,7 +146,7 @@ When changing HTTP session ID every attribute is copied to the new session, no m

```yaml
session-couchbase:
timeout-in-seconds: 1800 # HTTP session timeout.
timeout: 30m # HTTP session timeout.
application-namespace: default # HTTP session application namespace under which session data must be stored.
principal-sessions:
enabled: false # Flag for enabling and disabling finding HTTP sessions by principal. Can significantly decrease application performance when enabled.
Expand Down
25 changes: 7 additions & 18 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ plugins {
id 'groovy'
id 'maven-publish'
id 'jacoco'
id 'nebula.optional-base' version '3.2.0'
id 'com.github.kt3k.coveralls' version '2.8.2'
id 'maven'
id 'signing'
id 'pl.allegro.tech.build.axion-release' version '1.9.0'
id 'pl.allegro.tech.build.axion-release' version '1.9.2'
id 'io.codearte.nexus-staging' version '0.11.0'
}

Expand All @@ -27,35 +26,25 @@ repositories {
mavenCentral()
}

ext.springBootVersion = '1.5.11.RELEASE'
ext.springBootVersion = '2.0.3.RELEASE'

dependencies {

compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: springBootVersion
compile("org.springframework.boot:spring-boot-starter-data-couchbase:$springBootVersion") {
exclude group: 'org.springframework', module: 'spring-web'
}
compile group: 'org.springframework.session', name: 'spring-session', version: '1.3.2.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-couchbase', version: springBootVersion
compile group: 'org.springframework.session', name: 'spring-session-core', version: '2.0.4.RELEASE'
compile group: 'org.springframework.retry', name: 'spring-retry', version: '1.2.2.RELEASE'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.9'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'

compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: springBootVersion, optional
compileOnly group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: springBootVersion

testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBootVersion
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-undertow', version: springBootVersion
testCompile group: 'org.spockframework', name: 'spock-spring', version: '1.1-groovy-2.4'
}

configurations {
all*.exclude group: 'org.mortbay.jetty', module: 'servlet-api'
}

task wrapper(type: Wrapper) {
gradleVersion = '4.6'
gradleVersion = '4.8'
}

task javadocJar(type: Jar) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,27 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.mkopylec.sessioncouchbase.core.CouchbaseSessionRepository;
import com.github.mkopylec.sessioncouchbase.core.DelegatingSessionStrategy;
import com.github.mkopylec.sessioncouchbase.core.Serializer;
import com.github.mkopylec.sessioncouchbase.data.SessionDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.SessionRepository;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
import org.springframework.session.web.http.CookieHttpSessionStrategy;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.MultiHttpSessionStrategy;

@Configuration
@EnableSpringHttpSession
@EnableConfigurationProperties(SessionCouchbaseProperties.class)
public class SessionCouchbaseAutoConfiguration {

protected SessionCouchbaseProperties sessionCouchbase;
protected CookieSerializer cookieSerializer;

public SessionCouchbaseAutoConfiguration(SessionCouchbaseProperties sessionCouchbase) {
this.sessionCouchbase = sessionCouchbase;
}

@Bean
@ConditionalOnMissingBean
public MultiHttpSessionStrategy multiHttpSessionStrategy(SessionDao dao) {
CookieHttpSessionStrategy sessionStrategy = new CookieHttpSessionStrategy();
if (cookieSerializer != null) {
sessionStrategy.setCookieSerializer(cookieSerializer);
}
return new DelegatingSessionStrategy(sessionStrategy, dao);
}

@Bean
@ConditionalOnMissingBean
public Serializer serializer() {
Expand All @@ -57,9 +40,4 @@ public SessionRepository sessionRepository(SessionDao dao, ObjectMapper mapper,
public ObjectMapper objectMapper() {
return new ObjectMapper();
}

@Autowired(required = false)
public void setCookieSerializer(CookieSerializer cookieSerializer) {
this.cookieSerializer = cookieSerializer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.time.Duration;

import static com.couchbase.client.java.query.consistency.ScanConsistency.REQUEST_PLUS;
import static java.time.Duration.ofMinutes;
import static org.apache.commons.lang3.StringUtils.trimToNull;

/**
* Session couchbase configuration properties.
Expand All @@ -15,7 +19,7 @@ public class SessionCouchbaseProperties {
/**
* HTTP session timeout.
*/
private int timeoutInSeconds = 1800;
private Duration timeout = ofMinutes(30);
/**
* HTTP session application namespace under which session data must be stored.
*/
Expand All @@ -36,16 +40,16 @@ public class SessionCouchbaseProperties {
@NestedConfigurationProperty
private InMemory inMemory = new InMemory();

public int getTimeoutInSeconds() {
return timeoutInSeconds;
public Duration getTimeout() {
return timeout;
}

public void setTimeoutInSeconds(int timeoutInSeconds) {
this.timeoutInSeconds = timeoutInSeconds;
public void setTimeout(Duration timeout) {
this.timeout = timeout;
}

public String getApplicationNamespace() {
return applicationNamespace;
return trimToNull(applicationNamespace);
}

public void setApplicationNamespace(String applicationNamespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.springframework.session.ExpiringSession;
import org.springframework.session.Session;

import java.io.Serializable;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static java.lang.System.currentTimeMillis;
import static java.time.Duration.ofSeconds;
import static java.time.Instant.now;
import static java.time.Instant.ofEpochSecond;
import static java.util.Collections.unmodifiableSet;
import static java.util.UUID.randomUUID;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toSet;
import static org.apache.commons.lang3.StringUtils.removeStart;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;
import static org.springframework.util.Assert.hasText;
import static org.springframework.util.Assert.isTrue;

public class CouchbaseSession implements ExpiringSession, Serializable {

private static final long serialVersionUID = 1L;
public class CouchbaseSession implements Session {

public static final String CREATION_TIME_ATTRIBUTE = "$creationTime";
public static final String LAST_ACCESSED_TIME_ATTRIBUTE = "$lastAccessedTime";
Expand All @@ -33,20 +33,24 @@ public class CouchbaseSession implements ExpiringSession, Serializable {

private static final Logger log = getLogger(CouchbaseSession.class);

protected String id = randomUUID().toString();
protected String id = generateSessionId();

protected Map<String, Object> globalAttributesToUpdate = new HashMap<>();

protected Set<String> globalAttributesToRemove = new HashSet<>();
protected Map<String, Object> globalAttributes = new HashMap<>();
protected Map<String, Object> namespaceAttributesToUpdate = new HashMap<>();
protected Set<String> namespaceAttributesToRemove = new HashSet<>();
protected Map<String, Object> namespaceAttributes = new HashMap<>();
protected boolean principalSessionsUpdateRequired = false;
protected boolean idChanged = false;
protected String oldId;

public CouchbaseSession(int timeoutInSeconds) {
long now = currentTimeMillis();
public CouchbaseSession(Duration timeout) {
Instant now = now();
setCreationTime(now);
setLastAccessedTime(now);
setMaxInactiveIntervalInSeconds(timeoutInSeconds);
setMaxInactiveInterval(timeout);
}

public CouchbaseSession(String id, Map<String, Object> globalAttributes, Map<String, Object> namespaceAttributes) {
Expand All @@ -63,41 +67,52 @@ public static String globalAttributeName(String attributeName) {
}

@Override
public long getCreationTime() {
return getNumericGlobalAttributeValue(CREATION_TIME_ATTRIBUTE);
public Instant getCreationTime() {
return getDateGlobalAttributeValue(CREATION_TIME_ATTRIBUTE);
}

@Override
public long getLastAccessedTime() {
return getNumericGlobalAttributeValue(LAST_ACCESSED_TIME_ATTRIBUTE);
public void setLastAccessedTime(Instant lastAccessedTime) {
globalAttributes.put(LAST_ACCESSED_TIME_ATTRIBUTE, lastAccessedTime.getEpochSecond());
globalAttributesToUpdate.put(LAST_ACCESSED_TIME_ATTRIBUTE, lastAccessedTime.getEpochSecond());
}

public void setLastAccessedTime(long lastAccessedTime) {
globalAttributes.put(LAST_ACCESSED_TIME_ATTRIBUTE, lastAccessedTime);
globalAttributesToUpdate.put(LAST_ACCESSED_TIME_ATTRIBUTE, lastAccessedTime);
@Override
public Instant getLastAccessedTime() {
return getDateGlobalAttributeValue(LAST_ACCESSED_TIME_ATTRIBUTE);
}

@Override
public void setMaxInactiveIntervalInSeconds(int interval) {
globalAttributes.put(MAX_INACTIVE_INTERVAL_ATTRIBUTE, interval);
globalAttributesToUpdate.put(MAX_INACTIVE_INTERVAL_ATTRIBUTE, interval);
public void setMaxInactiveInterval(Duration interval) {
globalAttributes.put(MAX_INACTIVE_INTERVAL_ATTRIBUTE, interval.getSeconds());
globalAttributesToUpdate.put(MAX_INACTIVE_INTERVAL_ATTRIBUTE, interval.getSeconds());
}

@Override
public int getMaxInactiveIntervalInSeconds() {
return (int) globalAttributes.get(MAX_INACTIVE_INTERVAL_ATTRIBUTE);
public Duration getMaxInactiveInterval() {
long interval = getNumericGlobalAttributeValue(MAX_INACTIVE_INTERVAL_ATTRIBUTE);
return ofSeconds(interval);
}

@Override
public boolean isExpired() {
return getMaxInactiveIntervalInSeconds() >= 0 && currentTimeMillis() - SECONDS.toMillis(getMaxInactiveIntervalInSeconds()) >= getLastAccessedTime();
return now().minus(getMaxInactiveInterval()).compareTo(getLastAccessedTime()) >= 0;
}

@Override
public String getId() {
return id;
}

@Override
public String changeSessionId() {
oldId = id;
id = generateSessionId();
idChanged = true;
log.debug("HTTP session ID has changed from {} to {}", oldId, id);
return id;
}

@SuppressWarnings("unchecked")
@Override
public <T> T getAttribute(String attributeName) {
Expand Down Expand Up @@ -205,6 +220,14 @@ public boolean isPrincipalSessionsUpdateRequired() {
return principalSessionsUpdateRequired;
}

public boolean isIdChanged() {
return idChanged;
}

public String getOldId() {
return oldId;
}

public String getPrincipalAttribute() {
Object principal = globalAttributes.get(PRINCIPAL_NAME_INDEX_NAME);
if (principal == null) {
Expand All @@ -217,9 +240,9 @@ public void unsetPrincipalSessionsUpdateRequired() {
principalSessionsUpdateRequired = false;
}

protected void setCreationTime(long creationTime) {
globalAttributes.put(CREATION_TIME_ATTRIBUTE, creationTime);
globalAttributesToUpdate.put(CREATION_TIME_ATTRIBUTE, creationTime);
protected void setCreationTime(Instant creationTime) {
globalAttributes.put(CREATION_TIME_ATTRIBUTE, creationTime.getEpochSecond());
globalAttributesToUpdate.put(CREATION_TIME_ATTRIBUTE, creationTime.getEpochSecond());
}

protected void checkAttributeName(String attributeName) {
Expand All @@ -239,7 +262,16 @@ protected boolean containsPrincipalAttribute() {
return globalAttributes.containsKey(PRINCIPAL_NAME_INDEX_NAME) || namespaceAttributes.containsKey(PRINCIPAL_NAME_INDEX_NAME);
}

protected Instant getDateGlobalAttributeValue(String attributeName) {
long attributeValue = getNumericGlobalAttributeValue(attributeName);
return ofEpochSecond(attributeValue);
}

protected long getNumericGlobalAttributeValue(String attributeName) {
return ((Number) globalAttributes.get(attributeName)).longValue();
}

protected String generateSessionId() {
return randomUUID().toString();
}
}
Loading