-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: kaideng <kaideng@kaidengdeMacBook-Pro.local>
- Loading branch information
kaideng
authored and
kaideng
committed
Jan 3, 2023
1 parent
d75efc2
commit 223843f
Showing
7 changed files
with
618 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
java/osx/broker/src/main/java/com/osx/broker/http/DispatchServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.osx.broker.http; | ||
|
||
import com.osx.broker.ServiceContainer; | ||
import com.osx.core.constant.PtpHttpHeader; | ||
import com.osx.core.provider.TechProvider; | ||
import com.osx.tech.provider.TechProviderRegister; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.eclipse.jetty.http.HttpHeader; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public class DispatchServlet extends HttpServlet { | ||
|
||
Logger logger = LoggerFactory.getLogger(DispatchServlet.class); | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
String protocol = req.getProtocol(); | ||
if (!protocol.endsWith("1.1")) { | ||
resp.sendError(405, "http.method_get_not_supported"); | ||
} | ||
String techProviderCode =req.getHeader(PtpHttpHeader.TechProviderCode); | ||
if(StringUtils.isNotEmpty(techProviderCode)){ | ||
TechProvider techProvider = ServiceContainer.techProviderRegister.select(techProviderCode); | ||
if(techProvider!=null) { | ||
techProvider.processHttpInvoke(req, resp); | ||
}else{ | ||
resp.sendError(404,"tech-provider-code invalid"); | ||
} | ||
}else{ | ||
resp.sendError(404,"tech-provider-code invalid"); | ||
} | ||
String requestUri =req.getRequestURI(); | ||
logger.info("receive request uri {}",requestUri); | ||
} | ||
|
||
|
||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
String requestUri =req.getRequestURI(); | ||
logger.info("receive request uri {}",requestUri); | ||
String protocol = req.getProtocol(); | ||
if (!protocol.endsWith("1.1")) { | ||
resp.sendError(405, "http.method_get_not_supported"); | ||
} | ||
String techProviderCode =req.getHeader(PtpHttpHeader.TechProviderCode); | ||
if(StringUtils.isNotEmpty(techProviderCode)){ | ||
TechProvider techProvider = ServiceContainer.techProviderRegister.select(techProviderCode); | ||
if(techProvider!=null) { | ||
techProvider.processHttpInvoke(req, resp); | ||
}else{ | ||
resp.sendError(404,"tech-provider-code invalid"); | ||
} | ||
}else{ | ||
resp.sendError(404,"tech-provider-code invalid"); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
} |
258 changes: 258 additions & 0 deletions
258
java/osx/broker/src/main/java/com/osx/broker/http/HttpClientPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
/* | ||
* Copyright 2019 The FATE Authors. All Rights Reserved. | ||
* | ||
* Licensed 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 com.osx.broker.http; | ||
|
||
|
||
import com.google.common.collect.Maps; | ||
import com.google.protobuf.ByteString; | ||
import com.osx.core.config.MetaInfo; | ||
import com.osx.core.constant.Dict; | ||
import com.osx.core.constant.PtpHttpHeader; | ||
import com.osx.core.utils.JsonUtil; | ||
import org.apache.http.Header; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.methods.HttpRequestBase; | ||
import org.apache.http.client.protocol.HttpClientContext; | ||
import org.apache.http.config.Registry; | ||
import org.apache.http.config.RegistryBuilder; | ||
import org.apache.http.conn.socket.ConnectionSocketFactory; | ||
import org.apache.http.conn.socket.PlainConnectionSocketFactory; | ||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | ||
import org.apache.http.conn.ssl.TrustSelfSignedStrategy; | ||
import org.apache.http.entity.ByteArrayEntity; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | ||
import org.apache.http.ssl.SSLContextBuilder; | ||
import org.apache.http.util.EntityUtils; | ||
import org.ppc.ptp.Osx; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.security.KeyManagementException; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class HttpClientPool { | ||
private static final Logger logger = LoggerFactory.getLogger(HttpClientPool.class); | ||
private static PoolingHttpClientConnectionManager poolConnManager; | ||
private static RequestConfig requestConfig; | ||
private static CloseableHttpClient httpClient; | ||
|
||
private static void config(HttpRequestBase httpRequestBase, Map<String, String> headers) { | ||
RequestConfig requestConfig = RequestConfig.custom() | ||
.setConnectionRequestTimeout(MetaInfo.HTTP_CLIENT_CONFIG_CONN_REQ_TIME_OUT) | ||
.setConnectTimeout(MetaInfo.HTTP_CLIENT_CONFIG_CONN_TIME_OUT) | ||
.setSocketTimeout(MetaInfo.HTTP_CLIENT_CONFIG_SOCK_TIME_OUT).build(); | ||
httpRequestBase.addHeader(Dict.CONTENT_TYPE, Dict.CONTENT_TYPE_JSON_UTF8); | ||
if (headers != null) { | ||
headers.forEach((key, value) -> { | ||
httpRequestBase.addHeader(key, value); | ||
}); | ||
} | ||
httpRequestBase.setConfig(requestConfig); | ||
} | ||
|
||
public static void initPool() { | ||
try { | ||
SSLContextBuilder builder = new SSLContextBuilder(); | ||
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); | ||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build()); | ||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register( | ||
Dict.HTTP, PlainConnectionSocketFactory.getSocketFactory()).register( | ||
Dict.HTTPS, sslsf).build(); | ||
poolConnManager = new PoolingHttpClientConnectionManager( | ||
socketFactoryRegistry); | ||
poolConnManager.setMaxTotal(MetaInfo.HTTP_CLIENT_INIT_POOL_MAX_TOTAL); | ||
poolConnManager.setDefaultMaxPerRoute(MetaInfo.HTTP_CLIENT_INIT_POOL_DEF_MAX_PER_ROUTE); | ||
int socketTimeout = MetaInfo.HTTP_CLIENT_INIT_POOL_SOCK_TIME_OUT; | ||
int connectTimeout = MetaInfo.HTTP_CLIENT_INIT_POOL_CONN_TIME_OUT; | ||
int connectionRequestTimeout = MetaInfo.HTTP_CLIENT_INIT_POOL_CONN_REQ_TIME_OUT; | ||
requestConfig = RequestConfig.custom().setConnectionRequestTimeout( | ||
connectionRequestTimeout).setSocketTimeout(socketTimeout).setConnectTimeout( | ||
connectTimeout).build(); | ||
httpClient = createConnection(); | ||
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) { | ||
logger.error("init http client pool failed:", ex); | ||
} | ||
} | ||
public static CloseableHttpClient getConnection() { | ||
return httpClient; | ||
} | ||
|
||
public static CloseableHttpClient createConnection() { | ||
CloseableHttpClient httpClient = HttpClients.custom() | ||
.setConnectionManager(poolConnManager) | ||
.setDefaultRequestConfig(requestConfig) | ||
.evictExpiredConnections() | ||
.evictIdleConnections(5, TimeUnit.SECONDS) | ||
.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)) | ||
.build(); | ||
return httpClient; | ||
} | ||
|
||
// public static String post(String url, Map<String, Object> requestData) { | ||
// return sendPost(url, requestData, null); | ||
// } | ||
|
||
// public static String post(String url, Map<String, Object> requestData, Map<String, String> headers) { | ||
// return sendPost(url, requestData, headers); | ||
// } | ||
// public static PtpHttpResponse post(String url, byte[] body, Map<String, String> headers) { | ||
// return sendPtpPost(url, body, headers); | ||
// } | ||
|
||
public static Osx.Outbound sendPtpPost(String url, byte[] body, Map<String, String> headers) { | ||
HttpPost httpPost = new HttpPost(url); | ||
config(httpPost, headers); | ||
if(body!=null) { | ||
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(body); | ||
httpPost.setEntity(byteArrayEntity); | ||
} | ||
return getPtpHttpResponse(httpPost); | ||
} | ||
// public static String sendPost(String url, String requestData, Map<String, String> headers) { | ||
// HttpPost httpPost = new HttpPost(url); | ||
// config(httpPost, headers); | ||
// StringEntity stringEntity = new StringEntity(requestData, Dict.CHARSET_UTF8); | ||
// stringEntity.setContentEncoding(Dict.CHARSET_UTF8); | ||
// httpPost.setEntity(stringEntity); | ||
// return getResponse(httpPost); | ||
// } | ||
|
||
public static String sendPost(String url, byte[] body, Map<String, String> headers) { | ||
HttpPost httpPost = new HttpPost(url); | ||
config(httpPost, headers); | ||
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(body); | ||
httpPost.setEntity(byteArrayEntity); | ||
return getResponse(httpPost); | ||
} | ||
|
||
// public static String sendPost(String url, byte[] requestData, Map<String, String> headers) { | ||
// HttpPost httpPost = new HttpPost(url); | ||
// config(httpPost, headers); | ||
// ByteArrayEntity byteArrayEntity = new ByteArrayEntity(requestData); | ||
// byteArrayEntity.setContentEncoding(Dict.CHARSET_UTF8); | ||
// httpPost.setEntity(byteArrayEntity); | ||
// return getResponse(httpPost); | ||
// } | ||
|
||
public static String get(String url, Map<String, String> headers) { | ||
return sendGet(url, headers); | ||
} | ||
|
||
public static String get(String url) { | ||
return sendGet(url, null); | ||
} | ||
|
||
public static String sendGet(String url, Map<String, String> headers) { | ||
HttpGet httpGet = new HttpGet(url); | ||
config(httpGet, headers); | ||
return getResponse(httpGet); | ||
} | ||
|
||
private static String getResponse(HttpRequestBase request) { | ||
CloseableHttpResponse response = null; | ||
try { | ||
response = httpClient.execute(request, | ||
HttpClientContext.create()); | ||
HttpEntity entity = response.getEntity(); | ||
|
||
String result = EntityUtils.toString(entity, Dict.CHARSET_UTF8); | ||
EntityUtils.consume(entity); | ||
return result; | ||
} catch (IOException ex) { | ||
logger.error("get http response failed:", ex); | ||
return null; | ||
} finally { | ||
try { | ||
if (response != null) { | ||
response.close(); | ||
} | ||
} catch (IOException ex) { | ||
logger.error("get http response failed:", ex); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
private static Osx.Outbound getPtpHttpResponse(HttpRequestBase request) { | ||
|
||
Osx.Outbound.Builder outboundBuilder = Osx.Outbound.newBuilder(); | ||
CloseableHttpResponse response = null; | ||
try { | ||
response = httpClient.execute(request, | ||
HttpClientContext.create()); | ||
HttpEntity entity = response.getEntity(); | ||
byte[] payload = EntityUtils.toByteArray(entity); | ||
Header[] headers = response.getAllHeaders(); | ||
Map<String,String> headMap = Maps.newHashMap(); | ||
if(headers!=null){ | ||
for(int i=0;i<headers.length;i++){ | ||
Header temp = headers[i]; | ||
headMap.put(temp.getName(),temp.getValue()); | ||
} | ||
} | ||
if(payload!=null) | ||
outboundBuilder.setPayload(ByteString.copyFrom(payload)); | ||
if(headMap.get(PtpHttpHeader.ReturnCode)!=null) | ||
outboundBuilder.setCode(headMap.get(PtpHttpHeader.ReturnCode)); | ||
if(headMap.get(PtpHttpHeader.ReturnMessage)!=null) | ||
outboundBuilder.setMessage(headMap.get(PtpHttpHeader.ReturnMessage)); | ||
|
||
// String result = EntityUtils.toString(entity, Dict.CHARSET_UTF8); | ||
EntityUtils.consume(entity); | ||
return outboundBuilder.build(); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
logger.error("get http response failed:", ex); | ||
return null; | ||
} finally { | ||
try { | ||
if (response != null) { | ||
response.close(); | ||
} | ||
} catch (IOException ex) { | ||
logger.error("get http response failed:", ex); | ||
} | ||
} | ||
} | ||
|
||
public static String transferPost(String url, Map<String, Object> requestData) { | ||
HttpPost httpPost = new HttpPost(url); | ||
RequestConfig requestConfig = RequestConfig.custom() | ||
.setConnectionRequestTimeout(MetaInfo.HTTP_CLIENT_TRAN_CONN_REQ_TIME_OUT) | ||
.setConnectTimeout(MetaInfo.HTTP_CLIENT_TRAN_CONN_TIME_OUT) | ||
.setSocketTimeout(MetaInfo.HTTP_CLIENT_TRAN_SOCK_TIME_OUT).build(); | ||
httpPost.addHeader(Dict.CONTENT_TYPE, Dict.CONTENT_TYPE_JSON_UTF8); | ||
httpPost.setConfig(requestConfig); | ||
StringEntity stringEntity = new StringEntity(JsonUtil.object2Json(requestData), Dict.CHARSET_UTF8); | ||
stringEntity.setContentEncoding(Dict.CHARSET_UTF8); | ||
httpPost.setEntity(stringEntity); | ||
return getResponse(httpPost); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
java/osx/broker/src/main/java/com/osx/broker/http/PtpHttpResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.osx.broker.http; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.Map; | ||
@Data | ||
public class PtpHttpResponse { | ||
Map header; | ||
String code; | ||
String message; | ||
byte[] payload; | ||
} |
Oops, something went wrong.