Package org.apache.tuscany.sca.invocation

Examples of org.apache.tuscany.sca.invocation.Message


            return nextInvoker.invoke(msg);
        }

        msg.setBody(copy((Object[])msg.getBody(), inputDataBindings));

        Message resultMsg = nextInvoker.invoke(msg);

        if (!msg.isFault() && operation.getOutputType() != null) {
            resultMsg.setBody(copy(resultMsg.getBody(), outputDataBinding));
        }

        if (msg.isFault()) {
            msg.setFaultBody(copyFault(msg.getBody()));
        }
View Full Code Here


                // Let the component implementation create it
                if (supportsFeedEntries) {
                   
                    // The service implementation supports feed entries, pass the entry to it
                    Message requestMessage = messageFactory.createMessage();
                    requestMessage.setBody(new Object[] {feedEntry});
                    Message responseMessage = postInvoker.invoke(requestMessage);
                    if (responseMessage.isFault()) {
                        throw new ServletException((Throwable)responseMessage.getBody());
                    }
                    createdFeedEntry = responseMessage.getBody();
                } else {
                   
                    // The service implementation does not support feed entries, pass the data item to it
                    Message requestMessage = messageFactory.createMessage();
                    org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object> entry = createEntry(feedEntry);
                    requestMessage.setBody(new Object[] {entry.getKey(), entry.getData()});
                    Message responseMessage = postInvoker.invoke(requestMessage);
                    if (responseMessage.isFault()) {
                        throw new ServletException((Throwable)responseMessage.getBody());
                    }
                    entry.setKey(responseMessage.getBody());
                    createdFeedEntry = createFeedEntry(entry);
                }

            } else if (contentType != null) {

                // Create a new media entry

                // Get incoming headers
                String title = request.getHeader("Title");
                String slug = request.getHeader("Slug");

                // Let the component implementation create the media entry
                Message requestMessage = messageFactory.createMessage();
                requestMessage.setBody(new Object[] {title, slug, contentType, request.getInputStream()});
                Message responseMessage = postMediaInvoker.invoke(requestMessage);
                if (responseMessage.isFault()) {
                    throw new ServletException((Throwable)responseMessage.getBody());
                }
                createdFeedEntry = responseMessage.getBody();
            } else {
                response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
            }

            // A new entry was created successfully
View Full Code Here

            Object[] logParams = new Object[] {operation.getName(), sb.toString()};
            logger.logp(Level.FINER, context, "", "Invoking operation {0} with arguments {1}", logParams);
        }

        Message responseMsg = null;
        try {
            responseMsg = getNext().invoke(msg);
            return responseMsg;
        } catch (RuntimeException e) {
            logger.logp(Level.SEVERE, context, "", "Exception thrown from operation - " + operation.getName(), e);
            throw e;
        } finally {
            if (responseMsg != null) {
                Object[] logParams = new Object[] {operation.getName(), responseMsg.getBody()};
                logger.logp(Level.INFO, context, "", "Returned from operation - " + operation.getName());
                logger.logp(Level.FINER, context, "", "Returning from operation {0} with return value {1}", logParams);
            }
        }
    }
View Full Code Here

                // Let the component implementation create it
                if (supportsFeedEntries) {
                   
                    // The service implementation supports feed entries, pass the entry to it
                    Message requestMessage = messageFactory.createMessage();
                    requestMessage.setBody(new Object[] {id, feedEntry});
                    Message responseMessage = putInvoker.invoke(requestMessage);
                    if (responseMessage.isFault()) {
                        Object body = responseMessage.getBody();
                        if (body.getClass().getName().endsWith(".NotFoundException")) {
                            response.sendError(HttpServletResponse.SC_NOT_FOUND);
                        } else {
                            throw new ServletException((Throwable)responseMessage.getBody());
                        }
                    }
                } else {
                   
                    // The service implementation does not support feed entries, pass the data item to it
                    Message requestMessage = messageFactory.createMessage();
                    org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object> entry = createEntry(feedEntry);
                    requestMessage.setBody(new Object[] {entry.getKey(), entry.getData()});
                    Message responseMessage = putInvoker.invoke(requestMessage);
                    if (responseMessage.isFault()) {
                        Object body = responseMessage.getBody();
                        if (body.getClass().getName().endsWith(".NotFoundException")) {
                            response.sendError(HttpServletResponse.SC_NOT_FOUND);
                        } else {
                            throw new ServletException((Throwable)responseMessage.getBody());
                        }
                    }
                }

            } else if (contentType != null) {

                // Updated a media entry

                // Let the component implementation create the media entry
                Message requestMessage = messageFactory.createMessage();
                requestMessage.setBody(new Object[] {id, contentType, request.getInputStream()});
                Message responseMessage = putMediaInvoker.invoke(requestMessage);
                Object body = responseMessage.getBody();
                if (responseMessage.isFault()) {
                    if (body.getClass().getName().endsWith(".NotFoundException")) {
                        response.sendError(HttpServletResponse.SC_NOT_FOUND);
                    } else {
                        throw new ServletException((Throwable)responseMessage.getBody());
                    }
                }
            } else {
                response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
            }
View Full Code Here

        } else {
            id = "";
        }

        // Delete a specific entry from the collection
        Message requestMessage = messageFactory.createMessage();
        requestMessage.setBody(new Object[] {id});
        Message responseMessage = deleteInvoker.invoke(requestMessage);
        if (responseMessage.isFault()) {
            Object body = responseMessage.getBody();
            if (body.getClass().getName().endsWith(".NotFoundException")) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
            } else {
                throw new ServletException((Throwable)responseMessage.getBody());
            }
        }
    }
View Full Code Here

    }

    public Message invoke(Message msg) {
        Object input = transform(msg.getBody(), sourceOperation.getInputType(), targetOperation.getInputType(), false);
        msg.setBody(input);
        Message resultMsg = next.invoke(msg);
        Object result = resultMsg.getBody();
        if (sourceOperation.isNonBlocking()) {
            // Not to reset the message body
            return resultMsg;
        }

        // FIXME: Should we fix the Operation model so that getOutputType
        // returns DataType<DataType<T>>?
        DataType<DataType> targetType =
            new DataTypeImpl<DataType>(DataBinding.IDL_OUTPUT, Object.class, targetOperation.getOutputType());

        DataType<DataType> sourceType =
            new DataTypeImpl<DataType>(DataBinding.IDL_OUTPUT, Object.class, sourceOperation.getOutputType());

        if (resultMsg.isFault()) {

            // FIXME: We need to figure out what fault type it is and then
            // transform it
            // back the source fault type
            // throw new InvocationRuntimeException((Throwable) result);

            if ((result instanceof Exception) && !(result instanceof RuntimeException)) {
                // FIXME: How to match fault data to a fault type for the
                // operation?

                // If the result is from an InvocationTargetException look at
                // the actual cause.
                if (result instanceof InvocationTargetException) {
                    result = ((InvocationTargetException)result).getCause();
                }
                DataType targetDataType = null;
                for (DataType exType : targetOperation.getFaultTypes()) {
                    if (((Class)exType.getPhysical()).isInstance(result)) {
                        if (result instanceof FaultException) {
                            DataType faultType = (DataType)exType.getLogical();
                            if (((FaultException)result).isMatchingType(faultType.getLogical())) {
                                targetDataType = exType;
                                break;
                            }
                        } else {
                            targetDataType = exType;
                            break;
                        }
                    }
                }

                /*
                if (targetDataType == null) {
                    // Not a business exception
                    return resultMsg;
                }
                */

                DataType targetFaultType = getFaultType(targetDataType);
                if (targetFaultType == null) {
                    throw new ServiceRuntimeException("Target fault type cannot be resolved: " + targetDataType,
                                                      (Throwable)result);
                    // throw new TransformationException("Target fault type cannot be resolved: " + targetDataType);
                }

                // FIXME: How to match a source fault type to a target fault
                // type?
                DataType sourceDataType = null;
                DataType sourceFaultType = null;
                for (DataType exType : sourceOperation.getFaultTypes()) {
                    DataType faultType = getFaultType(exType);
                    // Match by the QName (XSD element) of the fault type
                    if (faultType != null && typesMatch(targetFaultType.getLogical(), faultType.getLogical())) {
                        sourceDataType = exType;
                        sourceFaultType = faultType;
                        break;
                    }
                }

                if (sourceFaultType == null) {
                    throw new ServiceRuntimeException("No matching source fault type is found: " + targetFaultType,
                                                      (Throwable)result);
                    // throw new TransformationException("No matching source fault type is found: " + targetFaultType);
                }

                Object newResult =
                    transformException(result, targetDataType, sourceDataType, targetFaultType, sourceFaultType);
                if (newResult != result) {
                    resultMsg.setFaultBody(newResult);
                }
            }

        } else {
            assert !(result instanceof Throwable) : "Expected messages that are not throwable " + result;

            Object newResult = transform(result, targetType, sourceType, true);
            if (newResult != result) {
                resultMsg.setBody(newResult);
            }
        }

        return resultMsg;
    }
