Search in sources :

Example 21 with Header

use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.

the class DefaultHttpClientRequest method build.

static HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity, RequestConfig defaultConfig) throws Exception {
    final Header headers = requestHttpEntity.getHeaders();
    final BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method);
    final HttpRequestBase httpRequestBase = httpMethod.init(uri.toString());
    HttpUtils.initRequestHeader(httpRequestBase, headers);
    if (MediaType.APPLICATION_FORM_URLENCODED.equals(headers.getValue(HttpHeaderConsts.CONTENT_TYPE)) && requestHttpEntity.getBody() instanceof Map) {
        HttpUtils.initRequestFromEntity(httpRequestBase, (Map<String, String>) requestHttpEntity.getBody(), headers.getCharset());
    } else {
        HttpUtils.initRequestEntity(httpRequestBase, requestHttpEntity.getBody(), headers);
    }
    mergeDefaultConfig(httpRequestBase, requestHttpEntity.getHttpClientConfig(), defaultConfig);
    return httpRequestBase;
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(com.alibaba.nacos.common.http.param.Header) BaseHttpMethod(com.alibaba.nacos.common.http.BaseHttpMethod) Map(java.util.Map)

Example 22 with Header

use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.

the class NamingHttpUtilTest method testBuilderHeader.

@Test
public void testBuilderHeader() {
    Header header = NamingHttpUtil.builderHeader();
    Assert.assertNotNull(header);
    Assert.assertEquals(header.getValue(HttpHeaderConsts.CLIENT_VERSION_HEADER), VersionUtils.version);
    Assert.assertEquals(header.getValue(HttpHeaderConsts.USER_AGENT_HEADER), VersionUtils.getFullClientVersion());
    Assert.assertEquals(header.getValue(HttpHeaderConsts.ACCEPT_ENCODING), "gzip,deflate,sdch");
    Assert.assertEquals(header.getValue(HttpHeaderConsts.CONNECTION), "Keep-Alive");
    Assert.assertNotNull(header.getValue(HttpHeaderConsts.REQUEST_ID));
    Assert.assertEquals(header.getValue(HttpHeaderConsts.REQUEST_MODULE), "Naming");
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) Test(org.junit.Test)

Example 23 with Header

use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.

the class ServerListManager method getServerListFromEndpoint.

private List<String> getServerListFromEndpoint() {
    try {
        String urlString = HTTP_PREFIX + endpoint + "/nacos/serverlist";
        Header header = NamingHttpUtil.builderHeader();
        Query query = StringUtils.isNotBlank(namespace) ? Query.newInstance().addParam("namespace", namespace) : Query.EMPTY;
        HttpRestResult<String> restResult = nacosRestTemplate.get(urlString, header, query, String.class);
        if (!restResult.ok()) {
            throw new IOException("Error while requesting: " + urlString + "'. Server returned: " + restResult.getCode());
        }
        String content = restResult.getData();
        List<String> list = new ArrayList<String>();
        for (String line : IoUtils.readLines(new StringReader(content))) {
            if (!line.trim().isEmpty()) {
                list.add(line.trim());
            }
        }
        return list;
    } catch (Exception e) {
        NAMING_LOGGER.error("[SERVER-LIST] failed to update server list.", e);
    }
    return null;
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) Query(com.alibaba.nacos.common.http.param.Query) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) NacosLoadException(com.alibaba.nacos.api.exception.runtime.NacosLoadException) NacosException(com.alibaba.nacos.api.exception.NacosException) IOException(java.io.IOException)

Example 24 with Header

use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.

the class NotifyService method invokeURL.

/**
 * Invoke http get request.
 *
 * @param url      url
 * @param headers  headers
 * @param encoding encoding
 * @return {@link com.alibaba.nacos.common.model.RestResult}
 * @throws Exception throw Exception
 */
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public static RestResult<String> invokeURL(String url, List<String> headers, String encoding) throws Exception {
    Header header = Header.newInstance();
    header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encoding);
    if (CollectionUtils.isNotEmpty(headers)) {
        header.addAll(headers);
    }
    return HttpClientManager.getNacosRestTemplate().get(url, header, Query.EMPTY, String.class);
}
Also used : Header(com.alibaba.nacos.common.http.param.Header)

Example 25 with Header

use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.

the class HttpUtilsTest method testInitRequestEntity2.

@Test
public void testInitRequestEntity2() throws Exception {
    BaseHttpMethod.HttpGetWithEntity httpRequest = new BaseHttpMethod.HttpGetWithEntity("");
    Header header = Header.newInstance();
    header.addParam(HttpHeaderConsts.CONTENT_TYPE, "text/html");
    HttpUtils.initRequestEntity(httpRequest, Collections.singletonMap("k", "v"), header);
    HttpEntity entity = httpRequest.getEntity();
    InputStream contentStream = entity.getContent();
    byte[] bytes = new byte[contentStream.available()];
    contentStream.read(bytes);
    Assert.assertEquals("{\"k\":\"v\"}", new String(bytes, Constants.ENCODE));
    Assert.assertEquals(HttpHeaderConsts.CONTENT_TYPE, entity.getContentType().getName());
    Assert.assertEquals("text/html; charset=UTF-8", entity.getContentType().getValue());
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

Header (com.alibaba.nacos.common.http.param.Header)33 InputStream (java.io.InputStream)6 NacosException (com.alibaba.nacos.api.exception.NacosException)5 HttpClientConfig (com.alibaba.nacos.common.http.HttpClientConfig)5 Query (com.alibaba.nacos.common.http.param.Query)5 Test (org.junit.Test)5 HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)3 ConnectException (java.net.ConnectException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 Map (java.util.Map)3 HttpEntity (org.apache.http.HttpEntity)3 Http (com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http)2 BaseHttpMethod (com.alibaba.nacos.common.http.BaseHttpMethod)2 IOException (java.io.IOException)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)2 NacosLoadException (com.alibaba.nacos.api.exception.runtime.NacosLoadException)1 JdkHttpClientResponse (com.alibaba.nacos.common.http.client.response.JdkHttpClientResponse)1 Cluster (com.alibaba.nacos.naming.core.Cluster)1