All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.shapestone.session.SessionController Maven / Gradle / Ivy

The newest version!
package com.shapestone.session;

import org.slf4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.Optional;

import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;

/**
 * Name: Michael Williams
 * Date: 9/11/16.
 */
@RestController
public class SessionController {

  private static final Logger log = getLogger(SessionController.class.getName());
  private SessionService sessionService;

  public SessionController(SessionService sessionService) {
    this.sessionService = sessionService;
  }

  @RequestMapping(value = "/sessions/parties/{partyId}", method = POST)
  public DeferredResult> getOrCreateSession(@PathVariable("partyId") String partyId,
                                                                    @RequestHeader("User-Agent") String userAgent,
                                                                    @RequestHeader(value = "Ip-Address", required = false) String inetAddress,
                                                                    @RequestHeader("Accept-Language") String acceptLanguage,
                                                                    @RequestHeader(value = "Finger-Print", required = false) String fingerPrint,
                                                                    @RequestHeader("customerPartyId") String customerPartyId,
                                                                    @RequestHeader("correlationId") String correlationId) {
    final DeferredResult> deferredResult = new DeferredResult<>();
    //noinspection CodeBlock2Expr
    final Session session = createSession(partyId, userAgent, inetAddress, acceptLanguage, fingerPrint, customerPartyId);
    sessionService
      .getOrCreatePartySession(session)
      .subscribe(newSessionData -> {
          deferredResult.setResult(new ResponseEntity<>(newSessionData, OK));
      }, throwable -> {
        if (deferredResult.isSetOrExpired()) {
          final String message = "Processing of non-blocking request {} with request id #{} already expired";
          log.warn(message, "getOrCreatePartySession", correlationId);
        } else {
          deferredResult.setErrorResult(new RuntimeException(throwable.getMessage()));
        }
      }, () -> {
        if (!deferredResult.isSetOrExpired()) {
          deferredResult.setErrorResult(new RuntimeException("The request completed without data or error."));
        }
      });
    return deferredResult;
  }

  private Session createSession(String partyId, String userAgent, String ipAddress, String language, String fingerPrint, String customerPartyId) {
    return Session.builder()
      .partyId(partyId)
      .userAgent(userAgent)
      .ipAddress(ipAddress)
      .language(language)
      .fingerPrint(fingerPrint)
      .customerPartyId(customerPartyId)
      .build();
  }

  @RequestMapping(value = "/sessions/{sessionId}/is-active", method = GET)
  public DeferredResult> activeSessionExists(@PathVariable("sessionId") String sessionId,
                                                 @RequestHeader(value="correlationId") String correlationId) {
    final DeferredResult> deferredResult = new DeferredResult<>();
    //noinspection CodeBlock2Expr
    sessionService
      .isSessionActive(sessionId)
      .subscribe(isActive -> {
        deferredResult.setResult(new ResponseEntity<>(isActive, OK));
      }, throwable -> {
        if (deferredResult.isSetOrExpired()) {
          final String message = "Processing of non-blocking request {} with request id #{} already expired";
          log.warn(message, "activeSessionExists", correlationId);
        } else {
          deferredResult.setErrorResult(new RuntimeException(throwable.getMessage()));
        }
      }, () -> {
        if (!deferredResult.isSetOrExpired()) {
          deferredResult.setErrorResult(new RuntimeException("The request completed without data or error."));
        }
      });
    return deferredResult;
  }

  @RequestMapping(value = "/sessions", method = POST)
  public DeferredResult> addSession(@RequestBody Session session,
                                                            @RequestHeader(value="correlationId") String correlationId) {
    final DeferredResult> deferredResult = new DeferredResult<>();
    //noinspection CodeBlock2Expr
    sessionService
      .addSession(session)
      .subscribe(newSessionData -> {
          deferredResult.setResult(new ResponseEntity<>(newSessionData, OK));
      }, throwable -> {
        if (deferredResult.isSetOrExpired()) {
          final String message = "Processing of non-blocking request {} with request id #{} already expired";
          log.warn(message, "addSession", correlationId);
        } else {
          deferredResult.setErrorResult(new RuntimeException(throwable.getMessage()));
        }
      }, () -> {
        if (!deferredResult.isSetOrExpired()) {
          deferredResult.setErrorResult(new RuntimeException("The request completed without data or error."));
        }
      });
    return deferredResult;
  }

  @RequestMapping(value = "/sessions/{sessionId}", method = GET)
  public DeferredResult> getSession(@PathVariable("sessionId") String sessionId,
                                                            @RequestHeader(value="correlationId") String correlationId) {
    final DeferredResult> deferredResult = new DeferredResult<>();
    sessionService
      .getSessionById(sessionId)
      .subscribe(newSessionData -> {
        deferredResult.setResult(new ResponseEntity<>(newSessionData, OK));
      }, throwable -> {
        if (deferredResult.isSetOrExpired()) {
          final String message = "Processing of non-blocking request {} with request id #{} already expired";
          log.warn(message, "getSession", correlationId);
        } else {
          deferredResult.setErrorResult(new RuntimeException(throwable.getMessage()));
        }
      }, () -> {
        if (!deferredResult.isSetOrExpired()) {
        deferredResult.setErrorResult(new RuntimeException("The request completed without data or error."));
      }
    });
    return deferredResult;
  }

  @RequestMapping(value = "/sessions/{sessionId}/active", method = GET)
  public DeferredResult> getActiveSessionById(@PathVariable("sessionId") String sessionId,
                                                                      @RequestHeader(value="correlationId") String correlationId) {
    final DeferredResult> deferredResult = new DeferredResult<>();
    sessionService
      .getActiveSessionById(sessionId)
      .subscribe(newSessions -> {
        final Optional sessionOptional = newSessions.stream().findFirst();
        if (sessionOptional.isPresent()) {
          deferredResult.setResult(new ResponseEntity<>(sessionOptional.get(), OK));
        } else {
          deferredResult.setResult(new ResponseEntity<>(NOT_FOUND));
        }
      }, throwable -> {
        if (deferredResult.isSetOrExpired()) {
          final String message = "Processing of non-blocking request {} with request id #{} already expired";
          log.warn(message, "getSession", correlationId);
        } else {
          deferredResult.setErrorResult(new RuntimeException(throwable.getMessage()));
        }
      }, () -> {
        if (!deferredResult.isSetOrExpired()) {
          deferredResult.setErrorResult(new RuntimeException("The request completed without data or error."));
        }
      });
    return deferredResult;
  }

  @RequestMapping(value = "/sessions/{sessionId}", method = PUT)
  public DeferredResult> updateSession(@PathVariable("sessionId") String sessionId,
                                                               @RequestBody Session session,
                                                               @RequestHeader(value="correlationId") String correlationId) {
    final DeferredResult> deferredResult = new DeferredResult<>();
    session.setSessionId(sessionId);
    //noinspection CodeBlock2Expr
    sessionService
      .updateSession(session)
      .subscribe(newSessionData -> {
          deferredResult.setResult(new ResponseEntity<>(newSessionData, OK));
      }, throwable -> {
        if (deferredResult.isSetOrExpired()) {
          final String message = "Processing of non-blocking request {} with request id #{} already expired";
          log.warn(message, "updateSession", correlationId);
        } else {
          deferredResult.setErrorResult(new RuntimeException(throwable.getMessage()));
        }
      }, () -> {
        if (!deferredResult.isSetOrExpired()) {
          deferredResult.setErrorResult(new RuntimeException("The request completed without data or error."));
        }
      });
    return deferredResult;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy