Package flex.messaging.io.amf

Examples of flex.messaging.io.amf.MessageBody


    response.setHeader("Content-Type", "application/x-amf;charset=x-user-defined");
    ServletOutputStream out = response.getOutputStream();

    ActionMessage requestMessage = new ActionMessage(MessageIOConstants.AMF3);

    MessageBody amfMessage = new MessageBody();

    if (command.equals("getTestString"))
    {
      amfMessage.setData("testString");
    }
    else if (command.equals("getTestObject"))
    {
      TestObject testObject = new TestObject("testString", true, false, new Date(), 1.0/3.0, 1, null);
      amfMessage.setData(testObject);
    }
    else if (command.equals("getTestArrayOfObjects"))
    {
      List<TestObject> list = new ArrayList<TestObject>();

      for (int i = 0; i < 10; i++)
      {
        TestObject testObject = new TestObject("testString " + i, true, false, new Date(), 1.0/3.0, 1, null);
        list.add(testObject);
      }

      amfMessage.setData(list);
    }
    else if (command.equals("getTestHashtable"))
    {
      Hashtable<String, Object> h = new Hashtable<String, Object>();

      h.put("right_now", new Date());
      h.put("test_double", Math.PI);
      String s = "testString";
      h.put("test_string", s);
      h.put("test_string_again", s);

      TestObject testObject = new TestObject("testString", true, false, new Date(), 1.0/3.0, 1, null);
      h.put("test_object", testObject);
      h.put("test_object_again", testObject);

      amfMessage.setData(h);
    }

    requestMessage.addBody(amfMessage);

    AmfMessageSerializer amfMessageSerializer = new AmfMessageSerializer();
View Full Code Here


            for (MessageHeader header : amfHeaders) {
                requestMessage.addHeader(header);
            }
        }

        MessageBody amfMessage = new MessageBody(command, responseURI, arguments);
        requestMessage.addBody(amfMessage);

        // Setup for AMF message serializer
        actionContext.setRequestMessage(requestMessage);
        ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
