All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Util.ResponseHelper Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Util;

import com.alachisoft.ncache.common.protobuf.*;
import com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception;
import com.alachisoft.ncache.runtime.exceptions.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class ResponseHelper {
    public static ResponseProtocol.Response deserializeResponse(byte[] bytes) {
        ResponseProtocol.Response response = null;
        byte[] length = new byte[10];
        System.arraycopy(bytes, 0, length, 0, length.length);
        int size = ToInteger(length);

        byte[] responseBytes = new byte[size];
        System.arraycopy(bytes, 10, responseBytes, 0, size);
        InputStream targetStream = new ByteArrayInputStream(responseBytes);
        try {
            response = ResponseProtocol.Response.parseFrom(targetStream);
        } catch (IOException e) {
        } finally {
            try {
                targetStream.close();
            } catch (IOException e) {

            }
        }
        return response;
    }

    public static ResponseProtocol.Response deserializeResponse(ResponseProtocol.Response.Type responseType, InputStream stream) throws IOException {
        ResponseProtocol.Response.Builder response = ResponseProtocol.Response.newBuilder();
        response.setResponseType(responseType);
        switch (responseType) {
            case INSERT:
                InsertResponseProtocol.InsertResponse insertResponse = InsertResponseProtocol.InsertResponse.parseFrom(stream);
                response.setRequestId(insertResponse.getRequestId())
                        .setCommandID(insertResponse.getCommandID())
                        .setInsert(insertResponse);
                break;
            case GET:
                GetResponseProtocol.GetResponse getResponse = GetResponseProtocol.GetResponse.parseFrom(stream);
                response.setRequestId(getResponse.getRequestId())
                        .setCommandID(getResponse.getCommandID())
                        .setGet(getResponse);
                break;
            case INSERT_BULK:
                BulkInsertResponseProtocol.BulkInsertResponse bulkInsertResponse = BulkInsertResponseProtocol.BulkInsertResponse.parseFrom(stream);
                response.setRequestId(bulkInsertResponse.getRequestId())
                        .setCommandID(bulkInsertResponse.getCommandID())
                        .setIntendedRecipient(bulkInsertResponse.getIntendedRecipient())
                        .setBulkInsert(bulkInsertResponse);
                break;
            case GET_BULK:
                BulkGetResponseProtocol.BulkGetResponse bulkGetResponse = BulkGetResponseProtocol.BulkGetResponse.parseFrom(stream);
                response.setRequestId(bulkGetResponse.getRequestId())
                        .setCommandID(bulkGetResponse.getCommandID())
                        .setIntendedRecipient(bulkGetResponse.getIntendedRecipient())
                        .setNumberOfChuncks(bulkGetResponse.getNumberOfChuncks())
                        .setSequenceId(bulkGetResponse.getSequenceId())
                        .setBulkGet(bulkGetResponse);
                break;
            case EXECUTE_READER:
                ExecuteReaderResponseProtocol.ExecuteReaderResponse executeReaderResponse = ExecuteReaderResponseProtocol.ExecuteReaderResponse.parseFrom(stream);
                response.setRequestId(executeReaderResponse.getRequestId())
                        .setCommandID(executeReaderResponse.getCommandID())
                        .setNumberOfChuncks(executeReaderResponse.getNumberOfChuncks())
                        .setSequenceId(executeReaderResponse.getSequenceId())
                        .setExecuteReaderResponse(executeReaderResponse);
                break;
            case GET_READER_CHUNK:
                GetReaderChunkResponseProtocol.GetReaderChunkResponse getreaderChunkResponse = GetReaderChunkResponseProtocol.GetReaderChunkResponse.parseFrom(stream);
                response.setRequestId(getreaderChunkResponse.getRequestId())
                        .setCommandID(getreaderChunkResponse.getCommandID())
                        .setNumberOfChuncks(getreaderChunkResponse.getNumberOfChuncks())
                        .setSequenceId(getreaderChunkResponse.getSequenceId())
                        .setGetReaderChunkResponse(getreaderChunkResponse);
                break;
            case GET_NEXT_CHUNK:
                GetNextChunkResponseProtocol.GetNextChunkResponse getNextChunkResponse = GetNextChunkResponseProtocol.GetNextChunkResponse.parseFrom(stream);
                response.setRequestId(getNextChunkResponse.getRequestId())
                        .setCommandID(getNextChunkResponse.getCommandID())
                        .setGetNextChunkResponse(getNextChunkResponse);
                break;
            case DELETE:
                DeleteResponseProtocol.DeleteResponse deleteResponse = DeleteResponseProtocol.DeleteResponse.parseFrom(stream);
                response.setRequestId(deleteResponse.getRequestId())
                        .setCommandID(deleteResponse.getCommandID())
                        .setDeleteResponse(deleteResponse);
                break;

            case REMOVE:
                RemoveResponseProtocol.RemoveResponse removeResponse = RemoveResponseProtocol.RemoveResponse.parseFrom(stream);
                response.setRequestId(removeResponse.getRequestId())
                        .setCommandID(removeResponse.getCommandID())
                        .setRemove(removeResponse);
                break;
            case REMOVE_GROUP:
                RemoveGroupResponseProtocol.RemoveGroupResponse removeGroupResponse = RemoveGroupResponseProtocol.RemoveGroupResponse.parseFrom(stream);
                response.setRequestId(removeGroupResponse.getRequestId())
                        .setCommandID(removeGroupResponse.getCommandID())
                        .setRemoveGroupResponse(removeGroupResponse);
                break;
            case REMOVE_TAG:
                RemoveByTagResponseProtocol.RemoveByTagResponse tagResponse = RemoveByTagResponseProtocol.RemoveByTagResponse.parseFrom(stream);
                response.setRequestId(tagResponse.getRequestId())
                        .setCommandID(tagResponse.getCommandID())
                        .setRemoveTagResponse(tagResponse);
                break;
            case COUNT:
                CountResponseProtocol.CountResponse countResponse = CountResponseProtocol.CountResponse.parseFrom(stream);
                response.setRequestId(countResponse.getRequestId())
                        .setCommandID(countResponse.getCommandID())
                        .setCount(countResponse);
                break;

            case ADD:
                AddResponseProtocol.AddResponse addResponse = AddResponseProtocol.AddResponse.parseFrom(stream);
                response.setRequestId(addResponse.getRequestId())
                        .setCommandID(addResponse.getCommandID())
                        .setAddResponse(addResponse);
                break;

            case ADD_BULK:
                BulkAddResponseProtocol.BulkAddResponse bulkAddResponse = BulkAddResponseProtocol.BulkAddResponse.parseFrom(stream);
                response.setRequestId(bulkAddResponse.getRequestId())
                        .setCommandID(bulkAddResponse.getCommandID())
                        .setIntendedRecipient(bulkAddResponse.getIntendedRecipient())
                        .setBulkAdd(bulkAddResponse);
                break;

            case CLEAR:
                ClearResponseProtocol.ClearResponse clearResponse = ClearResponseProtocol.ClearResponse.parseFrom(stream);
                response.setRequestId(clearResponse.getRequestId())
                        .setCommandID(clearResponse.getCommandID())
                        .setClearResponse(clearResponse);
                break;
            case CONTAINS_BULK:
                ContainBulkResponseProtocol.ContainBulkResponse containBulkResponse = ContainBulkResponseProtocol.ContainBulkResponse.parseFrom(stream);
                response.setRequestId(containBulkResponse.getRequestId());
                response.setCommandID(containBulkResponse.getCommandID());
                response.setContainBulkResponse(containBulkResponse);
                break;
            case ADD_ATTRIBUTE:
                AddAttributeResponseProtocol.AddAttributeResponse addAttribute = AddAttributeResponseProtocol.AddAttributeResponse.parseFrom(stream);
                response.setRequestId(addAttribute.getRequestId());
                response.setCommandID(addAttribute.getCommandID());
                response.setAddAttributeResponse(addAttribute);
                break;

            case ADD_DEPENDENCY:
                AddDependencyResponseProtocol.AddDependencyResponse addDependency = AddDependencyResponseProtocol.AddDependencyResponse.parseFrom(stream);
                
                response.setRequestId(addDependency.getRequestId());
                response.setCommandID(addDependency.getCommandID());
                
                response.setAddDep(addDependency);
                break;
            case ADD_SYNC_DEPENDENCY:
                AddSyncDependencyResponseProtocol.AddSyncDependencyResponse addSyncDependency = AddSyncDependencyResponseProtocol.AddSyncDependencyResponse.parseFrom(stream);
                
                response.setRequestId(addSyncDependency.getRequestId());
                response.setCommandID(addSyncDependency.getCommandID());
                
                response.setAddSyncDependencyResponse(addSyncDependency);
                break;
            case CANCEL_TASK:
                TaskCancelResponseProtocol.TaskCancelResponse cancelResponse = TaskCancelResponseProtocol.TaskCancelResponse.parseFrom(stream);
                
                response.setRequestId(cancelResponse.getRequestId());
                response.setCommandID(cancelResponse.getCommandID());
                
                response.setTaskCancelResponse(cancelResponse);
                break;
            case REGISTER_CQ:
                RegisterCQResponseProtocol.RegisterCQResponse registerCQResponse = RegisterCQResponseProtocol.RegisterCQResponse.parseFrom(stream);
                
                response.setRequestId(registerCQResponse.getRequestId());
                response.setCommandID(registerCQResponse.getCommandID());
                
                response.setRegisterCQResponse(registerCQResponse);
                break;
            case GET_TAG:
                GetTagResponseProtocol.GetTagResponse getTag = GetTagResponseProtocol.GetTagResponse.parseFrom(stream);
                
                response.setRequestId(getTag.getRequestId());
                response.setCommandID(getTag.getCommandID());
                response.setNumberOfChuncks(getTag.getNumberOfChuncks());
                response.setSequenceId(getTag.getSequenceId());
                
                response.setGetTag(getTag);
                break;
            case GET_CACHE_ITEM:
                GetCacheItemResponseProtocol.GetCacheItemResponse getCacheItem = GetCacheItemResponseProtocol.GetCacheItemResponse.parseFrom(stream);
                
                response.setRequestId(getCacheItem.getRequestId());
                response.setCommandID(getCacheItem.getCommandID());
                
                response.setGetItem(getCacheItem);
                break;
            case GET_GROUP_KEYS:
                GetGroupKeysResponseProtocol.GetGroupKeysResponse getGroupKeys = GetGroupKeysResponseProtocol.GetGroupKeysResponse.parseFrom(stream);
                
                response.setRequestId(getGroupKeys.getRequestId());
                response.setCommandID(getGroupKeys.getCommandID());
                
                response.setGetGroupKeys(getGroupKeys);
                break;
            case GET_GROUP_DATA:
                GetGroupDataResponseProtocol.GetGroupDataResponse getGroupData = GetGroupDataResponseProtocol.GetGroupDataResponse.parseFrom(stream);
                
                response.setRequestId(getGroupData.getRequestId());
                response.setCommandID(getGroupData.getCommandID());
                response.setSequenceId(getGroupData.getSequenceId());
                response.setNumberOfChuncks(getGroupData.getNumberOfChuncks());
                
                response.setGetGroupData(getGroupData);
                break;
            case GET_GROUP_NEXT_CHUNK:
                GetGroupNextChunkResponseProtocol.GetGroupNextChunkResponse getGroupNext = GetGroupNextChunkResponseProtocol.GetGroupNextChunkResponse.parseFrom(stream);
                
                response.setRequestId(getGroupNext.getRequestId());
                response.setCommandID(getGroupNext.getCommandID());
                
                response.setGetGroupNextChunkResponse(getGroupNext);
                break;
            case BULK_GET_CACHEITEM:
                BulkGetCacheItemResponseProtocol.BulkGetCacheItemResponse bulkGetCacheItem = BulkGetCacheItemResponseProtocol.BulkGetCacheItemResponse.parseFrom(stream);
                
                response.setRequestId(bulkGetCacheItem.getRequestId());
                response.setCommandID(bulkGetCacheItem.getCommandID());
                
                response.setBulkGetCacheItem(bulkGetCacheItem);
                break;
            case DATATYPE_RESPONSE:
                DataTypeResponseProtocol.DataTypeResponse dataTypeResponse = DataTypeResponseProtocol.DataTypeResponse.parseFrom(stream);
                
                response.setRequestId(dataTypeResponse.getRequestId());
                response.setCommandID(dataTypeResponse.getCommandID());
                response.setResponseType(ResponseProtocol.Response.Type.DATATYPE_RESPONSE);
                response.setDataTypeResponse(dataTypeResponse);
                break;
            case REMOVE_BULK:
                BulkRemoveResponseProtocol.BulkRemoveResponse bulkRemoveResponse = BulkRemoveResponseProtocol.BulkRemoveResponse.parseFrom(stream);
                
                response.setRequestId(bulkRemoveResponse.getRequestId());
                response.setCommandID(bulkRemoveResponse.getCommandID());
                response.setNumberOfChuncks(bulkRemoveResponse.getNumberOfChuncks());
                response.setSequenceId(bulkRemoveResponse.getSequenceId());
                
                response.setBulkRemove(bulkRemoveResponse);
                break;
            case DELETE_BULK:
                BulkDeleteResponseProtocol.BulkDeleteResponse bulkDeleteResponse = BulkDeleteResponseProtocol.BulkDeleteResponse.parseFrom(stream);
                
                response.setRequestId(bulkDeleteResponse.getRequestId());
                response.setCommandID(bulkDeleteResponse.getCommandID());
                response.setIntendedRecipient(bulkDeleteResponse.getIntendedRecipient());
                
                response.setBulkDeleteResponse(bulkDeleteResponse);
                break;
            case REMOVE_TOPIC:
                RemoveTopicResponseProtocol.RemoveTopicResponse removeTopicResponse = RemoveTopicResponseProtocol.RemoveTopicResponse.parseFrom(stream);
                
                response.setRequestId(removeTopicResponse.getRequestId());
                response.setCommandID(removeTopicResponse.getCommandID());
                
                response.setRemoveTopicResponse(removeTopicResponse);
                break;
            case GET_TOPIC:
                GetTopicResponseProtocol.GetTopicResponse getTopic = GetTopicResponseProtocol.GetTopicResponse.parseFrom(stream);
                
                response.setRequestId(getTopic.getRequestId());
                response.setCommandID(getTopic.getCommandID());
                
                response.setGetTopicResponse(getTopic);
                break;
            case GET_MESSAGE:
                GetMessageResponseProtocol.GetMessageResponse getMessage = GetMessageResponseProtocol.GetMessageResponse.parseFrom(stream);
                
                response.setRequestId(getMessage.getRequestId());
                response.setCommandID(getMessage.getCommandID());
                
                response.setGetMessageResponse(getMessage);
                break;
            case MESSAGE_PUBLISH:
                MessagePublishResponseProtocol.MessagePublishResponse messagePublish = MessagePublishResponseProtocol.MessagePublishResponse.parseFrom(stream);
                
                response.setRequestId(messagePublish.getRequestId());
                response.setCommandID(messagePublish.getCommandID());
                
                response.setMessagePublishResponse(messagePublish);
                break;
            case MESSAGE_PUBLISH_BULK:
                MessagePublishBulkResponseProtocol.MessagePublishBulkResponse messagePublishBulk = MessagePublishBulkResponseProtocol.MessagePublishBulkResponse.parseFrom(stream);
                    response.setRequestId( messagePublishBulk.getRequestId());
                    response.setCommandID(messagePublishBulk.getCommandID());
                    response.setResponseType(ResponseProtocol.Response.Type.MESSAGE_PUBLISH_BULK);
                    response.setMessagePublishBulkResponse( messagePublishBulk);
                    break;
            case MESSAGE_COUNT:
                MessageCountResponseProtocol.MessageCountResponse messageCount = MessageCountResponseProtocol.MessageCountResponse.parseFrom(stream);
                
                response.setRequestId(messageCount.getRequestId());
                response.setCommandID(messageCount.getCommandID());
                
                response.setMessageCountResponse(messageCount);
                break;
            case DELETE_QUERY:
                DeleteQueryResponseProtocol.DeleteQueryResponse delete = DeleteQueryResponseProtocol.DeleteQueryResponse.parseFrom(stream);
                
                response.setRequestId(delete.getRequestId());
                response.setCommandID(delete.getCommandID());
                
                response.setDeleteQueryResponse(delete);
                break;
            case DISPOSE:
                DisposeResponseProtocol.DisposeResponse disposeResponse = DisposeResponseProtocol.DisposeResponse.parseFrom(stream);
                
                response.setRequestId(disposeResponse.getRequestId());
                response.setCommandID(disposeResponse.getCommandID());
                
                response.setDisposeResponse(disposeResponse);
                break;
            case DISPOSE_READER:
                DisposeReaderResponseProtocol.DisposeReaderResponse disposeReader = DisposeReaderResponseProtocol.DisposeReaderResponse.parseFrom(stream);
                
                response.setRequestId(disposeReader.getRequestId());
                response.setCommandID(disposeReader.getCommandID());
                
                response.setDisposeReaderResponse(disposeReader);
                break;
            case EXECUTE_READER_CQ:
                ExecuteReaderCQResponseProtocol.ExecuteReaderCQResponse executeReaderCQ = ExecuteReaderCQResponseProtocol.ExecuteReaderCQResponse.parseFrom(stream);
                
                response.setRequestId(executeReaderCQ.getRequestId());
                response.setCommandID(executeReaderCQ.getCommandID());
                response.setNumberOfChuncks(executeReaderCQ.getNumberOfChuncks());
                response.setSequenceId(executeReaderCQ.getSequenceId());
                
                response.setExecuteReaderCQResponse(executeReaderCQ);
                break;
            case LOCK:
                LockResponseProtocol.LockResponse lockResponse = LockResponseProtocol.LockResponse.parseFrom(stream);

                response.setRequestId(lockResponse.getRequestId());
                response.setCommandID(lockResponse.getCommandID());

                response.setLockResponse(lockResponse);
                break;
            case UNLOCK:
                UnlockResponseProtocol.UnlockResponse unlockResponse = UnlockResponseProtocol.UnlockResponse.parseFrom(stream);


                response.setRequestId(unlockResponse.getRequestId());
                response.setCommandID(unlockResponse.getCommandID());

                response.setUnlockResponse(unlockResponse);
                break;
            case ISLOCKED:
                IsLockedResponseProtocol.IsLockedResponse isLockedResponse = IsLockedResponseProtocol.IsLockedResponse.parseFrom(stream);

                response.setRequestId(isLockedResponse.getRequestId());
                response.setCommandID(isLockedResponse.getCommandID());

                response.setIsLockedResponse(isLockedResponse);
                break;
            case LOCK_VERIFY:
                VerifyLockResponseProtocol.VerifyLockResponse verifyLockResponse = VerifyLockResponseProtocol.VerifyLockResponse.parseFrom(stream);

                response.setRequestId(verifyLockResponse.getRequestId());
                response.setCommandID(verifyLockResponse.getCommandID());

                response.setLockVerify(verifyLockResponse);
                break;
            case REGISTER_KEY_NOTIF:
                RegisterKeyNotifResponseProtocol.RegisterKeyNotifResponse registerKeyNotif = RegisterKeyNotifResponseProtocol.RegisterKeyNotifResponse.parseFrom(stream);

                response.setRequestId(registerKeyNotif.getRequestId());
                response.setCommandID(registerKeyNotif.getCommandID());

                response.setRegisterKeyNotifResponse(registerKeyNotif);
                break;
            case UNREGISTER_KEY_NOTIF:
                UnregisterKeyNotifResponseProtocol.UnregisterKeyNotifResponse unregisterKey = UnregisterKeyNotifResponseProtocol.UnregisterKeyNotifResponse.parseFrom(stream);

                response.setRequestId(unregisterKey.getRequestId());
                response.setCommandID(unregisterKey.getCommandID());

                response.setUnregisterKeyNotifResponse(unregisterKey);
                break;
            case UNREGISTER_BULK_KEY_NOTIF:
                UnregisterBulkKeyNotifResponseProtocol.UnregisterBulkKeyNotifResponse unregisterBulk = UnregisterBulkKeyNotifResponseProtocol.UnregisterBulkKeyNotifResponse.parseFrom(stream);

                response.setRequestId(unregisterBulk.getRequestId());
                response.setCommandID(unregisterBulk.getCommandID());

                response.setUnregisterBulkKeyNotifResponse(unregisterBulk);
                break;
            case REGISTER_BULK_KEY_NOTIF:
                RegisterBulkKeyNotifResponseProtocol.RegisterBulkKeyNotifResponse registerBulkKey = RegisterBulkKeyNotifResponseProtocol.RegisterBulkKeyNotifResponse.parseFrom(stream);

                response.setRequestId(registerBulkKey.getRequestId());
                response.setCommandID(registerBulkKey.getCommandID());

                response.setRegisterBulkKeyNotifResponse(registerBulkKey);
                break;
            case SUBSCRIBE_TOPIC:
                SubscribeTopicResponseProtocol.SubscribeTopicResponse subscribeTopic = SubscribeTopicResponseProtocol.SubscribeTopicResponse.parseFrom(stream);

                response.setRequestId(subscribeTopic.getRequestId());
                response.setCommandID(subscribeTopic.getCommandID());

                response.setSubscribeTopicResponse(subscribeTopic);
                break;
            case UNSUBSCRIBE_TOPIC:
                UnSubscribeTopicResponseProtocol.UnSubscribeTopicResponse unSubscribeTopic = UnSubscribeTopicResponseProtocol.UnSubscribeTopicResponse.parseFrom(stream);

                response.setRequestId(unSubscribeTopic.getRequestId());
                response.setCommandID(unSubscribeTopic.getCommandID());

                response.setUnSubscribeTopicResponse(unSubscribeTopic);
                break;
            case MESSAGE_ACKNOWLEDGEMENT:
                MessageAcknowledgmentResponseProtocol.MessageAcknowledgmentResponse messageAcknowledgment = MessageAcknowledgmentResponseProtocol.MessageAcknowledgmentResponse.parseFrom(stream);

                response.setRequestId(messageAcknowledgment.getRequestId());
                response.setCommandID(messageAcknowledgment.getCommandID());

                response.setMessageAcknowledgmentResponse(messageAcknowledgment);
                break;
            case RAISE_CUSTOM_EVENT:
                RaiseCustomEventResponseProtocol.RaiseCustomEventResponse raiseCustomEvent = RaiseCustomEventResponseProtocol.RaiseCustomEventResponse.parseFrom(stream);

                response.setRequestId(raiseCustomEvent.getRequestId());
                response.setCommandID(raiseCustomEvent.getCommandID());

                response.setRaiseCustomEventResponse(raiseCustomEvent);
                break;
            case GET_KEYS_TAG:
                GetKeysByTagResponseProtocol.GetKeysByTagResponse getKeysBy = GetKeysByTagResponseProtocol.GetKeysByTagResponse.parseFrom(stream);

                response.setRequestId(getKeysBy.getRequestId());
                response.setCommandID(getKeysBy.getCommandID());

                response.setGetKeysByTagResponse(getKeysBy);
                break;
            case GET_PRODUCT_VERSION:
                GetProductVersionResponseProtocol.GetProductVersionResponse getProductVersion = GetProductVersionResponseProtocol.GetProductVersionResponse.parseFrom(stream);

                response.setRequestId(getProductVersion.getRequestId());
                response.setCommandID(getProductVersion.getCommandID());

                response.setGetProductVersionResponse(getProductVersion);
                break;
            case CLOSE_STREAM:
                CloseStreamResponseProtocol.CloseStreamResponse closeStreamResponse = CloseStreamResponseProtocol.CloseStreamResponse.parseFrom(stream);

                response.setRequestId(closeStreamResponse.getRequestId());
                response.setCommandID(closeStreamResponse.getCommandID());

                response.setCloseStreamResponse(closeStreamResponse);

                break;
            case GET_STREAM_LENGTH:
                GetStreamLengthResponseProtocol.GetStreamLengthResponse getStreamLengthResponse = GetStreamLengthResponseProtocol.GetStreamLengthResponse.parseFrom(stream);

                response.setRequestId(getStreamLengthResponse.getRequestId());
                response.setCommandID(getStreamLengthResponse.getCommandID());

                response.setGetStreamLengthResponse(getStreamLengthResponse);
                break;
            case OPEN_STREAM:
                OpenStreamResponseProtocol.OpenStreamResponse openStreamResponse = OpenStreamResponseProtocol.OpenStreamResponse.parseFrom(stream);

                response.setRequestId(openStreamResponse.getRequestId());
                response.setCommandID(openStreamResponse.getCommandID());

                response.setOpenStreamResponse(openStreamResponse);
                break;
            case READ_FROM_STREAM:
                ReadFromStreamResponseProtocol.ReadFromStreamResponse readFromStreamResponse = ReadFromStreamResponseProtocol.ReadFromStreamResponse.parseFrom(stream);

                response.setRequestId(readFromStreamResponse.getRequestId());
                response.setCommandID(readFromStreamResponse.getCommandID());

                response.setReadFromStreamResponse(readFromStreamResponse);
                break;
            case WRITE_TO_STREAM:
                WriteToStreamResponseProtocol.WriteToStreamResponse writeToStreamResponse = WriteToStreamResponseProtocol.WriteToStreamResponse.parseFrom(stream);

                response.setRequestId(writeToStreamResponse.getRequestId());
                response.setCommandID(writeToStreamResponse.getCommandID());

                response.setWriteToStreamResponse(writeToStreamResponse);
                break;
            case SEARCH_CQ:
                SearchCQResponseProtocol.SearchCQResponse searchCQResponse = SearchCQResponseProtocol.SearchCQResponse.parseFrom(stream);

                response.setRequestId(searchCQResponse.getRequestId());
                response.setCommandID(searchCQResponse.getCommandID());

                response.setSearchCQResponse(searchCQResponse);
                break;
            case SEARCH_ENTRIES_CQ:
                SearchEntriesCQResponseProtocol.SearchEntriesCQResponse searchCQ = SearchEntriesCQResponseProtocol.SearchEntriesCQResponse.parseFrom(stream);

                response.setRequestId(searchCQ.getRequestId());
                response.setCommandID(searchCQ.getCommandID());

                response.setSearchEntriesCQResponse(searchCQ);
                response.setNumberOfChuncks(searchCQ.getNumberOfChuncks());
                response.setSequenceId(searchCQ.getSequenceId());
                break;
            case TASK_PROGRESS:
                TaskProgressResponseProtocol.TaskProgressResponse taskProgress = TaskProgressResponseProtocol.TaskProgressResponse.parseFrom(stream);

                response.setRequestId(taskProgress.getRequestId());
                response.setCommandID(taskProgress.getCommandID());

                response.setTaskProgressResponse(taskProgress);
                break;
            case TASK_NEXT_RECORD:
                GetNextRecordResponseProtocol.GetNextRecordResponse getNextRecordResponse = GetNextRecordResponseProtocol.GetNextRecordResponse.parseFrom(stream);

                response.setRequestId(getNextRecordResponse.getRequestId());
                response.setCommandID(getNextRecordResponse.getCommandID());

                response.setNextRecordResponse(getNextRecordResponse);
                break;
            case TASK_ENUMERATOR:
                TaskEnumeratorResponseProtocol.TaskEnumeratorResponse taskEnumerator = TaskEnumeratorResponseProtocol.TaskEnumeratorResponse.parseFrom(stream);

                response.setRequestId(taskEnumerator.getRequestId());
                response.setCommandID(taskEnumerator.getCommandID());

                response.setTaskEnumeratorResponse(taskEnumerator);
                break;
            case TASK_CALLBACK:
                TaskCallbackResponseProtocol.TaskCallbackResponse taskCallback = TaskCallbackResponseProtocol.TaskCallbackResponse.parseFrom(stream);

                response.setRequestId(taskCallback.getRequestId());
                response.setCommandID(taskCallback.getCommandID());

                response.setTaskCallbackResponse(taskCallback);
                break;
            case RUNNING_TASKS:
                GetRunningTasksResponseProtocol.GetRunningTasksResponse getRunningTasks = GetRunningTasksResponseProtocol.GetRunningTasksResponse.parseFrom(stream);

                response.setRequestId(getRunningTasks.getRequestId());
                response.setCommandID(getRunningTasks.getCommandID());

                response.setRunningTasksResponse(getRunningTasks);
                break;
            case TOUCH:
                TouchResponseProtocol.TouchResponse touch = TouchResponseProtocol.TouchResponse.parseFrom(stream);

                response.setRequestId(touch.getRequestId());
                response.setCommandID(touch.getCommandID());

                response.setTouchResponse(touch);
                break;
            case POLL:
                PollResponseProtocol.PollResponse poll = PollResponseProtocol.PollResponse.parseFrom(stream);

                response.setRequestId(poll.getRequestId());
                response.setCommandID(poll.getCommandID());

                response.setPollResponse(poll);
                break;
            case PING:
                PingResponseProtocol.PingResponse ping = PingResponseProtocol.PingResponse.parseFrom(stream);

                response.setRequestId(ping.getRequestId());
                response.setCommandID(ping.getCommandID());

                response.setPingResponse(ping);
                break;
            case REGISTER_POLL_NOTIF:
                RegisterPollNotifResponseProtocol.RegisterPollNotifResponse registerPollNotif = RegisterPollNotifResponseProtocol.RegisterPollNotifResponse.parseFrom(stream);

                response.setRequestId(registerPollNotif.getRequestId());
                response.setCommandID(registerPollNotif.getCommandID());

                response.setRegisterPollNotifResponse(registerPollNotif);
                break;

            case SYNC_EVENTS:
                SyncEventsResponseProtocol.SyncEventsResponse syncEvents = SyncEventsResponseProtocol.SyncEventsResponse.parseFrom(stream);

                response.setRequestId(syncEvents.getRequestId());
                response.setCommandID(syncEvents.getCommandID());

                response.setSyncEventsResponse(syncEvents);
                break;
            case INVOKE_ENTRY_PROCESSOR:
                InvokeEntryProcessorResponseProtocol.InvokeEntryProcessorResponse invokeEntryProcessor = InvokeEntryProcessorResponseProtocol.InvokeEntryProcessorResponse.parseFrom(stream);

                response.setRequestId(invokeEntryProcessor.getRequestId());
                response.setCommandID(invokeEntryProcessor.getCommandID());

                response.setInvokeEntryProcessorResponse(invokeEntryProcessor);
                break;
            case MAP_REDUCE_TASK:
                MapReduceTaskResponseProtocol.MapReduceTaskResponse mapReduce = MapReduceTaskResponseProtocol.MapReduceTaskResponse.parseFrom(stream);

                response.setRequestId(mapReduce.getRequestId());
                response.setCommandID(mapReduce.getCommandID());

                response.setMapReduceTaskResponse(mapReduce);
                break;
            case GET_SECURITY_RESPONSE:
                GetSecurityAuthorizationResponseProtocol.GetSecurityAuthorizationResponse securityAuthorizationResponse = GetSecurityAuthorizationResponseProtocol.GetSecurityAuthorizationResponse.parseFrom(stream);

                response.setRequestId(securityAuthorizationResponse.getRequestId());
                response.setCommandID(securityAuthorizationResponse.getCommandID());

                response.setGetSecurityAuthorizationResponse(securityAuthorizationResponse);
                break;
            case SEARCH:
                SearchResponseProtocol.SearchResponse searchResponse = SearchResponseProtocol.SearchResponse.parseFrom(stream);

                response.setRequestId(searchResponse.getRequestId());
                response.setCommandID(searchResponse.getCommandID());
                response.setNumberOfChuncks(searchResponse.getNumberOfChuncks());
                response.setSequenceId(searchResponse.getSequenceId());

                response.setSearch(searchResponse);
                break;
            case UNREGISTER_CQ:
                UnRegisterCQResponseProtocol.UnRegisterCQResponse unRegisterCQResponse = UnRegisterCQResponseProtocol.UnRegisterCQResponse.parseFrom(stream);

                response.setRequestId(unRegisterCQResponse.getRequestId());
                response.setCommandID(unRegisterCQResponse.getCommandID());

                response.setUnRegisterCQResponse(unRegisterCQResponse);
                break;
            case GET_SERIALIZATION_FORMAT:
                GetSerializationFormatResponseProtocol.GetSerializationFormatResponse getSerializationFormatResponse = GetSerializationFormatResponseProtocol.GetSerializationFormatResponse.parseFrom(stream);

                response.setRequestId(getSerializationFormatResponse.getRequestId());
                response.setCommandID(getSerializationFormatResponse.getCommandID());

                response.setGetSerializationFormatResponse(getSerializationFormatResponse);
                break;
            case INQUIRY_REQUEST_RESPONSE:
                InquiryRequestResponseProtocol.InquiryRequestResponse inquiryRequestResponse = InquiryRequestResponseProtocol.InquiryRequestResponse.parseFrom(stream);

                response.setRequestId(inquiryRequestResponse.getRequestId());
                response.setCommandID(inquiryRequestResponse.getCommandID());

                response.setInquiryRequestResponse(inquiryRequestResponse);
                break;
            case GET_CONNECTED_CLIENTS:
                GetConnectedClientsResponseProtocol.GetConnectedClientsResponse getConnectedClientsResponse = GetConnectedClientsResponseProtocol.GetConnectedClientsResponse.parseFrom(stream);

                response.setRequestId(getConnectedClientsResponse.getRequestId());
                response.setCommandID(getConnectedClientsResponse.getCommandID());

                response.setGetConnectedClientsResponse(getConnectedClientsResponse);
                break;
            case SEARCH_ENTRIES:
                SearchEntriesResponseProtocol.SearchEntriesResponse searchEntries = SearchEntriesResponseProtocol.SearchEntriesResponse .parseFrom(stream);

                response.setRequestId(searchEntries.getRequestId());
                response.setCommandID(searchEntries.getCommandID());

                response.setSearchEntries(searchEntries);
                response.setNumberOfChuncks(searchEntries.getNumberOfChuncks());
                response.setSequenceId(searchEntries.getSequenceId());
                break;
            case REMOVE_QUERY:
                RemoveQueryResponseProtocol.RemoveQueryResponse removeQuery = RemoveQueryResponseProtocol.RemoveQueryResponse.parseFrom(stream);

                response.setRequestId(removeQuery.getRequestId());
                response.setCommandID(removeQuery.getCommandID());

                response.setRemoveQueryResponse(removeQuery);
                response.setSequenceId(removeQuery.getSequenceId());
                break;
            default:
                return ResponseProtocol.Response.parseFrom(stream);
        }

        return response.build();
    }

    public static byte[] SerializeResponse(ResponseProtocol.Response command) {
        byte[] temp = command.toByteArray();
        byte[] bite = new byte[10 + temp.length];
        byte[] temp2 = ToByte(temp);
        System.arraycopy(temp2, 0, bite, 0, temp2.length);
        System.arraycopy(temp, 0, bite, 10, temp.length);
        return bite;
    }

    public static byte[] SerializeResponse(ManagementResponseProtocol.ManagementResponse.Builder command) {
        byte[] temp = command.build().toByteArray();
        byte[] bite = new byte[10 + temp.length];
        byte[] temp2 = null;
        try {
            temp2 = new Integer(temp.length).toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException unsupportedEncodingException) {

        }
        System.arraycopy(temp2, 0, bite, 0, temp2.length);
        System.arraycopy(temp, 0, bite, 10, temp.length);
        return bite;
    }

    public static byte[] SerializeExceptionResponse(java.lang.Exception exc, long requestId) {
        com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Builder ex = Exception.newBuilder();
        if (exc.getCause() != null) {
            StringBuilder builder = new StringBuilder(ex.getMessage());
            builder.append(builder.toString() == null || builder.toString().isEmpty() ? "" : " ");
            builder.append(exc.getCause().getMessage());
            ex.setMessage(builder.toString());
            ex.setException(exc.toString() + " " + exc.getCause().getMessage());
        } else {
            String message = exc.getMessage();
            if (message == null || message.isEmpty()) {
                message = exc.toString();
            }

            ex.setMessage(message);
            ex.setException(exc.toString());
        }
        if (exc instanceof java.lang.reflect.InvocationTargetException && exc.getCause() != null) {
            exc = (java.lang.Exception) exc.getCause();
        }
        if (exc instanceof OperationFailedException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.OPERATIONFAILED);
        } else if (exc instanceof AggregateException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.AGGREGATE);
        } else if (exc instanceof ConfigurationException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.CONFIGURATION);
        } else if (exc instanceof com.alachisoft.ncache.runtime.exceptions.SecurityException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.SECURITY);
        } else if (exc instanceof NotSupportedException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.NOTSUPPORTED);
        } else if (exc instanceof StreamAlreadyLockedException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.STREAM_ALREADY_LOCKED);
        } else if (exc instanceof StreamCloseException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.STREAM_CLOSED);
        } else if (exc instanceof StreamException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.STREAM_EXC);
        } else if (exc instanceof StreamInvalidLockException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.STREAM_INVALID_LOCK);
        } else if (exc instanceof StreamNotFoundException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.STREAM_NOT_FOUND);
        } else if (exc instanceof TypeIndexNotDefined) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.TYPE_INDEX_NOT_FOUND);
        } else if (exc instanceof AttributeIndexNotDefined) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.ATTRIBUTE_INDEX_NOT_FOUND);
        } else if (exc instanceof StateTransferInProgressException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.STATE_TRANSFER_EXCEPTION);
        } else if (exc instanceof MaxClientReachedException) {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.MAX_CLIENTS_REACHED);
        } else {
            ex.setType(com.alachisoft.ncache.common.protobuf.ExceptionProtocol.Exception.Type.GENERALFAILURE);
        }
        com.alachisoft.ncache.common.protobuf.ResponseProtocol.Response.Builder response = com.alachisoft.ncache.common.protobuf.ResponseProtocol.Response.newBuilder();
        response.setRequestId(requestId);
        response.setException(ex);
        response.setResponseType(ResponseProtocol.Response.Type.EXCEPTION);

        return SerializeResponse(response.build());
    }

    private static byte[] ToByte(byte[] size) {
        try {
            return Integer.toString(size.length).getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
        }
        return size;
    }

    private static int ToInteger(byte[] size) {
        try {
            return Integer.parseInt(new String(size, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
        }
        return 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy