package com.zl.utils;
import com.sun.istack.internal.NotNull;
import com.zl.model.AuthorizationDo;
import lombok.Getter;
import lombok.Setter;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.util.List;
import static com.zl.constant.Constant.*;
/**
* 自动化网络抓取海康NVR的设备列表及状态信息(使用的是同步的网络请求)
*
* @author 郑龙
* @date 2018-12-12 16:23
*/
public class NetUtils {
private OkHttpClient mClient;
private AuthorizationDo authorizationDo = new AuthorizationDo();
/**
* @param url
*/
public NetUtils(@NotNull String userAccount, @NotNull String userPassWord
, @NotNull String url) {
setUserAccount(userAccount);
setUserPassWord(userPassWord);
this.url = url;
//自动设置uri
url = url.substring(url.lastIndexOf("."));
String uri = url.substring(url.indexOf("/"));
//设置uri
setRequirResourceUrl(uri);
}
@Setter
@Getter
private String url;
/**
* 设置用户账户
*
* @param userAccount
*/
public void setUserAccount(String userAccount) {
this.authorizationDo.setUserAccount(userAccount);
}
/**
* 用户密码
*
* @param userPassWord
*/
public void setUserPassWord(String userPassWord) {
this.authorizationDo.setUserPassWord(userPassWord);
}
/**
* 设置请求的目标资源地址
*
* @param url
*/
public void setRequirResourceUrl(String url) {
this.authorizationDo.setUri(url);
}
/**
* 网络请求主体
*
* @param headerAuthorization
* @return
*/
public String request(String headerAuthorization) {
mClient = new OkHttpClient();
Request request;
if (headerAuthorization != null && headerAuthorization != "") {
//如果已计算出了Authorization的值
request = new Request.Builder().url(url).get().header(
"Authorization", headerAuthorization).build();
} else {
//否则按第一次请求来处理
request = new Request.Builder().url(url).get().build();
}
if (request != null) {
//第一次请求,拿到header
try {
Response response = mClient.newCall(request).execute();
if (response.code() == UNAUTHORIZED_CODE) {
//第一次请求一定会401,需要验证
List<String> headers = response.headers("WWW-Authenticate");
String header = null;
for (String str : headers) {
if ("D".equalsIgnoreCase(str.substring(0, 1))) {
header = str;
}
}
String newHeader = analysis(header);
//处理后重新请求
return request(newHeader);
} else {
if (response.code() == HTTP_OK) {
return response.body().string();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 拆分并解析原始Header
*
* @param originHeaderStr 原始header信息
* @return
*/
private String analysis(String originHeaderStr) {
originHeaderStr = originHeaderStr.substring(originHeaderStr.indexOf(" "));
String[] items = originHeaderStr.replaceAll(" ", "")
.replaceAll("\"", "").split(",");
for (String str : items) {
String type = str.substring(0, str.indexOf("="));
String value = str.substring(str.indexOf("=") + 1);
//完善实体
switch (type) {
case "qop":
this.authorizationDo.setQop(value);
break;
case "nonce":
this.authorizationDo.setNonce(value);
break;
case "realm":
this.authorizationDo.setRealm(value);
break;
case "algorithm":
this.authorizationDo.setAlgorithm(value);
break;
default:
break;
}
}
//尝试写死cnonce值
this.authorizationDo.setCnonce(CONST_CNONCE);
//写死nc
this.authorizationDo.setNc(CONST_NC);
//计算并写入response
this.authorizationDo.setResponse(CalcUtils
.calcResponse(this.authorizationDo.getUserAccount(),
this.authorizationDo.getRealm(),
this.authorizationDo.getUserPassWord(),
CONST_METHOD_GET, this.authorizationDo.getUri(),
this.authorizationDo.getNonce(),
this.authorizationDo.getNc(),
this.authorizationDo.getCnonce(),
this.authorizationDo.getQop()));
return this.authorizationDo.toString();
}
}