|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.starrocks.client; |
| 19 | + |
| 20 | +import org.apache.seatunnel.common.utils.JsonUtils; |
| 21 | + |
| 22 | +import lombok.extern.slf4j.Slf4j; |
| 23 | +import org.apache.http.HttpEntity; |
| 24 | +import org.apache.http.HttpStatus; |
| 25 | +import org.apache.http.client.config.RequestConfig; |
| 26 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 27 | +import org.apache.http.client.methods.HttpGet; |
| 28 | +import org.apache.http.client.methods.HttpPut; |
| 29 | +import org.apache.http.entity.ByteArrayEntity; |
| 30 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 31 | +import org.apache.http.impl.client.DefaultRedirectStrategy; |
| 32 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 33 | +import org.apache.http.impl.client.HttpClients; |
| 34 | +import org.apache.http.util.EntityUtils; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.net.HttpURLConnection; |
| 38 | +import java.net.URL; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +@Slf4j |
| 43 | +public class HttpHelper { |
| 44 | + private static final int DEFAULT_CONNECT_TIMEOUT = 1000000; |
| 45 | + |
| 46 | + public HttpEntity getHttpEntity(CloseableHttpResponse resp) { |
| 47 | + int code = resp.getStatusLine().getStatusCode(); |
| 48 | + if (HttpStatus.SC_OK != code) { |
| 49 | + log.warn("Request failed with code:{}", code); |
| 50 | + return null; |
| 51 | + } |
| 52 | + HttpEntity respEntity = resp.getEntity(); |
| 53 | + if (null == respEntity) { |
| 54 | + log.warn("Request failed with empty response."); |
| 55 | + return null; |
| 56 | + } |
| 57 | + return respEntity; |
| 58 | + } |
| 59 | + |
| 60 | + public String doHttpGet(String getUrl) throws IOException { |
| 61 | + log.info("Executing GET from {}.", getUrl); |
| 62 | + try (CloseableHttpClient httpclient = buildHttpClient()) { |
| 63 | + HttpGet httpGet = new HttpGet(getUrl); |
| 64 | + try (CloseableHttpResponse resp = httpclient.execute(httpGet)) { |
| 65 | + HttpEntity respEntity = resp.getEntity(); |
| 66 | + if (null == respEntity) { |
| 67 | + log.warn("Request failed with empty response."); |
| 68 | + return null; |
| 69 | + } |
| 70 | + return EntityUtils.toString(respEntity); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public Map<String, Object> doHttpGet(String getUrl, Map<String, String> header) throws IOException { |
| 76 | + log.info("Executing GET from {}.", getUrl); |
| 77 | + try (CloseableHttpClient httpclient = HttpClients.createDefault()) { |
| 78 | + HttpGet httpGet = new HttpGet(getUrl); |
| 79 | + if (null != header) { |
| 80 | + for (Map.Entry<String, String> entry : header.entrySet()) { |
| 81 | + httpGet.setHeader(entry.getKey(), String.valueOf(entry.getValue())); |
| 82 | + } |
| 83 | + } |
| 84 | + try (CloseableHttpResponse resp = httpclient.execute(httpGet)) { |
| 85 | + HttpEntity respEntity = getHttpEntity(resp); |
| 86 | + if (null == respEntity) { |
| 87 | + log.warn("Request failed with empty response."); |
| 88 | + return null; |
| 89 | + } |
| 90 | + return JsonUtils.parseObject(EntityUtils.toString(respEntity), Map.class); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + public Map<String, Object> doHttpPut(String url, byte[] data, Map<String, String> header) throws IOException { |
| 97 | + final HttpClientBuilder httpClientBuilder = HttpClients.custom() |
| 98 | + .setRedirectStrategy(new DefaultRedirectStrategy() { |
| 99 | + @Override |
| 100 | + protected boolean isRedirectable(String method) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + }); |
| 104 | + try (CloseableHttpClient httpclient = httpClientBuilder.build()) { |
| 105 | + HttpPut httpPut = new HttpPut(url); |
| 106 | + if (null != header) { |
| 107 | + for (Map.Entry<String, String> entry : header.entrySet()) { |
| 108 | + httpPut.setHeader(entry.getKey(), String.valueOf(entry.getValue())); |
| 109 | + } |
| 110 | + } |
| 111 | + httpPut.setEntity(new ByteArrayEntity(data)); |
| 112 | + httpPut.setConfig(RequestConfig.custom().setRedirectsEnabled(true).build()); |
| 113 | + try (CloseableHttpResponse resp = httpclient.execute(httpPut)) { |
| 114 | + int code = resp.getStatusLine().getStatusCode(); |
| 115 | + if (HttpStatus.SC_OK != code) { |
| 116 | + String errorText; |
| 117 | + try { |
| 118 | + HttpEntity respEntity = resp.getEntity(); |
| 119 | + errorText = EntityUtils.toString(respEntity); |
| 120 | + } catch (Exception err) { |
| 121 | + errorText = "find errorText failed: " + err.getMessage(); |
| 122 | + } |
| 123 | + log.warn("Request failed with code:{}, err:{}", code, errorText); |
| 124 | + Map<String, Object> errorMap = new HashMap<>(); |
| 125 | + errorMap.put("Status", "Fail"); |
| 126 | + errorMap.put("Message", errorText); |
| 127 | + return errorMap; |
| 128 | + } |
| 129 | + HttpEntity respEntity = resp.getEntity(); |
| 130 | + if (null == respEntity) { |
| 131 | + log.warn("Request failed with empty response."); |
| 132 | + return null; |
| 133 | + } |
| 134 | + return JsonUtils.parseObject(EntityUtils.toString(respEntity), Map.class); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private CloseableHttpClient buildHttpClient() { |
| 140 | + final HttpClientBuilder httpClientBuilder = HttpClients.custom() |
| 141 | + .setRedirectStrategy(new DefaultRedirectStrategy() { |
| 142 | + @Override |
| 143 | + protected boolean isRedirectable(String method) { |
| 144 | + return true; |
| 145 | + } |
| 146 | + }); |
| 147 | + return httpClientBuilder.build(); |
| 148 | + } |
| 149 | + |
| 150 | + public boolean tryHttpConnection(String host) { |
| 151 | + try { |
| 152 | + URL url = new URL(host); |
| 153 | + HttpURLConnection co = (HttpURLConnection) url.openConnection(); |
| 154 | + co.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT); |
| 155 | + co.connect(); |
| 156 | + co.disconnect(); |
| 157 | + return true; |
| 158 | + } catch (Exception e1) { |
| 159 | + log.warn("Failed to connect to address:{}", host, e1); |
| 160 | + return false; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments