001/*
002 * (C) Copyright 2006 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Bogdan Stefanescu
018 */
019package org.nuxeo.ecm.webengine.samples;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.PathParam;
024import javax.ws.rs.Produces;
025import javax.ws.rs.QueryParam;
026
027import org.nuxeo.ecm.webengine.model.WebObject;
028import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
029
030/**
031 * UserManager object. You can see the @WebObject annotation that defines a WebObject of type "UserManager"
032 *
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035@WebObject(type = "UserManager")
036@Produces("text/html;charset=UTF-8")
037public class UserManagerObject extends DefaultObject {
038
039    /**
040     * Get the index view. The view file name is computed as follows: index[-media_type_id].ftl First the
041     * skin/views/UserManager is searched for that file then the current directory. (The type of a module is the same as
042     * its name)
043     */
044    @GET
045    public Object doGet() {
046        return getView("index");
047    }
048
049    /**
050     * A hack to accept users as user?name=xxx query parameters
051     */
052    @GET
053    @Path("user")
054    public Object getUserByQueryString(@QueryParam("name") String name) {
055        if (name == null) {
056            return doGet();
057        } else {
058            return redirect(getPath() + "/user/" + name);
059        }
060    }
061
062    /**
063     * Get the user JAX-RS resource given the user name
064     */
065    @Path("user/{name}")
066    public Object getUser(@PathParam("name") String name) {
067        // create a new instance of a WebObject which type is "User" and push this object on the request chain
068        // the User object is initialized with the String "Username: name"
069        return newObject("User", "Username: " + name);
070    }
071
072}