All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nervousync.beans.servlet.request.RequestInfo Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.nervousync.beans.servlet.request;

import java.io.File;
import java.io.IOException;
import java.util.*;

import jakarta.annotation.Nonnull;
import org.nervousync.commons.Globals;
import org.nervousync.proxy.AbstractProxyConfigBuilder;
import org.nervousync.enumerations.web.HttpMethodOption;
import org.nervousync.http.cert.TrustCert;
import org.nervousync.http.cookie.CookieEntity;
import org.nervousync.http.header.SimpleHeader;
import org.nervousync.proxy.ProxyConfig;
import org.nervousync.utils.FileUtils;

/**
 * 

Request information define

*

Using for parameter of method: org.nervousync.utils.RequestUtils#sendRequest

*

网络请求信息定义

*

用于方法org.nervousync.utils.RequestUtils#sendRequest的参数值

* * @author Steven Wee [email protected] * @version $Revision: 1.1.4 $ $Date: Sep 25, 2022 11:04:17 $ */ public final class RequestInfo { /** * Enumeration value of HttpMethodOption * HttpMethodOption的枚举值 * @see org.nervousync.enumerations.web.HttpMethodOption */ private final HttpMethodOption methodOption; /** * Proxy server config for sending request *

Default is null for direct connect

* 发送请求时使用的代理服务器设置 *

默认为null代表不使用代理服务器

* @see org.nervousync.proxy.ProxyConfig */ private final ProxyConfig proxyConfig; /** * Trusted certificate list for sending secure request *

Default is empty list for using JDK certificate library

* 发送加密请求时信任的证书列表 *

默认为空列表,代表使用JDK默认的证书库

* @see org.nervousync.http.cert.TrustCert */ private final List trustTrustCerts; /** * Pass phrase for system certificate library * 系统信任证书库读取密钥 */ private final String passPhrase; /** * Using for setting user agent string of request header * 用于设置请求头中的用户代理信息 */ private final String userAgent; /** * Current request url path * 当前请求地址 */ private final String requestUrl; /** * Character encoding for http request header "Content-Type" and send request body * 请求头"Content-Type"及发送请求体使用的编码集 */ private final String charset; /** * String value for http request header "Content-Type" * 请求头"Content-Type"的字符串值 */ private final String contentType; /** * Request timeout setting * 请求超时时间 */ private final int timeOut; /** * Binary data array of current request will post * 当前请求要发送的二进制数据数组 */ private final byte[] postData; /** * Request header information list * 发送请求的请求头信息列表 */ private final List headers; /** * Request parameters information mapping * 发送请求的参数信息映射 */ private final Map parameters; /** * Upload files of request parameters mapping * 发送请求的上传文件参数信息映射 */ private final Map uploadParams; /** * Request cookies information list * 发送请求的Cookie信息列表 */ private final List cookieList; /** *

Constructor for RequestInfo

*

Only using for RequestBuilder instance to generate RequestInfo instance

*

RequestInfo的构造方法

*

仅用于请求构造器生成RequestInfo实例对象使用

* * @param methodOption Enumeration value of HttpMethodOption * HttpMethodOption的枚举值 * @param requestUrl Current request url path * 当前请求地址 * @param charset Character encoding for http request header "Content-Type" and send request body * 请求头"Content-Type"及发送请求体使用的编码集 * @param timeOut Request timeout setting * 请求超时时间 * @param headers Request header information list * 发送请求的请求头信息列表 * @param parameters Request parameters information mapping * 发送请求的参数信息映射 * @param uploadParams Upload files of request parameters mapping * 发送请求的上传文件参数信息映射 * @param cookieList Request cookies information list * 发送请求的Cookie信息列表 */ private RequestInfo(final HttpMethodOption methodOption, final ProxyConfig proxyConfig, final List trustTrustCerts, final String passPhrase, final String userAgent, final String requestUrl, final String charset, final String contentType, final int timeOut, final byte[] postData, final List headers, final Map parameters, final Map uploadParams, final List cookieList) { this.methodOption = methodOption; this.proxyConfig = proxyConfig; this.trustTrustCerts = trustTrustCerts; this.passPhrase = passPhrase; this.userAgent = userAgent; this.requestUrl = requestUrl; this.charset = charset; this.contentType = contentType; this.timeOut = timeOut; this.postData = postData; this.headers = headers; this.parameters = parameters; this.uploadParams = uploadParams; this.cookieList = cookieList; } /** * Builder request builder. * * @param httpMethodOption the http method option * @return the request builder */ public static RequestBuilder builder(final HttpMethodOption httpMethodOption) { return new RequestBuilder(httpMethodOption); } /** *

Getter method for method option

*

请求类型的Getter方法

*/ public HttpMethodOption getMethodOption() { return methodOption; } /** *

Getter method for upload parameters

*

上传文件信息映射的Getter方法

*/ public Map getUploadParams() { return uploadParams; } /** *

Getter method for cookies list

*

请求发送的Cookie信息列表的Getter方法

*/ public List getCookieList() { return cookieList; } /** *

Getter method for proxy config

*

代理服务器设置的Getter方法

*/ public ProxyConfig getProxyInfo() { return proxyConfig; } /** *

Getter method for trusted certificate list

*

信任证书列表的Getter方法

*/ public List getTrustCertInfos() { return trustTrustCerts; } /** *

Getter method for pass phrase of system certificate list

*

系统信任证书库读取密钥的Getter方法

*/ public String getPassPhrase() { return passPhrase; } /** *

Getter method for user agent string

*

用户代理字符串的Getter方法

*/ public String getUserAgent() { return userAgent; } /** *

Getter method for request url

*

请求地址的Getter方法

*/ public String getRequestUrl() { return requestUrl; } /** *

Getter method for character encoding

*

数据编码集的Getter方法

*/ public String getCharset() { return charset; } /** *

Getter method for content type string

*

请求头"Content-Type"字符串的Getter方法

*/ public String getContentType() { return contentType; } /** *

Getter method for request time out

*

请求超时时间的Getter方法

*/ public int getTimeOut() { return timeOut; } /** *

Getter method for post binary data array

*

POST发送二进制数据的Getter方法

*/ public byte[] getPostData() { return postData; } /** *

Getter method for request header list

*

请求头信息列表的Getter方法

*/ public List getHeaders() { return headers; } /** *

Getter method for parameters mapping

*

请求参数信息映射的Getter方法

*/ public Map getParameters() { return parameters; } /** *

Getter method for upload parameters mapping

*

上传文件信息映射的Getter方法

*/ public Map getUploadParam() { return uploadParams; } /** *

Request proxy configure builder

*

网络请求代理服务器构建器

* * @author Steven Wee [email protected] * @version $Revision: 1.0.0 $ $Date: Aug 25, 2017 11:08:22 $ */ public static final class RequestProxyBuilder extends AbstractProxyConfigBuilder { /** *

Private constructor for RequestProxyBuilder

*

RequestProxyBuilder的私有构造方法

* * @param requestBuilder RequestBuilder instance * RequestBuilder实例对象 */ private RequestProxyBuilder(@Nonnull final RequestBuilder requestBuilder) { super(requestBuilder, Globals.DEFAULT_VALUE_STRING, requestBuilder.proxyConfig); } /** *

Confirm proxy configure

*

确认代理服务器配置

*/ @Override protected void build() { this.parentBuilder.proxyConfig(this.proxyConfig); } } /** *

Request builder

*

网络请求构建器

* */ public static final class RequestBuilder { /** * Enumeration value of HttpMethodOption * HttpMethodOption的枚举值 * @see org.nervousync.enumerations.web.HttpMethodOption */ private final HttpMethodOption methodOption; /** * Proxy server config for sending request *

Default is null for direct connect

* 发送请求时使用的代理服务器设置 *

默认为null代表不使用代理服务器

* @see org.nervousync.proxy.ProxyConfig */ private ProxyConfig proxyConfig; /** * Trusted certificate list for sending secure request *

Default is empty list for using JDK certificate library

* 发送加密请求时信任的证书列表 *

默认为空列表,代表使用JDK默认的证书库

* @see org.nervousync.http.cert.TrustCert */ private final List trustTrustCerts = new ArrayList<>(); /** * Pass phrase for system certificate library * 系统信任证书库读取密钥 */ private String passPhrase; /** * Using for setting user agent string of request header * 用于设置请求头中的用户代理信息 */ private String userAgent; /** * Current request url path * 当前请求地址 */ private String requestUrl; /** * Character encoding for http request header "Content-Type" and send request body * 请求头"Content-Type"及发送请求体使用的编码集 */ private String charset; /** * String value for http request header "Content-Type" * 请求头"Content-Type"的字符串值 */ private String contentType; /** * Request timeout setting * 请求超时时间 */ private int timeOut = Globals.DEFAULT_TIME_OUT; /** * Binary data array of current request will post * 当前请求要发送的二进制数据数组 */ private byte[] postData; /** * Request header information list * 发送请求的请求头信息列表 */ private final List headers = new ArrayList<>(); /** * Request parameters information mapping * 发送请求的参数信息映射 */ private final Map parameters = new HashMap<>(); /** * Upload files of request parameters mapping * 发送请求的上传文件参数信息映射 */ private final Map uploadParams = new HashMap<>(); /** * Request cookies information list * 发送请求的Cookie信息列表 */ private final List cookieList = new ArrayList<>(); private RequestBuilder(final HttpMethodOption methodOption) { this.methodOption = methodOption; } /** *

Confirm request info and generate RequestInfo instance

*

确认请求配置信息并生成RequestInfo实例对象

* * @return RequestInfo instance * RequestInfo实例对象 */ public RequestInfo build() { return new RequestInfo(this.methodOption, this.proxyConfig, this.trustTrustCerts, this.passPhrase, this.userAgent, this.requestUrl, this.charset, this.contentType, this.timeOut, this.postData, this.headers, this.parameters, this.uploadParams, this.cookieList); } /** *

Generate RequestProxyBuilder instance to configure proxy server

*

生成RequestProxyBuilder实例对象用于配置代理服务器

* * @return RequestProxyBuilder instance * RequestProxyBuilder实例对象 */ public RequestProxyBuilder proxyConfig() { return new RequestProxyBuilder(this); } /** *

Add trusted certificate library

*

添加信任证书库

* * @param certPath Trust certificate path * 信任证书地址 * @param certPassword Password of trust certificate * 读取证书的密钥 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder addTrustCertificate(final String certPath, final String certPassword) { try { return this.addTrustCertificate(FileUtils.readFileBytes(certPath), certPassword); } catch (IOException ignore) { } return this; } /** *

Add trusted certificate library

*

添加信任证书库

* * @param certContent Trust certificate data bytes * 信任证书二进制字节数组 * @param certPassword Password of trust certificate * 读取证书的密钥 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder addTrustCertificate(final byte[] certContent, final String certPassword) { Optional.of(TrustCert.newInstance(certContent, certPassword)) .filter(trustCert -> this.trustTrustCerts.stream().noneMatch(existCert -> existCert.getSha256().equals(trustCert.getSha256()))) .ifPresent(this.trustTrustCerts::add); return this; } /** *

Configure pass phrase of system certificate library

*

设置系统证书库的读取密码

* * @param passPhrase Pass phrase of system certificate library * 系统证书库的读取密码 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder passPhrase(final String passPhrase) { this.passPhrase = passPhrase; return this; } /** *

Configure user agent string will used

*

设置即将使用的用户代理字符串

* * @param userAgent User agent string * 用户代理字符串 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder userAgent(final String userAgent) { this.userAgent = userAgent; return this; } /** *

Configure request url

*

设置请求地址

* * @param requestUrl Request url string * 请求地址字符串 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder requestUrl(final String requestUrl) { this.requestUrl = requestUrl; return this; } /** *

Configure character encoding

*

设置请求字符集

* * @param charset character encoding * 字符集 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder charset(final String charset) { this.charset = charset; return this; } /** *

Configure content type string

*

设置"Content-Type"值

* * @param contentType Content type string * 需要设置的字符串 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder contentType(final String contentType) { this.contentType = contentType; return this; } /** *

Configure request timeout

*

设置请求超时时间

* * @param timeOut Timeout value * 请求超时时间 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder timeOut(final int timeOut) { this.timeOut = timeOut; return this; } /** *

Configure request send data bytes

*

设置请求发送的二进制数据

* * @param postData Binary data bytes * 二进制数据 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder postData(final byte[] postData) { this.postData = postData; return this; } /** *

Add request header name and value

*

添加请求头的键和值

* * @param headerName Request header name * 请求头键名 * @param headerValue Request header value * 请求头键值 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder addHeader(final String headerName, final String headerValue) { this.headers.add(new SimpleHeader(headerName, headerValue)); return this; } /** *

Add request parameter name and value

*

添加请求参数的键和值

* * @param parameterName Request parameter name * 请求参数名 * @param parameterValues Request parameter value * 请求参数值 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder addParameter(final String parameterName, final String[] parameterValues) { this.parameters.put(parameterName, parameterValues); return this; } /** *

Add request upload parameter name and value

*

添加请求上传数据的键和值

* * @param parameterName Request upload parameter name * 请求上传参数名 * @param parameterValue Request upload parameter value * 请求上传文件实例对象 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder addUploadParam(final String parameterName, final File parameterValue) { this.uploadParams.put(parameterName, parameterValue); return this; } /** *

Add request cookie values

*

添加请求Cookie信息

* * @param cookieEntities CookieEntity instance array * CookieEntity实例对象数组 * @see org.nervousync.http.cookie.CookieEntity * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder addCookies(final CookieEntity... cookieEntities) { this.cookieList.addAll(Arrays.asList(cookieEntities)); return this; } /** *

Add request cookie values from response header "Set-Cookie"

*

解析响应数据头中的"Set-Cookie"信息,并添加请求Cookie信息

* * @param responseCookieValue String value of response header "Set-Cookie" * 响应头中的"Set-Cookie"字符串值 * * @return Current RequestBuilder instance * 当前RequestBuilder实例对象 */ public RequestBuilder addCookies(final String responseCookieValue) { this.cookieList.add(new CookieEntity(responseCookieValue)); return this; } /** *

Confirm proxy configure

*

确认代理服务器配置

* * @param proxyConfig ProxyConfig instance * ProxyConfig实例对象 * @see org.nervousync.proxy.ProxyConfig */ void proxyConfig(final ProxyConfig proxyConfig) { this.proxyConfig = proxyConfig; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy