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 Surrogate-Capabilities request header allows surrogates to advertise their capabilities with capability tokens.
10   * 
11   * @author Nicolas Richeton
12   * 
13   */
14  public final class SurrogateCapabilitiesHeader {
15      private List<SurrogateCapabilities> surrogates;
16  
17      private SurrogateCapabilitiesHeader() {
18          surrogates = new ArrayList<>();
19      }
20  
21      public static SurrogateCapabilitiesHeader fromHeaderValue(String value) {
22          SurrogateCapabilitiesHeader result = new SurrogateCapabilitiesHeader();
23  
24          if (value == null) {
25              return result;
26          }
27  
28          String[] surrogates = value.split(",");
29          for (String surrogate : surrogates) {
30              result.surrogates.add(SurrogateCapabilities.fromString(surrogate.trim()));
31          }
32  
33          return result;
34      }
35  
36      public List<SurrogateCapabilities> getSurrogates() {
37          return surrogates;
38      }
39  
40      public SurrogateCapabilities getSurrogate(String token) {
41          for (SurrogateCapabilities s : surrogates) {
42              if (token.equals(s.getDeviceToken())) {
43                  return s;
44              }
45          }
46          return null;
47      }
48  
49      @Override
50      public String toString() {
51  
52          return StringUtils.join(surrogates, ", ");
53      }
54  }