View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.kitchensink_ear.rest;
18  
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  import java.util.logging.Logger;
25  
26  import javax.enterprise.context.RequestScoped;
27  import javax.inject.Inject;
28  import javax.persistence.NoResultException;
29  import javax.validation.ConstraintViolation;
30  import javax.validation.ConstraintViolationException;
31  import javax.validation.ValidationException;
32  import javax.validation.Validator;
33  import javax.ws.rs.Consumes;
34  import javax.ws.rs.GET;
35  import javax.ws.rs.POST;
36  import javax.ws.rs.Path;
37  import javax.ws.rs.PathParam;
38  import javax.ws.rs.Produces;
39  import javax.ws.rs.WebApplicationException;
40  import javax.ws.rs.core.MediaType;
41  import javax.ws.rs.core.Response;
42  
43  import org.jboss.as.quickstarts.kitchensink_ear.data.MemberRepository;
44  import org.jboss.as.quickstarts.kitchensink_ear.model.Member;
45  import org.jboss.as.quickstarts.kitchensink_ear.service.MemberRegistration;
46  
47  /**
48   * JAX-RS Example
49   * <p/>
50   * This class produces a RESTful service to read/write the contents of the members table.
51   */
52  @Path("/members")
53  @RequestScoped
54  public class MemberResourceRESTService {
55      @Inject
56      private Logger log;
57  
58      @Inject
59      private Validator validator;
60  
61      @Inject
62      private MemberRepository repository;
63  
64      @Inject
65      MemberRegistration registration;
66  
67      @GET
68      @Produces(MediaType.APPLICATION_JSON)
69      public List<Member> listAllMembers() {
70          return repository.findAllOrderedByName();
71      }
72  
73      @GET
74      @Path("/{id:[0-9][0-9]*}")
75      @Produces(MediaType.APPLICATION_JSON)
76      public Member lookupMemberById(@PathParam("id") long id) {
77          Member member = repository.findById(id);
78          if (member == null) {
79              throw new WebApplicationException(Response.Status.NOT_FOUND);
80          }
81          return member;
82      }
83  
84      /**
85       * Creates a new member from the values provided. Performs validation, and will return a JAX-RS response with either 200 ok,
86       * or with a map of fields, and related errors.
87       */
88      @POST
89      @Consumes(MediaType.APPLICATION_JSON)
90      @Produces(MediaType.APPLICATION_JSON)
91      public Response createMember(Member member) {
92  
93          Response.ResponseBuilder builder = null;
94  
95          try {
96              // Validates member using bean validation
97              validateMember(member);
98  
99              registration.register(member);
100 
101             // Create an "ok" response
102             builder = Response.ok();
103         } catch (ConstraintViolationException ce) {
104             // Handle bean validation issues
105             builder = createViolationResponse(ce.getConstraintViolations());
106         } catch (ValidationException e) {
107             // Handle the unique constrain violation
108             Map<String, String> responseObj = new HashMap<String, String>();
109             responseObj.put("email", "Email taken");
110             builder = Response.status(Response.Status.CONFLICT).entity(responseObj);
111         } catch (Exception e) {
112             // Handle generic exceptions
113             Map<String, String> responseObj = new HashMap<String, String>();
114             responseObj.put("error", e.getMessage());
115             builder = Response.status(Response.Status.BAD_REQUEST).entity(responseObj);
116         }
117 
118         return builder.build();
119     }
120 
121     /**
122      * <p>
123      * Validates the given Member variable and throws validation exceptions based on the type of error. If the error is standard
124      * bean validation errors then it will throw a ConstraintValidationException with the set of the constraints violated.
125      * </p>
126      * <p>
127      * If the error is caused because an existing member with the same email is registered it throws a regular validation
128      * exception so that it can be interpreted separately.
129      * </p>
130      * 
131      * @param member Member to be validated
132      * @throws ConstraintViolationException If Bean Validation errors exist
133      * @throws ValidationException If member with the same email already exists
134      */
135     private void validateMember(Member member) throws ConstraintViolationException, ValidationException {
136         // Create a bean validator and check for issues.
137         Set<ConstraintViolation<Member>> violations = validator.validate(member);
138 
139         if (!violations.isEmpty()) {
140             throw new ConstraintViolationException(new HashSet<ConstraintViolation<?>>(violations));
141         }
142 
143         // Check the uniqueness of the email address
144         if (emailAlreadyExists(member.getEmail())) {
145             throw new ValidationException("Unique Email Violation");
146         }
147     }
148 
149     /**
150      * Creates a JAX-RS "Bad Request" response including a map of all violation fields, and their message. This can then be used
151      * by clients to show violations.
152      * 
153      * @param violations A set of violations that needs to be reported
154      * @return JAX-RS response containing all violations
155      */
156     private Response.ResponseBuilder createViolationResponse(Set<ConstraintViolation<?>> violations) {
157         log.fine("Validation completed. violations found: " + violations.size());
158 
159         Map<String, String> responseObj = new HashMap<String, String>();
160 
161         for (ConstraintViolation<?> violation : violations) {
162             responseObj.put(violation.getPropertyPath().toString(), violation.getMessage());
163         }
164 
165         return Response.status(Response.Status.BAD_REQUEST).entity(responseObj);
166     }
167 
168     /**
169      * Checks if a member with the same email address is already registered. This is the only way to easily capture the
170      * "@UniqueConstraint(columnNames = "email")" constraint from the Member class.
171      * 
172      * @param email The email to check
173      * @return True if the email already exists, and false otherwise
174      */
175     public boolean emailAlreadyExists(String email) {
176         Member member = null;
177         try {
178             member = repository.findByEmail(email);
179         } catch (NoResultException e) {
180             // ignore
181         }
182         return member != null;
183     }
184 }