com.shapestone.session.SessionClient Maven / Gradle / Ivy
The newest version!
package com.shapestone.session;
import com.shapestone.rest.jersey.hystrix.rx.HystrixRxCommand;
import com.shapestone.rest.jersey.hystrix.rx.HystrixRxMethod;
import rx.Observable;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import static java.util.UUID.randomUUID;
import static org.glassfish.jersey.client.rx.rxjava.RxObservable.from;
/**
* Name: Michael Williams
* Date: 9/12/16.
*/
public class SessionClient {
private SessionClientData sessionClientData;
private Client client;
public SessionClient(SessionClientData sessionClientData, Client client) {
this.sessionClientData = sessionClientData;
this.client = client;
}
public Observable getOrCreatedSessionByPartyId(String userAgent, String ipAddress, String acceptLanguage,
String fingerPrint, String partyId, String customerPartyId) {
return this.getOrCreatedSessionByPartyId(userAgent, ipAddress, acceptLanguage, fingerPrint, partyId, customerPartyId, null);
}
@SuppressWarnings("SameParameterValue")
public Observable getOrCreatedSessionByPartyId(String userAgent, String ipAddress, String acceptLanguage,
String fingerPrint, String partyId, String customerPartyId,
String correlationId) {
final HystrixRxMethod> hystrixMethod = () ->
from(client.target(sessionClientData.getUrl()))
.path("/sessions/parties/{partyId}")
.resolveTemplate("partyId", partyId)
.request()
.header("User-Agent", userAgent)
.header("Ip-Address", ipAddress)
.header("Accept-Language", acceptLanguage)
.header("Finger-Print", fingerPrint)
.header("customerPartyId", customerPartyId)
.header("correlationId", determineCorrelationId(correlationId))
.rx()
.post(Entity.json(null), Session.class);
final String commandKey = "getOrCreatedSessionByPartyId";
return new HystrixRxCommand<>(sessionClientData, hystrixMethod, commandKey).observe();
}
public Observable activeSessionExists(String sessionId) {
return this.activeSessionExists(sessionId, null);
}
public Observable activeSessionExists(String sessionId, String correlationId) {
final HystrixRxMethod> hystrixMethod = () ->
from(client.target(sessionClientData.getUrl()))
.path("/sessions/{sessionId}/is-active")
.resolveTemplate("sessionId", sessionId)
.request()
.header("correlationId", determineCorrelationId(correlationId))
.rx()
.get(Boolean.class);
final String commandKey = "activeSessionExists";
return new HystrixRxCommand<>(sessionClientData, hystrixMethod, commandKey).observe();
}
public Observable getSessionById(String sessionId, String correlationId) {
final HystrixRxMethod> hystrixMethod = () ->
from(client.target(sessionClientData.getUrl()))
.path("/sessions/{sessionId}")
.resolveTemplate("sessionId", sessionId)
.request()
.header("correlationId", determineCorrelationId(correlationId))
.rx()
.get(Session.class);
final String commandKey = "getSessionById";
return new HystrixRxCommand<>(sessionClientData, hystrixMethod, commandKey).observe();
}
public Observable getActiveSessionById(String sessionId, String correlationId) {
final HystrixRxMethod> hystrixMethod = () ->
from(client.target(sessionClientData.getUrl()))
.path("/sessions/{sessionId}/active")
.resolveTemplate("sessionId", sessionId)
.request()
.header("correlationId", determineCorrelationId(correlationId))
.rx()
.get(Session.class);
final String commandKey = "getActiveSessionById";
return new HystrixRxCommand<>(sessionClientData, hystrixMethod, commandKey).observe();
}
private String determineCorrelationId(String correlationId) {
return correlationId != null ? correlationId : randomUUID().toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy