use of io.netty.handler.codec.http.HttpMethod in project jocean-http by isdom.
the class DefaultSignalClientTestCase method testSignalClientOnlySignalForPostWithHeaderParam.
@Test
public void testSignalClientOnlySignalForPostWithHeaderParam() throws Exception {
final TestResponse respToSendback = new TestResponse("0", "OK");
final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
final AtomicReference<TestRequestByPostWithHeaderParam> 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());
final TestRequestByPostWithHeaderParam reqbean = (TestRequestByPostWithHeaderParam) JSON.parseObject(Nettys.dumpByteBufAsBytes(req.content()), TestRequestByPostWithHeaderParam.class);
reqbean._headerp = req.headers().get("X-P");
reqbeanReceivedRef.set(reqbean);
} 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(TestRequestByPostWithHeaderParam.class, TestResponse.class, null, buildUri2Addr(testAddr), Feature.ENABLE_LOGGING);
final TestRequestByPostWithHeaderParam reqToSend = new TestRequestByPostWithHeaderParam("1", "test");
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();
}
}
use of io.netty.handler.codec.http.HttpMethod in project jocean-http by isdom.
the class DefaultSignalClientTestCase method testSignalClientOnlySignalForGetDuplicateFeatures.
@Test
public void testSignalClientOnlySignalForGetDuplicateFeatures() throws Exception {
final TestResponse respToSendback = new TestResponse("0", "OK");
final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
final AtomicReference<String> reqbeanReceivedRef = new AtomicReference<>();
final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {
@Override
public void call(final FullHttpRequest req, final HttpTrade trade) {
reqMethodReceivedRef.set(req.method());
final QueryStringDecoder decoder = new QueryStringDecoder(req.uri());
reqpathReceivedRef.set(decoder.path());
reqbeanReceivedRef.set(decoder.parameters().get("id").get(0));
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(TestRequest.class, TestResponse.class, null, buildUri2Addr(testAddr), // duplicate ENABLE_SETURI
RosaProfiles.ENABLE_SETPATH, Feature.ENABLE_LOGGING);
final TestRequest reqToSend = new TestRequest("1");
final TestResponse respReceived = ((SignalClient) signalClient).interaction().request(reqToSend).<TestResponse>build().timeout(1, TimeUnit.SECONDS).toBlocking().single();
assertEquals(HttpMethod.GET, reqMethodReceivedRef.get());
assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
assertEquals(reqToSend.getId(), reqbeanReceivedRef.get());
assertEquals(respToSendback, respReceived);
pool.awaitRecycleChannels();
} finally {
server.unsubscribe();
}
}
use of io.netty.handler.codec.http.HttpMethod 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();
}
}
use of io.netty.handler.codec.http.HttpMethod 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();
}
}
use of io.netty.handler.codec.http.HttpMethod in project jocean-http by isdom.
the class MessageUtil method interact.
public static Interact interact(final HttpClient client) {
final InitiatorBuilder _initiatorBuilder = client.initiator();
final AtomicBoolean _isSSLEnabled = new AtomicBoolean(false);
final AtomicReference<Observable<Object>> _obsreqRef = new AtomicReference<>(fullRequestWithoutBody(HttpVersion.HTTP_1_1, HttpMethod.GET));
final List<String> _nvs = new ArrayList<>();
final AtomicReference<URI> _uriRef = new AtomicReference<>();
return new Interact() {
private void updateObsRequest(final Action1<Object> action) {
_obsreqRef.set(_obsreqRef.get().doOnNext(action));
}
private void addQueryParams() {
if (!_nvs.isEmpty()) {
updateObsRequest(MessageUtil.addQueryParam(_nvs.toArray(new String[0])));
}
}
private void extractUriWithHost(final Object... reqbeans) {
if (null == _uriRef.get()) {
for (Object bean : reqbeans) {
try {
final Path path = bean.getClass().getAnnotation(Path.class);
if (null != path) {
final URI uri = new URI(path.value());
if (null != uri.getHost()) {
uri(path.value());
return;
}
}
} catch (Exception e) {
LOG.warn("exception when extract uri from bean {}, detail: {}", bean, ExceptionUtils.exception2detail(e));
}
}
}
}
private void checkAddr() {
if (null == _uriRef.get()) {
throw new RuntimeException("remote address not set.");
}
}
private InitiatorBuilder addSSLFeatureIfNeed(final InitiatorBuilder builder) {
if (_isSSLEnabled.get()) {
return builder;
} else if ("https".equals(_uriRef.get().getScheme())) {
return builder.feature(F_SSL);
} else {
return builder;
}
}
@Override
public Interact method(final HttpMethod method) {
updateObsRequest(MessageUtil.setMethod(method));
return this;
}
@Override
public Interact uri(final String uriAsString) {
try {
final URI uri = new URI(uriAsString);
_uriRef.set(uri);
_initiatorBuilder.remoteAddress(uri2addr(uri));
updateObsRequest(MessageUtil.setHost(uri));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return this;
}
@Override
public Interact path(final String path) {
updateObsRequest(MessageUtil.setPath(path));
return this;
}
@Override
public Interact paramAsQuery(final String name, final String value) {
_nvs.add(name);
_nvs.add(value);
return this;
}
@Override
public Interact reqbean(final Object... reqbeans) {
updateObsRequest(MessageUtil.toRequest(reqbeans));
extractUriWithHost(reqbeans);
return this;
}
@Override
public Interact body(final Observable<? extends MessageBody> body) {
_obsreqRef.set(_obsreqRef.get().compose(MessageUtil.addBody(body)));
return this;
}
@Override
public Interact body(final Object bean, final ContentEncoder contentEncoder) {
return body(toBody(bean, contentEncoder.contentType(), contentEncoder.encoder()));
}
// @Override
// public Interact disposeBodyOnTerminate(final boolean doDispose) {
// _doDisposeBody.set(doDispose);
// return this;
// }
@Override
public Interact onrequest(final Action1<Object> action) {
updateObsRequest(action);
return this;
}
@Override
public Interact feature(final Feature... features) {
_initiatorBuilder.feature(features);
if (isSSLEnabled(features)) {
_isSSLEnabled.set(true);
}
return this;
}
private boolean isSSLEnabled(final Feature... features) {
for (Feature f : features) {
if (f instanceof Feature.ENABLE_SSL) {
return true;
}
}
return false;
}
private Observable<? extends Object> hookDisposeBody(final Observable<Object> obsreq, final HttpInitiator initiator) {
return obsreq.doOnNext(DisposableWrapperUtil.disposeOnForAny(initiator));
}
private Observable<? extends DisposableWrapper<HttpObject>> defineInteraction(final HttpInitiator initiator) {
return initiator.defineInteraction(hookDisposeBody(_obsreqRef.get(), initiator));
}
@Override
public Observable<? extends Interaction> execution() {
checkAddr();
addQueryParams();
return addSSLFeatureIfNeed(_initiatorBuilder).build().map(new Func1<HttpInitiator, Interaction>() {
@Override
public Interaction call(final HttpInitiator initiator) {
final Observable<? extends DisposableWrapper<HttpObject>> interaction = defineInteraction(initiator);
return new Interaction() {
@Override
public HttpInitiator initiator() {
return initiator;
}
@Override
public Observable<? extends DisposableWrapper<HttpObject>> execute() {
return interaction;
}
};
}
});
}
};
}
Aggregations