![JAR search and dependency download from the Maven repository](/logo.png)
atom.websocket.ContextWebSocketService Maven / Gradle / Ivy
/*
* Copyright © 2015 Geeoz, and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Research Projects is dual-licensed under the GNU General Public
* License, version 2.0 (GPLv2) and the Geeoz Commercial License.
*
* Solely for non-commercial purposes. A purpose is non-commercial only if
* it is in no manner primarily intended for or directed toward commercial
* advantage or private monetary compensation.
*
* This Geeoz Software is supplied to you by Geeoz in consideration of your
* agreement to the following terms, and your use, installation, modification
* or redistribution of this Geeoz Software constitutes acceptance of these
* terms. If you do not agree with these terms, please do not use, install,
* modify or redistribute this Geeoz Software.
*
* Neither the name, trademarks, service marks or logos of Geeoz may be used
* to endorse or promote products derived from the Geeoz Software without
* specific prior written permission from Geeoz.
*
* The Geeoz Software is provided by Geeoz on an "AS IS" basis. GEEOZ MAKES NO
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, REGARDING THE GEEOZ SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
* COMBINATION WITH YOUR PRODUCTS.
*
* IN NO EVENT SHALL GEEOZ BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
* AND/OR DISTRIBUTION OF THE GEEOZ SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER
* THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
* OTHERWISE, EVEN IF GEEOZ HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* A copy of the GNU General Public License is included in the distribution in
* the file LICENSE and at
*
* http://www.gnu.org/licenses/gpl-2.0.html
*
* If you are using the Research Projects for commercial purposes, we
* encourage you to visit
*
* http://products.geeoz.com/license
*
* for more details.
*
* This software or hardware and documentation may provide access to
* or information on content, products, and services from third parties.
* Geeoz and its affiliates are not responsible for and expressly disclaim
* all warranties of any kind with respect to third-party content, products,
* and services. Geeoz and its affiliates will not be responsible for any loss,
* costs, or damages incurred due to your access to or use of third-party
* content, products, or services. If a third-party content exists, the
* additional copyright notices and license terms applicable to portions of the
* software are set forth in the THIRD_PARTY_LICENSE_README file.
*
* Please contact Geeoz or visit www.geeoz.com if you need additional
* information or have any questions.
*/
package atom.websocket;
import atom.app.ActivityThread;
import atom.app.ClientNotifier;
import atom.beans.ChangeSet;
import atom.ws.model.AbstractContextService;
import atom.ws.model.AbstractMessage;
import atom.ws.model.ChangeSetFactory;
import atom.ws.model.ChangeSetMessage;
import atom.ws.model.ClientRequestContext;
import atom.ws.model.ContextService;
import atom.ws.model.Cookie;
import javax.servlet.http.HttpSession;
import javax.websocket.EncodeException;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import javax.ws.rs.core.HttpHeaders;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Locale.LanguageRange;
import java.util.Map;
/**
* Context WebSocket service.
*
* @author Serge Voloshyn
* @version 1.2 3/1/15
*/
@ServerEndpoint(value = "/context-ws-service",
encoders = WebSocketMessageEncoder.class,
decoders = WebSocketMessageDecoder.class,
configurator = WebSocketServiceConfigurator.class)
public final class ContextWebSocketService extends AbstractContextService {
/**
* Name of the http session attribute containing current client WS session.
*/
private static final String WS_SESSION = "WS_SESSION_";
/**
* Process message from client.
*
* @param message message from client
* @param session current WebSocket session
*/
@OnMessage
public void onMessage(final AbstractMessage message,
final Session session) {
final Map userProperties = session.getUserProperties();
final HttpSession httpSession = (HttpSession) userProperties
.get(HttpSession.class.getName());
httpSession.setAttribute(WS_SESSION.concat(message.getClientId()),
session);
final Locale clientLocale = getAcceptLanguage(userProperties);
final ClientRequestContext requestContext = new ClientRequestContext(
httpSession, clientLocale, message.getCookies());
setClientNotifier(message.getClientId(), requestContext,
new WebSocketClientNotifier(message.getClientId(),
httpSession));
processMessage(message, requestContext,
new ServiceCallbackImpl(requestContext));
}
/**
* Get client preferred language according to the header
* {@link javax.ws.rs.core.HttpHeaders#ACCEPT_LANGUAGE}.
*
* @param userProperties user properties
* @return client preferred language
*/
@SuppressWarnings("unchecked")
private Locale getAcceptLanguage(final Map userProperties) {
final Map> headers = (Map>)
userProperties.get(WebSocketServiceConfigurator.HEADERS_PARAM);
if (headers != null) {
final List languages
= headers.get(HttpHeaders.ACCEPT_LANGUAGE
.toLowerCase(Locale.ENGLISH));
if (languages != null && languages.size() > 0) {
final StringBuilder builder
= new StringBuilder(HttpHeaders.ACCEPT_LANGUAGE)
.append(": ");
for (int i = 0; i < languages.size(); i++) {
if (i > 0) {
builder.append(",");
}
builder.append(languages.get(i));
}
final List languageRanges
= LanguageRange.parse(builder.toString());
final Locale clientLocale = Locale.lookup(languageRanges,
Arrays.asList(Locale.getAvailableLocales()));
if (clientLocale != null) {
return clientLocale;
}
}
}
return Locale.getDefault();
}
/**
* Send change set message for client.
*
* @param clientId client identifier
* @param changeSets change sets list
* @param theCookies new cookies
* @param httpSession client http session
* @param requestedByClient whether changeSet was created
* after client request
*/
private static void sendChangeSetToClient(
final String clientId,
final List changeSets,
final Cookie[] theCookies,
final HttpSession httpSession,
final boolean requestedByClient) {
final Session session = (Session) httpSession
.getAttribute(WS_SESSION.concat(clientId));
if (session == null || !session.isOpen()) {
return;
}
final ChangeSetMessage responseMessage = new ChangeSetMessage();
responseMessage.setClientId(clientId);
responseMessage.setChangeSet(changeSets);
responseMessage.setCookies(theCookies);
responseMessage.setRequestedByClient(requestedByClient);
try {
session.getBasicRemote().sendObject(responseMessage);
} catch (EncodeException | IOException e) {
e.printStackTrace();
}
}
/**
* Service callback implementation.
*/
private static final class ServiceCallbackImpl implements ServiceCallback {
/**
* Current request context.
*/
private final ClientRequestContext context;
/**
* Default constructor.
*
* @param aContext current request context
*/
private ServiceCallbackImpl(final ClientRequestContext aContext) {
context = aContext;
}
@Override
public void onFinish(final String clientId,
final List changeSet) {
sendChangeSetToClient(clientId, changeSet,
context.getResponseCookies(),
context.getSession(), true);
}
}
/**
* WebSocket client notifier.
*/
private static final class WebSocketClientNotifier
implements ClientNotifier {
/**
* Client identifier.
*/
private final String clientId;
/**
* Client http session.
*/
private final HttpSession session;
/**
* Default constructor.
*
* @param aClientId client identifier
* @param aSession client http session
*/
private WebSocketClientNotifier(final String aClientId,
final HttpSession aSession) {
clientId = aClientId;
session = aSession;
}
@Override
public void onViewChange(final ActivityThread thread) {
ContextService.checkChangedActivities(thread);
final List changeSets = ChangeSetFactory
.prepareChangeSet(getClientLayers(thread), thread);
sendChangeSetToClient(clientId, changeSets, null, session, false);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy