/*
 * Copyright 2018 flow.ci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flowci.core.common.manager;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.flowci.core.common.domain.PushBody;
import com.flowci.core.common.domain.PushEvent;
import java.io.IOException;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

/**
 * @author yang
 */
@Log4j2
@Component
public class SocketPushManager {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    public void push(String topic, PushEvent event, Object obj) {
        try {
            PushBody push = new PushBody(event, obj);
            String json = objectMapper.writeValueAsString(push);
            simpMessagingTemplate.convertAndSend(topic, json);
        } catch (IOException e) {
            log.warn(e.getMessage());
        }
    }

    public void push(String topic, byte[] obj) {
//        Message<?> message = new ByteArrayMessageConverter().toMessage(obj, null);
//        simpMessagingTemplate.send(topic, message);
        simpMessagingTemplate.convertAndSend(topic, new String(obj));
    }
}

@@ -776,37 +776,8 @@
 nt;%0A
-import java.io.IOException;%0A%0A
 impo
@@ -985,16 +985,70 @@
 onent;%0A%0A
+import java.io.IOException;%0Aimport java.util.Base64;%0A%0A
 /**%0A * @
@@ -1641,25 +1641,25 @@
 e%5B%5D 
-obj
+bytes
 ) %7B%0A
-//
         
 Mess
@@ -1658,134 +1658,91 @@
     
-Message%3C?%3E message = new ByteArrayMessageConverter().toMessage(obj, null);%0A//        simpMessagingTemplate.send(topic, message
+// TODO: performance?%0A        String b64 = Base64.getEncoder().encodeToString(bytes
 );%0A 
@@ -1796,23 +1796,11 @@
 ic, 
-new String(obj)
+b64
 );%0A 

send base64 vai websocket