Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinckh committed Sep 15, 2018
0 parents commit dc6db35
Show file tree
Hide file tree
Showing 71 changed files with 16,821 additions and 0 deletions.
2 changes: 2 additions & 0 deletions webapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.logs
data
19 changes: 19 additions & 0 deletions webapp/WEB-INF/jetty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure_9_3.dtd">

<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- Default connector. The Jetty stop port can be specified
in the .basex or pom.xml configuration file. -->
<Call name="addConnector">
<Arg>
<New id="httpConnector" class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server"><Ref refid="Server"/></Arg>
<Set name="host">0.0.0.0</Set>
<Set name="port">8984</Set>
<Set name="idleTimeout">60000</Set>
<Set name="reuseAddress">true</Set>
</New>
</Arg>
</Call>
</Configure>
Empty file added webapp/WEB-INF/repo/.empty
Empty file.
147 changes: 147 additions & 0 deletions webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">

<display-name>BaseX: The XML Database and XQuery Processor</display-name>
<description>HTTP Services</description>

<!-- A BaseX option can be overwritten by prefixing the key with "org.basex."
and specifying it in <context-param/> elements, as shown below.
Check out http://docs.basex.org/wiki/Options for a list of all options.
<context-param>
<param-name>org.basex.restxqpath</param-name>
<param-value>.</param-value>
</context-param>
<context-param>
<param-name>org.basex.dbpath</param-name>
<param-value>WEB-INF/data</param-value>
</context-param>
<context-param>
<param-name>org.basex.repopath</param-name>
<param-value>WEB-INF/repo</param-value>
</context-param>
<context-param>
<param-name>org.basex.user</param-name>
<param-value>admin</param-value>
</context-param>
<context-param>
<param-name>org.basex.authmethod</param-name>
<param-value>Digest</param-value>
</context-param>
<context-param>
<param-name>org.basex.httplocal</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.basex.timeout</param-name>
<param-value>5</param-value>
</context-param>
<context-param>
<param-name>org.basex.log</param-name>
<param-value>false</param-value>
</context-param>
-->

<!-- Global session and servlet listener -->
<listener>
<listener-class>org.basex.http.SessionListener</listener-class>
</listener>
<listener>
<listener-class>org.basex.http.ServletListener</listener-class>
</listener>

<!-- CORS in Jetty: Access-Control-Allow-Origin: *
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->

<!-- Dynamic compression of contents:
<filter>
<filter-name>GzipFilter</filter-name>
<filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
<init-param>
<param-name>mimeTypes</param-name>
<param-value>text/html,text/xml,application/xhtml+xml,image/svg+xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->

<!-- RESTXQ Service (can be disabled by removing this entry) -->
<servlet>
<servlet-name>RESTXQ</servlet-name>
<servlet-class>org.basex.http.restxq.RestXqServlet</servlet-class>
<init-param>
<param-name>org.basex.user</param-name>
<param-value>admin</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RESTXQ</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<!-- WebSocket Service (can be disabled by removing this entry) -->
<servlet>
<servlet-name>WebSocket</servlet-name>
<servlet-class>org.basex.http.ws.WsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebSocket</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

<!-- REST Service (can be disabled by removing this entry) -->
<servlet>
<servlet-name>REST</servlet-name>
<servlet-class>org.basex.http.rest.RESTServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>REST</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<!-- WebDAV Service (can be disabled by removing this entry) -->
<servlet>
<servlet-name>WebDAV</servlet-name>
<servlet-class>org.basex.http.webdav.WebDAVServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebDAV</servlet-name>
<url-pattern>/webdav/*</url-pattern>
</servlet-mapping>

<!-- Mapping for static resources (may be restricted to a sub path) -->
<servlet>
<servlet-name>default</servlet-name>
<init-param>
<param-name>useFileMappedBuffer</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>

</web-app>
105 changes: 105 additions & 0 deletions webapp/chat/chat-ws.xqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
module namespace chat = 'http://basex.org/modules/web-page';

import module namespace session = 'http://basex.org/modules/Session';
import module namespace sessions = 'http://basex.org/modules/Sessions';
import module namespace ws = 'http://basex.org/modules/ws';

(:~ Session chat id. :)
declare variable $chat:ID := 'chat';
(:~ WebSocket ID of the WebSocket instance :)
declare variable $chat:ws-ID := 'ws-id';
(:~ Messages :)
declare variable $chat:messages := 'messages';

declare
%rest:path('/chat/getChat')
%rest:GET
function chat:ws-getchat() as xs:string{
let $json := chat:get-chat-info()
return json:serialize($json)
};

declare
%rest:path('/chat/sendMessage')
%rest:POST
%rest:form-param("message","{$message}", "(no message)")
function chat:ws-sendmessage(
$message as xs:string
) as empty-sequence(){
let $json := parse-json($message)
return chat:set-message($json?text, $json?to)
};

(:~
: Processes a WebSocket message.
: @param $message message
:)
declare
%ws:message('/chat', '{$message}')
function chat:ws-message(
$message as xs:string
) as empty-sequence() {
let $json := parse-json($message)
let $type := $json?type
return if($type = 'message') then (
chat:set-message($json?text, $json?to)
) else if($type = "ping") then(
chat:ping()
) else error()
};


declare %private function chat:get-chat-info() as xs:string {
json:serialize(
map {
'users': map {
'userslist': array { sort(user:list()) },
'active': array { distinct-values(
sessions:ids() ! sessions:get(., $chat:ID)
)} } ,
'messages' : array{session:get($chat:messages)}

}
)
};

(:~
: Sends a message to all clients, or to the clients of a specific user.
: @param $text text to be sent
: @param $to receiver of a private message (optional)
:)
declare %private function chat:set-message(
$text as xs:string,
$to as xs:string?
) as empty-sequence() {
let $message := json:serialize(map {
'type': 'message',
'text': serialize($text),
'from': session:get($chat:ID),
'date': format-time(current-time(), '[H02]:[m02]:[s02]'),
'private': boolean($to)
})


return if($to) then (
for $id in sessions:ids()
where sessions:get($id, $chat:ID) = $to
let $msg := sessions:get($id, $chat:messages)
let $new-msg := ($msg,$message)
return sessions:set($id, $chat:messages, $new-msg)
) else (
for $id in sessions:ids()
let $msg := sessions:get($id, $chat:messages)
let $new-msg := ($msg,$message)
return sessions:set($id,$chat:messages,$new-msg)
)
};

(:~
: Answers with a pong to a ping-message
:)
declare %private function chat:ping(){
ws:send(json:serialize(map{
'type': 'pong'
}),ws:id())
};
Loading

0 comments on commit dc6db35

Please sign in to comment.