summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java
blob: c47473b3eea769e19c85675bd371c821d48842fc (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * 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.binding.rest.operationselector.jaxrs.provider;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.List;

import javax.activation.DataSource;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;

import org.apache.tuscany.sca.common.http.HTTPContext;
import org.apache.tuscany.sca.common.http.HTTPUtils;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.interfacedef.java.JavaOperation;
import org.apache.tuscany.sca.invocation.Interceptor;
import org.apache.tuscany.sca.invocation.Invoker;
import org.apache.tuscany.sca.invocation.Message;
import org.apache.tuscany.sca.runtime.RuntimeComponentService;
import org.apache.tuscany.sca.runtime.RuntimeEndpoint;

/**
 * JAXRS operation selector Interceptor.
 * 
 * @version $Rev$ $Date$
*/
public class JAXRSOperationSelectorInterceptor implements Interceptor {
    private ExtensionPointRegistry extensionPoints;
    private RuntimeEndpoint endpoint;

    private RuntimeComponentService service;
    private InterfaceContract interfaceContract;
    private List<Operation> serviceOperations;

    private Invoker next;

    public JAXRSOperationSelectorInterceptor(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) {
        this.extensionPoints = extensionPoints;
        this.endpoint = endpoint;

        this.service = (RuntimeComponentService)endpoint.getService();
        this.interfaceContract = service.getInterfaceContract();
        this.serviceOperations = service.getInterfaceContract().getInterface().getOperations();
    }

    public Invoker getNext() {
        return next;
    }

    public void setNext(Invoker next) {
        this.next = next;
    }

    public Message invoke(Message msg) {

        HTTPContext bindingContext = (HTTPContext)msg.getBindingContext();

        // By-pass the operation selector
        if (bindingContext == null) {
            return getNext().invoke(msg);
        }

        String path = null;
        try {
            path = URLDecoder.decode(HTTPUtils.getRequestPath(bindingContext.getHttpRequest()), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }

        if (path.startsWith("/")) {
            path = path.substring(1);
        }

        List<Operation> operations =
            filterOperationsByHttpMethod(interfaceContract, bindingContext.getHttpRequest().getMethod());

        Operation operation = findOperation(path, operations);

        final JavaOperation javaOperation = (JavaOperation)operation;
        final Method method = javaOperation.getJavaMethod();

        if (path != null && path.length() > 0) {
            if (method.getAnnotation(Path.class) != null) {
                msg.setBody(new Object[] {path});
            }
        }

        // FIXME: [rfeng] We should follow JAX-RS rules to identify the entity parameter
        Class<?>[] paramTypes = method.getParameterTypes();
        if (paramTypes.length == 1) {
            Class<?> type = paramTypes[0];
            InputStream is = (InputStream)((Object[])msg.getBody())[0];
            Object target = convert(is, bindingContext.getHttpRequest().getContentType(), type);
            msg.setBody(new Object[] {target});
        } else if (paramTypes.length == 0) {
            msg.setBody(null);
        }

        msg.setOperation(operation);

        return getNext().invoke(msg);

    }

    private Object convert(InputStream content, String contentType, Class<?> type) {
        if (type == DataSource.class) {
            return type.cast(new InputStreamDataSource(content, contentType));
        } else if (type == InputStream.class) {
            return type.cast(content);
        } else if (type == String.class) {
            try {
                StringWriter sw = new StringWriter();
                InputStreamReader reader = new InputStreamReader(content, "UTF-8");
                char[] buf = new char[8192];
                while (true) {
                    int size = reader.read(buf);
                    if (size < 0) {
                        break;
                    }
                    sw.write(buf, 0, size);
                }
                return type.cast(sw.toString());
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        } else if (type == byte[].class) {
            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[8192];
                while (true) {
                    int size = content.read(buf);
                    if (size < 0) {
                        break;
                    }
                    bos.write(buf, 0, size);
                }
                return type.cast(bos.toByteArray());
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        } else {
            return content;
        }
    }
    
    public static final class InputStreamDataSource implements DataSource {

        public static final String DEFAULT_TYPE = "application/octet-stream";

        private final InputStream in;
        private final String ctype;

        public InputStreamDataSource(InputStream in) {
            this(in, null);
        }

        public InputStreamDataSource(InputStream in, String ctype) {
            this.in = in;
            this.ctype = (ctype != null) ? ctype : DEFAULT_TYPE;
        }

        public String getContentType() {
            return ctype;
        }

        public String getName() {
            return null;
        }

        public InputStream getInputStream() throws IOException {
            return in;
        }

        public OutputStream getOutputStream() throws IOException {
            return null;
        }

    }

    /**
     * Find the operation from the component service contract
     * @param componentService
     * @param http_method
     * @return
     */
    private static List<Operation> filterOperationsByHttpMethod(InterfaceContract interfaceContract, String http_method) {
        List<Operation> operations = null;

        if (http_method.equalsIgnoreCase("get")) {
            operations = (List<Operation>)interfaceContract.getInterface().getAttributes().get(GET.class);
        } else if (http_method.equalsIgnoreCase("put")) {
            operations = (List<Operation>)interfaceContract.getInterface().getAttributes().get(PUT.class);
        } else if (http_method.equalsIgnoreCase("post")) {
            operations = (List<Operation>)interfaceContract.getInterface().getAttributes().get(POST.class);
        } else if (http_method.equalsIgnoreCase("delete")) {
            operations = (List<Operation>)interfaceContract.getInterface().getAttributes().get(DELETE.class);
        }

        return operations;
    }

    /**
     * Find the operation from the component service contract
     * @param componentService
     * @param http_method
     * @return
     */
    private Operation findOperation(String path, List<Operation> operations) {
        Operation operation = null;

        for (Operation op : operations) {
            final JavaOperation javaOperation = (JavaOperation)op;
            final Method method = javaOperation.getJavaMethod();

            if (path != null && path.length() > 0) {
                if (method.getAnnotation(Path.class) != null) {
                    operation = op;
                    break;
                }
            } else {
                if (method.getAnnotation(Path.class) == null) {
                    operation = op;
                    break;
                }
            }
        }

        return operation;
    }
}