Coverage Report - org.apache.johnzon.websocket.mapper.JohnzonTextDecoder
 
Classes in this File Line Coverage Branch Coverage Complexity
JohnzonTextDecoder
0 %
0/40
0 %
0/26
3,667
 
 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.johnzon.websocket.mapper;
 20  
 
 21  
 import org.apache.johnzon.mapper.Mapper;
 22  
 import org.apache.johnzon.websocket.internal.mapper.MapperLocator;
 23  
 
 24  
 import java.io.Reader;
 25  
 import java.lang.annotation.Annotation;
 26  
 import java.lang.reflect.Method;
 27  
 import java.lang.reflect.Type;
 28  
 import javax.websocket.DecodeException;
 29  
 import javax.websocket.Decoder;
 30  
 import javax.websocket.EndpointConfig;
 31  
 import javax.websocket.OnMessage;
 32  
 import javax.websocket.Session;
 33  
 import javax.websocket.server.PathParam;
 34  
 import javax.websocket.server.ServerEndpointConfig;
 35  
 
 36  
 public class JohnzonTextDecoder implements Decoder.TextStream<Object> {
 37  
     protected Mapper mapper;
 38  
     protected Type type;
 39  
 
 40  0
     public JohnzonTextDecoder() {
 41  
         // no-op
 42  0
     }
 43  
 
 44  
     // for client side no way to guess the type so let the user provide it easily
 45  
     public JohnzonTextDecoder(final Type type) {
 46  0
         this(null, type);
 47  0
     }
 48  
 
 49  0
     public JohnzonTextDecoder(final Mapper mapper, final Type type) {
 50  0
         this.mapper = mapper;
 51  0
         this.type = type;
 52  0
     }
 53  
 
 54  
     @Override
 55  
     public Object decode(final Reader stream) throws DecodeException {
 56  0
         return mapper.readObject(stream, type);
 57  
     }
 58  
 
 59  
     @Override
 60  
     public void init(final EndpointConfig endpointConfig) {
 61  0
         if (mapper == null) {
 62  0
             mapper = MapperLocator.locate();
 63  
         }
 64  0
         if (type != null) {
 65  0
             return;
 66  
         }
 67  
 
 68  0
         if (ServerEndpointConfig.class.isInstance(endpointConfig)) {
 69  0
             final Class<?> endpointClass = ServerEndpointConfig.class.cast(endpointConfig).getEndpointClass();
 70  0
             for (final Method m : endpointClass.getMethods()) {
 71  0
                 if (Object.class == m.getDeclaringClass()) {
 72  0
                     continue;
 73  
                 }
 74  0
                 if (m.getAnnotation(OnMessage.class) != null) {
 75  0
                     final Type[] genericParameterTypes = m.getGenericParameterTypes();
 76  0
                     for (int i = 0; i < genericParameterTypes.length; i++) {
 77  0
                         if (genericParameterTypes[i] == Session.class) {
 78  0
                             continue;
 79  
                         }
 80  0
                         boolean param = false;
 81  0
                         for (final Annotation a : m.getParameterAnnotations()[i]) {
 82  0
                             if (PathParam.class == a.annotationType()) {
 83  0
                                 param = true;
 84  0
                                 break;
 85  
                             }
 86  
                         }
 87  0
                         if (!param) {
 88  0
                             this.type = genericParameterTypes[i];
 89  0
                             break;
 90  
                         }
 91  
                     }
 92  0
                     break;
 93  
                 }
 94  
             }
 95  0
             if (type == null) {
 96  0
                 throw new IllegalArgumentException("didn't find @OnMessage in " + endpointClass);
 97  
             }
 98  0
         } else {
 99  0
             type = Type.class.cast(endpointConfig.getUserProperties().get("johnzon.websocket.message.type"));
 100  0
             if (type == null) {
 101  0
                 throw new IllegalArgumentException("didn't find johnzon.websocket.message.type");
 102  
             }
 103  
         }
 104  0
     }
 105  
 
 106  
     @Override
 107  
     public void destroy() {
 108  
         // no-op
 109  0
     }
 110  
 }