Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed to the Apache Software Foundation (ASF) under one or more
   *  contributor license agreements.  See the NOTICE file distributed with
   *  this work for additional information regarding copyright ownership.
   *  The ASF licenses this file to You under the Apache License, Version 2.0
   *  (the "License"); you may not use this file except in compliance with
   *  the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.apache.tomcat.websocket.pojo;
 
 import static org.jboss.web.WebsocketsMessages.MESSAGES;
 
 import java.io.Reader;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
For a POJO class annotated with javax.websocket.server.ServerEndpoint, an instance of this class creates and caches the method handler, method information and parameter information for the onXXX calls.
 
 public class PojoMethodMapping {
 
     private final Method onOpen;
     private final Method onClose;
     private final Method onError;
     private final PojoPathParam[] onOpenParams;
     private final PojoPathParam[] onCloseParams;
     private final PojoPathParam[] onErrorParams;
     private final Set<MessageHandlerInfoonMessage = new HashSet<MessageHandlerInfo>();
     private final String wsPath;
 
 
     public PojoMethodMapping(Class<?> clazzPojo,
             Class<? extends Decoder>[] decoderClazzesString wsPath)
                     throws DeploymentException {
 
         this. = wsPath;
 
         List<DecoderEntrydecoders = Util.getDecoders(decoderClazzes);
         Method open = null;
         Method close = null;
         Method error = null;
         for (Method method : clazzPojo.getDeclaredMethods()) {
             if (method.getAnnotation(OnOpen.class) != null) {
                 checkPublic(method);
                 if (open == null) {
                     open = method;
                 } else {
                     // Duplicate annotation
                     throw new DeploymentException(.duplicateAnnotations(OnOpen.classclazzPojo));
                 }
             } else if (method.getAnnotation(OnClose.class) != null) {
                 checkPublic(method);
                 if (close == null) {
                     close = method;
                 } else {
                     // Duplicate annotation
                     throw new DeploymentException(.duplicateAnnotations(OnClose.classclazzPojo));
                 }
             } else if (method.getAnnotation(OnError.class) != null) {
                 checkPublic(method);
                 if (error == null) {
                     error = method;
                } else {
                    // Duplicate annotation
                    throw new DeploymentException(.duplicateAnnotations(OnError.classclazzPojo));
                }
            } else if (method.getAnnotation(OnMessage.class) != null) {
                checkPublic(method);
                .add(new MessageHandlerInfo(methoddecoders));
            } else {
                // Method not annotated
            }
        }
        this. = open;
        this. = close;
        this. = error;
    }
    private void checkPublic(Method mthrows DeploymentException {
        if (!Modifier.isPublic(m.getModifiers())) {
            throw new DeploymentException(.methodNotPublic(m.getName()));
        }
    }
    public String getWsPath() {
        return ;
    }
    public Method getOnOpen() {
        return ;
    }
    public Object[] getOnOpenArgs(Map<String,StringpathParameters,
            Session sessionEndpointConfig configthrows DecodeException {
        return buildArgs(pathParameterssessionconfignull,
                null);
    }
    public Method getOnClose() {
        return ;
    }
    public Object[] getOnCloseArgs(Map<String,StringpathParameters,
            Session sessionCloseReason closeReasonthrows DecodeException {
        return buildArgs(pathParameterssessionnullnull,
                closeReason);
    }
    public Method getOnError() {
        return ;
    }
    public Object[] getOnErrorArgs(Map<String,StringpathParameters,
            Session sessionThrowable throwablethrows DecodeException {
        return buildArgs(pathParameterssessionnull,
                throwablenull);
    }
            Map<String,StringpathParametersSession session,
            EndpointConfig config) {
        Set<MessageHandlerresult = new HashSet<MessageHandler>();
        for (MessageHandlerInfo messageMethod : ) {
            result.addAll(messageMethod.getMessageHandlers(pojopathParameters,
                    sessionconfig));
        }
        return result;
    }
    private static PojoPathParam[] getPathParams(Method m,
            MethodType methodTypethrows DeploymentException {
        if (m == null) {
            return new PojoPathParam[0];
        }
        boolean foundThrowable = false;
        Class<?>[] types = m.getParameterTypes();
        Annotation[][] paramsAnnotations = m.getParameterAnnotations();
        PojoPathParam[] result = new PojoPathParam[types.length];
        for (int i = 0; i < types.lengthi++) {
            Class<?> type = types[i];
            if (type.equals(Session.class)) {
                result[i] = new PojoPathParam(typenull);
            } else if (methodType == . &&
                    type.equals(EndpointConfig.class)) {
                result[i] = new PojoPathParam(typenull);
            } else if (methodType == .
                    && type.equals(Throwable.class)) {
                foundThrowable = true;
                result[i] = new PojoPathParam(typenull);
            } else if (methodType == . &&
                    type.equals(CloseReason.class)) {
                result[i] = new PojoPathParam(typenull);
            } else {
                Annotation[] paramAnnotations = paramsAnnotations[i];
                for (Annotation paramAnnotation : paramAnnotations) {
                    if (paramAnnotation.annotationType().equals(
                            PathParam.class)) {
                        // Check that the type is valid. "0" coerces to every
                        // valid type
                        try {
                            Util.coerceToType(type"0");
                        } catch (IllegalArgumentException iae) {
                            throw new DeploymentException(.invalidPathParamType(),
                                    iae);
                        }
                        result[i] = new PojoPathParam(type,
                                ((PathParamparamAnnotation).value());
                        break;
                    }
                }
                // Parameters without annotations are not permitted
                if (result[i] == null) {
                    throw new DeploymentException(.pathParamWithoutAnnotation(typem.getName(), m.getClass().getName()));
                }
            }
        }
        if (methodType == . && !foundThrowable) {
            throw new DeploymentException(.onErrorWithoutThrowable(m.getName(), m.getDeclaringClass().getName()));
        }
        return result;
    }
    private static Object[] buildArgs(PojoPathParam[] pathParams,
            Map<String,StringpathParametersSession session,
            EndpointConfig configThrowable throwableCloseReason closeReason)
            throws DecodeException {
        Object[] result = new Object[pathParams.length];
        for (int i = 0; i < pathParams.lengthi++) {
            Class<?> type = pathParams[i].getType();
            if (type.equals(Session.class)) {
                result[i] = session;
            } else if (type.equals(EndpointConfig.class)) {
                result[i] = config;
            } else if (type.equals(Throwable.class)) {
                result[i] = throwable;
            } else if (type.equals(CloseReason.class)) {
                result[i] = closeReason;
            } else {
                String name = pathParams[i].getName();
                String value = pathParameters.get(name);
                try {
                    result[i] = Util.coerceToType(typevalue);
                } catch (Exception e) {
                    throw new DecodeException(value.errorDecodingPathParam(valuetype), e);
                }
            }
        }
        return result;
    }
    private static class MessageHandlerInfo {
        private final Method m;
        private int indexString = -1;
        private int indexByteArray = -1;
        private int indexByteBuffer = -1;
        private int indexPong = -1;
        private int indexBoolean = -1;
        private int indexSession = -1;
        private int indexInputStream = -1;
        private int indexReader = -1;
        private int indexPrimitive = -1;
        private Map<Integer,PojoPathParamindexPathParams = new HashMap<IntegerPojoPathParam>();
        private int indexPayload = -1;
        private DecoderMatch decoderMatch = null;
        private long maxMessageSize = -1;
        public MessageHandlerInfo(Method mList<DecoderEntrydecoderEntries) {
            this. = m;
            Class<?>[] types = m.getParameterTypes();
            Annotation[][] paramsAnnotations = m.getParameterAnnotations();
            for (int i = 0; i < types.lengthi++) {
                boolean paramFound = false;
                Annotation[] paramAnnotations = paramsAnnotations[i];
                for (Annotation paramAnnotation : paramAnnotations) {
                    if (paramAnnotation.annotationType().equals(
                            PathParam.class)) {
                        .put(
                                Integer.valueOf(i), new PojoPathParam(types[i],
                                        ((PathParamparamAnnotation).value()));
                        paramFound = true;
                        break;
                    }
                }
                if (paramFound) {
                    continue;
                }
                if (String.class.isAssignableFrom(types[i])) {
                    if ( == -1) {
                         = i;
                    } else {
                        throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                } else if (Reader.class.isAssignableFrom(types[i])) {
                    if ( == -1) {
                         = i;
                    } else {
                        throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                } else if (boolean.class == types[i]) {
                    if ( == -1) {
                         = i;
                    } else {
                        throw .duplicateLastMessageParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                } else if (ByteBuffer.class.isAssignableFrom(types[i])) {
                    if ( == -1) {
                         = i;
                    } else {
                        throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                } else if (byte[].class == types[i]) {
                    if ( == -1) {
                         = i;
                    } else {
                        throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                } else if (InputStream.class.isAssignableFrom(types[i])) {
                    if ( == -1) {
                         = i;
                    } else {
                        throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                } else if (Util.isPrimitive(types[i])) {
                    if ( == -1) {
                         = i;
                    } else {
                        throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                } else if (Session.class.isAssignableFrom(types[i])) {
                    if ( == -1) {
                         = i;
                    } else {
                        throw .duplicateSessionParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                } else if (PongMessage.class.isAssignableFrom(types[i])) {
                    if ( == -1) {
                         = i;
                    } else {
                        throw .duplicatePongMessageParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                } else {
                    if ( != null && .hasMatches()) {
                        throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                    }
                     = new DecoderMatch(types[i], decoderEntries);
                    if (.hasMatches()) {
                         = i;
                    }
                }
            }
            // Additional checks required
            if ( != -1) {
                if ( != -1) {
                   throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                } else {
                     = ;
                }
            }
            if ( != -1) {
                if ( != -1) {
                    throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                } else {
                     = ;
                }
            }
            if ( != -1) {
                if ( != -1) {
                    throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                } else {
                     = ;
                }
            }
            if ( != -1) {
                if ( != -1) {
                    throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                } else {
                     = ;
                }
            }
            if ( != -1) {
                if ( != -1) {
                    throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                } else {
                     = ;
                }
            }
            if ( != -1) {
                if ( != -1) {
                    throw .duplicateMessageParameter(m.getName(), m.getDeclaringClass().getName());
                } else {
                     = ;
                }
            }
            if ( != -1) {
                if ( != -1) {
                    throw .invalidPongWithPayload(m.getName(), m.getDeclaringClass().getName());
                } else {
                     = ;
                }
            }
            if ( == -1 &&  == -1 &&
                     != -1) {
                // The boolean we found is a payload, not a last flag
                 = ;
                 = ;
                 = -1;
            }
            if ( == -1) {
                throw .missingPayload(m.getName(), m.getDeclaringClass().getName());
            }
            if ( != -1 &&  != -1) {
                throw .partialPong(m.getName(), m.getDeclaringClass().getName());
            }
            if( != -1 &&  != -1) {
                throw .partialReader(m.getName(), m.getDeclaringClass().getName());
            }
            if( != -1 &&  != -1) {
                throw .partialInputStream(m.getName(), m.getDeclaringClass().getName());
            }
            if ( != null && .hasMatches() &&
                     != -1) {
                throw .partialObject(m.getName(), m.getDeclaringClass().getName());
            }
             = m.getAnnotation(OnMessage.class).maxMessageSize();
        }
        public Set<MessageHandlergetMessageHandlers(Object pojo,
                Map<String,StringpathParametersSession session,
                EndpointConfig config) {
            Object[] params = new Object[.getParameterTypes().length];
            for (Map.Entry<Integer,PojoPathParamentry :
                    .entrySet()) {
                PojoPathParam pathParam = entry.getValue();
                String valueString = pathParameters.get(pathParam.getName());
                Object value = null;
                try {
                    value = Util.coerceToType(pathParam.getType(), valueString);
                } catch (Exception e) {
                    DecodeException de =  new DecodeException(valueString,
                            .errorDecodingPathParam(valueStringpathParam.getType()), e);
                    params = new Object[] { de };
                }
                params[entry.getKey().intValue()] = value;
            }
            Set<MessageHandlerresults = new HashSet<MessageHandler>(2);
            if ( == -1) {
                // Basic
                if ( != -1 ||  != -1) {
                    MessageHandler mh = new PojoMessageHandlerWholeText(pojo,
                            sessionconfignullparamsfalse,
                            );
                    results.add(mh);
                } else if ( != -1) {
                    MessageHandler mh = new PojoMessageHandlerWholeText(pojo,
                            sessionconfignullparamstrue,
                            );
                    results.add(mh);
                } else if ( != -1) {
                    MessageHandler mh = new PojoMessageHandlerWholeBinary(pojo,
                            sessionconfignullparams,
                            truefalse);
                    results.add(mh);
                } else if ( != -1) {
                    MessageHandler mh = new PojoMessageHandlerWholeBinary(pojo,
                            sessionconfignullparams,
                            falsefalse);
                    results.add(mh);
                } else if ( != -1) {
                    MessageHandler mh = new PojoMessageHandlerWholeBinary(pojo,
                            sessionconfignullparams,
                            truetrue);
                    results.add(mh);
                } else if ( != null && .hasMatches()) {
                    if (.getBinaryDecoders().size() > 0) {
                        MessageHandler mh = new PojoMessageHandlerWholeBinary(
                                pojosessionconfig,
                                .getBinaryDecoders(), params,
                                truetrue,
                                );
                        results.add(mh);
                    }
                    if (.getTextDecoders().size() > 0) {
                        MessageHandler mh = new PojoMessageHandlerWholeText(
                                pojosessionconfig,
                                .getTextDecoders(), params,
                                true);
                        results.add(mh);
                    }
                } else {
                    MessageHandler mh = new PojoMessageHandlerWholePong(pojo,
                            sessionparamsfalse);
                    results.add(mh);
                }
            } else {
                // ASync
                if ( != -1) {
                    MessageHandler mh = new PojoMessageHandlerPartialText(pojo,
                            sessionparamsfalse,
                            );
                    results.add(mh);
                } else if ( != -1) {
                    MessageHandler mh = new PojoMessageHandlerPartialBinary(
                            pojosessionparamstrue,
                            );
                    results.add(mh);
                } else {
                    MessageHandler mh = new PojoMessageHandlerPartialBinary(
                            pojosessionparamsfalse,
                            );
                    results.add(mh);
                }
            }
            return results;
        }
    }
    private static enum MethodType {
        ON_OPEN,
        ON_CLOSE,
        ON_ERROR
    }
New to GrepCode? Check out our FAQ X