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.server; 18 19 import java.util.ArrayList; 20 import java.util.HashSet; 21 import java.util.List; 22 import java.util.Set; 23 24 import javax.websocket.Extension; 25 import javax.websocket.HandshakeResponse; 26 import javax.websocket.server.HandshakeRequest; 27 import javax.websocket.server.ServerEndpointConfig; 28 29 public class DefaultServerEndpointConfigurator 30 extends ServerEndpointConfig.Configurator { 31 32 @Override getEndpointInstance(Class<T> clazz)33 public <T> T getEndpointInstance(Class<T> clazz) 34 throws InstantiationException { 35 try { 36 return clazz.getConstructor().newInstance(); 37 } catch (InstantiationException e) { 38 throw e; 39 } catch (ReflectiveOperationException e) { 40 InstantiationException ie = new InstantiationException(); 41 ie.initCause(e); 42 throw ie; 43 } 44 } 45 46 47 @Override getNegotiatedSubprotocol(List<String> supported, List<String> requested)48 public String getNegotiatedSubprotocol(List<String> supported, 49 List<String> requested) { 50 51 for (String request : requested) { 52 if (supported.contains(request)) { 53 return request; 54 } 55 } 56 return ""; 57 } 58 59 60 @Override getNegotiatedExtensions(List<Extension> installed, List<Extension> requested)61 public List<Extension> getNegotiatedExtensions(List<Extension> installed, 62 List<Extension> requested) { 63 Set<String> installedNames = new HashSet<>(); 64 for (Extension e : installed) { 65 installedNames.add(e.getName()); 66 } 67 List<Extension> result = new ArrayList<>(); 68 for (Extension request : requested) { 69 if (installedNames.contains(request.getName())) { 70 result.add(request); 71 } 72 } 73 return result; 74 } 75 76 77 @Override checkOrigin(String originHeaderValue)78 public boolean checkOrigin(String originHeaderValue) { 79 return true; 80 } 81 82 @Override modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response)83 public void modifyHandshake(ServerEndpointConfig sec, 84 HandshakeRequest request, HandshakeResponse response) { 85 // NO-OP 86 } 87 88 } 89