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.util.Locale;
20  
21  import javax.ws.rs.GET;
22  import javax.ws.rs.Path;
23  import javax.ws.rs.PathParam;
24  
25  import org.jboss.as.quickstarts.loggingToolsQS.exceptions.GreeterExceptionBundle;
26  import org.jboss.as.quickstarts.loggingToolsQS.loggers.GreeterLogger;
27  import org.jboss.as.quickstarts.loggingToolsQS.messages.GreetingMessagesBundle;
28  import org.jboss.logging.Messages;
29  
30  /**
31   * A simple REST service which says hello in different languages and logs the activity using localised loggers created using
32   * JBoss Logging Tools.
33   *
34   * @author dmison@me.com
35   *
36   */
37  
38  @Path("greetings")
39  public class GreeterService {
40      // ======================================================================
41      // Hello "name"!
42      @GET
43      @Path("{name}")
44      public String getHelloName(@PathParam("name") String name) {
45          GreeterLogger.LOGGER.logHelloMessageSent();
46          return GreetingMessagesBundle.MESSAGES.helloToYou(name);
47      }
48  
49      // ======================================================================
50      // Hello "name" in language
51      @GET
52      @Path("{locale}/{name}")
53      public String getHelloNameForLocale(@PathParam("name") String name, @PathParam("locale") String locale) {
54          String[] locale_parts = locale.split("-");
55          Locale newLocale = null;
56  
57          switch (locale_parts.length) {
58              case 1:
59                  newLocale = new Locale(locale_parts[0]);
60                  break;
61              case 2:
62                  newLocale = new Locale(locale_parts[0], locale_parts[1]);
63                  break;
64              case 3:
65                  newLocale = new Locale(locale_parts[0], locale_parts[1], locale_parts[2]);
66                  break;
67              default:
68                  throw GreeterExceptionBundle.EXCEPTIONS.localeNotValid(locale);
69          }
70  
71          GreetingMessagesBundle messages = Messages.getBundle(GreetingMessagesBundle.class, newLocale);
72          GreeterLogger.LOGGER.logHelloMessageSentForLocale(locale);
73          return messages.helloToYou(name);
74      }
75  
76      // ======================================================================
77      // this is a particularly contrived example.  It is here merely to demonstrate
78      // the throwing of a localized exception with another exception as the cause. 
79      @GET
80      @Path("crashme")
81      public String crashMe() throws Exception {
82          int value = 0;
83  
84          try {
85              value = 50 / 0;
86          } catch (Exception ex) {
87              throw GreeterExceptionBundle.EXCEPTIONS.thrownOnPurpose(ex);
88          }
89  
90          return "value=" + value + " (if you are reading this, then something went wrong!)";
91      }
92  
93  }