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.lang.reflect.InvocationTargetException; 20 import java.lang.reflect.Method; 21 22 import javax.websocket.DecodeException; 23 import javax.websocket.MessageHandler; 24 import javax.websocket.Session; 25 26 import nginx.unit.websocket.WsSession; 27 28 /** 29 * Common implementation code for the POJO whole message handlers. All the real 30 * work is done in this class and in the superclass. 31 * 32 * @param <T> The type of message to handle 33 */ 34 public abstract class PojoMessageHandlerWholeBase<T> 35 extends PojoMessageHandlerBase<T> implements MessageHandler.Whole<T> { 36 PojoMessageHandlerWholeBase(Object pojo, Method method, Session session, Object[] params, int indexPayload, boolean convert, int indexSession, long maxMessageSize)37 public PojoMessageHandlerWholeBase(Object pojo, Method method, 38 Session session, Object[] params, int indexPayload, 39 boolean convert, int indexSession, long maxMessageSize) { 40 super(pojo, method, session, params, indexPayload, convert, 41 indexSession, maxMessageSize); 42 } 43 44 45 @Override onMessage(T message)46 public final void onMessage(T message) { 47 48 if (params.length == 1 && params[0] instanceof DecodeException) { 49 ((WsSession) session).getLocal().onError(session, 50 (DecodeException) params[0]); 51 return; 52 } 53 54 // Can this message be decoded? 55 Object payload; 56 try { 57 payload = decode(message); 58 } catch (DecodeException de) { 59 ((WsSession) session).getLocal().onError(session, de); 60 return; 61 } 62 63 if (payload == null) { 64 // Not decoded. Convert if required. 65 if (convert) { 66 payload = convert(message); 67 } else { 68 payload = message; 69 } 70 } 71 72 Object[] parameters = params.clone(); 73 if (indexSession != -1) { 74 parameters[indexSession] = session; 75 } 76 parameters[indexPayload] = payload; 77 78 Object result = null; 79 try { 80 result = method.invoke(pojo, parameters); 81 } catch (IllegalAccessException | InvocationTargetException e) { 82 handlePojoMethodException(e); 83 } 84 processResult(result); 85 } 86 convert(T message)87 protected Object convert(T message) { 88 return message; 89 } 90 91 decode(T message)92 protected abstract Object decode(T message) throws DecodeException; onClose()93 protected abstract void onClose(); 94 } 95