public Message invoke()

in modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingInvoker.java [173:293]


    public Message invoke(Message msg) {

        Object entity = null;
        Object[] args = msg.getBody();

        URI uri = URI.create(endpointReference.getDeployedURI());
        UriBuilder uriBuilder = UriBuilder.fromUri(uri);

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

        if (method.isAnnotationPresent(Path.class)) {
            // Only for resource method
            uriBuilder.path(method);
        }

        if (!JAXRSHelper.isResourceMethod(method)) {
            // This is RPC over GET
            uriBuilder.replaceQueryParam("method", method.getName());
        }

        Map<String, Object> pathParams = new HashMap<String, Object>();
        Map<String, Object> matrixParams = new HashMap<String, Object>();
        Map<String, Object> queryParams = new HashMap<String, Object>();
        Map<String, Object> headerParams = new HashMap<String, Object>();
        Map<String, Object> formParams = new HashMap<String, Object>();
        Map<String, Object> cookieParams = new HashMap<String, Object>();

        for (int i = 0; i < method.getParameterTypes().length; i++) {
            boolean isEntity = true;
            Annotation[] annotations = method.getParameterAnnotations()[i];
            PathParam pathParam = getAnnotation(annotations, PathParam.class);
            if (pathParam != null) {
                isEntity = false;
                pathParams.put(pathParam.value(), args[i]);
            }
            MatrixParam matrixParam = getAnnotation(annotations, MatrixParam.class);
            if (matrixParam != null) {
                isEntity = false;
                matrixParams.put(matrixParam.value(), args[i]);
            }
            QueryParam queryParam = getAnnotation(annotations, QueryParam.class);
            if (queryParam != null) {
                isEntity = false;
                queryParams.put(queryParam.value(), args[i]);
            }
            HeaderParam headerParam = getAnnotation(annotations, HeaderParam.class);
            if (headerParam != null) {
                isEntity = false;
                headerParams.put(headerParam.value(), args[i]);
            }
            FormParam formParam = getAnnotation(annotations, FormParam.class);
            if (formParam != null) {
                isEntity = false;
                formParams.put(formParam.value(), args[i]);
            }
            CookieParam cookieParam = getAnnotation(annotations, CookieParam.class);
            if (cookieParam != null) {
                isEntity = false;
                cookieParams.put(cookieParam.value(), args[i]);
            }

            if (getAnnotation(annotations, Context.class) != null) {
                isEntity = false;
            }

            if (isEntity) {
                entity = args[i];
            }
        }

        for (Map.Entry<String, Object> p : queryParams.entrySet()) {
            uriBuilder.replaceQueryParam(p.getKey(), p.getValue());
        }
        for (Map.Entry<String, Object> p : matrixParams.entrySet()) {
            uriBuilder.replaceMatrixParam(p.getKey(), p.getValue());
        }

        uri = uriBuilder.buildFromMap(pathParams);
        Resource resource = restClient.resource(uri);

        for (Map.Entry<String, Object> p : headerParams.entrySet()) {
            resource.header(p.getKey(), String.valueOf(p.getValue()));
        }

        for (Map.Entry<String, Object> p : cookieParams.entrySet()) {
            Cookie cookie = new Cookie(p.getKey(), String.valueOf(p.getValue()));
            resource.cookie(cookie);
        }

        resource.contentType(getContentType());
        resource.accept(getAccepts());

        //handles declarative headers configured on the composite
        for (HTTPHeader header : binding.getHttpHeaders()) {
            //treat special headers that need to be calculated
            if (header.getName().equalsIgnoreCase("Expires")) {
                GregorianCalendar calendar = new GregorianCalendar();
                calendar.setTime(new Date());

                calendar.add(Calendar.HOUR, Integer.parseInt(header.getValue()));

                resource.header("Expires", HTTPCacheContext.RFC822DateFormat.format(calendar.getTime()));
            } else {
                //default behaviour to pass the header value to HTTP response
                resource.header(header.getName(), header.getValue());
            }
        }

        try {
            Object result = resource.invoke(httpMethod, responseType, entity);
            msg.setBody(result);
        } catch (ClientWebException e) {
            ClientResponse clientResponse = e.getResponse();
            // Consume the entity 
            byte[] error = clientResponse.getEntity(byte[].class);
            ByteArrayEntity errorEntity = error == null ? null : new ByteArrayEntity(error);
            clientResponse.setEntity(errorEntity);
            throw e;
        }
        return msg;
    }