View Full Code Here

    }

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
       
        // Dispatch the service interaction to the service invoker
        Message requestMessage = messageFactory.createMessage();
        requestMessage.setBody(new Object[]{request, response});
        Message responseMessage = serviceInvoker.invoke(requestMessage);
        if (responseMessage.isFault()) {
           
            // Turn a fault into an exception
            //throw new ServletException((Throwable)responseMessage.getBody());
            Throwable e = (Throwable)responseMessage.getBody();
            ((HttpServletResponse)response).sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
        }
    }
View Full Code Here

            response.sendRedirect(request.getRequestURL().append('/').toString());
            return;
        }

        // Invoke the get operation on the service implementation
        Message requestMessage = messageFactory.createMessage();
        String id = path.substring(1);
        requestMessage.setBody(new Object[] {id});
        Message responseMessage = getInvoker.invoke(requestMessage);
        if (responseMessage.isFault()) {
            throw new ServletException((Throwable)responseMessage.getBody());
        }
       
        // Write the response from the service implementation to the response
        // output stream
        InputStream is = (InputStream)responseMessage.getBody();
        OutputStream os = response.getOutputStream();
        byte[] buffer = new byte[2048];
        for (;;) {
            int n = is.read(buffer);
            if (n <= 0)
View Full Code Here

            // Return an RSS feed containing the entries in the collection
            SyndFeed feed = null;
            if (supportsFeedEntries) {

                // The service implementation supports feed entries, invoke its getFeed operation
                Message requestMessage = messageFactory.createMessage();
                Message responseMessage = getFeedInvoker.invoke(requestMessage);
                if (responseMessage.isFault()) {
                    throw new ServletException((Throwable)responseMessage.getBody());
                }
                feed = (SyndFeed)responseMessage.getBody();
               
            } else {

                // The service implementation does not support feed entries, invoke its
                // getAll operation to get the data item collection. then create feed entries
                // from the data items
                Message requestMessage = messageFactory.createMessage();
                Message responseMessage;
                if (request.getQueryString() != null) {
                    requestMessage.setBody(new Object[] {request.getQueryString()});
                    responseMessage = queryInvoker.invoke(requestMessage);
                } else {
                    responseMessage = getAllInvoker.invoke(requestMessage);
                }
                if (responseMessage.isFault()) {
                    throw new ServletException((Throwable)responseMessage.getBody());
                }
                org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object>[] collection =
                    (org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object>[])responseMessage.getBody();
                if (collection != null) {
                    // Create the feed
                    feed = new SyndFeedImpl();
                    feed.setTitle("Feed");
                   
View Full Code Here

    public Message invoke(final Message msg) {
        // Schedule the invocation of the next interceptor in a new Work instance
        try {
            workScheduler.scheduleWork(new Runnable() {
                public void run() {
                    Message context = ThreadMessageContext.setMessageContext(msg);
                    try {
                        next.invoke(msg);
                    } finally {
                        ThreadMessageContext.setMessageContext(context);
                    }
View Full Code Here

TOP

Related Classes of org.apache.tuscany.sca.invocation.Message

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.