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.wshelloworld;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.jws.WebService;
23  
24  /**
25   * The implementation of the HelloWorld JAX-WS Web Service.
26   * 
27   * @author lnewson@redhat.com
28   */
29  @WebService(serviceName = "HelloWorldService", portName = "HelloWorld", name = "HelloWorld", endpointInterface = "org.jboss.as.quickstarts.wshelloworld.HelloWorldService", targetNamespace = "http://www.jboss.org/jbossas/quickstarts/wshelloworld/HelloWorld")
30  public class HelloWorldServiceImpl implements HelloWorldService {
31  
32      @Override
33      public String sayHello() {
34          return "Hello World!";
35      }
36  
37      @Override
38      public String sayHelloToName(final String name) {
39  
40          /* Create a list with just the one value */
41          final List<String> names = new ArrayList<String>();
42          names.add(name);
43  
44          return sayHelloToNames(names);
45      }
46  
47      @Override
48      public String sayHelloToNames(final List<String> names) {
49          return "Hello " + createNameListString(names);
50      }
51  
52      /**
53       * Creates a list of names separated by commas or an and symbol if its the last separation. This is then used to say hello to
54       * the list of names.
55       * 
56       * i.e. if the input was {John, Mary, Luke} the output would be John, Mary & Luke
57       * 
58       * @param names A list of names
59       * @return The list of names separated as described above.
60       */
61      private String createNameListString(final List<String> names) {
62  
63          /*
64           * If the list is null or empty then assume the call was anonymous.
65           */
66          if (names == null || names.isEmpty()) {
67              return "Anonymous!";
68          }
69  
70          final StringBuilder nameBuilder = new StringBuilder();
71          for (int i = 0; i < names.size(); i++) {
72  
73              /*
74               * Add the separator if its not the first string or the last separator since that should be an and (&) symbol.
75               */
76              if (i != 0 && i != names.size() - 1)
77                  nameBuilder.append(", ");
78              else if (i != 0 && i == names.size() - 1)
79                  nameBuilder.append(" & ");
80  
81              nameBuilder.append(names.get(i));
82          }
83  
84          nameBuilder.append("!");
85  
86          return nameBuilder.toString();
87      }
88  }