BrightSide Workbench Full Report + Source Code
WsServer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.ws;
20 
21 import java.io.Serializable;
22 import java.net.UnknownHostException;
23 import java.util.Map;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.persistence.Entity;
27 import javax.persistence.Id;
28 import javax.persistence.IdClass;
29 import org.turro.string.Phrases;
30 import org.turro.string.Strings;
31 import org.turro.url.URIs;
32 import org.turro.action.Contacts;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.jpa.entity.IDaoEntity;
35 import org.turro.json.IJSONizable;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.util.Chars;
38 
43 @Entity
44 @IdClass(WsServerPK.class)
45 public class WsServer implements Serializable, IDaoEntity, IJSONizable {
46 
47  @Id private String serverDomain;
48  @Id private String service;
49 
50  private String contactId;
51 
52  public String getServerDomain() {
53  return serverDomain;
54  }
55 
56  public void setServerDomain(String serverDomain) {
57  this.serverDomain = serverDomain;
58  }
59 
60  public String getService() {
61  return service;
62  }
63 
64  public void setService(String service) {
65  this.service = service;
66  }
67 
68  public String getContactId() {
69  return contactId;
70  }
71 
72  public void setContactId(String contactId) {
73  this.contactId = contactId;
74  }
75 
76  /* Utils */
77 
78  public boolean isValidIP(String ip) {
79  try {
80  String[] parts = serverDomain.split("[\\:\\/]");
81  return URIs.validIP(parts[0], ip);
82  } catch (UnknownHostException ex) {
83  Logger.getLogger(WsServer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
84  return false;
85  }
86  }
87 
88  public String getServerStrId() {
89  return getServerDomain() + getService();
90  }
91 
92  public String getName() {
93  return Phrases.start(getContact().getName(), Chars.forward().toString(), getService()).toString();
94  }
95 
96  public String getStandardPars() {
97  return "serverDomain=%s&service=%s".formatted(serverDomain, service);
98  }
99 
100  /* Service name */
101 
102  private transient ServiceNamePair serviceName;
103 
105  return serviceName == null ? new ServiceNamePair(service, service) : serviceName;
106  }
107 
108  public void setServiceName(ServiceNamePair serviceName) {
109  this.serviceName = serviceName;
110  this.service = serviceName.getService();
111  }
112 
113  /* Contact */
114 
115  public IContact getContact() {
116  return Contacts.getContactById(contactId);
117  }
118 
119  public void setContact(IContact contact) {
120  contactId = contact == null ? null : contact.getId();
121  }
122 
123  /* IDaoEntity */
124 
125  @Override
126  public Object entityId() {
127  return WsServerPK.from(serverDomain, service);
128  }
129 
130  @Override
131  public boolean isEmpty() {
132  return Strings.isBlank(contactId) || Strings.isBlank(serverDomain);
133  }
134 
135  /* JSONizable */
136 
137  @Override
138  public String toJson() {
139  return toJson(this);
140  }
141 
142  @Override
143  public String toJson(Map<String, Object> properties) {
144  return toJson(this, properties);
145  }
146 
147  /* Factory */
148 
149  public static WsServer from(String serverDomain, String service) {
150  WsServer server = new WsServer();
151  server.setServerDomain(serverDomain);
152  server.setService(service);
153  return server;
154  }
155 
156 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static WsServerPK from(String serverDomain, String service)
Definition: WsServerPK.java:33
static WsServer from(String serverDomain, String service)
Definition: WsServer.java:149
void setContactId(String contactId)
Definition: WsServer.java:72
boolean isValidIP(String ip)
Definition: WsServer.java:78
ServiceNamePair getServiceName()
Definition: WsServer.java:104
void setService(String service)
Definition: WsServer.java:64
void setServiceName(ServiceNamePair serviceName)
Definition: WsServer.java:108
void setContact(IContact contact)
Definition: WsServer.java:119
String getServerStrId()
Definition: WsServer.java:88
String getServerDomain()
Definition: WsServer.java:52
void setServerDomain(String serverDomain)
Definition: WsServer.java:56
String toJson(Map< String, Object > properties)
Definition: WsServer.java:143
String getStandardPars()
Definition: WsServer.java:96
IContact getContact()
Definition: WsServer.java:115