35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott/*
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * The contents of this file are subject to the terms of the Common Development and
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * Distribution License (the License). You may not use this file except in compliance with the
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * License.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * specific language governing permission and limitations under the License.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * When distributing Covered Software, include this CDDL Header Notice in each file and include
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * Header, with the fields enclosed by brackets [] replaced by your own identifying
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * information: "Portions copyright [year] [name of copyright owner]".
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott *
67486d6b1ecf210ee0fba21ef9298f4fb6213d2cTom Rumsey * Copyright 2014-2016 ForgeRock AS.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott */
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshottpackage com.iplanet.dpro.session.operations;
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott
67486d6b1ecf210ee0fba21ef9298f4fb6213d2cTom Rumseyimport javax.inject.Inject;
67486d6b1ecf210ee0fba21ef9298f4fb6213d2cTom Rumseyimport javax.inject.Named;
67486d6b1ecf210ee0fba21ef9298f4fb6213d2cTom Rumsey
67486d6b1ecf210ee0fba21ef9298f4fb6213d2cTom Rumseyimport org.forgerock.openam.session.SessionConstants;
6309b849c2de831a0eaed9c27b5794bed9bd8fd1Neil Maddenimport org.forgerock.openam.sso.providers.stateless.StatelessSessionManager;
67486d6b1ecf210ee0fba21ef9298f4fb6213d2cTom Rumsey
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumseyimport com.iplanet.dpro.session.SessionID;
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnellimport com.iplanet.dpro.session.monitoring.MonitoredOperations;
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnellimport com.iplanet.dpro.session.monitoring.SessionMonitorType;
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnellimport com.iplanet.dpro.session.monitoring.SessionMonitoringStore;
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnellimport com.iplanet.dpro.session.operations.strategies.LocalOperations;
d0da70ccbba38b773e7a7cc71bc124b06206d201Robert Wapshottimport com.iplanet.dpro.session.operations.strategies.StatelessOperations;
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshottimport com.sun.identity.shared.debug.Debug;
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott/**
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * Server based SessionOperationStrategy implementation.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * SessionOperations represent the available operations that can be performed on a Session,
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * which are applicable under a number of situations. These situations correspond to the
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * number of situations Sessions can find themselves in.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * This strategy covers the following:
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * <b>local</b> - The Session is based on the current server and that server is responsible for
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * processing the request.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * <b>remote</b> - The Session is based on a remote server which will service the request and
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * respond with an appropriate response. This is performed by using a remote request and the
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * PLL signalling system.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * <b>CTS</b> - The Session is a remote session, however the Site appears to be down. The request
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * will be performed locally using the CTS instead.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * Between these strategies, the users Session should be available during fail-over of a Site.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott */
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshottpublic class ServerSessionOperationStrategy implements SessionOperationStrategy {
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna private final SessionOperations local;
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey private final SessionOperations stateless;
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott private final Debug debug;
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnell private final SessionMonitoringStore store;
6309b849c2de831a0eaed9c27b5794bed9bd8fd1Neil Madden private final StatelessSessionManager statelessSessionManager;
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott /**
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * Guice initialised constructor.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnell * @param local Required strategy.
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnell * @param store The store for session monitoring information.
6309b849c2de831a0eaed9c27b5794bed9bd8fd1Neil Madden * @param statelessSessionManager Required for JWT checks.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * @param debug Required for logging.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott */
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott @Inject
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey public ServerSessionOperationStrategy(
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey final SessionMonitoringStore store,
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey final LocalOperations local,
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey final StatelessOperations stateless,
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey final StatelessSessionManager statelessSessionManager,
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey final @Named(SessionConstants.SESSION_DEBUG) Debug debug) {
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnell this.store = store;
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnell this.local = local;
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey this.stateless = stateless;
6309b849c2de831a0eaed9c27b5794bed9bd8fd1Neil Madden this.statelessSessionManager = statelessSessionManager;
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott this.debug = debug;
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott }
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott /**
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * Based on the Session, determine the appropriate SessionOperations strategy to select.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * Local - For local Sessions which are hosted on the current Server.
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott * Remote - The Session is from a remote Site, and the Site is up.
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott * CTS - When cross talk is disabled, or if the Session is from a remote Site, which is down.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott *
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey * @param sessionID Non null SessionID to use.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * @return A non null SessionOperations implementation to use.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott */
7241a726fb5331ffaccfb2526002e01bc6347ce4Peter Major @Override
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey public SessionOperations getOperation(SessionID sessionID) {
a0e1846310a8366b5f92bc83ddba747707d42f3bTom Rumsey if (isStateless(sessionID)) {
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey return logAndWrap(sessionID, stateless, SessionMonitorType.STATELESS);
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott }
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey return logAndWrap(sessionID, local, SessionMonitorType.LOCAL);
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott }
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott
a0e1846310a8366b5f92bc83ddba747707d42f3bTom Rumsey private boolean isStateless(SessionID sessionID) {
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey return statelessSessionManager.containsJwt(sessionID);
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott }
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott /**
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * Inline logging function.
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey * @param sessionID Non null.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott * @param op Non null operation selected.
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnell * @param type
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnell * @return {code op}, wrapped in a MonitoredOperations.
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott */
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey private SessionOperations logAndWrap(SessionID sessionID, SessionOperations op, SessionMonitorType type) {
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott if (debug.messageEnabled()) {
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey debug.message(sessionID + ": " + op.getClass().getSimpleName() + " selected.");
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott }
cc7c18212481f5e9ee508afe2ffcaecb6b9330f5Craig McDonnell return new MonitoredOperations(op, type, store);
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott }
72450cb9c2ca854c6d3479832c2738196c1d3282Robert Wapshott}