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.support.model;
17  
18  import org.fourthline.cling.model.action.ActionArgumentValue;
19  import org.fourthline.cling.model.meta.Service;
20  import org.fourthline.cling.model.types.UnsignedIntegerFourBytes;
21  import org.fourthline.cling.model.types.UnsignedIntegerTwoBytes;
22  
23  import java.util.Map;
24  
25  /**
26   * @author Christian Bauer
27   */
28  public class PortMapping {
29  
30      public enum Protocol {
31          UDP,
32          TCP
33      }
34  
35      private boolean enabled;
36      private UnsignedIntegerFourBytes leaseDurationSeconds;
37      private String remoteHost;
38      private UnsignedIntegerTwoBytes externalPort;
39      private UnsignedIntegerTwoBytes internalPort;
40      private String internalClient;
41      private Protocol protocol;
42      private String description;
43  
44      public PortMapping() {
45      }
46  
47  
48      public PortMapping(Map<String, ActionArgumentValue<Service>> map) {
49          this(
50                  (Boolean) map.get("NewEnabled").getValue(),
51                  (UnsignedIntegerFourBytes) map.get("NewLeaseDuration").getValue(),
52                  (String) map.get("NewRemoteHost").getValue(),
53                  (UnsignedIntegerTwoBytes) map.get("NewExternalPort").getValue(),
54                  (UnsignedIntegerTwoBytes) map.get("NewInternalPort").getValue(),
55                  (String) map.get("NewInternalClient").getValue(),
56                  Protocol.valueOf(map.get("NewProtocol").toString()),
57                  (String) map.get("NewPortMappingDescription").getValue()
58          );
59      }
60  
61      public PortMapping(int port, String internalClient, Protocol protocol) {
62          this(
63                  true,
64                  new UnsignedIntegerFourBytes(0),
65                  null,
66                  new UnsignedIntegerTwoBytes(port),
67                  new UnsignedIntegerTwoBytes(port),
68                  internalClient,
69                  protocol,
70                  null
71          );
72      }
73  
74      public PortMapping(int port, String internalClient, Protocol protocol, String description) {
75          this(
76                  true,
77                  new UnsignedIntegerFourBytes(0),
78                  null,
79                  new UnsignedIntegerTwoBytes(port),
80                  new UnsignedIntegerTwoBytes(port),
81                  internalClient,
82                  protocol,
83                  description
84          );
85      }
86  
87      public PortMapping(String remoteHost, UnsignedIntegerTwoBytes externalPort, Protocol protocol) {
88          this(
89                  true,
90                  new UnsignedIntegerFourBytes(0),
91                  remoteHost,
92                  externalPort,
93                  null,
94                  null,
95                  protocol,
96                  null
97          );
98      }
99  
100     public PortMapping(boolean enabled, UnsignedIntegerFourBytes leaseDurationSeconds, String remoteHost, UnsignedIntegerTwoBytes externalPort,
101                        UnsignedIntegerTwoBytes internalPort, String internalClient, Protocol protocol, String description) {
102         this.enabled = enabled;
103         this.leaseDurationSeconds = leaseDurationSeconds;
104         this.remoteHost = remoteHost;
105         this.externalPort = externalPort;
106         this.internalPort = internalPort;
107         this.internalClient = internalClient;
108         this.protocol = protocol;
109         this.description = description;
110     }
111 
112     public boolean isEnabled() {
113         return enabled;
114     }
115 
116     public void setEnabled(boolean enabled) {
117         this.enabled = enabled;
118     }
119 
120     public UnsignedIntegerFourBytes getLeaseDurationSeconds() {
121         return leaseDurationSeconds;
122     }
123 
124     public void setLeaseDurationSeconds(UnsignedIntegerFourBytes leaseDurationSeconds) {
125         this.leaseDurationSeconds = leaseDurationSeconds;
126     }
127 
128     public boolean hasRemoteHost() {
129         return remoteHost != null && remoteHost.length() > 0;
130     }
131 
132     public String getRemoteHost() {
133         return remoteHost == null ? "-" : remoteHost;
134     }
135 
136     public void setRemoteHost(String remoteHost) {
137         this.remoteHost = remoteHost == null || remoteHost.equals("-") || remoteHost.length() == 0 ? null : remoteHost;
138     }
139 
140     public UnsignedIntegerTwoBytes getExternalPort() {
141         return externalPort;
142     }
143 
144     public void setExternalPort(UnsignedIntegerTwoBytes externalPort) {
145         this.externalPort = externalPort;
146     }
147 
148     public UnsignedIntegerTwoBytes getInternalPort() {
149         return internalPort;
150     }
151 
152     public void setInternalPort(UnsignedIntegerTwoBytes internalPort) {
153         this.internalPort = internalPort;
154     }
155 
156     public String getInternalClient() {
157         return internalClient;
158     }
159 
160     public void setInternalClient(String internalClient) {
161         this.internalClient = internalClient;
162     }
163 
164     public Protocol getProtocol() {
165         return protocol;
166     }
167 
168     public void setProtocol(Protocol protocol) {
169         this.protocol = protocol;
170     }
171 
172     public boolean hasDescription() {
173         return description != null;
174     }
175 
176     public String getDescription() {
177         return description == null ? "-" : description;
178     }
179 
180     public void setDescription(String description) {
181         this.description = description == null || description.equals("-") || description.length() == 0 ? null : description;
182     }
183 
184     @Override
185     public String toString() {
186         return "(" + getClass().getSimpleName() + ") Protocol: " + getProtocol() + ", " + getExternalPort() + " => " + getInternalClient();
187     }
188 }