Search in sources :

Example 11 with DefaultSignalClient

use of org.jocean.http.rosa.impl.DefaultSignalClient in project jocean-http by isdom.

the class DefaultSignalClientTestCase method testSignalClientOnlySignalForPost.

@Test
public void testSignalClientOnlySignalForPost() throws Exception {
    final TestResponse respToSendback = new TestResponse("0", "OK");
    final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
    final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
    final AtomicReference<TestRequestByPost> reqbeanReceivedRef = new AtomicReference<>();
    final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {

        @Override
        public void call(final FullHttpRequest req, final HttpTrade trade) {
            try {
                reqMethodReceivedRef.set(req.method());
                reqpathReceivedRef.set(req.uri());
                reqbeanReceivedRef.set((TestRequestByPost) JSON.parseObject(Nettys.dumpByteBufAsBytes(req.content()), TestRequestByPost.class));
            } catch (IOException e) {
                LOG.warn("exception when Nettys.dumpByteBufAsBytes, detail: {}", ExceptionUtils.exception2detail(e));
            }
            trade.outbound(buildResponse(respToSendback, trade.onTerminate()));
        }
    };
    final String testAddr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
    try {
        final TestChannelCreator creator = new TestChannelCreator();
        final TestChannelPool pool = new TestChannelPool(1);
        final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool);
        final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), httpclient);
        signalClient.registerRequestType(TestRequestByPost.class, TestResponse.class, null, buildUri2Addr(testAddr), Feature.ENABLE_LOGGING);
        final TestRequestByPost reqToSend = new TestRequestByPost("1", null);
        final TestResponse respReceived = ((SignalClient) signalClient).interaction().request(reqToSend).<TestResponse>build().timeout(1, TimeUnit.SECONDS).toBlocking().single();
        assertEquals(HttpMethod.POST, reqMethodReceivedRef.get());
        assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
        assertEquals(reqToSend, reqbeanReceivedRef.get());
        assertEquals(respToSendback, respReceived);
        pool.awaitRecycleChannels();
    } finally {
        server.unsubscribe();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Action2(rx.functions.Action2) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URI(java.net.URI) DefaultHttpClient(org.jocean.http.client.impl.DefaultHttpClient) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) TestChannelPool(org.jocean.http.client.impl.TestChannelPool) DefaultSignalClient(org.jocean.http.rosa.impl.DefaultSignalClient) Subscription(rx.Subscription) HttpMethod(io.netty.handler.codec.http.HttpMethod) TestChannelCreator(org.jocean.http.client.impl.TestChannelCreator) Test(org.junit.Test)

Example 12 with DefaultSignalClient

use of org.jocean.http.rosa.impl.DefaultSignalClient in project jocean-http by isdom.

the class DefaultSignalClientTestCase method testSignalClientOnlySignalForPostWithJSONContentWithoutRegisterRespType.

@Test
public void testSignalClientOnlySignalForPostWithJSONContentWithoutRegisterRespType() throws Exception {
    final byte[] respToSendback = new byte[] { 12, 13, 14, 15 };
    final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
    final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
    final AtomicReference<TestRequestByPost> reqbeanReceivedRef = new AtomicReference<>();
    final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {

        @Override
        public void call(final FullHttpRequest req, final HttpTrade trade) {
            try {
                reqMethodReceivedRef.set(req.method());
                reqpathReceivedRef.set(req.uri());
                reqbeanReceivedRef.set((TestRequestByPost) JSON.parseObject(Nettys.dumpByteBufAsBytes(req.content()), TestRequestByPost.class));
            } catch (IOException e) {
                LOG.warn("exception when Nettys.dumpByteBufAsBytes, detail: {}", ExceptionUtils.exception2detail(e));
            }
            trade.outbound(buildBytesResponse(respToSendback, trade.onTerminate()));
        }
    };
    final String testAddr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
    try {
        final TestChannelCreator creator = new TestChannelCreator();
        final TestChannelPool pool = new TestChannelPool(1);
        final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool, Feature.ENABLE_LOGGING);
        final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), buildUri2Addr(testAddr), httpclient);
        final TestRequestByPost reqToSend = new TestRequestByPost("1", null);
        final byte[] bytesReceived = ((SignalClient) signalClient).interaction().request(reqToSend).feature(new SignalClient.UsingPath("/test/simpleRequest"), new SignalClient.UsingMethod(POST.class), new SignalClient.JSONContent("{\"code\": \"added\"}")).<byte[]>build().toBlocking().single();
        assertEquals(HttpMethod.POST, reqMethodReceivedRef.get());
        assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
        reqToSend.setCode("added");
        assertEquals(reqToSend, reqbeanReceivedRef.get());
        assertTrue(Arrays.equals(respToSendback, bytesReceived));
        pool.awaitRecycleChannels();
    } finally {
        server.unsubscribe();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Action2(rx.functions.Action2) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URI(java.net.URI) DefaultHttpClient(org.jocean.http.client.impl.DefaultHttpClient) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) TestChannelPool(org.jocean.http.client.impl.TestChannelPool) DefaultSignalClient(org.jocean.http.rosa.impl.DefaultSignalClient) Subscription(rx.Subscription) HttpMethod(io.netty.handler.codec.http.HttpMethod) TestChannelCreator(org.jocean.http.client.impl.TestChannelCreator) Test(org.junit.Test)

Example 13 with DefaultSignalClient

use of org.jocean.http.rosa.impl.DefaultSignalClient in project jocean-http by isdom.

the class JolokiaApiDemo method main.

public static void main(String[] args) throws Exception {
    final SignalClient signal = new DefaultSignalClient(new DefaultHttpClient());
    final URI uri = new URI("http://192.168.8.8/jolokia/");
    queryAttrValue(signal, uri, "java.lang:type=Threading");
    Thread.sleep(100);
    queryAttrValue(signal, uri, "java.lang:type=Threading");
    Thread.sleep(100);
    System.exit(0);
}
Also used : DefaultSignalClient(org.jocean.http.rosa.impl.DefaultSignalClient) DefaultSignalClient(org.jocean.http.rosa.impl.DefaultSignalClient) SignalClient(org.jocean.http.rosa.SignalClient) URI(java.net.URI) DefaultHttpClient(org.jocean.http.client.impl.DefaultHttpClient)

Aggregations

URI (java.net.URI)13 DefaultHttpClient (org.jocean.http.client.impl.DefaultHttpClient)13 DefaultSignalClient (org.jocean.http.rosa.impl.DefaultSignalClient)13 TestChannelPool (org.jocean.http.client.impl.TestChannelPool)12 Subscription (rx.Subscription)12 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)11 HttpMethod (io.netty.handler.codec.http.HttpMethod)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 TestChannelCreator (org.jocean.http.client.impl.TestChannelCreator)11 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)11 Test (org.junit.Test)11 Action2 (rx.functions.Action2)11 IOException (java.io.IOException)8 SignalClient (org.jocean.http.rosa.SignalClient)7 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)4 HttpObject (io.netty.handler.codec.http.HttpObject)3 POST (javax.ws.rs.POST)3 Bootstrap (io.netty.bootstrap.Bootstrap)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1