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 import java.nio.ByteBuffer; 22 23 import javax.websocket.DecodeException; 24 import javax.websocket.MessageHandler; 25 import javax.websocket.Session; 26 27 import nginx.unit.websocket.WsSession; 28 29 /** 30 * Common implementation code for the POJO partial message handlers. All 31 * the real work is done in this class and in the superclass. 32 * 33 * @param <T> The type of message to handle 34 */ 35 public abstract class PojoMessageHandlerPartialBase<T> 36 extends PojoMessageHandlerBase<T> implements MessageHandler.Partial<T> { 37 38 private final int indexBoolean; 39 PojoMessageHandlerPartialBase(Object pojo, Method method, Session session, Object[] params, int indexPayload, boolean convert, int indexBoolean, int indexSession, long maxMessageSize)40 public PojoMessageHandlerPartialBase(Object pojo, Method method, 41 Session session, Object[] params, int indexPayload, 42 boolean convert, int indexBoolean, int indexSession, 43 long maxMessageSize) { 44 super(pojo, method, session, params, indexPayload, convert, 45 indexSession, maxMessageSize); 46 this.indexBoolean = indexBoolean; 47 } 48 49 50 @Override onMessage(T message, boolean last)51 public final void onMessage(T message, boolean last) { 52 if (params.length == 1 && params[0] instanceof DecodeException) { 53 ((WsSession) session).getLocal().onError(session, 54 (DecodeException) params[0]); 55 return; 56 } 57 Object[] parameters = params.clone(); 58 if (indexBoolean != -1) { 59 parameters[indexBoolean] = Boolean.valueOf(last); 60 } 61 if (indexSession != -1) { 62 parameters[indexSession] = session; 63 } 64 if (convert) { 65 parameters[indexPayload] = ((ByteBuffer) message).array(); 66 } else { 67 parameters[indexPayload] = message; 68 } 69 Object result = null; 70 try { 71 result = method.invoke(pojo, parameters); 72 } catch (IllegalAccessException | InvocationTargetException e) { 73 handlePojoMethodException(e); 74 } 75 processResult(result); 76 } 77 } 78