MANIFEST.MF : New apache http client packages added
[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 NAME_PRODUCTION = "edit-production";
42 private final static String SERVER_PRODUCTION = "dev.e-taxonomy.eu";
43
44 private final static String NAME_INTEGRATION = "edit-integration";
45 private final static String SERVER_INTEGRATION = "int.e-taxonomy.eu";
46
47 private final static String NAME_TEST = "edit-test";
48 private final static String SERVER_TEST = "test.e-taxonomy.eu";
49
50 private final static String SERVER_LOCALHOST = "localhost";
51 private final static String NAME_LOCALHOST = "localhost";
52 private final static String NAME_LOCALHOST_MGD = "localhost mgd.";
53
54 private final String name;
55 private final String server;
56 private final int port;
57 private final List<CdmInstanceInfo> instances;
58
59
60 public CdmServerInfo(String name, String server, int port) {
61 this.name = name;
62 this.server = server;
63 this.port = port;
64 instances = new ArrayList<CdmInstanceInfo>();
65 }
66
67
68 public void refreshInstances() throws CDMServerException {
69 String url = "http://" + server + ":" + String.valueOf(port) + "/cdmserver/instances.jsp";
70
71 HttpClient client = new DefaultHttpClient();
72 HttpGet httpGet = new HttpGet(url);
73
74 logger.info("Executing request " + httpGet.getRequestLine());
75
76 // Create a custom response handler
77 ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
78
79 @Override
80 public String handleResponse(
81 final HttpResponse response) throws ClientProtocolException, IOException {
82 int status = response.getStatusLine().getStatusCode();
83 if (status >= 200 && status < 300) {
84 HttpEntity entity = response.getEntity();
85 return entity != null ? EntityUtils.toString(entity) : null;
86 } else {
87 throw new ClientProtocolException("Unexpected response status: " + status);
88 }
89 }
90
91 };
92 String responseBody = null;
93 try {
94 responseBody = client.execute(httpGet, responseHandler);
95 } catch (ClientProtocolException e) {
96 throw new CDMServerException(e);
97 } catch (IOException e) {
98 throw new CDMServerException(e);
99 }
100
101
102 if(responseBody != null) {
103 try {
104 JSONArray array = new JSONArray(responseBody);
105 for(int i=0;i<array.length();i++) {
106 JSONObject instance = (JSONObject)array.get(i);
107 if(instance != null) {
108 JSONObject conf = (JSONObject)instance.get("configuration");
109 if(conf != null) {
110 String instanceName = conf.getString("instanceName");
111 // we need to remove the first (char) forward slash from
112 // the base path
113 String basePath = conf.getString("basePath").substring(1);
114 instances.add(new CdmInstanceInfo(instanceName, basePath));
115 logger.info("Added instance with name : " + instanceName + ", basePath : " + basePath);
116 }
117 }
118 }
119 } catch (JSONException e) {
120 throw new CDMServerException(e);
121 }
122 }
123
124 }
125
126 public CdmInstanceInfo getInstanceFromName(String instanceName) {
127 if(instanceName == null) {
128 return null;
129 }
130
131 for(CdmInstanceInfo instance : instances) {
132 if(instance.getName() != null && instance.getName().equals(instanceName)) {
133 return instance;
134 }
135 }
136 return null;
137 }
138
139 public CdmRemoteSource getCdmRemoteSource(CdmInstanceInfo instance) {
140 if(instance != null) {
141 return CdmRemoteSource.NewInstance(name,
142 server,
143 port,
144 instance.getBasePath(),
145 null);
146 }
147 return null;
148 }
149
150 public boolean pingServer(){
151 try {
152 Socket s = new Socket(server, port);
153 logger.info("[CDM-Server] Available @ " + server + ":" + port );
154 return true;
155 } catch (IOException ioe) {
156
157 }
158 return false;
159 }
160
161 public boolean pingInstance(CdmInstanceInfo instance) throws CDMServerException {
162
163 CdmRemoteSource crs = getCdmRemoteSource(instance);
164 try {
165 if(crs != null && crs.checkConnection()) {
166 logger.info("[CDM-Server] Running @ " + server + ":" + port + " for instance " + instance);
167 return true;
168 }
169 } catch (CdmSourceException e) {
170 throw new CDMServerException(e);
171 }
172
173 return false;
174 }
175
176 public static List<CdmServerInfo> getCdmServers() {
177 List<CdmServerInfo> cdmServerInfoList = new ArrayList<CdmServerInfo>();
178 cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80));
179 cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
180 cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
181 cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
182 cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST_MGD, SERVER_LOCALHOST,8080));
183 return cdmServerInfoList;
184 }
185
186 public String getName() {
187 return name;
188 }
189
190 public String getServer() {
191 return server;
192 }
193
194
195 public int getPort() {
196 return port;
197 }
198
199
200 public List<CdmInstanceInfo> getInstances() throws CDMServerException {
201 if(instances.isEmpty()) {
202 refreshInstances();
203 }
204 return instances;
205 }
206
207 public class CdmInstanceInfo {
208 private final String name;
209 private final String basePath;
210
211
212 public CdmInstanceInfo(String name, String basePath) {
213 this.name = name;
214 this.basePath = basePath;
215 }
216
217
218 public String getName() {
219 return name;
220 }
221
222 public String getBasePath() {
223 return basePath;
224 }
225 }
226 }