标签:mes als provider url 状态 tor ESS new return
package com.test;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import entity.ErrorInfo;
public class JerseyClient {
private static final Logger log = LoggerFactory.getLogger(ResourceUtil.EMS);
/**
*
* @return HttpAuthenticationFeature
* */
protected static HttpAuthenticationFeature getNetconfAuth() {
return HttpAuthenticationFeature.basicBuilder().credentials(
PropertiesUtil.getString("netconf_username"),
PropertiesUtil.getString("netconf_password")).build();
}
public static ErrorInfo post(String url, String input, boolean auth) {
log.info("Post Method: url={}, input={}", url, input);
Client client = ClientBuilder.newClient();
WebTarget webTar = client.target(url);
if (auth) {
HttpAuthenticationFeature authen = getNetconfAuth();
webTar = webTar.register(authen);
}
Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
Response res = invBuilder.post(Entity.entity(input, MediaType.APPLICATION_JSON));
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setErrorCode(res.getStatus());
String json = res.readEntity(String.class);
errorInfo.setErrorDesc(json);
String str = errorInfo.toString();
if (log.isDebugEnabled()) {
log.debug(str);
} else {
log.info(str.length()>512 ? str.substring(0, 511) : str);
}
return errorInfo;
}
public static ErrorInfo get(String url, boolean auth) {
log.info("Get Method: url={}", url);
Client client = ClientBuilder.newClient();
WebTarget webTar = client.target(url);
if (auth) {
HttpAuthenticationFeature authen = getNetconfAuth();
webTar = webTar.register(authen);
}
Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
Response res = invBuilder.get();
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setErrorCode(res.getStatus());
String json = res.readEntity(String.class);
errorInfo.setErrorDesc(json);
String str = errorInfo.toString();
if (log.isDebugEnabled()) {
log.debug(str);
} else {
log.info(str.length()>512 ? str.substring(0, 511) : str);
}
return errorInfo;
}
public static String getWithJson(String url, boolean auth) {
log.info("Get Method: url={}", url);
Client client = ClientBuilder.newClient();
WebTarget webTar = client.target(url);
if (auth) {
HttpAuthenticationFeature authen = getNetconfAuth();
webTar = webTar.register(authen);
}
Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON,MediaType.TEXT_PLAIN);
Response res = invBuilder.get();
String json = res.readEntity(String.class);
log.info("Get Method, body={}", json);
return json;
}
public static ErrorInfo delete(String url, boolean auth) {
log.info("Delete Method: url={}", url);
Client client = ClientBuilder.newClient();
WebTarget webTar = client.target(url);
if (auth) {
HttpAuthenticationFeature authen = getNetconfAuth();
webTar = webTar.register(authen);
}
Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
Response res = invBuilder.delete();
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setErrorCode(res.getStatus());
String json = res.readEntity(String.class);
errorInfo.setErrorDesc(json);
String str = errorInfo.toString();
if (log.isDebugEnabled()) {
log.debug(str);
} else {
log.info(str.length()>512 ? str.substring(0, 511) : str);
}
return errorInfo;
}
public static ErrorInfo patch(String url, String input, boolean auth) {
log.info("Patch Method: url={}, input={}", url, input);
Client client = ClientBuilder.newClient();
WebTarget webTar = client.target(url);
if (auth) {
HttpAuthenticationFeature authen = getNetconfAuth();
webTar = webTar.register(authen);
}
Invocation.Builder invBuilder = webTar.request();
Entity<String> entity = Entity.entity(input, "application/yang.patch+json");
Response res = invBuilder.build("PATCH", entity)
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true).invoke();
ErrorInfo errorInfo = new ErrorInfo();
/* patch方法的返回结果在状态码中无法获取,必须解析返回的应答内容 */
String json = res.readEntity(String.class);
if (json.contains(ResourceUtil.ERRORS)) {
errorInfo.setErrorCode(400);
} else {
errorInfo.setErrorCode(200);
}
errorInfo.setErrorDesc(json);
String str = errorInfo.toString();
if (log.isDebugEnabled()) {
log.debug(str);
} else {
log.info(str.length()>512 ? str.substring(0, 511) : str);
}
return errorInfo;
}
public static ErrorInfo put(String url, String input, boolean auth) {
log.info("Put Method: url={}, input={}", url, input);
Client client = ClientBuilder.newClient();
WebTarget webTar = client.target(url);
if (auth) {
HttpAuthenticationFeature authen = getNetconfAuth();
webTar = webTar.register(authen);
}
Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
Response res = invBuilder.put(Entity.entity(input, MediaType.APPLICATION_JSON));
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setErrorCode(res.getStatus());
String json = res.readEntity(String.class);
errorInfo.setErrorDesc(json);
String str = errorInfo.toString();
if (log.isDebugEnabled()) {
log.debug(str);
} else {
log.info(str.length()>512 ? str.substring(0, 511) : str);
}
return errorInfo;
}
public static String rpcPost(String url, String input, boolean auth) {
log.info("rpcPost Method: url={}, input={}", url, input);
Client client = ClientBuilder.newClient();
WebTarget webTar = client.target(url);
if (auth) {
HttpAuthenticationFeature authen = getNetconfAuth();
webTar = webTar.register(authen);
}
Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
Response res = invBuilder.post(Entity.entity(input, MediaType.APPLICATION_JSON));
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setErrorCode(res.getStatus());
errorInfo.setErrorDesc(res.getStatusInfo().getReasonPhrase());
JSONObject returnJson = (JSONObject) JSONObject.toJSON(errorInfo);
// returnJson.put("returnBody", res.readEntity(JSONObject.class));
JSONObject resJson = null;
try {
resJson = res.readEntity(JSONObject.class);
} catch (ProcessingException e) {
log.error("rpcPost Exception:", e.getMessage(), e);
} catch (IllegalStateException e) {
log.error("rpcPost Exception:", e.getMessage(), e);
}
returnJson.put("returnBody", resJson);
log.info("rpcPost Method return:{}", returnJson.toJSONString());
return returnJson.toJSONString();
}
public static String devrRpcPost(String url, String input, boolean auth) {
log.info("rpcPost Method: url={}, input={}", url, input);
Client client = ClientBuilder.newClient();
WebTarget webTar = client.target(url);
if (auth) {
HttpAuthenticationFeature authen = getNetconfAuth();
webTar = webTar.register(authen);
}
Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
Response res = invBuilder.post(Entity.entity(input, MediaType.APPLICATION_JSON));
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setErrorCode(res.getStatus());
errorInfo.setErrorDesc(res.getStatusInfo().getReasonPhrase());
// JSONObject returnJson = (JSONObject) JSONObject.toJSON(errorInfo);
// returnJson.put("returnBody", res.readEntity(JSONObject.class));
JSONObject jsonObject = new JSONObject();
jsonObject.put(Constant.ERRORCODE,res.getStatus());
JSONObject returnJson = (JSONObject) JSONObject.toJSON(errorInfo);
log.info("rpcPost Method return:{}", returnJson.toJSONString());
return jsonObject.toJSONString();
}
}
标签:mes als provider url 状态 tor ESS new return
原文地址:https://www.cnblogs.com/castielangel/p/11143646.html