mirror of
https://github.com/Dansen999/migor.git
synced 2026-01-10 21:13:30 +00:00
Initial import (partially)
This commit is contained in:
2
doc/info.txt
Normal file
2
doc/info.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
User: admin
|
||||||
|
Password: CvCJzU-5bY8s
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.migor.core.beans;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/23/13 9:13 PM
|
||||||
|
*/
|
||||||
|
public class UserBean {
|
||||||
|
|
||||||
|
}
|
||||||
31
org.migor.core/src/main/java/org/migor/core/cache/UserSessionCache.java
vendored
Normal file
31
org.migor.core/src/main/java/org/migor/core/cache/UserSessionCache.java
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package org.migor.core.cache;
|
||||||
|
|
||||||
|
import org.migor.core.models.internal.User;
|
||||||
|
|
||||||
|
import javax.ejb.Singleton;
|
||||||
|
import javax.ejb.Startup;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/23/13 9:14 PM
|
||||||
|
*/
|
||||||
|
@Startup
|
||||||
|
@Singleton
|
||||||
|
public class UserSessionCache {
|
||||||
|
|
||||||
|
private Map<String, User> userMap = new HashMap<String, User>();
|
||||||
|
|
||||||
|
public void add(User user) {
|
||||||
|
userMap.put(user.getSessionId(), user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public User get(String sessionId) {
|
||||||
|
return userMap.get(sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(String sessionId) {
|
||||||
|
userMap.remove(sessionId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.migor.core.models.internal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/23/13 9:13 PM
|
||||||
|
*/
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
private String sessionId;
|
||||||
|
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
public String getSessionId() {
|
||||||
|
return sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSessionId(String sessionId) {
|
||||||
|
this.sessionId = sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" +
|
||||||
|
"sessionId='" + sessionId + '\'' +
|
||||||
|
", userName='" + userName + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package org.migor.core.utils;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.map.DeserializationConfig;
|
||||||
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/24/13 10:06 AM
|
||||||
|
*/
|
||||||
|
public class JsonParser {
|
||||||
|
|
||||||
|
private JsonParser() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ObjectMapper getObjectMapper() {
|
||||||
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
return objectMapper;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,7 +19,6 @@ public class Response {
|
|||||||
@JsonProperty("message")
|
@JsonProperty("message")
|
||||||
public String message;
|
public String message;
|
||||||
|
|
||||||
//TODO format with timezone
|
|
||||||
@JsonProperty("timestamp")
|
@JsonProperty("timestamp")
|
||||||
public Date timestamp = new Date();
|
public Date timestamp = new Date();
|
||||||
|
|
||||||
@@ -36,11 +35,11 @@ public class Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Response ok() {
|
public static Response ok() {
|
||||||
return new Response(StatusCode.OK, null, null);
|
return new Response(StatusCode.OK, StatusCode.OK.getMessage(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Response ok(@NotNull final Object content) {
|
public static Response ok(@NotNull final Object content) {
|
||||||
return new Response(StatusCode.OK, null, content);
|
return new Response(StatusCode.OK, StatusCode.OK.getMessage(), content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Response error(@NotNull final String message) {
|
public static Response error(@NotNull final String message) {
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package org.migor.service.listeners;
|
package org.migor.service.listeners;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
import org.migor.core.cache.UserSessionCache;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
import javax.servlet.http.HttpSessionEvent;
|
import javax.servlet.http.HttpSessionEvent;
|
||||||
import javax.servlet.http.HttpSessionListener;
|
import javax.servlet.http.HttpSessionListener;
|
||||||
|
|
||||||
@@ -15,6 +17,9 @@ public class SessionListener implements HttpSessionListener {
|
|||||||
private static final Logger logger = Logger.getLogger(SessionListener.class);
|
private static final Logger logger = Logger.getLogger(SessionListener.class);
|
||||||
|
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private UserSessionCache userSessionCache;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sessionCreated(HttpSessionEvent se) {
|
public void sessionCreated(HttpSessionEvent se) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
@@ -27,5 +32,7 @@ public class SessionListener implements HttpSessionListener {
|
|||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("Destroyed session " + se.getSession().getId());
|
logger.debug("Destroyed session " + se.getSession().getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userSessionCache.remove(se.getSession().getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.migor.service.configuration;
|
package org.migor.service.rest.configuration;
|
||||||
|
|
||||||
import org.codehaus.jackson.map.ObjectMapper;
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
import org.codehaus.jackson.map.SerializationConfig;
|
import org.codehaus.jackson.map.SerializationConfig;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.migor.service.configuration;
|
package org.migor.service.rest.configuration;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.migor.core.MigorException;
|
import org.migor.core.MigorException;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.migor.service.configuration;
|
package org.migor.service.rest.configuration;
|
||||||
|
|
||||||
|
|
||||||
import javax.ws.rs.ApplicationPath;
|
import javax.ws.rs.ApplicationPath;
|
||||||
@@ -8,7 +8,7 @@ import javax.ws.rs.core.Application;
|
|||||||
* @author Daniel Scheidle
|
* @author Daniel Scheidle
|
||||||
* @since 11/4/13 10:20 PM
|
* @since 11/4/13 10:20 PM
|
||||||
*/
|
*/
|
||||||
@ApplicationPath("/rest")
|
@ApplicationPath("/")
|
||||||
public class ServiceApplication extends Application {
|
public class ServiceApplication extends Application {
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package org.migor.service.rest.user;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.migor.core.cache.UserSessionCache;
|
||||||
|
import org.migor.core.models.internal.User;
|
||||||
|
import org.migor.service.Response;
|
||||||
|
import org.migor.service.rest.user.in.UserCredentials;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/23/13 7:40 PM
|
||||||
|
*/
|
||||||
|
@Path("/private/user")
|
||||||
|
public class UserService {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(UserService.class);
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private UserSessionCache userSessionCache;
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("/login")
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
public Response login(@Context HttpServletRequest httpServletRequest, final UserCredentials credentials) {
|
||||||
|
|
||||||
|
|
||||||
|
User user = new User();
|
||||||
|
user.setSessionId(httpServletRequest.getSession().getId());
|
||||||
|
user.setUserName(credentials.getUserName());
|
||||||
|
|
||||||
|
logger.info("Adding user " + user);
|
||||||
|
|
||||||
|
userSessionCache.add(user);
|
||||||
|
|
||||||
|
return Response.ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.migor.service.rest.user.in;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.annotate.JsonProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/23/13 7:41 PM
|
||||||
|
*/
|
||||||
|
public class UserCredentials {
|
||||||
|
|
||||||
|
@JsonProperty("userName")
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserCredentials{" +
|
||||||
|
"userName='" + userName + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package org.migor.service.sockets;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import javax.websocket.OnClose;
|
||||||
|
import javax.websocket.OnMessage;
|
||||||
|
import javax.websocket.OnOpen;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/23/13 5:00 PM
|
||||||
|
*/
|
||||||
|
@ServerEndpoint("/socket/echo")
|
||||||
|
public class EchoSocket {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(EchoSocket.class);
|
||||||
|
|
||||||
|
|
||||||
|
@OnOpen
|
||||||
|
public void onConnectionOpen(Session session) {
|
||||||
|
logger.info("Connection opened ... " + session.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnMessage
|
||||||
|
public String onMessage(String message) {
|
||||||
|
if (StringUtils.isBlank(message)) {
|
||||||
|
return "Please send message";
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnClose
|
||||||
|
public void onConnectionClose(Session session) {
|
||||||
|
logger.info("Connection close .... " + session.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package org.migor.service.sockets;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.migor.core.utils.JsonParser;
|
||||||
|
import org.migor.service.sockets.data.Message;
|
||||||
|
import org.migor.service.sockets.data.Type;
|
||||||
|
import org.migor.service.sockets.data.UserSession;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.websocket.OnClose;
|
||||||
|
import javax.websocket.OnMessage;
|
||||||
|
import javax.websocket.OnOpen;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/23/13 5:00 PM
|
||||||
|
*/
|
||||||
|
@ServerEndpoint("/socket/chat/global")
|
||||||
|
public class GlobalChatSocket {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(GlobalChatSocket.class);
|
||||||
|
|
||||||
|
|
||||||
|
private static Map<String, UserSession> userSessionMap = new HashMap<String, UserSession>();
|
||||||
|
|
||||||
|
@OnOpen
|
||||||
|
public void onConnectionOpen(final Session session) {
|
||||||
|
logger.info("Connection opened for " + session.getId());
|
||||||
|
|
||||||
|
try {
|
||||||
|
session.getBasicRemote().sendText("");
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.warn("Couldn't deliver message to " + session.getId() + ": " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(final Session session, final String message) {
|
||||||
|
try {
|
||||||
|
final Message m = JsonParser.getObjectMapper().readValue(message, Message.class);
|
||||||
|
|
||||||
|
switch (m.getType()) {
|
||||||
|
case LOGIN:
|
||||||
|
UserSession userSession= new UserSession();
|
||||||
|
userSession.setUserName(m.getContent().get(Message.FIELD_SENDER_NAME));
|
||||||
|
userSession.setSession(session);
|
||||||
|
userSessionMap.put(session.getId(), userSession);
|
||||||
|
|
||||||
|
Message loginMessage = new Message();
|
||||||
|
loginMessage.setType(Type.LOGIN);
|
||||||
|
loginMessage.getContent().put(Message.FIELD_MESSAGE, "OK");
|
||||||
|
_send(session, loginMessage);
|
||||||
|
|
||||||
|
Message enteredMessage = new Message();
|
||||||
|
enteredMessage.setType(Type.SERVER);
|
||||||
|
enteredMessage.getContent().put(Message.FIELD_MESSAGE, userSession.getUserName() + " entered the room.");
|
||||||
|
_sendAll(session, enteredMessage, session.getId());
|
||||||
|
break;
|
||||||
|
case MSG:
|
||||||
|
_sendAll(session, m, session.getId());
|
||||||
|
break;
|
||||||
|
case TOPIC:
|
||||||
|
Message welcomeMessage = new Message();
|
||||||
|
welcomeMessage.setType(Type.TOPIC);
|
||||||
|
welcomeMessage.getContent().put(Message.FIELD_MESSAGE, "Welcome to Migor Chat. At the moment there are " + userSessionMap.size() + " users in this room.");
|
||||||
|
_send(session, welcomeMessage);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warn("Couldn't read message for " + session.getId() + ": " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void _send(final Session session, final Message message) {
|
||||||
|
try {
|
||||||
|
if (session.isOpen()) {
|
||||||
|
session.getBasicRemote().sendText(JsonParser.getObjectMapper().writeValueAsString(message));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.warn("Couldn't deliver message from " + session.getId() + ": " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void _sendAll(final Session session, final Message message, final String except) {
|
||||||
|
for (Session s : session.getOpenSessions()) {
|
||||||
|
if (!StringUtils.equals(except, s.getId())) {
|
||||||
|
_send(s, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnClose
|
||||||
|
public void onConnectionClose(final Session session) {
|
||||||
|
logger.info("Connection closed for " + session.getId());
|
||||||
|
|
||||||
|
Message enteredMessage = new Message();
|
||||||
|
enteredMessage.setType(Type.SERVER);
|
||||||
|
enteredMessage.getContent().put(Message.FIELD_MESSAGE, userSessionMap.get(session.getId()).getUserName() + " left the room.");
|
||||||
|
_sendAll(session, enteredMessage, session.getId());
|
||||||
|
|
||||||
|
userSessionMap.remove(session.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.migor.service.sockets.data;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.annotate.JsonProperty;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/23/13 5:33 PM
|
||||||
|
*/
|
||||||
|
public class Message {
|
||||||
|
|
||||||
|
public static final String FIELD_SENDER_NAME = "senderName";
|
||||||
|
public static final String FIELD_MESSAGE = "message";
|
||||||
|
|
||||||
|
@JsonProperty("type")
|
||||||
|
private Type type;
|
||||||
|
|
||||||
|
@JsonProperty("content")
|
||||||
|
private Map<String, String> content = new HashMap<String, String>();
|
||||||
|
|
||||||
|
|
||||||
|
public Type getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(Type type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(Map<String, String> content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package org.migor.service.sockets.data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/23/13 5:35 PM
|
||||||
|
*/
|
||||||
|
public enum Type {
|
||||||
|
|
||||||
|
MSG,
|
||||||
|
LOGIN,
|
||||||
|
SERVER,
|
||||||
|
TOPIC
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package org.migor.service.sockets.data;
|
||||||
|
|
||||||
|
import javax.websocket.Session;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Daniel Scheidle
|
||||||
|
* @since 11/24/13 10:14 AM
|
||||||
|
*/
|
||||||
|
public class UserSession {
|
||||||
|
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Session getSession() {
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSession(Session session) {
|
||||||
|
this.session = session;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,6 @@
|
|||||||
|
|
||||||
<servlet-mapping>
|
<servlet-mapping>
|
||||||
<servlet-name>RESTEasy JSAPI</servlet-name>
|
<servlet-name>RESTEasy JSAPI</servlet-name>
|
||||||
<url-pattern>/rest/js</url-pattern>
|
<url-pattern>/rest/rest.js</url-pattern>
|
||||||
</servlet-mapping>
|
</servlet-mapping>
|
||||||
</web-app>
|
</web-app>
|
||||||
|
|||||||
@@ -6,17 +6,23 @@ package org.migor.shared;
|
|||||||
*/
|
*/
|
||||||
public enum StatusCode {
|
public enum StatusCode {
|
||||||
|
|
||||||
OK(0),
|
OK(0, "ok"),
|
||||||
ERROR(1),
|
ERROR(1, "error"),
|
||||||
PERMISSION_DENIED(2);
|
PERMISSION_DENIED(2, "permission denied");
|
||||||
|
|
||||||
private int code;
|
private int code;
|
||||||
|
private String message;
|
||||||
|
|
||||||
private StatusCode(int code) {
|
private StatusCode(int code, String message) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCode() {
|
public int getCode() {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@
|
|||||||
<%-- ############### JS ############### --%>
|
<%-- ############### JS ############### --%>
|
||||||
<%-- RESReasy Stub; on this userLocation you can find all js requst methods generated by RESTeasy --%>
|
<%-- RESReasy Stub; on this userLocation you can find all js requst methods generated by RESTeasy --%>
|
||||||
<%--suppress JspAbsolutePathInspection, HtmlUnknownTarget --%>
|
<%--suppress JspAbsolutePathInspection, HtmlUnknownTarget --%>
|
||||||
<script type="text/javascript" src="/migor/services/rest/js"></script>
|
<script type="text/javascript" src="/migor/services/rest/rest.js"></script>
|
||||||
|
|
||||||
<%-- Plugins --%>
|
<%-- Plugins --%>
|
||||||
<script type="text/javascript" src="js/plugins/jquery-1.10.2.js"></script>
|
<script type="text/javascript" src="js/plugins/jquery-1.10.2.js"></script>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
migor.rest = new function () {
|
rest = new function () {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -42,7 +42,7 @@ migor.rest = new function () {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
migor.dialog.openErrorDialog(code + ': ' + request.responseText);
|
//dialog.openErrorDialog(code + ': ' + request.responseText);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,71 +1,38 @@
|
|||||||
<%@ page import="java.util.Date" %>
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Migor Location Chat</title>
|
<meta http-equiv="refresh" content="0;url=./index.jsp"/>
|
||||||
|
|
||||||
<%
|
<link href="css/migor-mobile.css" rel="stylesheet" type="text/css"/>
|
||||||
String buildDate = "${timestamp}";
|
<link href="css/jquery.mobile-1.3.2.min.css" rel="stylesheet" type="text/css"/>
|
||||||
%>
|
|
||||||
|
|
||||||
<link href="css/migor-mobile.css?_=<%=buildDate%>" rel="stylesheet" type="text/css"/>
|
<script type="text/javascript" src="js/plugin/jquery-1.10.2.js"></script>
|
||||||
<link href="css/jquery.mobile-1.3.2.min.css?_=<%=buildDate%>" rel="stylesheet" type="text/css"/>
|
<script type="text/javascript" src="js/plugin/jquery.mobile-1.3.2.js"></script>
|
||||||
|
|
||||||
|
|
||||||
<%-- Plugins --%>
|
|
||||||
<script type="text/javascript" src="js/plugin/jquery-1.10.2.js?_=<%=buildDate%>"></script>
|
|
||||||
<script type="text/javascript" src="js/plugin/jquery.mobile-1.3.2.js?_=<%=buildDate%>"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript" src="js/widget/widget-chat.js?_=<%=buildDate%>"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- EXECUTE INIT SCRIPT -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
$(document).ready(function () {
|
|
||||||
$('#chat-content').chat();
|
|
||||||
$('#send').on('click', function () {
|
|
||||||
var message = $('#message').val();
|
|
||||||
if (message.length > 0) {
|
|
||||||
$('#chat-content').chat('send', message);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
version = '${project.version}';
|
|
||||||
buildNumber = '${buildNumber}';
|
|
||||||
socketPath = "ws://${socketURL}";
|
|
||||||
try {
|
|
||||||
timestamp = new Date(${timestamp});
|
|
||||||
} catch (exception) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div data-role="page">
|
<div id="chat" data-role="page">
|
||||||
|
|
||||||
<div data-role="header" data-position="fixed">
|
<div data-role="header" data-position="fixed" data-tap-toggle="false">
|
||||||
<h1>Migor Location Chat</h1>
|
<h1>Migor Chat</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="chat-content" data-role="content">
|
<div id="chat-content" data-role="content">
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-role="footer" data-position="fixed">
|
<div data-role="footer" data-position="fixed" data-tap-toggle="false">
|
||||||
|
|
||||||
<div class="ui-grid-a">
|
<div class="ui-grid-a">
|
||||||
<div class="ui-block-a" style="width: 70%;">
|
<div class="ui-block-a" style="width: 70%;">
|
||||||
<input id="message" type="text" value="" placeholder="Message..."/>
|
<input id="chat-send-message" type="text" value="" placeholder="Message..."/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="ui-block-b" style="width: 30%;">
|
<div class="ui-block-b" style="width: 30%;">
|
||||||
<button id="send" data-role="button">send</button>
|
<button id="chat-send-button" data-role="button">send</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -6,4 +6,17 @@
|
|||||||
right: 0;
|
right: 0;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
left: 0;
|
left: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chat-content .userName {
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: larger;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chat-content .userName.own {
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: larger;
|
||||||
|
color: #00415e;
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,8 @@
|
|||||||
<head>
|
<head>
|
||||||
<title>Migor Location Chat</title>
|
<title>Migor Location Chat</title>
|
||||||
|
|
||||||
|
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
|
||||||
|
|
||||||
<%
|
<%
|
||||||
long buildDate = new Date().getTime();
|
long buildDate = new Date().getTime();
|
||||||
%>
|
%>
|
||||||
@@ -12,61 +14,129 @@
|
|||||||
<link href="css/jquery.mobile-1.3.2.min.css?_=<%=buildDate%>" rel="stylesheet" type="text/css"/>
|
<link href="css/jquery.mobile-1.3.2.min.css?_=<%=buildDate%>" rel="stylesheet" type="text/css"/>
|
||||||
|
|
||||||
<%-- Plugins --%>
|
<%-- Plugins --%>
|
||||||
|
<script type="text/javascript" src="/migor/services/rest/rest.js?_=<%=buildDate%>"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="js/plugin/jquery-1.10.2.js?_=<%=buildDate%>"></script>
|
<script type="text/javascript" src="js/plugin/jquery-1.10.2.js?_=<%=buildDate%>"></script>
|
||||||
<script type="text/javascript" src="js/plugin/jquery.mobile-1.3.2.js?_=<%=buildDate%>"></script>
|
<script type="text/javascript" src="js/plugin/jquery.mobile-1.3.2.js?_=<%=buildDate%>"></script>
|
||||||
|
<script type="text/javascript" src="js/rest.js?_=<%=buildDate%>"></script>
|
||||||
|
|
||||||
|
|
||||||
<!-- EXECUTE INIT SCRIPT -->
|
<!-- EXECUTE INIT SCRIPT -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
version = '${project.version}';
|
||||||
|
buildNumber = '${buildNumber}';
|
||||||
|
timestamp = new Date(${timestamp});
|
||||||
|
socket = null;
|
||||||
|
userName = null;
|
||||||
|
|
||||||
|
if (window.location.protocol == 'http:') {
|
||||||
|
endpoint = 'ws://' + window.location.host + ':8000/migor/services/socket/chat/global';
|
||||||
|
} else {
|
||||||
|
endpoint = 'wss://' + window.location.host + ':8443/migor/services/socket/chat/global';
|
||||||
|
}
|
||||||
|
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
$('#startButton')
|
$('#login-button').on('click', function () {
|
||||||
.button('disable')
|
login();
|
||||||
.on('click', function () {
|
});
|
||||||
$.mobile.changePage("chat.jsp", {
|
$('#login-username').keypress(function(e) {
|
||||||
transition: 'fade'
|
if(e.which == 13) {
|
||||||
}
|
login();
|
||||||
);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
$('#username').on('change', function (event) {
|
|
||||||
if ($('#username').val().length > 2) {
|
|
||||||
$('#startButton').button('enable');
|
|
||||||
} else {
|
|
||||||
$('#startButton').button('disable');
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
$(document).on('pagebeforeshow', "#index", function () {
|
||||||
|
logout();
|
||||||
|
});
|
||||||
|
$(document).on('pagebeforeshow', "#chat", function () {
|
||||||
|
socket.send(JSON.stringify({type: 'TOPIC', content: {senderName: userName}}));
|
||||||
|
$('#chat-send-button').on('click', function () {
|
||||||
|
send();
|
||||||
|
});
|
||||||
|
$('#chat-send-message').keypress(function(e) {
|
||||||
|
if(e.which == 13) {
|
||||||
|
send();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
function logout() {
|
||||||
|
if (socket != null) {
|
||||||
|
try {socket.close();} catch (e) {}
|
||||||
|
socket = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function login() {
|
||||||
|
userName = $('#login-username').val();
|
||||||
|
if (userName.length > 0) {
|
||||||
|
if (window.WebSocket) {
|
||||||
|
logout();
|
||||||
|
socket = new WebSocket(endpoint);
|
||||||
|
socket.onmessage = function (event) {
|
||||||
|
onMessage(event.data);
|
||||||
|
};
|
||||||
|
socket.onerror = function (event) {
|
||||||
|
$.mobile.changePage("index.jsp", {transition: 'fade'});
|
||||||
|
};
|
||||||
|
socket.onopen = function (event) {
|
||||||
|
$('#chat-content').empty();
|
||||||
|
socket.send(JSON.stringify({type: 'LOGIN', content: {senderName: userName}}));
|
||||||
|
$.mobile.changePage("chat.jsp", {transition: 'fade'});
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
version = '${project.version}';
|
}
|
||||||
buildNumber = '${buildNumber}';
|
}
|
||||||
|
}
|
||||||
|
function onMessage(message) {
|
||||||
|
if (message != null && message != '') {
|
||||||
|
var json = JSON.parse(message);
|
||||||
|
|
||||||
try {
|
if (json.type == 'MSG') {
|
||||||
timestamp = new Date(${timestamp});
|
$('#chat-content').append('<p><span class="userName">'+json.content.senderName+': </span><span class="message">'+json.content.message+'</span></p>');
|
||||||
} catch (exception) {
|
} else if (json.type == 'SERVER') {
|
||||||
// ignore
|
$('#chat-content').append('<p><span class="message">* '+json.content.message+'</span></p>');
|
||||||
|
} else if (json.type == 'TOPIC') {
|
||||||
|
$('#chat-content').append('<p><span class="message">* '+json.content.message+'</span></p>');
|
||||||
|
}
|
||||||
|
|
||||||
|
$(document).scrollTop($(document).height());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onError(message) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function send() {
|
||||||
|
var messageInput = $('#chat-send-message');
|
||||||
|
var message = messageInput.val();
|
||||||
|
if (message.length > 0) {
|
||||||
|
messageInput.val('');
|
||||||
|
if (socket) {
|
||||||
|
$('#chat-content').append('<p><span class="userName own">'+userName+': </span><span class="message">'+message+'</span></p>');
|
||||||
|
$(document).scrollTop($(document).height());
|
||||||
|
socket.send(JSON.stringify({type: 'MSG', content: {senderName: userName, message: message}}));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div data-role="page">
|
<div id="index" data-role="page">
|
||||||
|
|
||||||
<div data-theme="a" data-role="header">
|
<div data-theme="a" data-role="header" data-tap-toggle="false">
|
||||||
<h1>Migor Location Chat</h1>
|
<h1>Migor Chat</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="login-content" data-role="content" style="padding-top: 120px;">
|
<div id="login-content" data-role="content" style="padding-top: 120px;">
|
||||||
<p>
|
<p>
|
||||||
<input id="username" type="text" value="" placeholder="Username"/>
|
<input id="login-username" type="text" value="" placeholder="Username"/>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<button id="startButton" data-role="button">Start</button>
|
<button id="login-button" data-role="button">Start</button>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
115
org.migor.webclient.mobile/src/main/webapp/js/rest.js
Normal file
115
org.migor.webclient.mobile/src/main/webapp/js/rest.js
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
rest = new function () {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param restFunc
|
||||||
|
* @param [params]
|
||||||
|
* @param [successCallback]
|
||||||
|
* @param [validationCallback]
|
||||||
|
*/
|
||||||
|
this.request = function (restFunc, params, successCallback, validationCallback) {
|
||||||
|
|
||||||
|
if (typeof(restFunc) === 'function') {
|
||||||
|
|
||||||
|
if (params == null) {
|
||||||
|
params = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
params["$callback"] = function (code, request, response) {
|
||||||
|
// var response = ;
|
||||||
|
|
||||||
|
if (response != null && response.status != null) {
|
||||||
|
switch (response.status) {
|
||||||
|
// Status OK
|
||||||
|
case 0:
|
||||||
|
if (typeof(successCallback) === 'function') {
|
||||||
|
successCallback.call(this, response.content, response.totalRecords)
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// Validation error
|
||||||
|
case 100:
|
||||||
|
if (typeof(validationCallback) === 'function') {
|
||||||
|
validationCallback.call(this, response.affectedFields)
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// Other errors
|
||||||
|
default :
|
||||||
|
// TODO
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
migor.dialog.openErrorDialog(code + ': ' + request.responseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
restFunc.call(this, params);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
* @param urlReplacements
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
this.replaceInUrl = function (url, urlReplacements) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
if (urlReplacements == null)
|
||||||
|
return url;
|
||||||
|
for (var key in urlReplacements) {
|
||||||
|
//noinspection JSUnfilteredForInLoop
|
||||||
|
var value = urlReplacements[key];
|
||||||
|
if (value == null || value == "") {
|
||||||
|
//noinspection JSUnfilteredForInLoop
|
||||||
|
url = url.replace(key, " ");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//noinspection JSUnfilteredForInLoop
|
||||||
|
url = url.replace(key, self.urlEncodePathParameter(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode special characters which are known to
|
||||||
|
* cause problems when used in an URL as path
|
||||||
|
* parameter
|
||||||
|
*
|
||||||
|
* @param value string to encode
|
||||||
|
*/
|
||||||
|
this.urlEncodePathParameter = function (value) {
|
||||||
|
|
||||||
|
// be on the safe side
|
||||||
|
if (value == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
var _value = '' + value;
|
||||||
|
|
||||||
|
var encodeMe = {
|
||||||
|
"?": "%3F",
|
||||||
|
"%": "%25",
|
||||||
|
"/": "%2F"
|
||||||
|
};
|
||||||
|
var result = "";
|
||||||
|
for (var i = 0; i < _value.length; i++) {
|
||||||
|
result += encodeMe[_value.charAt(i)] == undefined ? _value.charAt(i) : encodeMe[_value.charAt(i)];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.block = function () {
|
||||||
|
$.blockUI({ message: $('#block'), css: { position: 'relative', top: '0px', left: '0px', width: '100%', height: '100%', 'vertical-align': 'middle', background: 'none', color: '#fff'} });
|
||||||
|
};
|
||||||
|
|
||||||
|
this.unblock = function () {
|
||||||
|
$.unblockUI();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
$.widget("migor.chat", {
|
|
||||||
|
|
||||||
socket: null,
|
|
||||||
|
|
||||||
options: {
|
|
||||||
endpoint: '/migor/services/client/socket/chat'
|
|
||||||
},
|
|
||||||
_create: function () {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
|
|
||||||
if (window.WebSocket) {
|
|
||||||
var endpoint = 'ws://192.168.0.18:8080' + self.options.endpoint;
|
|
||||||
|
|
||||||
self.socket = new WebSocket(endpoint);
|
|
||||||
self.socket.onmessage = function (event) {
|
|
||||||
self._onMessage(event.data);
|
|
||||||
};
|
|
||||||
self.socket.onerror = function (event) {
|
|
||||||
self.element.prepend('<p class="message error">Cannot connect to web socket!!</p>');
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
self.element.prepend('<p class="message error">Web sockets are not supported!!</p>');
|
|
||||||
}
|
|
||||||
|
|
||||||
},
|
|
||||||
_destroy: function () {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
if (self.socket) {
|
|
||||||
self.socket.close();
|
|
||||||
self.socket = null;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_setOptions: function () {
|
|
||||||
this._superApply(arguments);
|
|
||||||
},
|
|
||||||
_onMessage: function (message) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
self.element.prepend('<p class="message received">' + message + '</p>');
|
|
||||||
},
|
|
||||||
send: function (message) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
if (self.socket) {
|
|
||||||
self.socket.send(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
$.widget("migor.mobile", {
|
|
||||||
|
|
||||||
user: null,
|
|
||||||
|
|
||||||
options: {
|
|
||||||
|
|
||||||
},
|
|
||||||
_create: function () {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
if (!self.user) {
|
|
||||||
|
|
||||||
var loginTextField = $('<input class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset" value="" placeholder="Username"/>').textinput({ preventFocusZoom: true });
|
|
||||||
var loginButton = $('<a>Start</a>').buttonMarkup();
|
|
||||||
|
|
||||||
self.element.empty();
|
|
||||||
self.element.append($('<p></p>').append('<div style="margin-top: 100px;"> </div>'));
|
|
||||||
self.element.append($('<p></p>').append(loginTextField));
|
|
||||||
self.element.append($('<p></p>').append(loginButton));
|
|
||||||
}
|
|
||||||
|
|
||||||
},
|
|
||||||
_setOptions: function () {
|
|
||||||
this._superApply(arguments);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user