View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.adopter.registration;
23  
24  import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
25  import static javax.servlet.http.HttpServletResponse.SC_OK;
26  import static org.opencastproject.util.RestUtil.R.ok;
27  import static org.opencastproject.util.RestUtil.R.serverError;
28  import static org.opencastproject.util.doc.rest.RestParameter.Type.BOOLEAN;
29  import static org.opencastproject.util.doc.rest.RestParameter.Type.STRING;
30  
31  import org.opencastproject.adopter.registration.dto.Adopter;
32  import org.opencastproject.util.doc.rest.RestParameter;
33  import org.opencastproject.util.doc.rest.RestQuery;
34  import org.opencastproject.util.doc.rest.RestResponse;
35  import org.opencastproject.util.doc.rest.RestService;
36  
37  import com.google.gson.Gson;
38  
39  import org.osgi.service.component.annotations.Component;
40  import org.osgi.service.component.annotations.Reference;
41  import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsResource;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import java.util.Calendar;
46  
47  import javax.servlet.http.HttpServletResponse;
48  import javax.ws.rs.DELETE;
49  import javax.ws.rs.FormParam;
50  import javax.ws.rs.GET;
51  import javax.ws.rs.POST;
52  import javax.ws.rs.Path;
53  import javax.ws.rs.Produces;
54  import javax.ws.rs.core.MediaType;
55  import javax.ws.rs.core.Response;
56  
57  /**
58   * The REST endpoint for the adopter statistics service.
59   */
60  @Path("/admin-ng/adopter")
61  @RestService(name = "registrationController",
62          title = "Adopter Statistics Registration Service Endpoint",
63          abstractText = "Rest Endpoint for the registration form.",
64          notes = {"Provides operations regarding the adopter registration form"})
65  @Component(
66      immediate = true,
67      service = AdopterRegistrationRestService.class,
68      property = {
69          "service.description=Adopter Statistics REST Endpoint",
70          "opencast.service.type=org.opencastproject.adopter.registration.AdopterRegistrationRestService",
71          "opencast.service.path=/admin-ng/adopter",
72          "opencast.service.jobproducer=false"
73      }
74  )
75  @JaxrsResource
76  public class AdopterRegistrationRestService {
77  
78    /** The logger */
79    private static final Logger logger = LoggerFactory.getLogger(AdopterRegistrationRestService.class);
80  
81    /** The JSON parser */
82    private static final Gson gson = new Gson();
83  
84    /** The rest docs */
85    protected String docs;
86  
87    /** The service that provides methods for the registration */
88    protected AdopterRegistrationServiceImpl registrationService;
89  
90    /** OSGi setter for the registration service */
91    @Reference
92    public void setRegistrationService(AdopterRegistrationServiceImpl registrationService) {
93      this.registrationService = registrationService;
94    }
95  
96    @GET
97    @Path("registration")
98    @Produces(MediaType.APPLICATION_JSON)
99    @RestQuery(name = "getregistrationform", description = "GETs the adopter registration data.", responses = {
100           @RestResponse(description = "Retrieved registration data.",
101                         responseCode = HttpServletResponse.SC_OK),
102           @RestResponse(description = "Error while retrieving adopter registration data.",
103                         responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR) },
104                         returnDescription = "GETs the adopter registration data.")
105   public String getRegistrationForm() {
106     logger.debug("Retrieving adopter registration data.");
107     return gson.toJson(registrationService.get());
108   }
109 
110   @GET
111   @Path("summary")
112   @Produces(MediaType.APPLICATION_JSON)
113   @RestQuery(name = "getsummary", description = "GETs the adopter registration statistics data.",
114       responses = {
115           @RestResponse(description = "Retrieved statistic data.",
116               responseCode = HttpServletResponse.SC_OK),
117           @RestResponse(description = "Error while retrieving adopter statistic data.",
118               responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR)
119       },
120       returnDescription = "GETs the adopter registration statistics data.")
121   public Response getAdopterStatistics() {
122     logger.debug("Retrieving adopter registration statistics data.");
123     try {
124       return Response.ok(registrationService.getRegistrationDataAsString()).build();
125     } catch (Exception e) {
126       return Response.serverError().build();
127     }
128   }
129 
130   @POST
131   @Path("registration")
132   @RestQuery(name = "saveregistrationform",
133           description = "Saves the adopter registration data.",
134           returnDescription = "Status",
135           restParameters = {
136                   @RestParameter(description = "The Name of the organisation.",
137                           isRequired = false, name = "organisationName", type = STRING),
138                   @RestParameter(description = "The Name of the department.",
139                           isRequired = false, name = "departmentName", type = STRING),
140                   @RestParameter(description = "The First name.",
141                           isRequired = false, name = "firstName", type = STRING),
142                   @RestParameter(description = "The Last name.",
143                           isRequired = false, name = "lastName", type = STRING),
144                   @RestParameter(description = "The e-mail address.",
145                           isRequired = false, name = "email", type = STRING),
146                   @RestParameter(description = "The country.",
147                           isRequired = false, name = "country", type = STRING),
148                   @RestParameter(description = "The postal code.",
149                           isRequired = false, name = "postalCode", type = STRING),
150                   @RestParameter(description = "The city.",
151                           isRequired = false, name = "city", type = STRING),
152                   @RestParameter(description = "The street.",
153                           isRequired = false, name = "street", type = STRING),
154                   @RestParameter(description = "The street number.",
155                           isRequired = false, name = "streetNo", type = STRING),
156                   @RestParameter(description = "Does the adopter allows to be contacted.",
157                           isRequired = false, name = "contactMe", type = BOOLEAN),
158                   @RestParameter(description = "Does the adopter agreed to the policy.",
159                           isRequired = false, name = "agreedToPolicy", type = BOOLEAN),
160                   @RestParameter(description = "Does the adopter allow the gathering of error reports.",
161                           isRequired = false, name = "allowsErrorReports", type = BOOLEAN),
162                   @RestParameter(description = "Which type of system is this.",
163                           isRequired = false, name = "systemType", type = STRING),
164                   @RestParameter(description = "Does the adopter allow the gathering of statistic data.",
165                           isRequired = false, name = "allowsStatistics", type = BOOLEAN),
166                   @RestParameter(description = "Is the adopter already registered.",
167                           isRequired = false, name = "registered", type = BOOLEAN)
168           },
169           responses = {
170           @RestResponse(responseCode = SC_OK, description = "Adopter registration data saved."),
171           @RestResponse(responseCode = SC_BAD_REQUEST, description = "Couldn't save adopter registration data.")})
172   public Response register(
173           @FormParam("organisationName") String organisationName,
174           @FormParam("departmentName") String departmentName,
175           @FormParam("firstName") String firstName,
176           @FormParam("lastName") String lastName,
177           @FormParam("email") String email,
178           @FormParam("country") String country,
179           @FormParam("postalCode") String postalCode,
180           @FormParam("city") String city,
181           @FormParam("street") String street,
182           @FormParam("streetNo") String streetNo,
183           @FormParam("contactMe") boolean contactMe,
184           @FormParam("agreedToPolicy") boolean agreedToPolicy,
185           @FormParam("systemType") String systemType,
186           @FormParam("allowsErrorReports") boolean allowsErrorReports,
187           @FormParam("allowsStatistics") boolean allowsStatistics,
188           @FormParam("registered") boolean registered) {
189     logger.debug("Saving adopter registration data.");
190 
191     Adopter adopter = new Adopter(organisationName, departmentName, firstName, lastName, email, country, postalCode,
192         city, street, streetNo, contactMe, systemType, allowsStatistics, allowsErrorReports, agreedToPolicy, registered
193     );
194     try {
195       registrationService.save(adopter);
196     } catch (Exception e) {
197       logger.error("Error while saving adopter registration data.", e);
198       return Response.serverError().build();
199     }
200     return Response.ok().build();
201   }
202 
203   @POST
204   @Path("registration/finalize")
205   @RestQuery(name = "finalizeRegistration",
206       description = "Finalizes the registration and starts sending data.",
207       returnDescription = "Status",
208       restParameters = {},
209       responses = { @RestResponse(responseCode = SC_OK, description = "Registration finalized.") })
210   public Response register() {
211     logger.debug("Finalizing adopter registration.");
212 
213     Adopter adopter = (Adopter) registrationService.get();
214     adopter.setRegistered(true);
215 
216     try {
217       registrationService.save(adopter);
218     } catch (Exception e) {
219       logger.error("Error while saving adopter registration data.", e);
220       return Response.serverError().build();
221     }
222     return Response.ok().build();
223   }
224 
225   @GET
226   @Produces(MediaType.TEXT_PLAIN)
227   @Path("isUpToDate")
228   @RestQuery(name = "isUpToDate", description = "Returns true if Opencast has been able to register", responses = {
229       @RestResponse(description = "Registratino status",
230           responseCode = HttpServletResponse.SC_OK)
231       },
232       returnDescription = "true if registration has been updated in the last week, false otherwise")
233   public Response isUpToDate() {
234     Adopter data = (Adopter) registrationService.get();
235     Calendar cal = Calendar.getInstance();
236     cal.add(Calendar.DAY_OF_MONTH, -7);
237     //A fresh install might have no data at all, so we null check everything
238     if (data != null && data.getDateModified() != null && data.getDateModified().after(cal.getTime())) {
239       return Response.ok("true").build();
240     }
241     return Response.ok("false").build();
242   }
243 
244   @DELETE
245   @Path("registration")
246   @RestQuery(name = "deleteregistrationform", description = "Deletes the adopter registration data", responses = {
247           @RestResponse(description = "Successful deleted form data.",
248                   responseCode = HttpServletResponse.SC_OK),
249           @RestResponse(description = "Error while deleting adopter registration data.",
250                   responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR) },
251           returnDescription = "DELETEs the adopter registration data.")
252   public Response deleteRegistrationData() {
253     logger.debug("Deleting adopter registration data.");
254     try {
255       registrationService.markForDeletion();
256       return ok();
257     } catch (Exception e) {
258       logger.error("Error while deleting adopter registration data.", e);
259       return serverError();
260     }
261   }
262 
263 
264   @GET
265   @Produces(MediaType.TEXT_PLAIN)
266   @Path("latestToU")
267   @RestQuery(name = "getLatestTermsOfUse", description = "Gets the latest terms of use version.", responses = {
268       @RestResponse(description = "Retrieved statistic data.", responseCode = HttpServletResponse.SC_OK) },
269       returnDescription = "The latest terms of use version.")
270   public String getLatestTermsofUse() {
271     return Adopter.getLatestTermsOfUse().name();
272   }
273 
274 
275   @GET
276   @Produces(MediaType.TEXT_HTML)
277   @Path("docs")
278   public String getDocs() {
279     return docs;
280   }
281 
282 }