webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 17 Jul 2011 15:40:50 +0200
branchglassfish
changeset 284 90be53f96e0c
parent 282 40fc213a7f43
permissions -rw-r--r--
webidor rewritten to run as glassfish application
jtulach@35
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@35
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jtulach@35
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jtulach@35
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jtulach@35
    17
 */
jtulach@35
    18
jtulach@35
    19
package cz.xelfi.quoridor.webidor.resources;
jtulach@35
    20
jaroslav@189
    21
import com.sun.jersey.api.json.JSONWithPadding;
jtulach@37
    22
import java.io.File;
jtulach@82
    23
import java.io.FileInputStream;
jtulach@35
    24
import java.io.IOException;
jtulach@82
    25
import java.util.HashMap;
jtulach@82
    26
import java.util.Map;
jtulach@82
    27
import java.util.Properties;
jtulach@82
    28
import java.util.UUID;
jaroslav@284
    29
import javax.annotation.ManagedBean;
jaroslav@284
    30
import javax.inject.Singleton;
jtulach@82
    31
import javax.ws.rs.GET;
jtulach@82
    32
import javax.ws.rs.PUT;
jtulach@35
    33
import javax.ws.rs.Path;
jtulach@82
    34
import javax.ws.rs.Produces;
jtulach@82
    35
import javax.ws.rs.QueryParam;
jaroslav@189
    36
import javax.ws.rs.WebApplicationException;
jtulach@82
    37
import javax.ws.rs.core.MediaType;
jaroslav@189
    38
import javax.ws.rs.core.Response.Status;
jtulach@35
    39
jtulach@35
    40
/**
jtulach@35
    41
 *
jtulach@35
    42
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@35
    43
 */
jaroslav@124
    44
@Path("/")
jtulach@35
    45
@Singleton
jaroslav@284
    46
@ManagedBean
jaroslav@284
    47
public class Quoridor {
jtulach@37
    48
    private final File path;
jtulach@37
    49
    private Games games;
jaroslav@143
    50
    private Users users;
jtulach@82
    51
    private final Map<UUID,String> loggedIn;
jtulach@37
    52
jtulach@37
    53
    public Quoridor() {
jaroslav@284
    54
        String prop = System.getProperty("quoridor.dir"); // NOI18N
jtulach@37
    55
        if (prop == null) {
jaroslav@284
    56
            prop = System.getProperty("user.dir") + File.separatorChar + ".quoridor"; // NOI18N
jtulach@37
    57
        }
jtulach@37
    58
        path = new File(prop);
jtulach@37
    59
        path.mkdirs();
jtulach@82
    60
        loggedIn = new HashMap<UUID, String>();
jaroslav@189
    61
        games = new Games(new File(path, "games"), this); // NOI18N
jaroslav@189
    62
        users = new Users(new File(path, "users"), this); // NOI18N
jtulach@37
    63
    }
jtulach@35
    64
jtulach@35
    65
    @Path("games")
jtulach@36
    66
    public Games getGames() {
jtulach@35
    67
        return games;
jtulach@35
    68
    }
jtulach@35
    69
jaroslav@143
    70
    @Path("users")
jaroslav@143
    71
    public Users getUsers() {
jaroslav@143
    72
        return users;
jaroslav@143
    73
    }
jaroslav@143
    74
jtulach@82
    75
    @Path("login")
jtulach@82
    76
    @PUT
jaroslav@145
    77
    @Produces({ MediaType.TEXT_PLAIN })
jtulach@82
    78
    public String login(
jtulach@82
    79
        @QueryParam("name") String name,
jtulach@82
    80
        @QueryParam("password") String password
jtulach@239
    81
    ) throws IOException {
jtulach@82
    82
        File f = new File(path, "passwd"); // NOI18Nt
jtulach@82
    83
        Properties p = new Properties();
jtulach@82
    84
        try {
jtulach@82
    85
            p.load(new FileInputStream(f));
jtulach@82
    86
        } catch (IOException ex) {
jtulach@82
    87
            ex.printStackTrace();
jtulach@82
    88
        }
jtulach@239
    89
        boolean loggedInOK = false;
jtulach@82
    90
        if (name != null && password.equals(p.getProperty(name))) {
jtulach@239
    91
            loggedInOK = true;
jtulach@239
    92
        } else {
jtulach@239
    93
            loggedInOK = getUsers().verifyPassword(name, password);
jtulach@239
    94
        }
jtulach@239
    95
jtulach@239
    96
        if (loggedInOK) {
jtulach@82
    97
            UUID uuid = UUID.randomUUID();
jtulach@82
    98
            loggedIn.put(uuid, name);
jtulach@82
    99
            return uuid.toString();
jtulach@82
   100
        } else {
jtulach@82
   101
            return null;
jtulach@82
   102
        }
jtulach@82
   103
        
jtulach@82
   104
    }
jtulach@82
   105
jtulach@82
   106
    @Path("login")
jtulach@82
   107
    @GET
jaroslav@145
   108
    @Produces({ MediaType.TEXT_PLAIN })
jtulach@82
   109
    public String isLoggedIn(
jtulach@82
   110
        @QueryParam("id") String id
jtulach@82
   111
    ) {
jaroslav@85
   112
        String ret = null;
jtulach@83
   113
        try {
jaroslav@85
   114
            if (id != null) {
jaroslav@85
   115
                ret = loggedIn.get(UUID.fromString(id));
jaroslav@85
   116
            }
jtulach@83
   117
        } catch (IllegalArgumentException ex) {
jaroslav@85
   118
            // OK, happens for invalid ids
jtulach@83
   119
        }
jaroslav@85
   120
        return ret == null ? "" : ret;
jtulach@82
   121
    }
jtulach@82
   122
jaroslav@145
   123
    @Path("login")
jaroslav@145
   124
    @GET
jaroslav@189
   125
    @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@189
   126
    public JSONWithPadding loggedInInfo(
jaroslav@189
   127
        @QueryParam("callback") String callback,
jaroslav@145
   128
        @QueryParam("id") String id
jaroslav@145
   129
    ) throws IOException {
jaroslav@145
   130
        String ret = null;
jaroslav@145
   131
        try {
jaroslav@145
   132
            if (id != null) {
jaroslav@145
   133
                ret = loggedIn.get(UUID.fromString(id));
jaroslav@145
   134
            }
jaroslav@145
   135
        } catch (IllegalArgumentException ex) {
jaroslav@145
   136
            // OK, happens for invalid ids
jaroslav@145
   137
        }
jaroslav@189
   138
        if (ret == null) {
jaroslav@189
   139
            throw new WebApplicationException(Status.UNAUTHORIZED);
jaroslav@189
   140
        } else {
jaroslav@189
   141
            return getUsers().getUserInfo(callback, id, ret);
jaroslav@189
   142
        }
jaroslav@145
   143
    }
jtulach@35
   144
}