Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9f352d700cefb37cc95b1c2af6534168f8cedab8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket.common.events.annotated;

import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Method;

import org.eclipse.jetty.websocket.api.Session;

/**
 * Simple CallableMethod that manages the optional {@link Session} argument
 */
public class OptionalSessionCallableMethod extends CallableMethod
{
    private final boolean wantsSession;
    private final boolean streaming;

    public OptionalSessionCallableMethod(Class<?> pojo, Method method)
    {
        super(pojo,method);

        boolean foundConnection = false;
        boolean foundStreaming = false;

        if (paramTypes != null)
        {
            for (Class<?> paramType : paramTypes)
            {
                if (Session.class.isAssignableFrom(paramType))
                {
                    foundConnection = true;
                }
                if (Reader.class.isAssignableFrom(paramType) || InputStream.class.isAssignableFrom(paramType))
                {
                    foundStreaming = true;
                }
            }
        }

        this.wantsSession = foundConnection;
        this.streaming = foundStreaming;
    }

    public void call(Object obj, Session connection, Object... args)
    {
        if (wantsSession)
        {
            Object fullArgs[] = new Object[args.length + 1];
            fullArgs[0] = connection;
            System.arraycopy(args,0,fullArgs,1,args.length);
            call(obj,fullArgs);
        }
        else
        {
            call(obj,args);
        }
    }

    public boolean isSessionAware()
    {
        return wantsSession;
    }

    public boolean isStreaming()
    {
        return streaming;
    }

    @Override
    public String toString()
    {
        return String.format("%s[%s]",this.getClass().getSimpleName(),method.toGenericString());
    }
}

Back to the top