View Javadoc
1   package org.esigate.extension.surrogate.http;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.apache.commons.lang3.StringUtils;
7   
8   /**
9    * The name in each capability set identifies a device token, which uniquely identifies the surrogate that appended it.
10   * Device tokens must be unique within a request's Surrogate-Capabilities header.
11   * <p>
12   * The value contains a space-separated list of capability tokens.
13   * 
14   * @author Nicolas Richeton
15   * 
16   */
17  public final class SurrogateCapabilities {
18      private List<Capability> capabilities;
19      private String deviceToken;
20  
21      public List<Capability> getCapabilities() {
22          return capabilities;
23      }
24  
25      public String getDeviceToken() {
26          return deviceToken;
27      }
28  
29      private SurrogateCapabilities() {
30          capabilities = new ArrayList<>();
31          deviceToken = null;
32      }
33  
34      public static SurrogateCapabilities fromString(String surrogateString) {
35          SurrogateCapabilities cap = new SurrogateCapabilities();
36          int equalsIndex = surrogateString.indexOf('=');
37  
38          if (equalsIndex >= 0) {
39  
40              cap.deviceToken = surrogateString.substring(0, equalsIndex).trim();
41  
42              String allCapabiities = surrogateString.substring(equalsIndex + 1).trim();
43  
44              if (allCapabiities.startsWith("\"") && allCapabiities.endsWith("\"")) {
45                  allCapabiities = allCapabiities.substring(1, allCapabiities.length() - 1);
46              }
47              String[] capabilities = allCapabiities.split(" ");
48              for (String c : capabilities) {
49  
50                  c = c.trim();
51  
52                  if (c.length() > 0) {
53                      cap.capabilities.add(Capability.fromToken(c));
54                  }
55              }
56  
57          }
58          return cap;
59      }
60  
61      @Override
62      public String toString() {
63          return deviceToken + "=" + "\"" + StringUtils.join(capabilities, " ") + "\"";
64      }
65  }