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.message.header;
17  
18  import java.net.MalformedURLException;
19  import java.net.URISyntaxException;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.logging.Level;
24  import java.util.logging.Logger;
25  
26  /**
27   * @author Christian Bauer
28   */
29  public class CallbackHeader extends UpnpHeader<List<URL>> {
30  
31      final private static Logger log = Logger.getLogger(CallbackHeader.class.getName());
32  
33      public CallbackHeader() {
34          setValue(new ArrayList<URL>());
35      }
36  
37      public CallbackHeader(List<URL> urls) {
38          this();
39          getValue().addAll(urls);
40      }
41  
42      public CallbackHeader(URL url) {
43          this();
44          getValue().add(url);
45      }
46  
47      public void setString(String s) throws InvalidHeaderException {
48  
49          if (s.length() == 0) {
50              // Well, no callback URLs are not useful but we have to consider this state
51              return;
52          }
53  
54          if (!s.contains("<") || !s.contains(">")) {
55              throw new InvalidHeaderException("URLs not in brackets: " + s);
56          }
57  
58          s = s.replaceAll("<", "");
59          String[] split = s.split(">");
60          try {
61              List<URL> urls = new ArrayList<>();
62              for (String sp : split) {
63                  sp = sp.trim();
64  
65                  if (!sp.startsWith("http://")) {
66                      log.warning("Discarding non-http callback URL: " + sp);
67                      continue;
68                  }
69  
70                  URL url = new URL(sp);
71                  try {
72                      /*
73                          On some platforms (Android...), a valid URL might not be a valid URI, so
74                          we need to test for this and skip any invalid URI, e.g.
75  
76                          Java.net.URISyntaxException: Invalid % sequence: %wl in authority at index 32: http://[fe80::208:caff:fec4:824e%wlan0]:8485/eventSub
77      		                at libcore.net.UriCodec.validate(UriCodec.java:58)
78                              at java.net.URI.parseURI(URI.java:394)
79                              at java.net.URI.<init>(URI.java:204)
80                              at java.net.URL.toURI(URL.java:497)
81              	    */
82                      url.toURI();
83                  } catch (URISyntaxException ex) {
84                      log.log(Level.WARNING, "Discarding callback URL, not a valid URI on this platform: " + url, ex);
85                      continue;
86                  }
87  
88                  urls.add(url);
89              }
90              setValue(urls);
91          } catch (MalformedURLException ex) {
92              throw new InvalidHeaderException("Can't parse callback URLs from '" + s + "': " + ex);
93          }
94      }
95  
96      public String getString() {
97          StringBuilder s = new StringBuilder();
98          for (URL url : getValue()) {
99              s.append("<").append(url.toString()).append(">");
100         }
101         return s.toString();
102     }
103 }