|
@ -1,55 +1,100 @@
|
|
|
package com.yihu.hos.client;
|
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
/**
|
|
|
* @created Airhead 2016/8/16.
|
|
|
*/
|
|
|
public class BrokerServer {
|
|
|
private String host;
|
|
|
private String ip;
|
|
|
class BrokerServer {
|
|
|
private String hostName;
|
|
|
private String hostAddress;
|
|
|
private String port;
|
|
|
private Boolean enable;
|
|
|
|
|
|
public String getHost() {
|
|
|
return host;
|
|
|
public String getHostName() {
|
|
|
return hostName;
|
|
|
}
|
|
|
|
|
|
public void setHost(String host) {
|
|
|
this.host = host;
|
|
|
void setHostName(String hostName) {
|
|
|
this.hostName = hostName;
|
|
|
}
|
|
|
|
|
|
public String getIp() {
|
|
|
return ip;
|
|
|
public String getHostAddress() {
|
|
|
return hostAddress;
|
|
|
}
|
|
|
|
|
|
public void setIp(String ip) {
|
|
|
this.ip = ip;
|
|
|
void setHostAddress(String hostAddress) {
|
|
|
this.hostAddress = hostAddress;
|
|
|
}
|
|
|
|
|
|
public String getPort() {
|
|
|
return port;
|
|
|
}
|
|
|
|
|
|
public void setPort(String port) {
|
|
|
void setPort(String port) {
|
|
|
this.port = port;
|
|
|
}
|
|
|
|
|
|
public Boolean getEnable() {
|
|
|
Boolean getEnable() {
|
|
|
return enable;
|
|
|
}
|
|
|
|
|
|
public void setEnable(Boolean enable) {
|
|
|
void setEnable(Boolean enable) {
|
|
|
this.enable = enable;
|
|
|
}
|
|
|
|
|
|
public String getIdentity() {
|
|
|
return this.ip + ":" + this.port;
|
|
|
String getIdentity() {
|
|
|
return this.hostAddress + ":" + this.port;
|
|
|
}
|
|
|
|
|
|
public Response invokeSync(Request request) {
|
|
|
return null;
|
|
|
Response invokeSync(Request request) {
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
String method = request.getMethod();
|
|
|
if (request.getMethod().startsWith("/")) {
|
|
|
method = request.getMethod().substring(1);
|
|
|
}
|
|
|
|
|
|
HttpPost httpPost = new HttpPost(hostAddress + ":" + port + method);
|
|
|
CloseableHttpResponse response = null;
|
|
|
Response result = new Response();
|
|
|
try {
|
|
|
response = httpclient.execute(httpPost);
|
|
|
if (response.getStatusLine().getStatusCode() != 200) {
|
|
|
EsbException esbException = new EsbException("Can not connect the server.");
|
|
|
result.setError(esbException);
|
|
|
result.setStackTrace(Arrays.toString(esbException.getStackTrace()));
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
String body = EntityUtils.toString(entity, "UTF-8");
|
|
|
result.setResult(body);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
|
|
|
result.setError(e);
|
|
|
result.setStackTrace(Arrays.toString(e.getStackTrace()));
|
|
|
} finally {
|
|
|
assert response != null;
|
|
|
try {
|
|
|
response.close();
|
|
|
httpclient.close();
|
|
|
} catch (IOException ignored) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public void invokeAsync(Request request, final ResultCallback<Response> callback) {
|
|
|
void invokeAsync(Request request, final ResultCallback<Response> callback) {
|
|
|
}
|
|
|
|
|
|
}
|