View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.push;
20  
21  import java.io.Serializable;
22  import javax.enterprise.inject.spi.BeanManager;
23  import javax.enterprise.inject.spi.CDI;
24  import javax.faces.context.ExternalContext;
25  import javax.websocket.HandshakeResponse;
26  import javax.websocket.server.HandshakeRequest;
27  import javax.websocket.server.ServerEndpointConfig;
28  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
29  import org.apache.myfaces.cdi.util.CDIUtils;
30  import org.apache.myfaces.push.cdi.WebsocketSessionBean;
31  import org.apache.myfaces.shared.util.WebConfigParamUtils;
32  
33  /**
34   *
35   */
36  public class WebsocketConfigurator extends ServerEndpointConfig.Configurator
37  {
38      @JSFWebConfigParam(defaultValue = "300000")
39      public static final String INIT_PARAM_WEBSOCKET_MAX_IDLE_TIMEOUT = "org.apache.myfaces.WEBSOCKET_MAX_IDLE_TIMEOUT";
40      
41      public static final String MAX_IDLE_TIMEOUT = "oam.websocket.maxIdleTimeout";
42      
43      public static final String WEBSOCKET_VALID = "oam.websocket.valid";
44      
45      public static final String WEBSOCKET_USER = "oam.websocket.user";
46      
47      private final Long maxIdleTimeout;
48      
49      public WebsocketConfigurator(ExternalContext context)
50      {
51          this.maxIdleTimeout = WebConfigParamUtils.getLongInitParameter(context, 
52                  INIT_PARAM_WEBSOCKET_MAX_IDLE_TIMEOUT);
53      }
54  
55      @Override
56      public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response)
57      {
58          if (this.maxIdleTimeout != null)
59          {
60              sec.getUserProperties().put(MAX_IDLE_TIMEOUT, this.maxIdleTimeout);
61          }
62          
63          String channelToken = request.getQueryString();
64          if (channelToken == null)
65          {
66              String uri = request.getRequestURI().toString();
67              if (uri.indexOf('?') >= 0)
68              {
69                  channelToken = uri.substring(uri.indexOf('?')+1);
70              }
71              else
72              {
73                  channelToken = uri.substring(uri.lastIndexOf('/')+1);
74              }
75          }
76          
77          BeanManager beanManager = CDI.current().getBeanManager();
78          WebsocketSessionBean websocketSessionBean = CDIUtils.getInstance(
79                  beanManager, WebsocketSessionBean.class, false);
80          
81          if (websocketSessionBean != null)
82          {
83              Serializable user = websocketSessionBean.getUserFromChannelToken(channelToken);
84              if (user != null)
85              {
86                  sec.getUserProperties().put(WEBSOCKET_USER, user);
87              }
88  
89              sec.getUserProperties().put(WEBSOCKET_VALID, websocketSessionBean.isTokenValid(channelToken));
90          }
91          else
92          {
93              // Cannot validate session, mark as invalid
94              sec.getUserProperties().put(WEBSOCKET_VALID, false);
95          }
96      }
97  }