View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.model;
17  
18  /**
19   * The agent string of the UPnP stack in network messages, either as a server or client.
20   * <p>
21   * Tries to detect the operating system name and version, defaults to {@link UserConstants}
22   * for product name and version.
23   * </p>
24   *
25   * @author Christian Bauer
26   */
27  public class ServerClientTokens {
28  
29      public static final String UNKNOWN_PLACEHOLDER = "UNKNOWN";
30  
31      // TODO: This means we default to UDA 1.0
32      private int majorVersion = 1;
33      private int minorVersion = 0;
34  
35      private String osName  =  System.getProperty("os.name").replaceAll("[^a-zA-Z0-9\\.\\-_]", "");
36      private String osVersion = System.getProperty("os.version").replaceAll("[^a-zA-Z0-9\\.\\-_]", "");
37      private String productName = UserConstants.PRODUCT_TOKEN_NAME;
38      private String productVersion = UserConstants.PRODUCT_TOKEN_VERSION;
39  
40      public ServerClientTokens() {
41      }
42  
43      public ServerClientTokens(int majorVersion, int minorVersion) {
44          this.majorVersion = majorVersion;
45          this.minorVersion = minorVersion;
46      }
47  
48      public ServerClientTokens(String productName, String productVersion) {
49          this.productName = productName;
50          this.productVersion = productVersion;
51      }
52  
53      public ServerClientTokens(int majorVersion, int minorVersion, String osName, String osVersion, String productName, String productVersion) {
54          this.majorVersion = majorVersion;
55          this.minorVersion = minorVersion;
56          this.osName = osName;
57          this.osVersion = osVersion;
58          this.productName = productName;
59          this.productVersion = productVersion;
60      }
61  
62      public int getMajorVersion() {
63          return majorVersion;
64      }
65  
66      public void setMajorVersion(int majorVersion) {
67          this.majorVersion = majorVersion;
68      }
69  
70      public int getMinorVersion() {
71          return minorVersion;
72      }
73  
74      public void setMinorVersion(int minorVersion) {
75          this.minorVersion = minorVersion;
76      }
77  
78      public String getOsName() {
79          return osName;
80      }
81  
82      public void setOsName(String osName) {
83          this.osName = osName;
84      }
85  
86      public String getOsVersion() {
87          return osVersion;
88      }
89  
90      public void setOsVersion(String osVersion) {
91          this.osVersion = osVersion;
92      }
93  
94      public String getProductName() {
95          return productName;
96      }
97  
98      public void setProductName(String productName) {
99          this.productName = productName;
100     }
101 
102     public String getProductVersion() {
103         return productVersion;
104     }
105 
106     public void setProductVersion(String productVersion) {
107         this.productVersion = productVersion;
108     }
109 
110     @Override
111     public String toString() {
112         return getOsName()+"/"+getOsVersion() 
113                 + " UPnP/" + getMajorVersion() + "." + getMinorVersion() + " "
114                 + getProductName() + "/" + getProductVersion();
115     }
116 
117     public String getHttpToken() {
118         StringBuilder sb = new StringBuilder(256);
119         sb.append(osName.indexOf(' ') != -1 ? osName.replace(' ', '_') : osName);
120         sb.append('/');
121         sb.append(osVersion.indexOf(' ') != -1 ? osVersion.replace(' ', '_') : osVersion);
122         sb.append(" UPnP/");
123         sb.append(majorVersion);
124         sb.append('.');
125         sb.append(minorVersion);
126         sb.append(' ');
127         sb.append(productName.indexOf(' ') != -1 ? productName.replace(' ', '_') : productName);
128         sb.append('/');
129         sb.append(productVersion.indexOf(' ') != -1 ? productVersion.replace(' ', '_') : productVersion);
130 
131         return sb.toString();
132     }
133 
134     public String getOsToken() {
135         return getOsName().replaceAll(" ", "_")+"/"+getOsVersion().replaceAll(" ", "_");
136     }
137 
138     public String getProductToken() {
139         return getProductName().replaceAll(" ", "_") + "/" + getProductVersion().replaceAll(" ", "_");
140     }
141 
142     @Override
143     public boolean equals(Object o) {
144         if (this == o) return true;
145         if (o == null || getClass() != o.getClass()) return false;
146 
147         ServerClientTokens that = (ServerClientTokens) o;
148 
149         if (majorVersion != that.majorVersion) return false;
150         if (minorVersion != that.minorVersion) return false;
151         if (!osName.equals(that.osName)) return false;
152         if (!osVersion.equals(that.osVersion)) return false;
153         if (!productName.equals(that.productName)) return false;
154         if (!productVersion.equals(that.productVersion)) return false;
155 
156         return true;
157     }
158 
159     @Override
160     public int hashCode() {
161         int result = majorVersion;
162         result = 31 * result + minorVersion;
163         result = 31 * result + osName.hashCode();
164         result = 31 * result + osVersion.hashCode();
165         result = 31 * result + productName.hashCode();
166         result = 31 * result + productVersion.hashCode();
167         return result;
168     }
169 }