xref: /unit/src/java/nginx/unit/websocket/pojo/PojoEndpointServer.java (revision 1157:7ae152bda303)
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 package nginx.unit.websocket.pojo;
18 
19 import java.util.Map;
20 
21 import javax.websocket.EndpointConfig;
22 import javax.websocket.Session;
23 import javax.websocket.server.ServerEndpointConfig;
24 
25 import org.apache.tomcat.util.res.StringManager;
26 
27 /**
28  * Wrapper class for instances of POJOs annotated with
29  * {@link javax.websocket.server.ServerEndpoint} so they appear as standard
30  * {@link javax.websocket.Endpoint} instances.
31  */
32 public class PojoEndpointServer extends PojoEndpointBase {
33 
34     private static final StringManager sm =
35             StringManager.getManager(PojoEndpointServer.class);
36 
37     @Override
onOpen(Session session, EndpointConfig endpointConfig)38     public void onOpen(Session session, EndpointConfig endpointConfig) {
39 
40         ServerEndpointConfig sec = (ServerEndpointConfig) endpointConfig;
41 
42         Object pojo;
43         try {
44             pojo = sec.getConfigurator().getEndpointInstance(
45                     sec.getEndpointClass());
46         } catch (InstantiationException e) {
47             throw new IllegalArgumentException(sm.getString(
48                     "pojoEndpointServer.getPojoInstanceFail",
49                     sec.getEndpointClass().getName()), e);
50         }
51         setPojo(pojo);
52 
53         @SuppressWarnings("unchecked")
54         Map<String,String> pathParameters =
55                 (Map<String, String>) sec.getUserProperties().get(
56                         Constants.POJO_PATH_PARAM_KEY);
57         setPathParameters(pathParameters);
58 
59         PojoMethodMapping methodMapping =
60                 (PojoMethodMapping) sec.getUserProperties().get(
61                         Constants.POJO_METHOD_MAPPING_KEY);
62         setMethodMapping(methodMapping);
63 
64         doOnOpen(session, endpointConfig);
65     }
66 }
67