View Full Code Here

        this.endpoint = endpoint;
    }

    public void invoke(final ActionContext context)
    {
        MessageBody request = context.getRequestMessageBody();
        MessageBody response = context.getResponseMessageBody();

        Object data = request.getData();
        if (data instanceof List)
        {
            data = ((List)data).get(0);
        }
        else if (data.getClass().isArray())
        {
            data = Array.get(data, 0);
        }

        Message inMessage;
        if (data instanceof Message)
        {
            inMessage = (Message)data;
        }
        else
        {
            throw new MessageException("Request was not of type flex.messaging.messages.Message");
        }
       
        Object outMessage = null;

        String replyMethodName = MessageIOConstants.STATUS_METHOD;

        try
        {
            // Lookup or create the correct FlexClient.
            endpoint.setupFlexClient(inMessage);

            // Assign a clientId if necessary.
            // We don't need to assign clientIds to general poll requests.
            if (inMessage.getClientId() == null &&
                (!(inMessage instanceof CommandMessage) || ((CommandMessage)inMessage).getOperation() != CommandMessage.POLL_OPERATION))
            {
                Object clientId = UUIDUtils.createUUID();
                inMessage.setClientId(clientId);
            }
           
            // Messages received via the AMF channel can be batched (by NetConnection on the client) and
            // we must not put the handler thread into a poll-wait state if a poll command message is followed by
            // or preceeded by other messages in the batch; the request-response loop must complete without waiting.
            // If the poll command is the only message in the batch it's ok to wait.
            // If it isn't ok to wait, tag the poll message with a header that short-circuits any potential poll-wait.
            if (inMessage instanceof CommandMessage)
            {
                CommandMessage command = (CommandMessage)inMessage;
                if ((command.getOperation() == CommandMessage.POLL_OPERATION) && (context.getRequestMessage().getBodyCount() != 1))
                    command.setHeader(CommandMessage.SUPPRESS_POLL_WAIT_HEADER, Boolean.TRUE);
            }           
           
            // If MPI is enabled update the MPI metrics on the object referred to by the context
            // and the messages
            if (context.isMPIenabled())           
              MessagePerformanceUtils.setupMPII(context, inMessage);

            // Service the message.
            outMessage = endpoint.serviceMessage(inMessage);                      

            // if processing of the message resulted in an error, set up context and reply method accordingly
            if (outMessage instanceof ErrorMessage)
            {
                context.setStatus(MessageIOConstants.STATUS_ERR);
                replyMethodName = MessageIOConstants.STATUS_METHOD;               
            }
            else
            {
                replyMethodName = MessageIOConstants.RESULT_METHOD;
            }
        }
        catch (MessageException e)
        {
            context.setStatus(MessageIOConstants.STATUS_ERR);
            replyMethodName = MessageIOConstants.STATUS_METHOD;

            outMessage = e.createErrorMessage();
            ((ErrorMessage)outMessage).setCorrelationId(inMessage.getMessageId());
            ((ErrorMessage)outMessage).setDestination(inMessage.getDestination());
            ((ErrorMessage)outMessage).setClientId(inMessage.getClientId());

            if (e instanceof flex.messaging.security.SecurityException)
            {
                if (Log.isDebug())
                    Log.getLogger(LOG_CATEGORY).debug("Security error for message: " +
                         e.toString() + StringUtils.NEWLINE +
                         "  incomingMessage: " + inMessage + StringUtils.NEWLINE +
                         "  errorReply: " + outMessage);
            }
            else if (e.getCode() != null && e.getCode().equals(MessageService.NOT_SUBSCRIBED_CODE))
            {
                if (Log.isDebug())
                    Log.getLogger(LOG_CATEGORY).debug("Client not subscribed: " +
                         e.toString() + StringUtils.NEWLINE +
                         "  incomingMessage: " + inMessage + StringUtils.NEWLINE +
                         "  errorReply: " + outMessage);
            }
            else if (Log.isError())
                Log.getLogger(LOG_CATEGORY).error("Error handling message: " +
                     e.toString() + StringUtils.NEWLINE +
                     "  incomingMessage: " + inMessage + StringUtils.NEWLINE +
                     "  errorReply: " + outMessage);
        }
        catch (Throwable t)
        {
            // Handle any uncaught failures. The normal exception path on the server
            // is to throw MessageExceptions which are handled in the catch block above,
            // so if that was skipped we have an overlooked or serious problem.
            context.setStatus(MessageIOConstants.STATUS_ERR);
            replyMethodName = MessageIOConstants.STATUS_METHOD;
           
            String lmeMessage = t.getMessage();
            if (lmeMessage == null)
              lmeMessage = t.getClass().getName();
           
            MessageException lme = new MessageException();
            lme.setMessage(UNHANDLED_ERROR, new Object[] {lmeMessage});
                       
            outMessage = lme.createErrorMessage();
            ((ErrorMessage)outMessage).setCorrelationId(inMessage.getMessageId());
            ((ErrorMessage)outMessage).setDestination(inMessage.getDestination());
            ((ErrorMessage)outMessage).setClientId(inMessage.getClientId());           
           
            if (Log.isError())
            {
                StringBuffer sb = new StringBuffer();
                StackTraceElement [] el = t.getStackTrace();
                if (el != null)
                {
                    for (int i = 0; i < el.length; i++)
                    {
                        sb.append("    ");
                        sb.append(el[i].toString());
                        sb.append(StringUtils.NEWLINE);
                    }
                }
                Log.getLogger(LOG_CATEGORY).error("Unhandled error when processing a message: " +
                        t.toString() + StringUtils.NEWLINE +
                        "  incomingMessage: " + inMessage + StringUtils.NEWLINE +
                        "  errorReply: " + outMessage + StringUtils.NEWLINE +
                        "  stackTrace for: " + t.toString() +
                        StringUtils.NEWLINE + sb.toString());
            }
        }
        finally
        {
            // If MPI is enabled update the MPI metrics on the object referred to by the context
            // and the messages
            if (context.isRecordMessageSizes() || context.isRecordMessageTimes())
            {
                MessagePerformanceUtils.updateOutgoingMPI(context, inMessage, outMessage);
            }             

            // If our channel-endpoint combination supports small messages, and
            // if we know the current protocol version supports small messages,
            // try to replace the message...
            FlexSession session = FlexContext.getFlexSession();
            if (session != null && session.useSmallMessages()
                    && !context.isLegacy()
                    && context.getVersion() >= MessageIOConstants.AMF3
                    && outMessage instanceof Message)
            {
                outMessage = endpoint.convertToSmallMessage((Message)outMessage);
            }           
           
            response.setReplyMethod(replyMethodName);
            response.setData(outMessage);                      
        }
    }                 
