annotate src/main/java/de/intevation/lada/rest/LStatusService.java @ 244:f3e15ba7c3b7

Use a sequence for pk in LStatusId and create a new ID object.
author Raimund Renkert <rrenkert@intevation.de>
date Mon, 15 Jul 2013 13:22:00 +0200
parents d8a3d188046f
children 2dbe200b73f5
rev   line source
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
1 package de.intevation.lada.rest;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
2
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
3 import java.util.ArrayList;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
4
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
5 import javax.enterprise.context.RequestScoped;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
6 import javax.inject.Inject;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
7 import javax.inject.Named;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
8 import javax.ws.rs.Consumes;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
9 import javax.ws.rs.GET;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
10 import javax.ws.rs.POST;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
11 import javax.ws.rs.PUT;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
12 import javax.ws.rs.Path;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
13 import javax.ws.rs.Produces;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
14 import javax.ws.rs.core.Context;
208
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
15 import javax.ws.rs.core.HttpHeaders;
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
16 import javax.ws.rs.core.MultivaluedMap;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
17 import javax.ws.rs.core.UriInfo;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
18
232
d8a3d188046f Renamed package authentication to auth.
Raimund Renkert <rrenkert@intevation.de>
parents: 214
diff changeset
19 import de.intevation.lada.auth.Authentication;
d8a3d188046f Renamed package authentication to auth.
Raimund Renkert <rrenkert@intevation.de>
parents: 214
diff changeset
20 import de.intevation.lada.auth.AuthenticationException;
182
a2e04ab2cd44 Use the new filter and query builder and have authentication for LProbeInfo objects.
Raimund Renkert <rrenkert@intevation.de>
parents: 156
diff changeset
21 import de.intevation.lada.data.QueryBuilder;
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
22 import de.intevation.lada.data.Repository;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
23 import de.intevation.lada.model.LStatus;
244
f3e15ba7c3b7 Use a sequence for pk in LStatusId and create a new ID object.
Raimund Renkert <rrenkert@intevation.de>
parents: 232
diff changeset
24 import de.intevation.lada.model.LStatusId;
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
25
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
26 /**
210
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
27 * This class produces a RESTful service to read, write and update
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
28 * LStatus objects.
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
29 *
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
30 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
31 */
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
32 @Path("/status")
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
33 @RequestScoped
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
34 public class LStatusService
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
35 {
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
36 /**
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
37 * The Repository for SUmwelt.
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
38 */
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
39 @Inject
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
40 @Named("lstatusrepository")
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
41 private Repository repository;
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
42
210
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
43 /**
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
44 * The authorization module.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
45 */
208
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
46 @Inject
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
47 @Named("ldapauth")
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
48 private Authentication authentication;
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
49
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
50 /**
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
51 * Request LStatus via a filter.
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
52 *
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
53 * Query parameters are used for the filter in form of key-value pairs.
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
54 *
210
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
55 * @param info The URL query parameters.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
56 * @param header The HTTP header containing authorization information.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
57 * @return Response object.
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
58 */
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
59 @GET
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
60 @Produces("text/json")
208
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
61 public Response filter(
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
62 @Context UriInfo info,
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
63 @Context HttpHeaders headers
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
64 ) {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
65 try {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
66 if (!authentication.isAuthorizedUser(headers)) {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
67 return new Response(false, 699, new ArrayList<LStatus>());
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
68 }
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
69 MultivaluedMap<String, String> params = info.getQueryParameters();
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
70 if (params.isEmpty() ||
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
71 !params.containsKey("probeId") ||
214
0b233de80756 Fixed typos in url parameter validation.
Raimund Renkert <rrenkert@intevation.de>
parents: 210
diff changeset
72 !params.containsKey("messungsId")) {
208
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
73 return new Response(false, 609, new ArrayList<LStatus>());
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
74 }
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
75 String probeId = params.getFirst("probeId");
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
76 if (authentication.hasAccess(headers, probeId)) {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
77 QueryBuilder<LStatus> builder =
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
78 new QueryBuilder<LStatus>(
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
79 repository.getEntityManager(), LStatus.class);
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
80 builder.and("probeId", probeId)
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
81 .and("messungsId", params.getFirst("messungsId"));
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
82 return repository.filter(builder.getQuery());
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
83 }
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
84 return new Response(false, 698, new ArrayList<LStatus>());
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
85 }
208
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
86 catch(AuthenticationException ae) {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
87 return new Response(false, 699, new ArrayList<LStatus>());
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
88 }
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
89 }
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
90
210
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
91 /**
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
92 * Update a LStatus object.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
93 *
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
94 * @param status The LStatus object to update.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
95 * @param headers The HTTP header containing authorization information.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
96 * @return Response object.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
97 */
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
98 @PUT
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
99 @Produces("text/json")
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
100 @Consumes("application/json")
208
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
101 public Response update(
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
102 LStatus status,
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
103 @Context HttpHeaders headers
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
104 ) {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
105 try {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
106 String probeId = status.getProbeId();
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
107 if (authentication.hasAccess(headers, probeId)) {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
108 return repository.update(status);
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
109 }
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
110 return new Response(false, 698, new ArrayList<LStatus>());
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
111 }
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
112 catch(AuthenticationException ae) {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
113 return new Response(false, 699, new ArrayList<LStatus>());
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
114 }
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
115 }
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
116
210
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
117 /**
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
118 * Create a new LStatus object.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
119 *
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
120 * @param status The new LStatus object.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
121 * @param headers The HTTP header containing authorization information.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
122 * @return Response object.
a305412206a3 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 208
diff changeset
123 */
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
124 @POST
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
125 @Produces("text/json")
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
126 @Consumes("application/json")
208
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
127 public Response create(
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
128 LStatus status,
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
129 @Context HttpHeaders headers
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
130 ) {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
131 try {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
132 String probeId = status.getProbeId();
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
133 if (authentication.hasAccess(headers, probeId)) {
244
f3e15ba7c3b7 Use a sequence for pk in LStatusId and create a new ID object.
Raimund Renkert <rrenkert@intevation.de>
parents: 232
diff changeset
134 LStatusId id = new LStatusId();
f3e15ba7c3b7 Use a sequence for pk in LStatusId and create a new ID object.
Raimund Renkert <rrenkert@intevation.de>
parents: 232
diff changeset
135 id.setMessungsId(status.getMessungsId());
f3e15ba7c3b7 Use a sequence for pk in LStatusId and create a new ID object.
Raimund Renkert <rrenkert@intevation.de>
parents: 232
diff changeset
136 id.setProbeId(probeId);
f3e15ba7c3b7 Use a sequence for pk in LStatusId and create a new ID object.
Raimund Renkert <rrenkert@intevation.de>
parents: 232
diff changeset
137 status.setId(id);
208
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
138 return repository.create(status);
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
139 }
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
140 return new Response(false, 698, new ArrayList<LStatus>());
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
141 }
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
142 catch(AuthenticationException ae) {
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
143 return new Response(false, 699, new ArrayList<LStatus>());
832e67663fd9 Added authorization to all services.
Raimund Renkert <rrenkert@intevation.de>
parents: 182
diff changeset
144 }
156
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
145 }
a7ddd34dd8f9 New service for LStatus objects.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
146 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)