Converting the webidor into Jersey based REST server
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 12 Jul 2009 13:59:07 +0200
changeset 352e85dd878f04
parent 34 34baf57f2d4e
child 36 d5ccf73ebbe5
Converting the webidor into Jersey based REST server
pom.xml
webidor/nb-configuration.xml
webidor/nbactions.xml
webidor/pom.xml
webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/JAXBContextResolver.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/UserDetailsImpl.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
webidor/src/main/webapp/WEB-INF/applicationContext.xml
webidor/src/main/webapp/WEB-INF/dispatcher-servlet.xml
webidor/src/main/webapp/WEB-INF/jsp/index.jsp
webidor/src/main/webapp/WEB-INF/jsp/login.jsp
webidor/src/main/webapp/WEB-INF/jsp/loginredirection.jsp
webidor/src/main/webapp/WEB-INF/sun-web.xml
webidor/src/main/webapp/WEB-INF/web.xml
webidor/src/main/webapp/index.jsp
webidor/src/main/webapp/redirect.jsp
webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
     1.1 --- a/pom.xml	Sun Jul 12 13:35:58 2009 +0200
     1.2 +++ b/pom.xml	Sun Jul 12 13:59:07 2009 +0200
     1.3 @@ -36,8 +36,9 @@
     1.4    <modules>
     1.5      <module>quoridor</module>
     1.6      <module>visidor</module>
     1.7 +    <module>webidor</module>
     1.8    </modules>
     1.9      <name>Quoridor related projects</name>
    1.10      <description>Master project that agregates all quoridor related functionality.
    1.11      </description>
    1.12 -</project>
    1.13 +</project>
    1.14 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/webidor/nb-configuration.xml	Sun Jul 12 13:59:07 2009 +0200
     2.3 @@ -0,0 +1,21 @@
     2.4 +<?xml version="1.0" encoding="UTF-8"?>
     2.5 +<project-shared-configuration>
     2.6 +    <!--
     2.7 +This file contains additional configuration written by modules in the NetBeans IDE.
     2.8 +The configuration is intended to be shared among all the users of project and
     2.9 +therefore it is assumed to be part of version control checkout.
    2.10 +Without this configuration present, some functionality in the IDE may be limited or fail altogether.
    2.11 +-->
    2.12 +    <spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
    2.13 +        <config-files>
    2.14 +            <config-file>src/main/webapp/WEB-INF/applicationContext.xml</config-file>
    2.15 +            <config-file>src/main/webapp/WEB-INF/dispatcher-servlet.xml</config-file>
    2.16 +        </config-files>
    2.17 +        <config-file-groups>
    2.18 +            <config-file-group name="Default Group">
    2.19 +                <config-file>src/main/webapp/WEB-INF/applicationContext.xml</config-file>
    2.20 +                <config-file>src/main/webapp/WEB-INF/dispatcher-servlet.xml</config-file>
    2.21 +            </config-file-group>
    2.22 +        </config-file-groups>
    2.23 +    </spring-data>
    2.24 +</project-shared-configuration>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/webidor/nbactions.xml	Sun Jul 12 13:59:07 2009 +0200
     3.3 @@ -0,0 +1,15 @@
     3.4 +<?xml version="1.0" encoding="UTF-8"?>
     3.5 +<actions>
     3.6 +        <action>
     3.7 +            <actionName>run</actionName>
     3.8 +            <goals>
     3.9 +                <goal>process-classes</goal>
    3.10 +                <goal>org.codehaus.mojo:exec-maven-plugin:1.1.1:exec</goal>
    3.11 +            </goals>
    3.12 +            <properties>
    3.13 +                <exec.classpathScope>runtime</exec.classpathScope>
    3.14 +                <exec.args>-classpath %classpath cz.xelfi.quoridor.webidor.Main</exec.args>
    3.15 +                <exec.executable>java</exec.executable>
    3.16 +            </properties>
    3.17 +        </action>
    3.18 +    </actions>
     4.1 --- a/webidor/pom.xml	Sun Jul 12 13:35:58 2009 +0200
     4.2 +++ b/webidor/pom.xml	Sun Jul 12 13:59:07 2009 +0200
     4.3 @@ -8,56 +8,59 @@
     4.4      </parent>
     4.5    <groupId>org.apidesign</groupId>
     4.6    <artifactId>webidor</artifactId>
     4.7 -  <packaging>war</packaging>
     4.8 +  <packaging>jar</packaging>
     4.9    <version>1.0</version>
    4.10 -  <name>webidor JEE5 Webapp</name>
    4.11 +  <name>webidor server</name>
    4.12    <url>http://maven.apache.org</url>
    4.13 +  <repositories>
    4.14 +    <repository>
    4.15 +        <id>maven2-repository.dev.java.net</id>
    4.16 +        <name>Java.net Repository for Maven</name>
    4.17 +        <url>http://download.java.net/maven/2/</url>
    4.18 +        <layout>default</layout>
    4.19 +    </repository>
    4.20 +    <repository>
    4.21 +        <id>maven-repository.dev.java.net</id>
    4.22 +        <name>Java.net Maven 1 Repository (legacy)</name>
    4.23 +        <url>http://download.java.net/maven/1</url>
    4.24 +        <layout>legacy</layout>
    4.25 +    </repository>
    4.26 +  </repositories>
    4.27    <dependencies>
    4.28  
    4.29      <dependency>
    4.30 -      <groupId>javax.servlet</groupId>
    4.31 -      <artifactId>servlet-api</artifactId>
    4.32 -      <version>2.5</version>
    4.33 -      <scope>provided</scope>
    4.34 -    </dependency>
    4.35 -
    4.36 -    <dependency>
    4.37 -      <groupId>javax.servlet.jsp</groupId>
    4.38 -      <artifactId>jsp-api</artifactId>
    4.39 -      <version>2.1</version>
    4.40 -      <scope>provided</scope>
    4.41 -    </dependency>
    4.42 -
    4.43 -    <dependency>
    4.44        <groupId>junit</groupId>
    4.45        <artifactId>junit</artifactId>
    4.46 -      <version>3.8.1</version>
    4.47 +      <version>4.5</version>
    4.48        <scope>test</scope>
    4.49      </dependency>
    4.50      <dependency>
    4.51 -      <groupId>org.springframework</groupId>
    4.52 -      <artifactId>spring-webmvc</artifactId>
    4.53 -      <version>2.5</version>
    4.54 +      <groupId>com.sun.jersey</groupId>
    4.55 +      <artifactId>jersey-core</artifactId>
    4.56 +      <version>1.1.0-ea</version>
    4.57      </dependency>
    4.58      <dependency>
    4.59 -      <groupId>org.springframework</groupId>
    4.60 -      <artifactId>spring</artifactId>
    4.61 -      <version>2.5</version>
    4.62 +      <groupId>com.sun.jersey</groupId>
    4.63 +      <artifactId>jersey-server</artifactId>
    4.64 +      <version>1.1.0-ea</version>
    4.65      </dependency>
    4.66      <dependency>
    4.67 -      <groupId>org.springframework.security</groupId>
    4.68 -      <artifactId>spring-security-openid</artifactId>
    4.69 -      <version>2.0.4</version>
    4.70 -      <exclusions>
    4.71 -        <exclusion>
    4.72 -          <artifactId>icu4j</artifactId>
    4.73 -          <groupId>com.ibm.icu</groupId>
    4.74 -        </exclusion>
    4.75 -        <exclusion>
    4.76 -          <artifactId>xml-apis</artifactId>
    4.77 -          <groupId>xml-apis</groupId>
    4.78 -        </exclusion>
    4.79 -      </exclusions>
    4.80 +      <groupId>com.sun.jersey</groupId>
    4.81 +      <artifactId>jersey-json</artifactId>
    4.82 +      <version>1.1.0-ea</version>
    4.83 +      <type>jar</type>
    4.84 +    </dependency>
    4.85 +    <dependency>
    4.86 +      <groupId>com.sun.jersey.test.framework</groupId>
    4.87 +      <artifactId>jersey-test-framework</artifactId>
    4.88 +      <version>1.1.0-ea</version>
    4.89 +      <scope>test</scope>
    4.90 +    </dependency>
    4.91 +    <dependency>
    4.92 +      <groupId>org.apidesign</groupId>
    4.93 +      <artifactId>quoridor</artifactId>
    4.94 +      <version>1.0</version>
    4.95 +      <type>jar</type>
    4.96      </dependency>
    4.97    </dependencies>
    4.98    <build>
    4.99 @@ -71,15 +74,14 @@
   4.100            <target>1.5</target>
   4.101          </configuration>
   4.102        </plugin>
   4.103 +      <plugin>
   4.104 +        <groupId>org.codehaus.mojo</groupId>
   4.105 +        <artifactId>exec-maven-plugin</artifactId>
   4.106 +        <configuration>
   4.107 +            <mainClass>cz.xelfi.quoridor.webidor.Main</mainClass>
   4.108 +        </configuration>
   4.109 +      </plugin>
   4.110      </plugins>
   4.111      <finalName>webidor</finalName>
   4.112    </build>
   4.113 -  <properties>
   4.114 -    <netbeans.hint.deploy.server>gfv3</netbeans.hint.deploy.server>
   4.115 -  </properties>
   4.116  </project>
   4.117 -
   4.118 -
   4.119 -
   4.120 -
   4.121 -
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Sun Jul 12 13:59:07 2009 +0200
     5.3 @@ -0,0 +1,99 @@
     5.4 +/*
     5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6 + *
     5.7 + * The contents of this file are subject to the terms of either the GNU
     5.8 + * General Public License Version 2 only ("GPL") or the Common
     5.9 + * Development and Distribution License("CDDL") (collectively, the
    5.10 + * "License"). You may not use this file except in compliance with the
    5.11 + * License. You can obtain a copy of the License at
    5.12 + * http://www.netbeans.org/cddl-gplv2.html
    5.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    5.14 + * specific language governing permissions and limitations under the
    5.15 + * License.  When distributing the software, include this License Header
    5.16 + * Notice in each file and include the License file at
    5.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    5.18 + * particular file as subject to the "Classpath" exception as provided
    5.19 + * by Sun in the GPL Version 2 section of the License file that
    5.20 + * accompanied this code. If applicable, add the following below the
    5.21 + * License Header, with the fields enclosed by brackets [] replaced by
    5.22 + * your own identifying information:
    5.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    5.24 + *
    5.25 + * Contributor(s):
    5.26 + *
    5.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    5.28 + */
    5.29 +
    5.30 +package cz.xelfi.quoridor.webidor;
    5.31 +
    5.32 +import cz.xelfi.quoridor.Board;
    5.33 +import cz.xelfi.quoridor.IllegalPositionException;
    5.34 +import cz.xelfi.quoridor.Move;
    5.35 +import cz.xelfi.quoridor.Player;
    5.36 +import java.util.UUID;
    5.37 +import javax.xml.bind.annotation.XmlAccessType;
    5.38 +import javax.xml.bind.annotation.XmlAccessorType;
    5.39 +import javax.xml.bind.annotation.XmlAttribute;
    5.40 +import javax.xml.bind.annotation.XmlID;
    5.41 +import javax.xml.bind.annotation.XmlRootElement;
    5.42 +
    5.43 +/**
    5.44 + *
    5.45 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.46 + */
    5.47 +@XmlRootElement
    5.48 +@XmlAccessorType(XmlAccessType.FIELD)
    5.49 +public final class Game {
    5.50 +    @XmlAttribute
    5.51 +    private String white;
    5.52 +    @XmlAttribute
    5.53 +    private String black;
    5.54 +    @XmlID
    5.55 +    private String id;
    5.56 +
    5.57 +    private transient Board board;
    5.58 +
    5.59 +    Game() {
    5.60 +    }
    5.61 +
    5.62 +    public Game(String first, String second) {
    5.63 +        this.white = first;
    5.64 +        this.black = second;
    5.65 +        this.id = UUID.randomUUID().toString();
    5.66 +    }
    5.67 +
    5.68 +    public String getId() {
    5.69 +        return id;
    5.70 +    }
    5.71 +
    5.72 +    public String getWhite() {
    5.73 +        return white;
    5.74 +    }
    5.75 +
    5.76 +    public String getBlack() {
    5.77 +        return black;
    5.78 +    }
    5.79 +
    5.80 +    public Board getBoard() {
    5.81 +        if (board == null) {
    5.82 +            board = Board.empty();
    5.83 +        }
    5.84 +        return board;
    5.85 +    }
    5.86 +
    5.87 +    public void apply(String player, Move m) throws IllegalPositionException {
    5.88 +        Player p = null;
    5.89 +        if (getWhite().equals(player)) {
    5.90 +            p = getBoard().getPlayers().get(0);
    5.91 +        } else {
    5.92 +            if (getBlack().equals(player)) {
    5.93 +                p = getBoard().getPlayers().get(1);
    5.94 +            }
    5.95 +        }
    5.96 +        if (p != getBoard().getCurrentPlayer()) {
    5.97 +            throw new IllegalArgumentException("Wrong player: " + player);
    5.98 +        }
    5.99 +
   5.100 +        board = getBoard().apply(m);
   5.101 +    }
   5.102 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/JAXBContextResolver.java	Sun Jul 12 13:59:07 2009 +0200
     6.3 @@ -0,0 +1,70 @@
     6.4 +/*
     6.5 + *
     6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     6.7 + * 
     6.8 + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
     6.9 + * 
    6.10 + * The contents of this file are subject to the terms of either the GNU
    6.11 + * General Public License Version 2 only ("GPL") or the Common Development
    6.12 + * and Distribution License("CDDL") (collectively, the "License").  You
    6.13 + * may not use this file except in compliance with the License. You can obtain
    6.14 + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html
    6.15 + * or jersey/legal/LICENSE.txt.  See the License for the specific
    6.16 + * language governing permissions and limitations under the License.
    6.17 + * 
    6.18 + * When distributing the software, include this License Header Notice in each
    6.19 + * file and include the License file at jersey/legal/LICENSE.txt.
    6.20 + * Sun designates this particular file as subject to the "Classpath" exception
    6.21 + * as provided by Sun in the GPL Version 2 section of the License file that
    6.22 + * accompanied this code.  If applicable, add the following below the License
    6.23 + * Header, with the fields enclosed by brackets [] replaced by your own
    6.24 + * identifying information: "Portions Copyrighted [year]
    6.25 + * [name of copyright owner]"
    6.26 + * 
    6.27 + * Contributor(s):
    6.28 + * 
    6.29 + * If you wish your version of this file to be governed by only the CDDL or
    6.30 + * only the GPL Version 2, indicate your decision by adding "[Contributor]
    6.31 + * elects to include this software in this distribution under the [CDDL or GPL
    6.32 + * Version 2] license."  If you don't indicate a single choice of license, a
    6.33 + * recipient has the option to distribute your version of this file under
    6.34 + * either the CDDL, the GPL Version 2 or to extend the choice of license to
    6.35 + * its licensees as provided above.  However, if you add GPL Version 2 code
    6.36 + * and therefore, elected the GPL Version 2 license, then the option applies
    6.37 + * only if the new code is made subject to such option by the copyright
    6.38 + * holder.
    6.39 + */
    6.40 +
    6.41 +package cz.xelfi.quoridor.webidor;
    6.42 +
    6.43 +import com.sun.jersey.api.json.JSONConfiguration;
    6.44 +import com.sun.jersey.api.json.JSONJAXBContext;
    6.45 +import java.util.Arrays;
    6.46 +import java.util.HashSet;
    6.47 +import java.util.Set;
    6.48 +import javax.ws.rs.ext.ContextResolver;
    6.49 +import javax.ws.rs.ext.Provider;
    6.50 +import javax.xml.bind.JAXBContext;
    6.51 +
    6.52 +/**
    6.53 + *
    6.54 + * @author japod
    6.55 + */
    6.56 +@Provider
    6.57 +public final class JAXBContextResolver implements ContextResolver<JAXBContext> {
    6.58 +    
    6.59 +    private final JAXBContext context;
    6.60 +    
    6.61 +    private final Set<Class> types;
    6.62 +    
    6.63 +    private final Class[] cTypes = {Game.class };
    6.64 +    
    6.65 +    public JAXBContextResolver() throws Exception {
    6.66 +        this.types = new HashSet(Arrays.asList(cTypes));
    6.67 +        this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes);
    6.68 +    }
    6.69 +    
    6.70 +    public JAXBContext getContext(Class<?> objectType) {
    6.71 +        return (types.contains(objectType)) ? context : null;
    6.72 +    }
    6.73 +}
    6.74 \ No newline at end of file
     7.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/UserDetailsImpl.java	Sun Jul 12 13:35:58 2009 +0200
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,53 +0,0 @@
     7.4 -/*
     7.5 - * To change this template, choose Tools | Templates
     7.6 - * and open the template in the editor.
     7.7 - */
     7.8 -
     7.9 -package cz.xelfi.quoridor.webidor;
    7.10 -
    7.11 -import org.springframework.dao.DataAccessException;
    7.12 -import org.springframework.security.GrantedAuthority;
    7.13 -import org.springframework.security.GrantedAuthorityImpl;
    7.14 -import org.springframework.security.userdetails.UserDetails;
    7.15 -import org.springframework.security.userdetails.UserDetailsService;
    7.16 -import org.springframework.security.userdetails.UsernameNotFoundException;
    7.17 -
    7.18 -/**
    7.19 - *
    7.20 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    7.21 - */
    7.22 -public class UserDetailsImpl implements UserDetailsService {
    7.23 -
    7.24 -    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    7.25 -        return new UserDetails() {
    7.26 -            public GrantedAuthority[] getAuthorities() {
    7.27 -                return new GrantedAuthority[] { new GrantedAuthorityImpl("ROLE_USER") };
    7.28 -            }
    7.29 -
    7.30 -            public String getPassword() {
    7.31 -                return "ok";
    7.32 -            }
    7.33 -
    7.34 -            public String getUsername() {
    7.35 -                return "jarda";
    7.36 -            }
    7.37 -
    7.38 -            public boolean isAccountNonExpired() {
    7.39 -                return true;
    7.40 -            }
    7.41 -
    7.42 -            public boolean isAccountNonLocked() {
    7.43 -                return true;
    7.44 -            }
    7.45 -
    7.46 -            public boolean isCredentialsNonExpired() {
    7.47 -                return true;
    7.48 -            }
    7.49 -
    7.50 -            public boolean isEnabled() {
    7.51 -                return true;
    7.52 -            }
    7.53 -        };
    7.54 -    }
    7.55 -
    7.56 -}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sun Jul 12 13:59:07 2009 +0200
     8.3 @@ -0,0 +1,122 @@
     8.4 +/*
     8.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     8.6 + *
     8.7 + * The contents of this file are subject to the terms of either the GNU
     8.8 + * General Public License Version 2 only ("GPL") or the Common
     8.9 + * Development and Distribution License("CDDL") (collectively, the
    8.10 + * "License"). You may not use this file except in compliance with the
    8.11 + * License. You can obtain a copy of the License at
    8.12 + * http://www.netbeans.org/cddl-gplv2.html
    8.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    8.14 + * specific language governing permissions and limitations under the
    8.15 + * License.  When distributing the software, include this License Header
    8.16 + * Notice in each file and include the License file at
    8.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    8.18 + * particular file as subject to the "Classpath" exception as provided
    8.19 + * by Sun in the GPL Version 2 section of the License file that
    8.20 + * accompanied this code. If applicable, add the following below the
    8.21 + * License Header, with the fields enclosed by brackets [] replaced by
    8.22 + * your own identifying information:
    8.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    8.24 + *
    8.25 + * Contributor(s):
    8.26 + *
    8.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    8.28 + */
    8.29 +
    8.30 +package cz.xelfi.quoridor.webidor.resources;
    8.31 +
    8.32 +import com.sun.jersey.api.container.httpserver.HttpServerFactory;
    8.33 +import com.sun.jersey.api.core.PackagesResourceConfig;
    8.34 +import com.sun.jersey.api.core.ResourceConfig;
    8.35 +import cz.xelfi.quoridor.IllegalPositionException;
    8.36 +import cz.xelfi.quoridor.webidor.*;
    8.37 +import com.sun.jersey.spi.resource.Singleton;
    8.38 +import com.sun.net.httpserver.HttpServer;
    8.39 +import cz.xelfi.quoridor.Move;
    8.40 +import java.io.IOException;
    8.41 +import java.util.ArrayList;
    8.42 +import java.util.HashMap;
    8.43 +import java.util.List;
    8.44 +import java.util.Map;
    8.45 +import javax.ws.rs.GET;
    8.46 +import javax.ws.rs.POST;
    8.47 +import javax.ws.rs.PUT;
    8.48 +import javax.ws.rs.Path;
    8.49 +import javax.ws.rs.PathParam;
    8.50 +import javax.ws.rs.Produces;
    8.51 +import javax.ws.rs.QueryParam;
    8.52 +import javax.ws.rs.core.MediaType;
    8.53 +
    8.54 +/**
    8.55 + *
    8.56 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    8.57 + */
    8.58 +@Path("/")
    8.59 +@Singleton
    8.60 +public final class Quoridor {
    8.61 +    private List<Game> games = new ArrayList<Game>();
    8.62 +
    8.63 +    @POST
    8.64 +    @Path("games")
    8.65 +    public Game createGame(@QueryParam("white") String user1, @QueryParam("black") String user2) {
    8.66 +        Game g = new Game(user1, user2);
    8.67 +        games.add(g);
    8.68 +        return g;
    8.69 +    }
    8.70 +
    8.71 +    @PUT
    8.72 +    @Path("games/{id}")
    8.73 +    public Game applyMove(@PathParam("id") String id, @QueryParam("player") String player, @QueryParam("move") String move)
    8.74 +    throws IllegalPositionException {
    8.75 +        Game g = findGame(id);
    8.76 +        if (g == null) {
    8.77 +            throw new IllegalArgumentException("Unknown game " + id);
    8.78 +        }
    8.79 +        Move m = Move.valueOf(move);
    8.80 +        g.apply(player, m);
    8.81 +        return g;
    8.82 +    }
    8.83 +
    8.84 +    @GET
    8.85 +    @Produces(MediaType.APPLICATION_JSON)
    8.86 +    @Path("games")
    8.87 +    public List<Game> getGames() {
    8.88 +        return games;
    8.89 +    }
    8.90 +
    8.91 +    private Game findGame(String id) {
    8.92 +        for (Game g : games) {
    8.93 +            if (g.getId().equals(id)) {
    8.94 +                return g;
    8.95 +            }
    8.96 +        }
    8.97 +        return null;
    8.98 +    }
    8.99 +
   8.100 +
   8.101 +    //
   8.102 +    // start the server
   8.103 +    //
   8.104 +
   8.105 +    public static void main(String[] args) throws IOException {
   8.106 +
   8.107 +        final String baseUri = "http://localhost:9998/";
   8.108 +        final Map<String, String> initParams = new HashMap<String, String>();
   8.109 +
   8.110 +        initParams.put("com.sun.jersey.config.property.packages",
   8.111 +                "cz.xelfi.quoridor.webidor.resources");
   8.112 +
   8.113 +        System.out.println("Starting HttpServer...");
   8.114 +        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
   8.115 +        HttpServer threadSelector = HttpServerFactory.create(baseUri, rc);
   8.116 +        threadSelector.start();
   8.117 +        System.out.println(String.format(
   8.118 +                    "Jersey app started with WADL available at %sapplication.wadl\n" +
   8.119 +            "Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
   8.120 +            System.in.read();
   8.121 +            threadSelector.stop(0);
   8.122 +            System.exit(0);
   8.123 +    }
   8.124 +
   8.125 +}
     9.1 --- a/webidor/src/main/webapp/WEB-INF/applicationContext.xml	Sun Jul 12 13:35:58 2009 +0200
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,41 +0,0 @@
     9.4 -<?xml version="1.0" encoding="UTF-8"?>
     9.5 -<beans xmlns="http://www.springframework.org/schema/beans"
     9.6 -       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     9.7 -       xmlns:p="http://www.springframework.org/schema/p"
     9.8 -       xmlns:aop="http://www.springframework.org/schema/aop"
     9.9 -       xmlns:tx="http://www.springframework.org/schema/tx"
    9.10 -       xmlns:security="http://www.springframework.org/schema/security"
    9.11 -       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    9.12 -       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    9.13 -       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    9.14 -       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
    9.15 -
    9.16 -    <!--bean id="propertyConfigurer"
    9.17 -          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    9.18 -          p:location="/WEB-INF/jdbc.properties" />
    9.19 -    
    9.20 -    <bean id="dataSource"
    9.21 -          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    9.22 -          p:driverClassName="${jdbc.driverClassName}"
    9.23 -          p:url="${jdbc.url}"
    9.24 -          p:username="${jdbc.username}"
    9.25 -          p:password="${jdbc.password}" /-->
    9.26 -    
    9.27 -    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    9.28 -
    9.29 -    <security:http>
    9.30 -        <security:intercept-url pattern="/login.html" filters="none"/>
    9.31 -        <security:intercept-url pattern="/**" access="ROLE_USER"/>
    9.32 -        <!-- <security:form-login/> -->
    9.33 -        <security:anonymous/>
    9.34 -        <!-- <security:http-basic/> -->
    9.35 -        <security:logout/>
    9.36 -        <security:remember-me/>
    9.37 -        <security:openid-login/>
    9.38 -    </security:http>
    9.39 -
    9.40 -    <bean id="quoridorUserDetailsService" class="cz.xelfi.quoridor.webidor.UserDetailsImpl"/>
    9.41 -
    9.42 -    <security:authentication-provider user-service-ref='quoridorUserDetailsService'>
    9.43 -    </security:authentication-provider>
    9.44 -</beans>
    10.1 --- a/webidor/src/main/webapp/WEB-INF/dispatcher-servlet.xml	Sun Jul 12 13:35:58 2009 +0200
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,39 +0,0 @@
    10.4 -<?xml version="1.0" encoding="UTF-8"?>
    10.5 -<beans xmlns="http://www.springframework.org/schema/beans"
    10.6 -       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    10.7 -       xmlns:p="http://www.springframework.org/schema/p"
    10.8 -       xmlns:aop="http://www.springframework.org/schema/aop"
    10.9 -       xmlns:tx="http://www.springframework.org/schema/tx"
   10.10 -       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   10.11 -       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   10.12 -http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   10.13 -    
   10.14 -    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
   10.15 -    
   10.16 -    <!--
   10.17 -    Most controllers will use the ControllerClassNameHandlerMapping above, but
   10.18 -    for the index controller we are using ParameterizableViewController, so we must
   10.19 -    define an explicit mapping for it.
   10.20 -    -->
   10.21 -    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
   10.22 -        <property name="mappings">
   10.23 -            <props>
   10.24 -                <prop key="index.html">indexController</prop>
   10.25 -            </props>
   10.26 -        </property>
   10.27 -    </bean>
   10.28 -    
   10.29 -    <bean id="viewResolver"
   10.30 -          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
   10.31 -          p:prefix="/WEB-INF/jsp/"
   10.32 -          p:suffix=".jsp" />
   10.33 -    
   10.34 -    <!--
   10.35 -    The index controller.
   10.36 -    -->
   10.37 -    <bean name="indexController"
   10.38 -          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
   10.39 -          p:viewName="index" />
   10.40 -
   10.41 -
   10.42 -</beans>
    11.1 --- a/webidor/src/main/webapp/WEB-INF/jsp/index.jsp	Sun Jul 12 13:35:58 2009 +0200
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,19 +0,0 @@
    11.4 -<%@page contentType="text/html" pageEncoding="UTF-8"%>
    11.5 -<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    11.6 -   "http://www.w3.org/TR/html4/loose.dtd">
    11.7 -
    11.8 -<html>
    11.9 -    <head>
   11.10 -        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   11.11 -        <title>Welcome to Spring Web MVC project</title>
   11.12 -    </head>
   11.13 -    
   11.14 -    <body>
   11.15 -        <p>Hello ${id}! This is the default welcome page for a Spring Web MVC project.</p>
   11.16 -        <p><i>To display a different welcome page for this project, modify</i>
   11.17 -          <tt>index.jsp</tt> <i>, or create your own welcome page then change
   11.18 -          the redirection in</i> <tt>redirect.jsp</tt> <i>to point to the new
   11.19 -          welcome page and also update the welcome-file setting in</i>
   11.20 -          <tt>web.xml</tt>.</p>
   11.21 -    </body>
   11.22 -</html>
    12.1 --- a/webidor/src/main/webapp/WEB-INF/jsp/login.jsp	Sun Jul 12 13:35:58 2009 +0200
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,18 +0,0 @@
    12.4 -<%@page contentType="text/html" pageEncoding="UTF-8"%>
    12.5 -<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    12.6 -   "http://www.w3.org/TR/html4/loose.dtd">
    12.7 -
    12.8 -<html>
    12.9 -    <head>
   12.10 -        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   12.11 -        <title>Login</title>
   12.12 -    </head>
   12.13 -    <body>
   12.14 -        <h1>Login</h1>
   12.15 -
   12.16 -        <form action="login.html" method="post">
   12.17 -            <a href="http://en.wikipedia.org/wiki/Openid">Open ID</a>:
   12.18 -            <input type="text" name="openid" value="http://"/>
   12.19 -        </form>
   12.20 -    </body>
   12.21 -</html>
    13.1 --- a/webidor/src/main/webapp/WEB-INF/jsp/loginredirection.jsp	Sun Jul 12 13:35:58 2009 +0200
    13.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.3 @@ -1,20 +0,0 @@
    13.4 -<%--
    13.5 -  ~ Copyright 2006-2008 Sxip Identity Corporation
    13.6 -  --%>
    13.7 -  
    13.8 -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    13.9 -"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   13.10 -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
   13.11 -<html xmlns="http://www.w3.org/1999/xhtml">
   13.12 -<head>
   13.13 -    <title>OpenID HTML FORM Redirection</title>
   13.14 -</head>
   13.15 -<body onload="document.forms['openid-form-redirection'].submit();">
   13.16 -    <form name="openid-form-redirection" action="${OPEndpoint}" method="post" accept-charset="utf-8">
   13.17 -        <c:forEach var="parameter" items="${message}">
   13.18 -        <input type="hidden" name="${parameter.key}" value="${parameter.value}"/>
   13.19 -        </c:forEach>
   13.20 -        <button type="submit">Continue...</button>
   13.21 -    </form>
   13.22 -</body>
   13.23 -</html>
    14.1 --- a/webidor/src/main/webapp/WEB-INF/sun-web.xml	Sun Jul 12 13:35:58 2009 +0200
    14.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.3 @@ -1,11 +0,0 @@
    14.4 -<?xml version="1.0" encoding="UTF-8"?>
    14.5 -<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
    14.6 -<sun-web-app error-url="">
    14.7 -  <class-loader delegate="true"/>
    14.8 -  <jsp-config>
    14.9 -    <property name="keepgenerated" value="true">
   14.10 -      <description>Keep a copy of the generated servlet class' java code.</description>
   14.11 -    </property>
   14.12 -  </jsp-config>
   14.13 -</sun-web-app>
   14.14 -
    15.1 --- a/webidor/src/main/webapp/WEB-INF/web.xml	Sun Jul 12 13:35:58 2009 +0200
    15.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.3 @@ -1,37 +0,0 @@
    15.4 -<?xml version="1.0" encoding="UTF-8"?>
    15.5 -<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    15.6 -    <display-name>webidor</display-name>
    15.7 -    <context-param>
    15.8 -        <param-name>contextConfigLocation</param-name>
    15.9 -        <param-value>/WEB-INF/applicationContext.xml</param-value>
   15.10 -    </context-param>
   15.11 -    <filter>
   15.12 -      <filter-name>springSecurityFilterChain</filter-name>
   15.13 -      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
   15.14 -    </filter>
   15.15 -
   15.16 -    <filter-mapping>
   15.17 -      <filter-name>springSecurityFilterChain</filter-name>
   15.18 -      <url-pattern>/*</url-pattern>
   15.19 -    </filter-mapping>
   15.20 -    <listener>
   15.21 -        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   15.22 -    </listener>
   15.23 -    <servlet>
   15.24 -        <servlet-name>dispatcher</servlet-name>
   15.25 -        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   15.26 -        <load-on-startup>2</load-on-startup>
   15.27 -    </servlet>
   15.28 -    <servlet-mapping>
   15.29 -        <servlet-name>dispatcher</servlet-name>
   15.30 -        <url-pattern>*.html</url-pattern>
   15.31 -    </servlet-mapping>
   15.32 -    <session-config>
   15.33 -        <session-timeout>
   15.34 -            30
   15.35 -        </session-timeout>
   15.36 -    </session-config>
   15.37 -    <welcome-file-list>
   15.38 -        <welcome-file>index.jsp</welcome-file>
   15.39 -    </welcome-file-list>
   15.40 -</web-app>
    16.1 --- a/webidor/src/main/webapp/index.jsp	Sun Jul 12 13:35:58 2009 +0200
    16.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.3 @@ -1,13 +0,0 @@
    16.4 -<%@page contentType="text/html" pageEncoding="UTF-8"%>
    16.5 -<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    16.6 -   "http://www.w3.org/TR/html4/loose.dtd">
    16.7 -
    16.8 -<html>
    16.9 -    <head>
   16.10 -        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   16.11 -        <title>JSP Page</title>
   16.12 -    </head>
   16.13 -    <body>
   16.14 -        <h1>Hello World!</h1>
   16.15 -    </body>
   16.16 -</html>
    17.1 --- a/webidor/src/main/webapp/redirect.jsp	Sun Jul 12 13:35:58 2009 +0200
    17.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3 @@ -1,9 +0,0 @@
    17.4 -<%--
    17.5 -Views should be stored under the WEB-INF folder so that
    17.6 -they are not accessible except through controller process.
    17.7 -
    17.8 -This JSP is here to provide a redirect to the dispatcher
    17.9 -servlet but should be the only JSP outside of WEB-INF.
   17.10 ---%>
   17.11 -<%@page contentType="text/html" pageEncoding="UTF-8"%>
   17.12 -<% response.sendRedirect("index.html"); %>
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Sun Jul 12 13:59:07 2009 +0200
    18.3 @@ -0,0 +1,88 @@
    18.4 +/*
    18.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    18.6 + *
    18.7 + * The contents of this file are subject to the terms of either the GNU
    18.8 + * General Public License Version 2 only ("GPL") or the Common
    18.9 + * Development and Distribution License("CDDL") (collectively, the
   18.10 + * "License"). You may not use this file except in compliance with the
   18.11 + * License. You can obtain a copy of the License at
   18.12 + * http://www.netbeans.org/cddl-gplv2.html
   18.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
   18.14 + * specific language governing permissions and limitations under the
   18.15 + * License.  When distributing the software, include this License Header
   18.16 + * Notice in each file and include the License file at
   18.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
   18.18 + * particular file as subject to the "Classpath" exception as provided
   18.19 + * by Sun in the GPL Version 2 section of the License file that
   18.20 + * accompanied this code. If applicable, add the following below the
   18.21 + * License Header, with the fields enclosed by brackets [] replaced by
   18.22 + * your own identifying information:
   18.23 + * "Portions Copyrighted [year] [name of copyright owner]"
   18.24 + *
   18.25 + * Contributor(s):
   18.26 + *
   18.27 + * Portions Copyrighted 2009 Jaroslav Tulach
   18.28 + */
   18.29 +
   18.30 +package cz.xelfi.quoridor.webidor;
   18.31 +
   18.32 +import com.sun.jersey.api.client.GenericType;
   18.33 +import com.sun.jersey.api.client.UniformInterfaceException;
   18.34 +import com.sun.jersey.core.header.MediaTypes;
   18.35 +import com.sun.jersey.test.framework.JerseyTest;
   18.36 +import java.util.List;
   18.37 +import org.junit.Test;
   18.38 +import static org.junit.Assert.*;
   18.39 +
   18.40 +/**
   18.41 + *
   18.42 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   18.43 + */
   18.44 +public class QuoridorTest extends JerseyTest {
   18.45 +
   18.46 +    public QuoridorTest() throws Exception {
   18.47 +        super("cz.xelfi.quoridor.webidor.resources");
   18.48 +    }
   18.49 +
   18.50 +    /**
   18.51 +     * Test if a WADL document is available at the relative path
   18.52 +     * "application.wadl".
   18.53 +     */
   18.54 +    @Test public void testApplicationWadl() {
   18.55 +        String serviceWadl = webResource.path("application.wadl").
   18.56 +                accept(MediaTypes.WADL).get(String.class);
   18.57 +        assertTrue(serviceWadl.length() > 0);
   18.58 +    }
   18.59 +
   18.60 +    @Test public void testCreateAGame() {
   18.61 +        Game s = webResource.path("games").queryParam("white", "Jarda")
   18.62 +                .queryParam("black", "Jirka").post(Game.class);
   18.63 +
   18.64 +        String msg = webResource.path("games").get(String.class);
   18.65 +        //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
   18.66 +
   18.67 +        GenericType<List<Game>> gType = new GenericType<List<Game>>() {};
   18.68 +
   18.69 +        List<Game> games = webResource.path("games").accept("application/json").get(gType);
   18.70 +        assertEquals("One game", 1, games.size());
   18.71 +        assertEquals("Same white", "Jarda", games.get(0).getWhite());
   18.72 +        assertEquals("Same black", "Jirka", games.get(0).getBlack());
   18.73 +
   18.74 +        Game s1 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
   18.75 +        try {
   18.76 +            Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
   18.77 +            fail("Not Jarda's turn, previous call shall fail");
   18.78 +        } catch (UniformInterfaceException ex) {
   18.79 +            // OK
   18.80 +        }
   18.81 +        try {
   18.82 +            Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(Game.class);
   18.83 +            fail("Invalid move");
   18.84 +        } catch (UniformInterfaceException ex) {
   18.85 +            // OK
   18.86 +        }
   18.87 +        Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(Game.class);
   18.88 +        assertNotNull("Successful move", s2);
   18.89 +    }
   18.90 +
   18.91 +}