View Full Code Here

        this.endpoint = endpoint;
    }

    public void invoke(final ActionContext context) throws IOException
    {
        MessageBody requestBody = context.getRequestMessageBody();
        context.setLegacy(true);

        // Parameters are usually sent as an AMF Array
        Object data = requestBody.getData();
        List newParams = null;

        // Check whether we're a new Flex 2.0 Messaging request
        if (data != null)
        {
            if (data.getClass().isArray())
            {
                int paramLength = Array.getLength(data);
                if (paramLength == 1)
                {
                    Object obj = Array.get(data, 0);
                    if (obj != null && obj instanceof Message)
                    {
                        context.setLegacy(false);
                        newParams = new ArrayList();
                        newParams.add(obj);
                    }
                }

                // It was not a Flex 2.0 Message, but we have an array, use its contents as our params
                if (newParams == null)
                {
                    newParams = new ArrayList();
                    for (int i = 0; i < paramLength; i++)
                    {
                        try
                        {
                            newParams.add(Array.get(data, i));
                        }
                        catch (Throwable t)
                        {
                        }
                    }
                }
            }
            else if (data instanceof List)
            {
                List paramList = (List)data;
                if (paramList.size() == 1)
                {
                    Object obj = paramList.get(0);
                    if (obj != null && obj instanceof Message)
                    {
                        context.setLegacy(false);
                        newParams = new ArrayList();
                        newParams.add(obj);
                    }
                }

                // It was not a Flex 2.0 Message, but we have a list, so use it as our params
                if (newParams == null)
                {
                    newParams = (List)data;
                }
            }
        }

        // We still haven't found any lists of params, so create one with
        // whatever data we have.
        if (newParams == null)
        {
            newParams = new ArrayList();
            newParams.add(data);

        }

        if (context.isLegacy())
        {
            newParams = legacyRequest(context, newParams);
        }

        requestBody.setData(newParams);


        next.invoke(context);


        if (context.isLegacy())
        {
            MessageBody responseBody = context.getResponseMessageBody();
            Object response = responseBody.getData();

            if (response instanceof ErrorMessage)
            {
                ErrorMessage error = (ErrorMessage)response;
                ASObject aso = new ASObject();
                aso.put("message", error.faultString);
                aso.put("code", error.faultCode);
                aso.put("details", error.faultDetail);
                aso.put("rootCause", error.rootCause);
                response = aso;
            }
            else if (response instanceof Message)
            {
                response = ((Message)response).getBody();
            }
            responseBody.setData(response);
        }
    }
