Skeleton of client part of the chat application
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 04 May 2013 07:21:05 +0200
changeset 5f0a52c72ef74
parent 4 a707111432df
child 6 ecb1ef414295
Skeleton of client part of the chat application
pom.xml
serverside/nbactions.xml
serverside/pom.xml
serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatClient.java
serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/MessageImpl.java
serverside/src/main/resources/org/apidesign/bck2brwsr/demo/serverside/chat.html
serverside/src/test/java/org/apidesign/bck2brwsr/demo/serverside/ChatClientTest.java
     1.1 --- a/pom.xml	Thu May 02 14:41:10 2013 +0200
     1.2 +++ b/pom.xml	Sat May 04 07:21:05 2013 +0200
     1.3 @@ -18,6 +18,7 @@
     1.4    </properties>
     1.5    <modules>
     1.6      <module>twitter</module>
     1.7 +    <module>serverside</module>
     1.8    </modules>
     1.9    <licenses>
    1.10        <license>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/serverside/nbactions.xml	Sat May 04 07:21:05 2013 +0200
     2.3 @@ -0,0 +1,45 @@
     2.4 +<?xml version="1.0" encoding="UTF-8"?>
     2.5 +<!--
     2.6 +
     2.7 +    The MIT License (MIT)
     2.8 +
     2.9 +    Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    2.10 +
    2.11 +    Permission is hereby granted, free of charge, to any person obtaining a copy
    2.12 +    of this software and associated documentation files (the "Software"), to deal
    2.13 +    in the Software without restriction, including without limitation the rights
    2.14 +    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    2.15 +    copies of the Software, and to permit persons to whom the Software is
    2.16 +    furnished to do so, subject to the following conditions:
    2.17 +
    2.18 +    The above copyright notice and this permission notice shall be included in
    2.19 +    all copies or substantial portions of the Software.
    2.20 +
    2.21 +    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    2.22 +    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    2.23 +    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    2.24 +    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    2.25 +    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    2.26 +    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    2.27 +    THE SOFTWARE.
    2.28 +
    2.29 +-->
    2.30 +<actions>
    2.31 +    <action>
    2.32 +        <actionName>run</actionName>
    2.33 +        <goals>
    2.34 +            <goal>process-classes</goal>
    2.35 +            <goal>bck2brwsr:brwsr</goal>
    2.36 +        </goals>
    2.37 +    </action>
    2.38 +    <action>
    2.39 +        <actionName>debug</actionName>
    2.40 +        <goals>
    2.41 +            <goal>process-classes</goal>
    2.42 +            <goal>bck2brwsr:brwsr</goal>
    2.43 +        </goals>
    2.44 +        <properties>
    2.45 +            <jpda.listen>maven</jpda.listen>
    2.46 +        </properties>
    2.47 +    </action>
    2.48 +</actions>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/serverside/pom.xml	Sat May 04 07:21:05 2013 +0200
     3.3 @@ -0,0 +1,84 @@
     3.4 +<?xml version="1.0"?>
     3.5 +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
     3.6 +    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     3.7 +  <modelVersion>4.0.0</modelVersion>
     3.8 +  <parent>
     3.9 +    <groupId>org.apidesign.html</groupId>
    3.10 +    <artifactId>demo</artifactId>
    3.11 +    <version>1.0-SNAPSHOT</version>
    3.12 +  </parent>
    3.13 +  <groupId>org.apidesign.bck2brwsr</groupId>
    3.14 +  <artifactId>demo-serverside</artifactId>
    3.15 +  <version>1.0-SNAPSHOT</version>
    3.16 +  <name>Bck2Brwsr and Bck2Server</name>
    3.17 +  <url>http://maven.apache.org</url>
    3.18 +  <properties>
    3.19 +    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    3.20 +    <net.java.html.version>0.2</net.java.html.version>
    3.21 +    <bck2brwsr.version>0.7</bck2brwsr.version>
    3.22 +    <bck2brwsr.obfuscationlevel>MINIMAL</bck2brwsr.obfuscationlevel>
    3.23 +    <brwsr.startpage>org/apidesign/bck2brwsr/demo/serverside/chat.html</brwsr.startpage>
    3.24 +  </properties>
    3.25 +  <build>
    3.26 +      <plugins>
    3.27 +          <plugin>
    3.28 +              <groupId>org.apidesign.bck2brwsr</groupId>
    3.29 +              <artifactId>bck2brwsr-maven-plugin</artifactId>
    3.30 +              <version>${bck2brwsr.version}</version>
    3.31 +              <executions>
    3.32 +                  <execution>
    3.33 +                      <goals>
    3.34 +                          <goal>brwsr</goal>
    3.35 +                      </goals>
    3.36 +                  </execution>
    3.37 +              </executions>
    3.38 +              <configuration>
    3.39 +                  <startpage>${brwsr.startpage}</startpage>
    3.40 +                  <launcher>bck2brwsr</launcher>
    3.41 +              </configuration>
    3.42 +          </plugin>
    3.43 +         <plugin>
    3.44 +            <groupId>org.apache.maven.plugins</groupId>
    3.45 +            <artifactId>maven-compiler-plugin</artifactId>
    3.46 +            <version>2.3.2</version>
    3.47 +            <configuration>
    3.48 +               <source>1.7</source>
    3.49 +               <target>1.7</target>
    3.50 +            </configuration>
    3.51 +         </plugin>
    3.52 +      </plugins>
    3.53 +  </build>
    3.54 +  <dependencies>
    3.55 +    <dependency>
    3.56 +      <groupId>org.apidesign.html</groupId>
    3.57 +      <artifactId>net.java.html.json</artifactId>
    3.58 +      <version>${net.java.html.version}</version>
    3.59 +      <type>jar</type>
    3.60 +    </dependency>
    3.61 +    <dependency>
    3.62 +      <groupId>org.apidesign.html</groupId>
    3.63 +      <artifactId>ko-bck2brwsr</artifactId>
    3.64 +      <version>${net.java.html.version}</version>
    3.65 +      <scope>runtime</scope>
    3.66 +    </dependency>
    3.67 +    <dependency>
    3.68 +      <groupId>${project.groupId}</groupId>
    3.69 +      <artifactId>launcher.http</artifactId>
    3.70 +      <version>${bck2brwsr.version}</version>
    3.71 +      <scope>runtime</scope>
    3.72 +    </dependency>
    3.73 +    <dependency>
    3.74 +      <groupId>${project.groupId}</groupId>
    3.75 +      <artifactId>emul</artifactId>
    3.76 +      <version>${bck2brwsr.version}</version>
    3.77 +      <classifier>rt</classifier>
    3.78 +      <scope>runtime</scope>
    3.79 +    </dependency>
    3.80 +    <dependency>
    3.81 +      <groupId>org.testng</groupId>
    3.82 +      <artifactId>testng</artifactId>
    3.83 +      <version>6.5.2</version>
    3.84 +      <scope>test</scope>
    3.85 +    </dependency>
    3.86 +  </dependencies>
    3.87 +</project>
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatClient.java	Sat May 04 07:21:05 2013 +0200
     4.3 @@ -0,0 +1,73 @@
     4.4 +/**
     4.5 + * The MIT License (MIT)
     4.6 + *
     4.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.8 + *
     4.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    4.10 + * of this software and associated documentation files (the "Software"), to deal
    4.11 + * in the Software without restriction, including without limitation the rights
    4.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    4.13 + * copies of the Software, and to permit persons to whom the Software is
    4.14 + * furnished to do so, subject to the following conditions:
    4.15 + *
    4.16 + * The above copyright notice and this permission notice shall be included in
    4.17 + * all copies or substantial portions of the Software.
    4.18 + *
    4.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    4.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    4.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    4.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    4.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    4.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    4.25 + * THE SOFTWARE.
    4.26 + */
    4.27 +package org.apidesign.bck2brwsr.demo.serverside;
    4.28 +
    4.29 +import net.java.html.json.ComputedProperty;
    4.30 +import net.java.html.json.Context;
    4.31 +import net.java.html.json.Function;
    4.32 +import net.java.html.json.Model;
    4.33 +import net.java.html.json.Property;
    4.34 +
    4.35 +/**
    4.36 + *
    4.37 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    4.38 + */
    4.39 +@Model(className = "ChatModel", properties = {
    4.40 +    @Property(name = "user", type = String.class),
    4.41 +    @Property(name = "text", type = String.class),
    4.42 +    @Property(name = "msgs", type = Message.class, array = true)
    4.43 +})
    4.44 +class ChatClient {
    4.45 +    @ComputedProperty
    4.46 +    static boolean sendEnabled(String user, String text) {
    4.47 +        boolean res = user != null && text != null && !user.isEmpty() && !text.isEmpty();
    4.48 +        try {
    4.49 +            if (true) throw new IllegalStateException("Query for msg '" + text + "' by user " + user + " res: " + res);
    4.50 +        } catch (Exception ex) {
    4.51 +            
    4.52 +        }
    4.53 +        return res;
    4.54 +    }
    4.55 +    
    4.56 +    @Function
    4.57 +    static void submit(ChatModel m) {
    4.58 +        if (!sendEnabled(m.getUser(), m.getText())) {
    4.59 +            return;
    4.60 +        }
    4.61 +        Message msg = new Message(CNTX);
    4.62 +        msg.setComment(m.getText());
    4.63 +        msg.setUser(m.getUser());
    4.64 +        m.getMsgs().add(msg);
    4.65 +    }
    4.66 +    
    4.67 +    static final Context CNTX = Context.findDefault(ChatClient.class);
    4.68 +    static {
    4.69 +        ChatModel chm = new ChatModel(CNTX);
    4.70 +        Message m = new Message(CNTX);
    4.71 +        m.setComment("Hello World!");
    4.72 +        m.setUser("Jarda");
    4.73 +        chm.getMsgs().add(m);
    4.74 +        chm.applyBindings();
    4.75 +    }
    4.76 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/MessageImpl.java	Sat May 04 07:21:05 2013 +0200
     5.3 @@ -0,0 +1,49 @@
     5.4 +/**
     5.5 + * The MIT License (MIT)
     5.6 + *
     5.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5.8 + *
     5.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    5.10 + * of this software and associated documentation files (the "Software"), to deal
    5.11 + * in the Software without restriction, including without limitation the rights
    5.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    5.13 + * copies of the Software, and to permit persons to whom the Software is
    5.14 + * furnished to do so, subject to the following conditions:
    5.15 + *
    5.16 + * The above copyright notice and this permission notice shall be included in
    5.17 + * all copies or substantial portions of the Software.
    5.18 + *
    5.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    5.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    5.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    5.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    5.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    5.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    5.25 + * THE SOFTWARE.
    5.26 + */
    5.27 +package org.apidesign.bck2brwsr.demo.serverside;
    5.28 +
    5.29 +import net.java.html.json.ComputedProperty;
    5.30 +import net.java.html.json.Model;
    5.31 +import net.java.html.json.Property;
    5.32 +
    5.33 +/**
    5.34 + *
    5.35 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    5.36 + */
    5.37 +@Model(className = "Message", properties = {
    5.38 +    @Property(name = "user", type = String.class),
    5.39 +    @Property(name = "comment", type = String.class),
    5.40 +//    @Property(name = "when", type = long.class)    
    5.41 +})
    5.42 +class MessageImpl {
    5.43 +    @ComputedProperty static String at() {
    5.44 +        return "nekdy";
    5.45 +    }
    5.46 +
    5.47 +    @Model(className = "Query", properties = {
    5.48 +        @Property(name = "messages", type = Message.class, array = true)
    5.49 +    })
    5.50 +    class QryMsgs {
    5.51 +    }
    5.52 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/serverside/src/main/resources/org/apidesign/bck2brwsr/demo/serverside/chat.html	Sat May 04 07:21:05 2013 +0200
     6.3 @@ -0,0 +1,54 @@
     6.4 +<!--
     6.5 +
     6.6 +    The MIT License (MIT)
     6.7 +
     6.8 +    Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     6.9 +
    6.10 +    Permission is hereby granted, free of charge, to any person obtaining a copy
    6.11 +    of this software and associated documentation files (the "Software"), to deal
    6.12 +    in the Software without restriction, including without limitation the rights
    6.13 +    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    6.14 +    copies of the Software, and to permit persons to whom the Software is
    6.15 +    furnished to do so, subject to the following conditions:
    6.16 +
    6.17 +    The above copyright notice and this permission notice shall be included in
    6.18 +    all copies or substantial portions of the Software.
    6.19 +
    6.20 +    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    6.21 +    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    6.22 +    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    6.23 +    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    6.24 +    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    6.25 +    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    6.26 +    THE SOFTWARE.
    6.27 +
    6.28 +-->
    6.29 +<!DOCTYPE html>
    6.30 +<html>
    6.31 +    <head>
    6.32 +        <title>Chat via Jersey Server</title>
    6.33 +        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    6.34 +    </head>
    6.35 +    <body>
    6.36 +        <div>Username:</div>
    6.37 +        <input data-bind="value: user"/>
    6.38 +        <div>Message:</div>
    6.39 +        <input data-bind="value: text"/>
    6.40 +        <button data-bind="enable: sendEnabled, click: submit">Send</button>
    6.41 +        
    6.42 +        <ul data-bind="foreach: msgs">
    6.43 +            <li>
    6.44 +                <b data-bind="text: user"></b>@<em data-bind="text: at"></em>
    6.45 +                :
    6.46 +                <span data-bind="text: comment"></span>
    6.47 +            </li>
    6.48 +        </ul>
    6.49 +        
    6.50 +        <script src="bck2brwsr.js"></script>
    6.51 +        <script type="text/javascript">
    6.52 +            var vm = bck2brwsr('demo-serverside-1.0-SNAPSHOT.jar');
    6.53 +            vm.loadClass('org.apidesign.bck2brwsr.demo.serverside.ChatClient');
    6.54 +        </script>
    6.55 +        
    6.56 +    </body>
    6.57 +</html>
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/serverside/src/test/java/org/apidesign/bck2brwsr/demo/serverside/ChatClientTest.java	Sat May 04 07:21:05 2013 +0200
     7.3 @@ -0,0 +1,55 @@
     7.4 +/**
     7.5 + * The MIT License (MIT)
     7.6 + *
     7.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     7.8 + *
     7.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    7.10 + * of this software and associated documentation files (the "Software"), to deal
    7.11 + * in the Software without restriction, including without limitation the rights
    7.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    7.13 + * copies of the Software, and to permit persons to whom the Software is
    7.14 + * furnished to do so, subject to the following conditions:
    7.15 + *
    7.16 + * The above copyright notice and this permission notice shall be included in
    7.17 + * all copies or substantial portions of the Software.
    7.18 + *
    7.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    7.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    7.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    7.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    7.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    7.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    7.25 + * THE SOFTWARE.
    7.26 + */
    7.27 +package org.apidesign.bck2brwsr.demo.serverside;
    7.28 +
    7.29 +import net.java.html.json.Context;
    7.30 +import static org.testng.Assert.*;
    7.31 +import org.testng.annotations.AfterClass;
    7.32 +import org.testng.annotations.AfterMethod;
    7.33 +import org.testng.annotations.BeforeClass;
    7.34 +import org.testng.annotations.BeforeMethod;
    7.35 +import org.testng.annotations.Test;
    7.36 +
    7.37 +/**
    7.38 + *
    7.39 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    7.40 + */
    7.41 +public class ChatClientTest {
    7.42 +    public ChatClientTest() {
    7.43 +    }
    7.44 +    
    7.45 +    @Test public void hasSendEnabled() {
    7.46 +        ChatModel m = new ChatModel(Context.EMPTY);
    7.47 +        assertFalse(m.isSendEnabled(), "By default disabled");
    7.48 +        m.setText("some msg");
    7.49 +        m.setUser("by me");
    7.50 +        assertTrue(m.isSendEnabled(), "Now it is enabled");
    7.51 +        m.setUser(null);
    7.52 +        assertFalse(m.isSendEnabled(), "No user means disabled");
    7.53 +        m.setUser("by him");
    7.54 +        assertTrue(m.isSendEnabled(), "Again enabled");
    7.55 +        m.setText("");
    7.56 +        assertFalse(m.isSendEnabled(), "Empty text means disabled");
    7.57 +    }
    7.58 +}