1 | package clarin.cmdi.componentregistry.rest; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.net.URI; |
---|
6 | import java.security.Principal; |
---|
7 | import java.util.Arrays; |
---|
8 | import java.util.Date; |
---|
9 | import java.util.List; |
---|
10 | |
---|
11 | import javax.ws.rs.Consumes; |
---|
12 | import javax.ws.rs.DELETE; |
---|
13 | import javax.ws.rs.GET; |
---|
14 | import javax.ws.rs.POST; |
---|
15 | import javax.ws.rs.Path; |
---|
16 | import javax.ws.rs.PathParam; |
---|
17 | import javax.ws.rs.Produces; |
---|
18 | import javax.ws.rs.WebApplicationException; |
---|
19 | import javax.ws.rs.core.Context; |
---|
20 | import javax.ws.rs.core.MediaType; |
---|
21 | import javax.ws.rs.core.Response; |
---|
22 | import javax.ws.rs.core.SecurityContext; |
---|
23 | import javax.ws.rs.core.UriInfo; |
---|
24 | import javax.ws.rs.core.Response.Status; |
---|
25 | |
---|
26 | import org.slf4j.Logger; |
---|
27 | import org.slf4j.LoggerFactory; |
---|
28 | |
---|
29 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
30 | import clarin.cmdi.componentregistry.ComponentRegistryImpl; |
---|
31 | import clarin.cmdi.componentregistry.UserUnauthorizedException; |
---|
32 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
33 | import clarin.cmdi.componentregistry.model.AbstractDescription; |
---|
34 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
35 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
36 | import clarin.cmdi.componentregistry.model.RegisterResponse; |
---|
37 | |
---|
38 | import com.sun.jersey.multipart.FormDataParam; |
---|
39 | |
---|
40 | @Path("/registry") |
---|
41 | public class ComponentRegistryRestService { |
---|
42 | |
---|
43 | @Context |
---|
44 | UriInfo uriInfo; |
---|
45 | @Context |
---|
46 | SecurityContext security; |
---|
47 | |
---|
48 | private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryRestService.class); |
---|
49 | |
---|
50 | public static final String DATA_FORM_FIELD = "data"; |
---|
51 | public static final String NAME_FORM_FIELD = "name"; |
---|
52 | public static final String DESCRIPTION_FORM_FIELD = "description"; |
---|
53 | public static final String GROUP_FORM_FIELD = "group"; |
---|
54 | |
---|
55 | private ComponentRegistry registry = ComponentRegistryImpl.getInstance(); |
---|
56 | |
---|
57 | @GET |
---|
58 | @Path("/components") |
---|
59 | @Produces( { MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) |
---|
60 | public List<ComponentDescription> getRegisteredComponents() { |
---|
61 | long start = System.currentTimeMillis(); |
---|
62 | List<ComponentDescription> components = registry.getComponentDescriptions(); |
---|
63 | LOG.info("Releasing " + components.size() + " registered components into the world (" + (System.currentTimeMillis() - start) |
---|
64 | + " millisecs)"); |
---|
65 | return components; |
---|
66 | } |
---|
67 | |
---|
68 | @GET |
---|
69 | @Path("/profiles") |
---|
70 | @Produces( { MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) |
---|
71 | public List<ProfileDescription> getRegisteredProfiles() { |
---|
72 | long start = System.currentTimeMillis(); |
---|
73 | List<ProfileDescription> profiles = registry.getProfileDescriptions(); |
---|
74 | LOG.info("Releasing " + profiles.size() + " registered profiles into the world (" + (System.currentTimeMillis() - start) |
---|
75 | + " millisecs)"); |
---|
76 | return profiles; |
---|
77 | } |
---|
78 | |
---|
79 | @GET |
---|
80 | @Path("/components/{componentId}") |
---|
81 | @Produces( { MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) |
---|
82 | public CMDComponentSpec getRegisteredComponent(@PathParam("componentId") String componentId) { |
---|
83 | LOG.info("Component with id: " + componentId + " is requested."); |
---|
84 | return registry.getMDComponent(componentId); |
---|
85 | } |
---|
86 | |
---|
87 | @DELETE |
---|
88 | @Path("/components/{componentId}") |
---|
89 | public Response deleteRegisteredComponent(@PathParam("componentId") String componentId) { |
---|
90 | Principal principal = security.getUserPrincipal(); |
---|
91 | if (principal == null) { |
---|
92 | throw new IllegalArgumentException("no user principal found."); |
---|
93 | } |
---|
94 | LOG.info("Component with id: " + componentId + " set for deletion."); |
---|
95 | try { |
---|
96 | registry.deleteMDComponent(componentId, principal); |
---|
97 | } catch (IOException e) { |
---|
98 | LOG.info("Component with id: " + componentId + " deletion failed.", e); |
---|
99 | return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); |
---|
100 | } catch (UserUnauthorizedException e) { |
---|
101 | LOG.info("Component with id: " + componentId + " deletion failed: "+e.getMessage()); |
---|
102 | return Response.serverError().status(Status.UNAUTHORIZED).build(); |
---|
103 | } |
---|
104 | LOG.info("Component with id: " + componentId + " deleted."); |
---|
105 | return Response.ok().build(); |
---|
106 | } |
---|
107 | |
---|
108 | @GET |
---|
109 | @Path("/components/{componentId}/{rawType}") |
---|
110 | @Produces( { MediaType.TEXT_XML, MediaType.APPLICATION_XML }) |
---|
111 | public String getRegisteredComponentRawType(@PathParam("componentId") String componentId, @PathParam("rawType") String rawType) { |
---|
112 | LOG.info("Component with id: " + componentId + " and rawType:" + rawType + " is requested."); |
---|
113 | String result = ""; |
---|
114 | if ("xml".equalsIgnoreCase(rawType)) { |
---|
115 | result = registry.getMDComponentAsXml(componentId); |
---|
116 | } else if ("xsd".equalsIgnoreCase(rawType)) { |
---|
117 | result = registry.getMDComponentAsXsd(componentId); |
---|
118 | } else { |
---|
119 | throw new WebApplicationException(Response.serverError().entity( |
---|
120 | "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); |
---|
121 | } |
---|
122 | return result; |
---|
123 | } |
---|
124 | |
---|
125 | @GET |
---|
126 | @Path("/profiles/{profileId}") |
---|
127 | @Produces( { MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) |
---|
128 | public CMDComponentSpec getRegisteredProfile(@PathParam("profileId") String profileId) { |
---|
129 | LOG.info("Profile with id: " + profileId + " is requested."); |
---|
130 | return registry.getMDProfile(profileId); |
---|
131 | } |
---|
132 | |
---|
133 | @DELETE |
---|
134 | @Path("/profiles/{profileId}") |
---|
135 | public Response deleteRegisteredProfile(@PathParam("profileId") String profileId) { |
---|
136 | Principal principal = security.getUserPrincipal(); |
---|
137 | if (principal == null) { |
---|
138 | throw new IllegalArgumentException("no user principal found."); |
---|
139 | } |
---|
140 | LOG.info("Profile with id: " + profileId + " set for deletion."); |
---|
141 | try { |
---|
142 | registry.deleteMDProfile(profileId, principal); |
---|
143 | } catch (IOException e) { |
---|
144 | LOG.info("Profile with id: " + profileId + " deletion failed.", e); |
---|
145 | return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); |
---|
146 | } catch (UserUnauthorizedException e) { |
---|
147 | LOG.info("Profile with id: " + profileId + " deletion failed: "+e.getMessage()); |
---|
148 | return Response.serverError().status(Status.UNAUTHORIZED).build(); |
---|
149 | } |
---|
150 | LOG.info("Profile with id: " + profileId + " deleted."); |
---|
151 | return Response.ok().build(); |
---|
152 | } |
---|
153 | |
---|
154 | @GET |
---|
155 | @Path("/profiles/{profileId}/{rawType}") |
---|
156 | @Produces( { MediaType.TEXT_XML, MediaType.APPLICATION_XML }) |
---|
157 | public String getRegisteredProfileRawType(@PathParam("profileId") String profileId, @PathParam("rawType") String rawType) { |
---|
158 | LOG.info("Profile with id: " + profileId + " and rawType:" + rawType + " is requested."); |
---|
159 | String result = ""; |
---|
160 | if ("xml".equalsIgnoreCase(rawType)) { |
---|
161 | result = registry.getMDProfileAsXml(profileId); |
---|
162 | } else if ("xsd".equalsIgnoreCase(rawType)) { |
---|
163 | result = registry.getMDProfileAsXsd(profileId); |
---|
164 | } else { |
---|
165 | throw new WebApplicationException(Response.serverError().entity( |
---|
166 | "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); |
---|
167 | } |
---|
168 | return result; |
---|
169 | } |
---|
170 | |
---|
171 | @POST |
---|
172 | @Path("/profiles") |
---|
173 | @Produces( { MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) |
---|
174 | @Consumes("multipart/form-data") |
---|
175 | public RegisterResponse registerProfile(@FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, |
---|
176 | @FormDataParam(DESCRIPTION_FORM_FIELD) String description) { |
---|
177 | Principal principal = security.getUserPrincipal(); |
---|
178 | if (principal == null) { |
---|
179 | throw new IllegalArgumentException("no user principal found."); |
---|
180 | } |
---|
181 | ProfileDescription desc = createNewProfileDescription(); |
---|
182 | desc.setCreatorName(principal.getName()); |
---|
183 | desc.setName(name); |
---|
184 | desc.setDescription(description); |
---|
185 | desc.setRegistrationDate(AbstractDescription.DATE_FORMAT.format(new Date())); |
---|
186 | LOG.info("Trying to register Profile: " + desc); |
---|
187 | return register(input, desc, principal); |
---|
188 | } |
---|
189 | |
---|
190 | @POST |
---|
191 | @Path("/components") |
---|
192 | @Produces( { MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) |
---|
193 | @Consumes("multipart/form-data") |
---|
194 | public RegisterResponse registerComponent(@FormDataParam(DATA_FORM_FIELD) InputStream input, |
---|
195 | @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, |
---|
196 | @FormDataParam(GROUP_FORM_FIELD) String group) { |
---|
197 | Principal principal = security.getUserPrincipal(); |
---|
198 | if (principal == null) { |
---|
199 | throw new IllegalArgumentException("no user principal found."); |
---|
200 | } |
---|
201 | ComponentDescription desc = createNewComponentDescription(); |
---|
202 | desc.setCreatorName(principal.getName()); |
---|
203 | desc.setName(name); |
---|
204 | desc.setDescription(description); |
---|
205 | desc.setGroupName(group); |
---|
206 | desc.setRegistrationDate(AbstractDescription.DATE_FORMAT.format(new Date())); |
---|
207 | LOG.info("Trying to register Component: " + desc); |
---|
208 | return register(input, desc, principal); |
---|
209 | } |
---|
210 | |
---|
211 | private RegisterResponse register(InputStream input, AbstractDescription desc, Principal principal) { |
---|
212 | try { |
---|
213 | DescriptionValidator descriptionValidator = new DescriptionValidator(desc); |
---|
214 | MDValidator validator = new MDValidator(input, desc, registry); |
---|
215 | RegisterResponse response = new RegisterResponse(); |
---|
216 | validate(response, descriptionValidator, validator); |
---|
217 | if (response.getErrors().isEmpty()) { |
---|
218 | CMDComponentSpec spec = validator.getCMDComponentSpec(); |
---|
219 | int returnCode = spec.isIsProfile() ? registry.registerMDProfile((ProfileDescription) desc, spec) : registry |
---|
220 | .registerMDComponent((ComponentDescription) desc, spec); |
---|
221 | if (returnCode == 0) { |
---|
222 | response.setRegistered(true); |
---|
223 | response.setDescription(desc); |
---|
224 | } else { |
---|
225 | response.setRegistered(false); |
---|
226 | response.addError("Unable to register at this moment. Internal server error."); |
---|
227 | } |
---|
228 | } else { |
---|
229 | LOG.info("Registration failed with validation errors:" + Arrays.toString(response.getErrors().toArray())); |
---|
230 | response.setRegistered(false); |
---|
231 | } |
---|
232 | response.setIsProfile(desc.isProfile()); |
---|
233 | return response; |
---|
234 | } finally { |
---|
235 | try { |
---|
236 | input.close();//either we read the input or there was an exception, we need to close it. |
---|
237 | } catch (IOException e) { |
---|
238 | LOG.error("Error when closing inputstream: ", e); |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | private ComponentDescription createNewComponentDescription() { |
---|
244 | ComponentDescription desc = ComponentDescription.createNewDescription(); |
---|
245 | desc.setHref(createXlink(desc.getId())); |
---|
246 | return desc; |
---|
247 | } |
---|
248 | |
---|
249 | private ProfileDescription createNewProfileDescription() { |
---|
250 | ProfileDescription desc = ProfileDescription.createNewDescription(); |
---|
251 | desc.setHref(createXlink(desc.getId())); |
---|
252 | return desc; |
---|
253 | } |
---|
254 | |
---|
255 | private String createXlink(String id) { |
---|
256 | URI uri = uriInfo.getRequestUriBuilder().path(id).build(); |
---|
257 | return uri.toString(); |
---|
258 | } |
---|
259 | |
---|
260 | private void validate(RegisterResponse response, Validator... validators) { |
---|
261 | for (Validator validator : validators) { |
---|
262 | if (!validator.validate()) { |
---|
263 | for (String error : validator.getErrorMessages()) { |
---|
264 | response.addError(error); |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|