View Full Code Here

    {
        List newParams = new ArrayList(1);
        Map headerMap = new HashMap();
        Object body = oldParams;
        Message message = null;
        MessageBody requestBody = context.getRequestMessageBody();

        // Legacy Packet Security
        List packetHeaders = context.getRequestMessage().getHeaders();
        packetCredentials(packetHeaders, headerMap);
       
View Full Code Here

    private void deserializationError(ActionContext context, Throwable t)
    {
        context.setStatus(MessageIOConstants.STATUS_ERR);

        // Create a single message body to hold the error
        MessageBody responseBody = new MessageBody();
        if (context.getMessageNumber() < context.getRequestMessage().getBodyCount())
        {
            responseBody.setTargetURI(context.getRequestMessageBody().getResponseURI());
        }

        // if the message couldn't be deserialized enough to know the version, set the current version here
        if (context.getVersion() == 0)
        {
            context.setVersion(ActionMessage.CURRENT_VERSION);
        }

        // append the response body to the output message
        context.getResponseMessage().addBody(responseBody);

        String message;

        MessageException methodResult;
        if (t instanceof MessageException)
        {
            methodResult = (MessageException)t;
            message = methodResult.getMessage();
        }
        else
        {
            //Error deserializing client message.
            methodResult = new SerializationException();
            methodResult.setMessage(REQUEST_ERROR);
            methodResult.setRootCause(t);
            message = methodResult.getMessage();
        }
        responseBody.setData(methodResult.createErrorMessage());
        responseBody.setReplyMethod(MessageIOConstants.STATUS_METHOD);

        if (Log.isError())
            logger.error(message + StringUtils.NEWLINE + ExceptionUtil.toString(t));
    }
View Full Code Here

    private void unhandledError(ActionContext context, Throwable t)
    {
        ActionMessage responseMessage = new ActionMessage();
        context.setResponseMessage(responseMessage);

        MessageBody responseBody = new MessageBody();
        responseBody.setTargetURI(context.getRequestMessageBody().getResponseURI());

        context.getResponseMessage().addBody(responseBody);

        MessageException methodResult;

        if (t instanceof MessageException)
        {
            methodResult = (MessageException)t;
        }
        else
        {
            // An unhandled error occurred while processing client request(s).
            methodResult = new SerializationException();
            methodResult.setMessage(UNHANDLED_ERROR);
            methodResult.setRootCause(t);
        }

        responseBody.setData(methodResult);
        responseBody.setReplyMethod(MessageIOConstants.STATUS_METHOD);

        logger.info(t.getMessage());
    }
View Full Code Here

        context.setResponseMessage(responseMessage);

        int bodyCount = context.getRequestMessage().getBodyCount();
        for (context.setMessageNumber(0); context.getMessageNumber() < bodyCount; context.incrementMessageNumber())
        {
            MessageBody responseBody = new MessageBody();
            responseBody.setTargetURI(context.getRequestMessageBody().getResponseURI());
            context.getResponseMessage().addBody(responseBody);

            Object methodResult;

            if (t instanceof MessageException)
            {
                methodResult = ((MessageException)t).createErrorMessage();
            }
            else
            {
                String message = "An error occurred while serializing server response(s).";
                if (t.getMessage() != null)
                {
                    message = t.getMessage();
                    if (message == null)
                        message = t.toString();
                }

                methodResult = new MessageException(message, t).createErrorMessage();
            }

            if (context.isLegacy())
            {
                if (methodResult instanceof ErrorMessage)
                {
                    ErrorMessage error = (ErrorMessage)methodResult;
                    ASObject aso = new ASObject();
                    aso.put("message", error.faultString);
                    aso.put("code", error.faultCode);
                    aso.put("details", error.faultDetail);
                    aso.put("rootCause", error.rootCause);
                    methodResult = aso;
                }
                else if (methodResult instanceof Message)
                {
                    methodResult = ((Message)methodResult).getBody();
                }
            }
            else
            {
                Object data = context.getRequestMessageBody().getData();
                if (data instanceof List)
                {
                    data = ((List)data).get(0);
                }
                else if (data.getClass().isArray())
                {
                    data = Array.get(data, 0);
                }

                Message inMessage;
                if (data instanceof Message)
                {
                    inMessage = (Message)data;
                    if (inMessage.getClientId() != null)
                    {
                        ((ErrorMessage)methodResult).setClientId(inMessage.getClientId().toString());
                    }
                    if (inMessage.getMessageId() != null)
                    {
                        ((ErrorMessage)methodResult).setCorrelationId(inMessage.getMessageId());
                        ((ErrorMessage)methodResult).setDestination(inMessage.getDestination());
                    }
                }
            }

            responseBody.setData(methodResult);
            responseBody.setReplyMethod(MessageIOConstants.STATUS_METHOD);
        }

        if (Log.isError())
            logger.error("Exception occurred during serialization: " + ExceptionUtil.toString(t));
View Full Code Here

        for (context.setMessageNumber(0); context.getMessageNumber() < bodyCount; context.incrementMessageNumber())
        {
            try
            {
                // create the response body
                MessageBody responseBody = new MessageBody();
                responseBody.setTargetURI(context.getRequestMessageBody().getResponseURI());

                // append the response body to the output message
                context.getResponseMessage().addBody(responseBody);

                //Check that deserialized message body data type was valid. If not, skip this message.
View Full Code Here

        // Write out the body
        int bodyCount = m.getBodyCount();
        for (int i = 0; i < bodyCount; ++i)
        {
            MessageBody body = m.getBody(i);

            if (isDebug)
                debugTrace.startMessage(body.getTargetURI(), body.getResponseURI(), i);

            writeBody(body);

            if (isDebug)
                debugTrace.endMessage();
View Full Code Here

TOP

Related Classes of flex.messaging.io.amf.MessageBody

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.