19 package org.turro.message;
21 import java.io.IOException;
22 import java.nio.file.FileSystems;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.PathMatcher;
26 import java.util.HashMap;
27 import java.util.HashSet;
29 import java.util.concurrent.BlockingQueue;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ConcurrentMap;
32 import java.util.concurrent.LinkedBlockingQueue;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 import java.util.stream.Stream;
36 import org.turro.string.Strings;
37 import org.turro.collections.KeyValueMap;
38 import org.turro.elephant.context.ElephantContext;
39 import org.turro.elephant.context.IConstructor;
40 import org.turro.elephant.direct.AbstractDirectContentCtrl;
41 import org.turro.elephant.direct.DirectContent;
42 import org.turro.elephant.impl.util.FileUtil;
43 import org.turro.marker.ElephantMarker;
44 import org.turro.plugin.contacts.IContact;
54 getQueue(contact).add(msg);
58 return getQueue(contact).poll();
62 return !(getQueue(contact).isEmpty());
65 public static void pushMessage(String idContact, String msg) {
66 getQueue(idContact).add(msg);
70 return getQueue(idContact).poll();
74 return !(getQueue(idContact).isEmpty());
80 KeyValueMap map =
new KeyValueMap();
81 map.put(
"userId", user.
getId());
82 map.put(
"type",
"query");
83 return getAjaxEvalUrl(map);
91 private static final String GENERIC_MESSAGE_FOLDER =
"/WEB-INF/elephant/message";
95 if(Files.exists(folder)) {
96 PathMatcher matcher = FileSystems.getDefault().getPathMatcher(
"glob:**.txt");
97 try (Stream<Path> entries = Files.list(folder).filter(p -> matcher.matches(p))) {
98 entries.forEach(file -> {
101 }
catch (IOException ex) {
105 }
catch (IOException ex) {
113 private static final ConcurrentMap<String, BlockingQueue<String>> QUEUE_MAP =
new ConcurrentHashMap<>();
116 return getQueue(contact.
getId());
119 public static BlockingQueue<String>
getQueue(String idContact) {
120 return QUEUE_MAP.computeIfAbsent(idContact, k ->
new LinkedBlockingQueue<String>());
126 super(
"widgets/messages");
142 setTemplate(
"toast");
148 String type = map.get(
"type"),
149 userId = map.get(
"userId");
150 if(
"query".equals(type) && !Strings.isBlank(userId)) {
151 Set<String> messages =
new HashSet<>();
152 while(hasMessage(userId)) {
153 messages.add(pollMessage(userId));
155 if(!messages.isEmpty()) {
156 HashMap extra =
new HashMap();
157 extra.put(
"messages", messages);
158 writeMarkerToResponse(constructor, map, extra);
static String getRealPath(String path)
static String logMsg(String msg)
static String getContent(File file)
static void pushGenerics(String idContact)
static void pushMessage(IContact contact, String msg)
void prepareCleanMarker(ElephantMarker marker, KeyValueMap map)
static boolean hasMessage(IContact contact)
String getIntervalCall(IConstructor constructor)
static void pushMessage(String idContact, String msg)
static BlockingQueue< String > getQueue(IContact contact)
static String pollMessage(String idContact)
void doExecute(IConstructor constructor, KeyValueMap map)
static String pollMessage(IContact contact)
void prepareMarker(ElephantMarker marker)
static BlockingQueue< String > getQueue(String idContact)
static boolean hasMessage(String idContact)