2 * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.aaa.idm.rest;
13 * @author peter.mellquist@hp.com
17 import javax.servlet.http.HttpServletRequest;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.DELETE;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.PathParam;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.UriInfo;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import java.util.List;
32 import java.util.ArrayList;
33 import org.opendaylight.aaa.idm.model.Users;
34 import org.opendaylight.aaa.idm.model.User;
35 import org.opendaylight.aaa.idm.model.IDMError;
36 import org.opendaylight.aaa.idm.persistence.UserStore;
37 import org.opendaylight.aaa.idm.persistence.StoreException;
38 import org.opendaylight.aaa.idm.IdmLightProxy;
41 public class UserHandler {
42 private static Logger logger = LoggerFactory.getLogger(UserHandler.class);
43 private static UserStore userStore = new UserStore();
44 protected final static String DEFAULT_PWD = "changeme";
47 @Produces("application/json")
48 public Response getUsers() {
49 logger.info("get all users");
52 users = userStore.getUsers();
54 catch (StoreException se) {
55 return new IDMError(500,"internal error getting users",se.message).response();
59 for (int z=0;z<users.getUsers().size();z++)
60 users.getUsers().get(z).setPassword("**********");
62 return Response.ok(users).build();
68 @Produces("application/json")
69 public Response getUser(@PathParam("id") String id) {
70 logger.info("get /users/" + id);
74 longId=Long.parseLong(id);
76 catch (NumberFormatException nfe) {
77 return new IDMError(400,"invalid user id :" + id,"").response();
80 user = userStore.getUser(longId);
82 catch(StoreException se) {
83 return new IDMError(500,"internal error getting user",se.message).response();
86 return new IDMError(404,"user not found! id:" + id,"").response();
89 user.setPassword("*********");
90 return Response.ok(user).build();
94 @Consumes("application/json")
95 @Produces("application/json")
96 public Response createUser(@Context UriInfo info,User user) {
97 logger.info("post /users");
100 if (user.getEnabled()==null)
101 user.setEnabled(true);
103 // user name is required
104 if (user.getName()==null)
105 return new IDMError(400,"user name is required","").response();
107 if (user.getName().length()>UserStore.MAX_FIELD_LEN)
108 return new IDMError(400,"user name max length is :" + UserStore.MAX_FIELD_LEN,"").response();
110 // user description is optional
111 if (user.getDescription()==null)
112 user.setDescription("");
114 if (user.getDescription().length()>UserStore.MAX_FIELD_LEN)
115 return new IDMError(400,"user description max length is :" + UserStore.MAX_FIELD_LEN,"").response();
117 // user email is optional
118 if (user.getEmail()==null)
121 if (user.getEmail().length()>UserStore.MAX_FIELD_LEN)
122 return new IDMError(400,"user email max length is :" + UserStore.MAX_FIELD_LEN,"").response();
124 // user password optional and will default if not provided
125 if (user.getPassword()==null)
126 user.setPassword(DEFAULT_PWD);
128 if (user.getPassword().length()>UserStore.MAX_FIELD_LEN)
129 return new IDMError(400,"user password max length is :" + UserStore.MAX_FIELD_LEN,"").response();
132 user = userStore.createUser(user);
134 catch (StoreException se) {
135 return new IDMError(500,"internal error creating user",se.message).response();
139 return Response.status(201).entity(user).build();
145 @Consumes("application/json")
146 @Produces("application/json")
147 public Response putUser(@Context UriInfo info,User user,@PathParam("id") String id) {
149 logger.info("put /users/" + id);
151 longId= Long.parseLong(id);
153 catch (NumberFormatException nfe) {
154 return new IDMError(400,"invalid user id:"+id,"").response();
158 user.setUserid((int)longId);
159 user = userStore.putUser(user);
161 return new IDMError(404,"user id not found id :"+id,"").response();
163 IdmLightProxy.clearClaimCache();
164 return Response.status(200).entity(user).build();
166 catch (StoreException se) {
167 return new IDMError(500,"internal error putting user",se.message).response();
173 public Response deleteUser(@Context UriInfo info,@PathParam("id") String id) {
175 logger.info("delete /users/" + id);
177 longId= Long.parseLong(id);
179 catch (NumberFormatException nfe) {
180 return new IDMError(400,"invalid user id:"+id,"").response();
184 User user = new User();
185 user.setUserid((int)longId);
186 user = userStore.deleteUser(user);
188 return new IDMError(404,"user id not found id :"+id,"").response();
191 catch (StoreException se) {
192 return new IDMError(500,"internal error deleting user",se.message).response();
194 IdmLightProxy.clearClaimCache();
195 return Response.status(204).build();