134d4f6870974457a8f13660568ae9282cd7b034
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / source / CdmServerInfo.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.remoting.source;
11
12 import java.io.IOException;
13 import java.net.Socket;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.apache.http.HttpEntity;
18 import org.apache.http.HttpResponse;
19 import org.apache.http.client.ClientProtocolException;
20 import org.apache.http.client.HttpClient;
21 import org.apache.http.client.ResponseHandler;
22 import org.apache.http.client.methods.HttpGet;
23 import org.apache.http.impl.client.DefaultHttpClient;
24 import org.apache.http.util.EntityUtils;
25 import org.apache.log4j.Logger;
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29
30 import eu.etaxonomy.cdm.config.CdmSourceException;
31 import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
32
33 /**
34 * @author cmathew
35 * @date 20 Jan 2015
36 *
37 */
38 public class CdmServerInfo {
39 public static final Logger logger = Logger.getLogger(CdmServerInfo.class);
40
41 private final static String CDMSERVER_PREFIX = "cdmserver";
42 private final static String NAME_PRODUCTION = "edit-production";
43 private final static String SERVER_PRODUCTION = "dev.e-taxonomy.eu";
44
45 private final static String NAME_INTEGRATION = "edit-integration";
46 private final static String SERVER_INTEGRATION = "int.e-taxonomy.eu";
47
48 private final static String NAME_TEST = "edit-test";
49 private final static String SERVER_TEST = "test.e-taxonomy.eu";
50
51 private final static String SERVER_LOCALHOST = "localhost";
52 private final static String NAME_LOCALHOST = "localhost";
53 private final static String NAME_LOCALHOST_MGD = "localhost mgd.";
54
55 private final static String NAME_LOCALHOST_DEV = "localhost-dev";
56 private final static String NAME_INSTANCE_LOCALHOST_DEV = "local-dev";
57 private final static String SERVER_LOCALHOST_DEV = "localhost";
58 private final static int PORT_LOCALHOST_DEV = 8080;
59 private final static String BASEPATH_LOCALHOST_DEV = "";
60
61 private final String name;
62 private final String server;
63 private final int port;
64 private final List<CdmInstanceInfo> instances;
65
66
67 public CdmServerInfo(String name, String server, int port) {
68 this.name = name;
69 this.server = server;
70 this.port = port;
71 instances = new ArrayList<CdmInstanceInfo>();
72 }
73
74
75 public CdmInstanceInfo addInstance(String name, String basePath) {
76 CdmInstanceInfo cii = new CdmInstanceInfo(name, basePath);
77 instances.add(cii);
78 return cii;
79
80 }
81
82 public boolean isLocalhost() {
83 return name.startsWith(SERVER_LOCALHOST_DEV);
84 }
85 public void refreshInstances() throws CDMServerException {
86 instances.clear();
87 String url = "http://" + server + ":" + String.valueOf(port) + "/" + CDMSERVER_PREFIX + "/instances.jsp";
88
89 HttpClient client = new DefaultHttpClient();
90 HttpGet httpGet = new HttpGet(url);
91
92 logger.info("Executing request " + httpGet.getRequestLine());
93
94 // Create a custom response handler
95 ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
96
97 @Override
98 public String handleResponse(
99 final HttpResponse response) throws ClientProtocolException, IOException {
100 int status = response.getStatusLine().getStatusCode();
101 if (status >= 200 && status < 300) {
102 HttpEntity entity = response.getEntity();
103 return entity != null ? EntityUtils.toString(entity) : null;
104 } else {
105 throw new ClientProtocolException("Unexpected response status: " + status);
106 }
107 }
108
109 };
110 String responseBody = null;
111 try {
112 responseBody = client.execute(httpGet, responseHandler);
113 } catch (ClientProtocolException e) {
114 throw new CDMServerException(e);
115 } catch (IOException e) {
116 throw new CDMServerException(e);
117 }
118
119
120 if(responseBody != null) {
121 try {
122 JSONArray array = new JSONArray(responseBody);
123 for(int i=0;i<array.length();i++) {
124 JSONObject instance = (JSONObject)array.get(i);
125 if(instance != null) {
126 JSONObject conf = (JSONObject)instance.get("configuration");
127 if(conf != null) {
128 String instanceName = conf.getString("instanceName");
129 // we need to remove the first (char) forward slash from
130 // the base path
131 String basePath = conf.getString("basePath").substring(1);
132 addInstance(instanceName, basePath);
133 logger.info("Added instance with name : " + instanceName + ", basePath : " + basePath);
134 }
135 }
136 }
137 } catch (JSONException e) {
138 throw new CDMServerException(e);
139 }
140 }
141
142 }
143
144 public String toString(String instanceName, int port) {
145 return server + ":" + String.valueOf(port) + "/" + instanceName;
146 }
147
148 public CdmInstanceInfo getInstanceFromName(String instanceName) {
149 if(instanceName == null) {
150 return null;
151 }
152
153 for(CdmInstanceInfo instance : instances) {
154 if(instance.getName() != null && instance.getName().equals(instanceName)) {
155 return instance;
156 }
157 }
158 return null;
159 }
160
161 public CdmRemoteSource getCdmRemoteSource(CdmInstanceInfo instance, int port) {
162 if(instance != null) {
163 return CdmRemoteSource.NewInstance(name,
164 server,
165 port,
166 instance.getBasePath(),
167 null);
168 }
169 return null;
170 }
171
172 public boolean pingServer(){
173 try {
174 Socket s = new Socket(server, port);
175 logger.info("[CDM-Server] Available @ " + server + ":" + port );
176 return true;
177 } catch (IOException ioe) {
178
179 }
180 return false;
181 }
182
183 public boolean pingInstance(CdmInstanceInfo instance, int port) throws CDMServerException {
184
185 ICdmRemoteSource crs = getCdmRemoteSource(instance, port);
186 try {
187 if(crs != null && crs.checkConnection()) {
188 logger.info("[CDM-Server] Running @ " + server + ":" + port + " for instance " + instance);
189 return true;
190 }
191 } catch (CdmSourceException e) {
192 throw new CDMServerException(e);
193 }
194
195 return false;
196 }
197
198 public static List<CdmServerInfo> getCdmServers() {
199 List<CdmServerInfo> cdmServerInfoList = new ArrayList<CdmServerInfo>();
200 cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80));
201 cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
202 cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
203 cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
204 cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST_MGD, SERVER_LOCALHOST,8080));
205 return cdmServerInfoList;
206 }
207
208 public String getName() {
209 return name;
210 }
211
212 public String getServer() {
213 return server;
214 }
215
216
217 public int getPort() {
218 return port;
219 }
220
221
222 public List<CdmInstanceInfo> getInstances() throws CDMServerException {
223 if(instances.isEmpty()) {
224 refreshInstances();
225 }
226 return instances;
227 }
228
229 public static CdmRemoteSource getDevServerRemoteSource() {
230 String value = System.getProperty("cdm.server.dev.port");
231 boolean available = false;
232 CdmInstanceInfo devInstance = null;
233 if(value != null && !value.isEmpty()) {
234 int devPort = Integer.valueOf(value);
235 CdmServerInfo devCii = new CdmServerInfo(NAME_LOCALHOST_DEV, SERVER_LOCALHOST_DEV, devPort);
236 try {
237 devInstance = devCii.addInstance(NAME_INSTANCE_LOCALHOST_DEV, BASEPATH_LOCALHOST_DEV);
238 available = devCii.pingInstance(devInstance, devPort);
239 if(available) {
240 return devCii.getCdmRemoteSource(devInstance, devPort);
241 }
242 } catch (Exception e) {
243
244 }
245 }
246 return null;
247 }
248
249 public class CdmInstanceInfo {
250 private final String name;
251 private final String basePath;
252
253
254 public CdmInstanceInfo(String name, String basePath) {
255 this.name = name;
256 this.basePath = basePath;
257 }
258
259
260 public String getName() {
261 return name;
262 }
263
264 public String getBasePath() {
265 return basePath;
266 }
267 }
268 }