View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.buildtools.cxf;
20  
21  import java.util.Date;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Optional;
26  import java.util.UUID;
27  import java.util.stream.Collectors;
28  import java.util.stream.Stream;
29  import javax.ws.rs.ClientErrorException;
30  import javax.ws.rs.Consumes;
31  import javax.ws.rs.DELETE;
32  import javax.ws.rs.ForbiddenException;
33  import javax.ws.rs.GET;
34  import javax.ws.rs.NotFoundException;
35  import javax.ws.rs.POST;
36  import javax.ws.rs.PUT;
37  import javax.ws.rs.Path;
38  import javax.ws.rs.PathParam;
39  import javax.ws.rs.Produces;
40  import javax.ws.rs.QueryParam;
41  import javax.ws.rs.core.Context;
42  import javax.ws.rs.core.MediaType;
43  import javax.ws.rs.core.Response;
44  import javax.ws.rs.core.UriInfo;
45  
46  @Path("users")
47  public class UserService {
48  
49      private static final Map<UUID, UserMetadata> USERS = new HashMap<>();
50  
51      @Context
52      private UriInfo uriInfo;
53  
54      @GET
55      @Produces({ MediaType.APPLICATION_JSON })
56      public List<User> list() {
57          return USERS.values().stream().
58                  filter(meta -> !meta.isDeleted()).
59                  map(UserMetadata::getUser).
60                  collect(Collectors.toList());
61      }
62  
63      @GET
64      @Path("changelog")
65      @Produces({ MediaType.APPLICATION_JSON })
66      public List<UserMetadata> changelog(@QueryParam("from") final Date from) {
67          Stream<UserMetadata> users = USERS.values().stream();
68          if (from != null) {
69              users = users.filter(meta -> meta.getLastChangeDate().after(from));
70          }
71          return users.collect(Collectors.toList());
72      }
73  
74      @GET
75      @Path("{key}")
76      @Produces({ MediaType.APPLICATION_JSON })
77      public User read(@PathParam("key") final UUID key) {
78          UserMetadata meta = USERS.get(key);
79          if (meta == null || meta.isDeleted()) {
80              throw new NotFoundException(key.toString());
81          }
82          return meta.getUser();
83      }
84  
85      @POST
86      @Consumes({ MediaType.APPLICATION_JSON })
87      public Response create(final User user) {
88          if (user.getKey() == null) {
89              user.setKey(UUID.randomUUID());
90          }
91          if (user.getStatus() == null) {
92              user.setStatus(User.Status.ACTIVE);
93          }
94  
95          UserMetadata meta = USERS.get(user.getKey());
96          if (meta != null && !meta.isDeleted()) {
97              throw new ClientErrorException("User already exists: " + user.getKey(), Response.Status.CONFLICT);
98          }
99  
100         meta = new UserMetadata();
101         meta.setLastChangeDate(new Date());
102         meta.setUser(user);
103         USERS.put(user.getKey(), meta);
104 
105         return Response.created(uriInfo.getAbsolutePathBuilder().path(user.getKey().toString()).build()).build();
106     }
107 
108     @PUT
109     @Path("{key}")
110     @Consumes({ MediaType.APPLICATION_JSON })
111     public void update(@PathParam("key") final UUID key, final User updatedUser) {
112         UserMetadata meta = USERS.get(key);
113         if (meta == null || meta.isDeleted()) {
114             throw new NotFoundException(key.toString());
115         }
116 
117         if (updatedUser.getUsername() != null) {
118             meta.getUser().setUsername(updatedUser.getUsername());
119         }
120         if (updatedUser.getPassword() != null) {
121             meta.getUser().setPassword(updatedUser.getPassword());
122         }
123         if (updatedUser.getFirstName() != null) {
124             meta.getUser().setFirstName(updatedUser.getFirstName());
125         }
126         if (updatedUser.getSurname() != null) {
127             meta.getUser().setSurname(updatedUser.getSurname());
128         }
129         if (updatedUser.getEmail() != null) {
130             meta.getUser().setEmail(updatedUser.getEmail());
131         }
132         if (updatedUser.getStatus() != null) {
133             meta.getUser().setStatus(updatedUser.getStatus());
134         }
135 
136         meta.setLastChangeDate(new Date());
137     }
138 
139     @DELETE
140     @Path("{key}")
141     public void delete(@PathParam("key") final UUID key) {
142         UserMetadata meta = USERS.get(key);
143         if (meta == null || meta.isDeleted()) {
144             throw new NotFoundException(key.toString());
145         }
146 
147         meta.setDeleted(true);
148         meta.setLastChangeDate(new Date());
149     }
150 
151     @POST
152     @Path("authenticate")
153     @Produces({ MediaType.APPLICATION_JSON })
154     public User authenticate(
155             @QueryParam("username") final String username,
156             @QueryParam("password") final String password) {
157 
158         Optional<User> user = USERS.values().stream().
159                 filter(meta -> !meta.isDeleted() && username.equals(meta.getUser().getUsername())).
160                 findFirst().map(UserMetadata::getUser);
161 
162         if (user.isEmpty()) {
163             throw new NotFoundException(username);
164         }
165 
166         return user.filter(u -> password.equals(u.getPassword())).
167                 orElseThrow(() -> new ForbiddenException());
168     }
169 
170     @POST
171     @Path("clear")
172     public void clear() {
173         USERS.clear();
174     }
175 }