package com.xx.xx;;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.params.CoreConnectionPNames;
import com.test.kmss.plugin.util.StringUtil;
import net.sf.json.JSONObject;
import sun.misc.BASE64Encoder;
public class SignRestServiceUtil {
private final static String CONNECTION_ERROR = "false";
private final static String TOKEN_PASSWORD ="账号:密码"; //认证接口的账号密码
private final static String NATIVE_NUMBER = "native"; // 取本地的number标识
private final static String WEBSERVICE_NUMBER = "webService"; //webService拿到的number标识
/**
* 给接口传入日志记录
* @throws Exception
*/
public static boolean addSignLog(ProdQualitySignParameter prodQualitySignParameter) throws Exception {
boolean returnStatu = false;
String url = "http://ip:8080/xxxx/xxx/xxxx"; //这里是请求地址
ProdQualitySignParameter parameter = new ProdQualitySignParameter();
parameter.setSvnUrl(prodQualitySignParameter.getSvnUrl());
parameter.setSignAccount(prodQualitySignParameter.getSignAccount());
parameter.setSignFiles(prodQualitySignParameter.getSignType());
parameter.setSignFiles(prodQualitySignParameter.getSignFiles());
parameter.setSignTime(prodQualitySignParameter.getSignTime());
try {
String str = TOKEN_PASSWORD;
byte[] b = str.getBytes("utf-8");
str = new BASE64Encoder().encode(b); //使用base64对用户名:密码进行加密
HttpClient client = HttpClientUtil.createClient();
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,5000);
PostMethod postMethod = HttpClientUtil.createPostMethod(url);
postMethod.setRequestHeader("Authorization", "Basic " + str); //在请求头添加Authorization字段 “Basic ”这里有个空格
JSONObject jsonParam = JSONObject.fromObject(parameter);
String jsonStr = jsonParam.toString();
StringRequestEntity entity = new StringRequestEntity(jsonStr, "application/json", "UTF-8");
postMethod.setRequestEntity(entity);
postMethod.setRequestHeader("Content-Type", "application/json;charset=utf8");
String result = HttpClientUtil.getDataByHttpClient(client, postMethod);
JSONObject json = JSONObject.fromObject(result);
String status = json.getString("returnState");
if(StringUtil.isNotNull(status) && "1".equals(status)){
returnStatu = true;
}
} catch (Exception e) {
returnStatu = false;
}
return returnStatu;
}
/*获取剩余次数*/
public static Map<String,Integer> getSignRemainderTimes(String svnurlParam) throws Exception {
Map<String,Integer> map = new HashMap<String,Integer>();
int num = 0;
String url = "http://XXX:8080/XXX/XXXX?param="; //请求的地址
String param = "XXX"; //参数
String value = "XXX"; //对应的值
url = setQueryParameter(url, param, value);
try {
HttpClient client = HttpClientUtil.createClient();
String result = getAuth(url); //请求用get
JSONObject json = JSONObject.fromObject(result);
String status = json.getString("returnState");
String message = json.getString("message");
JSONObject messageJson = JSONObject.fromObject(json.getString("message")); //这里是json里面还有个json,可以一次性拿到
num = messageJson.getInt("times");
map.put(WEBSERVICE_NUMBER, num);
} catch (Exception e) {
num = getCount();
map.put(NATIVE_NUMBER, num);
}
return map;
}
/*校验权限的方法*/
public static String validateSignAuth(String param1,String param2,String param3) throws Exception {
String result = "";
String url = "http://XXX/XXX?param1=¶m2=¶m3=";
Map<String, String> param_map = new HashMap();
param_map.put("param1", param1);
param_map.put("param2", param2);
param_map.put("param3",param3);
for (Map.Entry<String, String> entry : param_map.entrySet()) {
url = setQueryParameter(url, entry.getKey(), entry.getValue());
}
try {
HttpClient client = HttpClientUtil.createClient();
String data = getAuth(url);
JSONObject json = JSONObject.fromObject(data);
String returnState = json.getString("returnState");
String message = json.getString("message");
if(StringUtil.isNotNull(returnState)){
result = returnState;
}
} catch (Exception e) {
//校验权限得接口出错,返回false
result = CONNECTION_ERROR;
e.printStackTrace();
}
return result;
}
/**
* 替换URL中?后面部分的参数。
*/
private static String setQueryParameter(String url, String param,
String value) {
String rtnVal = null;
try {
String m_query = StringUtil.isNull(url) ? "" : url;
String m_param = param + "=";
String m_value = URLEncoder.encode(value, "UTF-8");
rtnVal = m_query.replaceFirst(m_param, m_param+ m_value);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return rtnVal;
}
public static String getAuth(String url) {
String rb = null;
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
try {
String str = TOKEN_PASSWORD; //这是能通过认证的用户名和密码
byte[] b = str.getBytes("utf-8");
//设置响应时间,不设置,可能会响应很久才会报出timeout异常,一般5秒左右没有响应认为服务端出现问题
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,5000);
str = new BASE64Encoder().encode(b); //使用base64对用户名:密码进行加密
getMethod.setRequestHeader("Authorization", "Basic " + str); //在请求头添加Authorization字段 “Basic ”这里有个空格
client.executeMethod(getMethod);
rb = getMethod.getResponseBodyAsString();
}catch (Exception e) {
e.printStackTrace();
}
return rb;
}
/*当接口调不通时候,取本地的记录日志的次数*/
public static int getCount() throws Exception {
int number = 0;
File file = getFile();
BufferedReader br = new BufferedReader(new FileReader(file));
String firstLine = "";
firstLine = br.readLine();
br.close();
if(StringUtil.isNotNull(firstLine)){
try{
number = Integer.parseInt(firstLine);
}catch(Exception e){
number = 0;
}
}
return number;
}
/*操作本地的记录日志的txt文件*/
public static File getFile() throws Exception{
String path = System.getProperty("user.dir");
System.out.println("path = "+ path);
String filePath = path + "\\features\\KmssDevPlugin_1.0.0.v20200123.txt";
System.out.println("filePath = "+ filePath);
File file = new File(filePath);
if(!file.exists()){
file.createNewFile();
}
return file;
}
}
版权声明:本文为qq_42180345原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。