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;
17  
18  import java.net.URI;
19  import java.net.URL;
20  import java.net.URISyntaxException;
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Locale;
24  
25  /**
26   * A request message, with a method (GET, POST, NOTIFY, etc).
27   *
28   * @author Christian Bauer
29   */
30  public class UpnpRequest extends UpnpOperation {
31  
32      public static enum Method {
33  
34          GET("GET"),
35          POST("POST"),
36          NOTIFY("NOTIFY"),
37          MSEARCH("M-SEARCH"),
38          SUBSCRIBE("SUBSCRIBE"),
39          UNSUBSCRIBE("UNSUBSCRIBE"),
40          UNKNOWN("UNKNOWN");
41  
42          private static Map<String, Method> byName = new HashMap<String, Method>() {{
43              for (Method m : Method.values()) {
44                  put(m.getHttpName(), m);
45              }
46          }};
47  
48          private String httpName;
49  
50          Method(String httpName) {
51              this.httpName = httpName;
52          }
53  
54          public String getHttpName() {
55              return httpName;
56          }
57  
58          public static Method getByHttpName(String httpName) {
59              if (httpName == null) return UNKNOWN;
60          	Method m = byName.get(httpName.toUpperCase(Locale.ROOT));
61              return m != null ? m : UNKNOWN;
62          }
63      }
64  
65      private Method method;
66      private URI uri;
67  
68      public UpnpRequest(Method method) {
69          this.method = method;
70      }
71  
72      public UpnpRequest(Method method, URI uri) {
73          this.method = method;
74          this.uri = uri;
75      }
76  
77      public UpnpRequest(Method method, URL url) {
78          this.method = method;
79          try {
80              if (url != null) {
81                  this.uri = url.toURI();
82              }
83          } catch (URISyntaxException e) {
84              throw new IllegalArgumentException(e);
85          }
86      }
87  
88      public Method getMethod() {
89          return method;
90      }
91  
92      public String getHttpMethodName() {
93          return method.getHttpName();
94      }
95  
96      public URI getURI() {
97          return uri;
98      }
99  
100     public void setUri(URI uri) {
101         this.uri = uri;
102     }
103 
104     @Override
105     public String toString() {
106         return getHttpMethodName() + (getURI() != null ? " " + getURI() : "");
107     }
108 }