Search in sources :

Example 1 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class DefaultAsyncHttpClientRequest method execute.

@Override
public <T> void execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity, final ResponseHandler<T> responseHandler, final Callback<T> callback) throws Exception {
    HttpRequestBase httpRequestBase = DefaultHttpClientRequest.build(uri, httpMethod, requestHttpEntity);
    asyncClient.execute(httpRequestBase, new FutureCallback<HttpResponse>() {

        @Override
        public void completed(HttpResponse result) {
            DefaultClientHttpResponse response = new DefaultClientHttpResponse(result);
            try {
                HttpRestResult<T> httpRestResult = responseHandler.handle(response);
                callback.onReceive(httpRestResult);
            } catch (Exception e) {
                callback.onError(e);
            } finally {
                HttpClientUtils.closeQuietly(result);
            }
        }

        @Override
        public void failed(Exception ex) {
            callback.onError(ex);
        }

        @Override
        public void cancelled() {
            callback.onCancel();
        }
    });
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) DefaultClientHttpResponse(com.alibaba.nacos.common.http.client.response.DefaultClientHttpResponse) HttpResponse(org.apache.http.HttpResponse) DefaultClientHttpResponse(com.alibaba.nacos.common.http.client.response.DefaultClientHttpResponse) IOException(java.io.IOException)

Example 2 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class SecurityProxyTest method testTestLoginFailCode.

@Test
public void testTestLoginFailCode() throws Exception {
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setCode(400);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
    boolean ret = securityProxy.login("localhost");
    Assert.assertFalse(ret);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 3 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class SecurityProxyTest method testLoginSuccess.

@Test
public void testLoginSuccess() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setData("{\"accessToken\":\"ttttttttttttttttt\",\"tokenTtl\":1000}");
    result.setCode(200);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
    // when
    boolean ret = securityProxy.login("localhost");
    // then
    Assert.assertTrue(ret);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 4 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class SecurityProxyTest method testGetAccessToken.

@Test
public void testGetAccessToken() throws Exception {
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setData("{\"accessToken\":\"abc\",\"tokenTtl\":1000}");
    result.setCode(200);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
    securityProxy.login("localhost");
    String accessToken = securityProxy.getAccessToken();
    Assert.assertEquals("abc", accessToken);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 5 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class NacosClientAuthServiceImplTest method testLoginSuccess.

@Test
public void testLoginSuccess() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setData("{\"accessToken\":\"ttttttttttttttttt\",\"tokenTtl\":1000}");
    result.setCode(200);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    List<String> serverList = new ArrayList<>();
    serverList.add("localhost");
    NacosClientAuthServiceImpl nacosClientAuthService = new NacosClientAuthServiceImpl();
    nacosClientAuthService.setServerList(serverList);
    nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
    // when
    boolean ret = nacosClientAuthService.login(properties);
    // then
    Assert.assertTrue(ret);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) ArrayList(java.util.ArrayList) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Aggregations

HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)33 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)29 Properties (java.util.Properties)29 Test (org.junit.Test)29 Field (java.lang.reflect.Field)19 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)18 ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)18 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)18 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 Service (com.alibaba.nacos.api.naming.pojo.Service)3 NoneSelector (com.alibaba.nacos.api.selector.NoneSelector)3 Header (com.alibaba.nacos.common.http.param.Header)3 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 BeatInfo (com.alibaba.nacos.client.naming.beat.BeatInfo)1 DefaultClientHttpResponse (com.alibaba.nacos.common.http.client.response.DefaultClientHttpResponse)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1