1   package com.atlassian.seraph.util;
2   
3   /**
4    * Parses {@code ServletContext.getServerInfo()} into separate pieces.
5    * 
6    * @since 2.4.0
7    */
8   public class ServerInformationParser
9   {
10      /**
11       * Given a String in the expected format, returns parsed server information.
12       * The textual server information must be in the format of "ServerName/ServerVersion (OtherInformation)".
13       * For example, Apache Tomcat 6.0.20 would be formatted as "Apache Tomcat/6.0.20".
14       * The "other information" part is optional.
15       *
16       * http://download.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getServerInfo%28%29
17       *
18       * @param serverInformationText the textual version of the server information
19       * @return
20       */
21      public static ServerInformation parse(String serverInformationText)
22      {
23          if (!serverInformationText.contains("/"))
24          {
25              throw new IllegalArgumentException("Server information is not present: " + serverInformationText);
26          }
27  
28          int slashLoc = serverInformationText.indexOf("/");
29          String name = serverInformationText.substring(0, slashLoc);
30  
31          //test for other information
32          if (serverInformationText.indexOf(" ", slashLoc) == -1)
33          {
34              //no other information exists
35              String version = serverInformationText.substring(slashLoc + 1);
36              return new ServerInformation(name, version);
37          }
38          else
39          {
40              //other information exists
41              int spaceLoc = serverInformationText.indexOf(" ", slashLoc);
42              String version = serverInformationText.substring(slashLoc + 1, spaceLoc);
43              String otherInformation = serverInformationText.substring(spaceLoc + 2, serverInformationText.length() - 1); //remove the parens
44              return new ServerInformation(name, version, otherInformation);
45          }
46      }
47  
48      /**
49       * Holder for easily accessible information about a server.
50       *
51       * Every server has at least a name and a version. {@code otherInformation} is null if not specified.
52       */
53      public static class ServerInformation
54      {
55          private final String name;
56          private final String version;
57          private final String otherInformation;
58  
59          private ServerInformation(String name, String version)
60          {
61              this(name, version, null);
62          }
63  
64          private ServerInformation(String name, String version, String otherInformation)
65          {
66              this.name = name;
67              this.version = version;
68              this.otherInformation = otherInformation;
69          }
70  
71          public String getName()
72          {
73              return name;
74          }
75  
76          public String getVersion()
77          {
78              return version;
79          }
80  
81          public String getOtherInformation()
82          {
83              return otherInformation;
84          }
85  
86          public boolean isApacheTomcat()
87          {
88              return "Apache Tomcat".equals(name);
89          }
90      }
91  }