freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/Requests.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 04 Mar 2010 19:37:58 +0100
changeset 233 ecddc9f373bb
child 264 d60370059c3c
permissions -rw-r--r--
Changing an email needs to be confirmed by receiving an email and responding to an URL send inside it
jaroslav@233
     1
/*
jaroslav@233
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@233
     3
 *
jaroslav@233
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@233
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@233
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@233
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@233
     8
 * License. You can obtain a copy of the License at
jaroslav@233
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@233
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@233
    11
 * specific language governing permissions and limitations under the
jaroslav@233
    12
 * License.  When distributing the software, include this License Header
jaroslav@233
    13
 * Notice in each file and include the License file at
jaroslav@233
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@233
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@233
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@233
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@233
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@233
    19
 * your own identifying information:
jaroslav@233
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@233
    21
 *
jaroslav@233
    22
 * Contributor(s):
jaroslav@233
    23
 *
jaroslav@233
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@233
    25
 */
jaroslav@233
    26
jaroslav@233
    27
package cz.xelfi.quoridor.freemarkerdor;
jaroslav@233
    28
jaroslav@233
    29
import com.sun.jersey.api.client.WebResource;
jaroslav@233
    30
import com.sun.jersey.api.client.WebResource.Builder;
jaroslav@233
    31
import java.net.URI;
jaroslav@233
    32
import java.util.HashMap;
jaroslav@233
    33
import java.util.Map;
jaroslav@233
    34
import java.util.UUID;
jaroslav@233
    35
import javax.ws.rs.GET;
jaroslav@233
    36
import javax.ws.rs.Path;
jaroslav@233
    37
import javax.ws.rs.PathParam;
jaroslav@233
    38
import javax.ws.rs.Produces;
jaroslav@233
    39
import javax.ws.rs.core.Cookie;
jaroslav@233
    40
import javax.ws.rs.core.MediaType;
jaroslav@233
    41
import javax.ws.rs.core.Response;
jaroslav@233
    42
jaroslav@233
    43
/** Keeps "requests". A request is UUID that maps to a URL which can
jaroslav@233
    44
 * be executed later.
jaroslav@233
    45
 *
jaroslav@233
    46
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@233
    47
 */
jaroslav@233
    48
public final class Requests extends Object {
jaroslav@233
    49
    private final Map<UUID,Builder> requests = new HashMap<UUID, Builder>();
jaroslav@233
    50
    private final WebResource root;
jaroslav@233
    51
jaroslav@233
    52
    Requests(WebResource path) {
jaroslav@233
    53
        root = path;
jaroslav@233
    54
    }
jaroslav@233
    55
jaroslav@233
    56
    @GET
jaroslav@233
    57
    @Path("{id}")
jaroslav@233
    58
    @Produces(MediaType.TEXT_HTML)
jaroslav@233
    59
    public Response redirect(@PathParam("id") String uuid) {
jaroslav@233
    60
        UUID r = UUID.fromString(uuid);
jaroslav@233
    61
        Builder web = requests.get(r);
jaroslav@233
    62
        if (web == null) {
jaroslav@233
    63
            return Response.status(Response.Status.NOT_FOUND).build();
jaroslav@233
    64
        }
jaroslav@233
    65
        return Response.ok(web.get(String.class)).build();
jaroslav@233
    66
    }
jaroslav@233
    67
jaroslav@233
    68
    boolean isVerified(String uuid) {
jaroslav@233
    69
        return uuid != null && requests.containsKey(UUID.fromString(uuid));
jaroslav@233
    70
    }
jaroslav@233
    71
jaroslav@233
    72
    URI register(String login, WebResource request) {
jaroslav@233
    73
        UUID uuid = UUID.randomUUID();
jaroslav@233
    74
        WebResource callback = root.path(uuid.toString());
jaroslav@233
    75
        Builder builder = request.queryParam("verified", uuid.toString()).
jaroslav@233
    76
            cookie(Cookie.valueOf("login=" + login));
jaroslav@233
    77
        requests.put(uuid, builder);
jaroslav@233
    78
        return callback.getURI();
jaroslav@233
    79
    }
jaroslav@233
    80
jaroslav@233
    81
}