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.loggingToolsQS;
18  
19  import java.text.DateFormat;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  
24  import javax.ws.rs.GET;
25  import javax.ws.rs.Path;
26  import javax.ws.rs.PathParam;
27  import javax.ws.rs.WebApplicationException;
28  import javax.ws.rs.core.MediaType;
29  import javax.ws.rs.core.Response;
30  
31  import org.jboss.as.quickstarts.loggingToolsQS.exceptions.DateExceptionsBundle;
32  import org.jboss.as.quickstarts.loggingToolsQS.loggers.DateLogger;
33  
34  /**
35   * A simple REST service which returns the number of days until a date and provides localised logging of the activity
36   * 
37   * @author dmison@me.com
38   * 
39   */
40  
41  @Path("dates")
42  public class DateService {
43  
44      @GET
45      @Path("daysuntil/{targetdate}")
46      public int showDaysUntil(@PathParam("targetdate") String targetdate) {
47          DateLogger.LOGGER.logDaysUntilRequest(targetdate);
48  
49          DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
50          Date target = null;
51          Date now = new Date();
52  
53          float days = 0;
54  
55          try {
56              df.setLenient(false);               //make sure no invalid dates sneak through
57              target = df.parse(targetdate);
58              days = (float) target.getTime() - now.getTime();
59              days = days / (1000 * 60 * 60 * 24); // turn milliseconds into days
60          } catch (ParseException ex) {
61              // ** DISCLAIMER **
62              // This example is contrived and overly verbose for the purposes of showing the
63              // different logging methods. It's generally not recommended to recreate exceptions
64              // or log exceptions that are being thrown.
65  
66              // create localized ParseException using method from bundle with details from ex
67              ParseException nex = DateExceptionsBundle.EXCEPTIONS.targetDateStringDidntParse(targetdate, ex.getErrorOffset());
68  
69              // log a message using nex as the cause
70              DateLogger.LOGGER.logStringCouldntParseAsDate(targetdate, nex);
71  
72              // throw a WebApplicationException (400) with the localized message from nex
73              throw new WebApplicationException(Response.status(400).entity(nex.getLocalizedMessage()).type(MediaType.TEXT_PLAIN)
74                      .build());
75          }
76  
77          return Math.round(days);
78      }
79  
80  }