summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSJavaInterfaceProcessor.java
blob: ff3229771dc5e3d5f764ee3fd9692ff07ec6bd37 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * 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.tuscany.sca.interfacedef.java.jaxrs;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;

import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
import org.apache.tuscany.sca.interfacedef.java.JavaOperation;
import org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceVisitor;

public class JAXRSJavaInterfaceProcessor implements JavaInterfaceVisitor {

    public void visitInterface(JavaInterface contract) throws InvalidInterfaceException {
        
        final Class<?> clazz = contract.getJavaClass();

        List<Operation> getOperations = new ArrayList<Operation>();
        List<Operation> putOperations = new ArrayList<Operation>();
        List<Operation> postOperations = new ArrayList<Operation>();
        List<Operation> deleteOperations = new ArrayList<Operation>();
        
        boolean hasJAXRSAnnotarions = false;
        
        for (Iterator<Operation> it = contract.getOperations().iterator(); it.hasNext();) {
            final JavaOperation operation = (JavaOperation)it.next();
            final Method method = operation.getJavaMethod();
         
            GET get = method.getAnnotation(GET.class);
            if(get != null) {
                hasJAXRSAnnotarions = true;
                operation.getAttributes().put(GET.class, true);
                getOperations.add(operation);
            }
            
            if(! getOperations.isEmpty()) {
                contract.getAttributes().put(GET.class, getOperations);
            }
            
            PUT put = method.getAnnotation(PUT.class);
            if(put != null) {
                hasJAXRSAnnotarions = true;
                operation.getAttributes().put(PUT.class, true);
                putOperations.add(operation);
            }

            if(! putOperations.isEmpty()) {
                contract.getAttributes().put(PUT.class, putOperations);
            }

            
            POST post = method.getAnnotation(POST.class);
            if(post != null) {
                hasJAXRSAnnotarions = true;
                operation.getAttributes().put(POST.class, true);
                postOperations.add(operation);
            }
            
            if(! postOperations.isEmpty()) {
                contract.getAttributes().put(POST.class, postOperations);
            }

            DELETE delete = method.getAnnotation(DELETE.class);
            if(delete != null) {
                hasJAXRSAnnotarions = true;
                operation.getAttributes().put(DELETE.class, true);
                deleteOperations.add(operation);
            }

            if(! deleteOperations.isEmpty()) {
                contract.getAttributes().put(GET.class, deleteOperations);
            }
            
        }
        
        // Always set JAX-RS annotated interfaces as remotables
        if (hasJAXRSAnnotarions) {
            contract.setRemotable(true);
        }
    }
}