freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/Requests.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 233 ecddc9f373bb
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 package cz.xelfi.quoridor.freemarkerdor;
    19 
    20 import com.sun.jersey.api.client.WebResource;
    21 import com.sun.jersey.api.client.WebResource.Builder;
    22 import java.net.URI;
    23 import java.util.HashMap;
    24 import java.util.Map;
    25 import java.util.UUID;
    26 import javax.ws.rs.GET;
    27 import javax.ws.rs.Path;
    28 import javax.ws.rs.PathParam;
    29 import javax.ws.rs.Produces;
    30 import javax.ws.rs.core.Cookie;
    31 import javax.ws.rs.core.MediaType;
    32 import javax.ws.rs.core.Response;
    33 
    34 /** Keeps "requests". A request is UUID that maps to a URL which can
    35  * be executed later.
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 public final class Requests extends Object {
    40     private final Map<UUID,Builder> requests = new HashMap<UUID, Builder>();
    41     private final WebResource root;
    42 
    43     Requests(WebResource path) {
    44         root = path;
    45     }
    46 
    47     @GET
    48     @Path("{id}")
    49     @Produces(MediaType.TEXT_HTML)
    50     public Response redirect(@PathParam("id") String uuid) {
    51         UUID r = UUID.fromString(uuid);
    52         Builder web = requests.get(r);
    53         if (web == null) {
    54             return Response.status(Response.Status.NOT_FOUND).build();
    55         }
    56         return Response.ok(web.get(String.class)).build();
    57     }
    58 
    59     boolean isVerified(String uuid) {
    60         return uuid != null && requests.containsKey(UUID.fromString(uuid));
    61     }
    62 
    63     URI register(String login, WebResource request) {
    64         UUID uuid = UUID.randomUUID();
    65         WebResource callback = root.path(uuid.toString());
    66         Builder builder = request.queryParam("verified", uuid.toString()).
    67             cookie(Cookie.valueOf("login=" + login));
    68         requests.put(uuid, builder);
    69         return callback.getURI();
    70     }
    71 
    72 }