Android Open Source - RealtimeMessaging-Android Ortc Message






From Project

Back to project page RealtimeMessaging-Android.

License

The source code is released under:

MIT License

If you think the Android project RealtimeMessaging-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 *//  w ww .  j a  v a2s .  c  om
 */
package ibt.ortc.plugins.IbtRealtimeSJ;

import ibt.ortc.api.Strings;


//import ibt.ortc.extensibility.CharEscaper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class OrtcMessage {
    private static final String OPERATION_PATTERN = "^a\\[\"\\{\\\\\"op\\\\\":\\\\\"([^\"]+)\\\\\",(.*)\\}\"\\]$";
    private static final String CHANNEL_PATTERN = "^\\\\\"ch\\\\\":\\\\\"(.*)\\\\\"$";
    private static final String RECEIVED_PATTERN = "^a?\\[\"\\{\\\\\"ch\\\\\":\\\\\"(.*)\\\\\",\\\\\"m\\\\\":\\\\\"([\\s\\S]*?)\\\\\"\\}\"\\]$";
    private static final String MULTI_PART_MESSAGE_PATTERN = "^(.[^_]*)_(.[^-]*)-(.[^_]*)_([\\s\\S]*?)$";
    private static final String EXCEPTION_PATTERN = "^\\\\\"ex\\\\\":(\\{.*\\})$";
    private static final String PERMISSIONS_PATTERN = "^\\\\\"up\\\\\":{1}(.*),\\\\\"set\\\\\":(.*)$";

    private static Pattern operationPattern;
    private static Pattern subscribedPattern;
    private static Pattern receivedPattern;
    private static Pattern multipartMessagePattern;
    private static Pattern unsubscribedPattern;
    private static Pattern exceptionPattern;
    private static Pattern permissionsPattern;

    private OrtcOperation operation;
    private String message;
    private String messageChannel;
    private String messageId;
    private int messagePart;
    private int messageTotalParts;
    private static final Hashtable<String, OrtcOperation> operationIndex = new Hashtable<String, OrtcOperation>(11);
    private static final Hashtable<String, OrtcServerErrorException.OrtcServerErrorOperation> errorOperationIndex = new Hashtable<String, OrtcServerErrorException.OrtcServerErrorOperation>(11);

    static {
            operationPattern = Pattern.compile(OPERATION_PATTERN);
            subscribedPattern = Pattern.compile(CHANNEL_PATTERN);
            receivedPattern = Pattern.compile(RECEIVED_PATTERN);
            multipartMessagePattern = Pattern.compile(MULTI_PART_MESSAGE_PATTERN);
        unsubscribedPattern = Pattern.compile(CHANNEL_PATTERN);
            exceptionPattern = Pattern.compile(EXCEPTION_PATTERN);
            permissionsPattern = Pattern.compile(PERMISSIONS_PATTERN);

        operationIndex.put("ortc-validated", OrtcOperation.Validated);
        operationIndex.put("ortc-subscribed", OrtcOperation.Subscribed);
        operationIndex.put("ortc-unsubscribed", OrtcOperation.Unsubscribed);
        operationIndex.put("ortc-error", OrtcOperation.Error);

        errorOperationIndex.put("ex", OrtcServerErrorException.OrtcServerErrorOperation.Unexpected);
        errorOperationIndex.put("validate", OrtcServerErrorException.OrtcServerErrorOperation.Validate);
        errorOperationIndex.put("subscribe", OrtcServerErrorException.OrtcServerErrorOperation.Subscribe);
        errorOperationIndex.put("subscribe_maxsize", OrtcServerErrorException.OrtcServerErrorOperation.Subscribe_MaxSize);
        errorOperationIndex.put("unsubscribe_maxsize", OrtcServerErrorException.OrtcServerErrorOperation.Unsubscribe_MaxSize);
        errorOperationIndex.put("send_maxsize", OrtcServerErrorException.OrtcServerErrorOperation.Send_MaxSize);
    }

    public OrtcMessage(OrtcOperation operation, String message, String messageChannel, String messageId, int messagePart, int messageTotalParts) {
        this.operation = operation;
        this.message = message;
        this.messageChannel = messageChannel;
        this.messageId = messageId;
        this.messagePart = messagePart;
        this.messageTotalParts = messageTotalParts;
    }

    // CAUSE: Prefer throwing/catching meaningful exceptions instead of Exception
    public static OrtcMessage parseMessage(String message) throws IOException {
        OrtcOperation operation = null;
        String parsedMessage = null;
        String messageChannel = null;
        String messageId = null;
        int messagePart = -1;
        int messageTotalParts = -1;

        Matcher matcher = operationPattern.matcher(message);

        if (matcher != null && !matcher.matches()) {
            //matcher = receivedPattern.matcher(message.replace("\\\"", "\""));
      matcher = receivedPattern.matcher(message);
      
            if (matcher != null && matcher.matches()) {
                operation = OrtcOperation.Received;
                parsedMessage = matcher.group(2);
                messageChannel = matcher.group(1);

                //parsedMessage = parsedMessage.replace("\\\\n", "\n").replace("\\\"", "\"").replace("\\\\\\\\", "\\");

                Matcher multiPartMatcher = parseMultiPartMessage(parsedMessage);

                try{
                    if (multiPartMatcher.matches()) {
                        parsedMessage = multiPartMatcher.group(4);
                        messageId = multiPartMatcher.group(1);
                        messagePart = Strings.isNullOrEmpty(multiPartMatcher.group(2)) ? -1 : Integer.parseInt(multiPartMatcher.group(2));
                        messageTotalParts = Strings.isNullOrEmpty(multiPartMatcher.group(3)) ? -1 : Integer.parseInt(multiPartMatcher.group(3));
                    }
                }catch(NumberFormatException parseException){
                    //throw new NumberFormatException("Invalid message format: " + message + " - Error " + parseException.toString());
                    parsedMessage = matcher.group(2);
                    messageId = null;
                    messagePart = -1;
                    messageTotalParts = -1;
                }
            } else {
                // CAUSE: Prefer String.format to +
                throw new IOException(String.format("Invalid message format: %s", message));
            }
        // CAUSE: Possible null pointer dereference
        } else {
            operation = operationIndex.get(matcher.group(1));
            parsedMessage = matcher.group(2);
        }

        return new OrtcMessage(operation, parsedMessage, messageChannel, messageId, messagePart, messageTotalParts);
    }

    private static Matcher parseMultiPartMessage(String message) {
        Matcher result = multipartMessagePattern.matcher(message);

        return result;
    }

    public int getSessionExpirationTime() {
        int result = 0;

        Matcher matcher = permissionsPattern.matcher(this.message);

        if (matcher != null && matcher.matches()) {
            String content = matcher.group(2);

            if (!Strings.isNullOrEmpty(content)) {
                result = Integer.parseInt(content);
            }
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    // TODO: Hashtable is an absolete Collection
    public Hashtable<String, String> getPermissions() {
        // CAUSE: Instantiating collection without specified initial capacity
        // TODO: Hashtable is an absolete Collection
        Hashtable<String, String> result = new Hashtable<String, String>(11);

        Matcher matcher = permissionsPattern.matcher(this.message);

        if (matcher != null && matcher.matches()) {
            String content = matcher.group(1);

      content = JSONValue.parse("\""+content+"\"").toString();
            JSONObject json = (JSONObject) (JSONValue.parse(content));

            if (json != null) {
                Iterator<?> iter = ((Map<String, OrtcOperation>) json).entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();

                    // CAUSE: Redundant cast
                    result.put(entry.getKey(), entry.getValue());
                }
            }
        }

        return result;
    }

    public String channelSubscribed() throws Exception {
        Matcher matcher = subscribedPattern.matcher(this.message);

        if (!matcher.matches()) {
            // TODO: create a custom exception
            throw new Exception("Subscribe channel match to channel not found");
        }

        String result = matcher.group(1);

        return result;
    }

    public String channelUnsubscribed() throws Exception {
        Matcher matcher = unsubscribedPattern.matcher(this.message);

        if (!matcher.matches()) {
            // TODO: create a custom exception
            throw new Exception("Unsubscribe channel match to channel not found");
        }

        String result = matcher.group(1);

        return result;
    }

    public OrtcServerErrorException serverError() throws Exception {
        Matcher matcher = exceptionPattern.matcher(this.message);

        if (!matcher.matches()) {
            // TODO: create a custom exception
            throw new Exception("Exception match not found");
        }

        String content = matcher.group(1).replace("\\\"", "\"");        

        JSONObject json = (JSONObject) (JSONValue.parse(content));

        String errorMessage = (String)(json.get("ex"));
        String errorOperation = (String)(json.get("op"));
        String channel = (String)(json.get("ch"));

        OrtcServerErrorException.OrtcServerErrorOperation op = errorOperationIndex.get(errorOperation);

        return new OrtcServerErrorException(op,channel,errorMessage);
    }

    public OrtcOperation getOperation() {
        return operation;
    }

    public String getMessage() {
        return message;
    }

    public String getMessageChannel() {
        return this.messageChannel;
    }

    public String getMessageId() {
        return messageId;
    }

    public int getMessagePart() {
        return messagePart;
    }

    public int getMessageTotalParts() {
        return messageTotalParts;
    }
}




Java Source Code List

ibt.ortc.api.ApplicationTest.java
ibt.ortc.api.Authentication.java
ibt.ortc.api.Balancer.java
ibt.ortc.api.ChannelPermissions.java
ibt.ortc.api.InvalidBalancerServerException.java
ibt.ortc.api.OnDisablePresence.java
ibt.ortc.api.OnEnablePresence.java
ibt.ortc.api.OnPresence.java
ibt.ortc.api.OnRestWebserviceResponse.java
ibt.ortc.api.OrtcAuthenticationNotAuthorizedException.java
ibt.ortc.api.Ortc.java
ibt.ortc.api.Pair.java
ibt.ortc.api.Presence.java
ibt.ortc.api.RestWebservice.java
ibt.ortc.api.SecureWebConnections.java
ibt.ortc.api.Strings.java
ibt.ortc.extensibility.ChannelSubscription.java
ibt.ortc.extensibility.CharEscaper.java
ibt.ortc.extensibility.ConnectionProtocol.java
ibt.ortc.extensibility.DispatchedMessages.java
ibt.ortc.extensibility.EventEnum.java
ibt.ortc.extensibility.GcmOrtcBroadcastReceiver.java
ibt.ortc.extensibility.GcmOrtcIntentService.java
ibt.ortc.extensibility.GcmRegistration.java
ibt.ortc.extensibility.HeartbeatSender.java
ibt.ortc.extensibility.OnConnected.java
ibt.ortc.extensibility.OnDisconnected.java
ibt.ortc.extensibility.OnException.java
ibt.ortc.extensibility.OnMessageWithPayload.java
ibt.ortc.extensibility.OnMessage.java
ibt.ortc.extensibility.OnReconnected.java
ibt.ortc.extensibility.OnReconnecting.java
ibt.ortc.extensibility.OnSubscribed.java
ibt.ortc.extensibility.OnUnsubscribed.java
ibt.ortc.extensibility.OrtcClient.java
ibt.ortc.extensibility.OrtcFactory.java
ibt.ortc.extensibility.exception.OrtcAlreadyConnectedException.java
ibt.ortc.extensibility.exception.OrtcDoesNotHavePermissionException.java
ibt.ortc.extensibility.exception.OrtcEmptyFieldException.java
ibt.ortc.extensibility.exception.OrtcGcmException.java
ibt.ortc.extensibility.exception.OrtcInvalidCharactersException.java
ibt.ortc.extensibility.exception.OrtcMaxLengthException.java
ibt.ortc.extensibility.exception.OrtcNotConnectedException.java
ibt.ortc.extensibility.exception.OrtcNotSubscribedException.java
ibt.ortc.extensibility.exception.OrtcSubscribedException.java
ibt.ortc.ortclib.ApplicationTest.java
ibt.ortc.ortclib.MainActivity.java
ibt.ortc.ortclib.SettingsActivity.java
ibt.ortc.plugins.IbtRealtimeSJ.IbtRealtimeSJClient.java
ibt.ortc.plugins.IbtRealtimeSJ.IbtRealtimeSJFactory.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcMessage.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcOperation.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcServerErrorException.java
ibt.ortc.plugins.websocket.WebSocketConnection.java
ibt.ortc.plugins.websocket.WebSocketEventHandler.java
ibt.ortc.plugins.websocket.WebSocketException.java
ibt.ortc.plugins.websocket.WebSocketHandshake.java
ibt.ortc.plugins.websocket.WebSocketMessage.java
ibt.ortc.plugins.websocket.WebSocketReceiver.java
ibt.ortc.plugins.websocket.WebSocketSender.java
ibt.ortc.plugins.websocket.WebSocket.java