Elasticsearch Rest Client - ConnectionClosedException when calling performRequestAsync - java

I'm having a problem on HttpAsyncRequestExecutor.
I'm using elasticsearch Java Rest Client and I'm always getting a ConnectionClosedException (see below) when I call the performRequestAsync:
// variables (all with valid format):
// endpoint is just a List<String> with "14655/_search"
// params is just a Map<String, String> with
// "pretty", "true"
// "search_type", "query_then_fetch"
// entity is just a HttpEntity entity with the Json body request
final int numRequests = endpoints.size();
final CountDownLatch latch = new CountDownLatch(numRequests);
try (Timer.Context ctx = this.requestTimer.time()) {
for (final String endpoint : endpoints) {
// ERROR hapens here:
restClient.performRequestAsync("GET", endpoint, params, entity,
new ResponseListener() {
#Override
public void onSuccess(final Response response) {
if (response != null) {
responses.add(response);
latch.countDown();
}
}
#Override
public void onFailure(final Exception exception) {
latch.countDown();
logger.error("could not get search results for....",exception);
exception.printStackTrace();
}
});
}
}
Exception here:
org.apache.http.ConnectionClosedException: Connection closed
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:341)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:263)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:116)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:164)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:339)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:317)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:278)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:590)
at java.lang.Thread.run(Thread.java:745)
I don't know what is the real cause for the connection close. The exact same request works well in kopf and returns valid search results.
Plus, I don't call any restClient.close() or anything similar.
Any ideas where the problem might be?
Is the end of the input the cause to get a closed connection state (according to org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(conn))? If yes, what input is that?
Thanks
UPDATE:
I suspect the problem is related to Tomcat's HttpClient, because this code works correctly in an integration test (that is, returns results)... But it does not work (get the same ConnectionClosedException) when I make a REST request through a tomcat deployed "interface"
Any lights on this?

The problem was the port was wrong. For REST requests the port should be 9200 (and not 9300 like it was configured). More info on elasticsearch ports.
I wish elasticsearch could make a more explicit error log or a hint like "are you connecting with the right port?" for when one tries to access the 9300 port with anything other than the built-in clients.

Related

How to test if requests increase metrics while not getting any response in Vert.x?

Job of my Vert.x server, when it gets some invalid requests, is to NOT send any resposne back, but only increase associated metric. How can I test it?
The following code don't work, because as we never get the response back, the handler's code never is executed - I've just posted it to show you what I want in my test.
#Test
void invalidUrlTest(Vertx vertx, VertxTestContext testContext) {
HttpRequest<String> request = WebClient
.create(vertx, this.webClientOptions)
.get(8080, "localhost", "/someinvalidaddress/")
.as(BodyCodec.string());
request.send(s -> {assertThat(meterRegistry.counter("invalid.request.counter")).isEqualTo(1.0);
});
Not sure why your handle is not called (maybe your test is falling trough due missing ctx.async() ?) But in any case you can try to use the httpClient for your call and set an exceptionhandler when you make your assertion:
#Test
void invalidUrlTest(Vertx vertx, VertxTestContext ctx) {
Async async = ctx.async();
HttpClient client = vertx.createHttpClient();
client.get(8080,"localhost","/someinvalidaddress/")
.exceptionHandler(ex -> {
assertThat(meterRegistry.counter("invalid.request.counter")).isEqualTo(1.0);
async.complete();
}).end();
}

HttpServerRequest: request is handled several times

I wrote the following code:
public static void handleRequest(HttpServerRequest request, Vertx vertx) throws FileNotFoundException {
if (request.method() == HttpMethod.GET) {
if (request.path().equals("/healthcheck")) {
returnResponse(request, "I'm alive!!!\n", true);
System.out.println("OK");
return;
}
...
}
returnResponse(request, "Not Valid Request", false);
System.out.println("This request cannot be handled");
}
The weird part is that once I get a GET request with path "/healthcheck", I get in console both:
OK
and
This request cannot be handled
I'd expect to get only "OK", and then the method has to return.
Do you know how to make it happen?
You may be getting more than one request and one of them is not a get request. Can you monitor the server by inserting Log Statements
I finally found out that my browser does send two requests.
The first request is the GET localhost:8080/healthcheck and the second one is GET localhost:8080/favicon.
GET localhost:8080/favicon doesn't satisfy the condition, and the code prints "This request cannot be handled".
You are trying to handle a Preflighted request.
According to MDN
Preflighted requests unlike simple requests (discussed above),
"preflighted" requests first send an HTTP OPTIONS request header to
the resource on the other domain, in order to determine whether the
actual request is safe to send. Cross-site requests are preflighted
like this since they may have implications to user data
try this instead
public static void handleRequest(HttpServerRequest request, Vertx vertx) throws FileNotFoundException {
if(request.method() == HttpMethod.OPTIONS) {
return;
}
if (request.method() == HttpMethod.GET) {
if (request.path().equals("/healthcheck")) {
returnResponse(request, "I'm alive!!!\n", true);
System.out.println("OK");
return;
}
...
}
returnResponse(request, "Not Valid Request", false);
System.out.println("This request cannot be handled");
}

Spring - Retry request if service returns 409 HTTP Code

I have an Spring + CXF application which consumes a Transmission API: Transmission RPC running in another server.
According to Transmission docs, you need to send a token which is generated on the first request. The server then responds with a 409 http code along with a header containing the token. This token should be sent on all subsequent calls:
2.3.1. CSRF Protection Most Transmission RPC servers require a X-Transmission-Session-Id header to be sent with requests, to prevent
CSRF attacks. When your request has the wrong id -- such as when you
send your first request, or when the server expires the CSRF token --
the Transmission RPC server will return an HTTP 409 error with the
right X-Transmission-Session-Id in its own headers. So, the correct
way to handle a 409 response is to update your
X-Transmission-Session-Id and to resend the previous request.
I was looking for solution either using a CXF filter or interceptor, that basically will handle the 409 response and retry the initial request adding the token header. I'm thinking that clients can persist this token and send it in future calls.
I'm not very familiar with cxf so I was wondering if this can be accomplish and how. Any hint would be helpful.
Thanks!
Here spring-retry can be utilized which is now an independent project and no longer part of spring-batch.
As explained here retry callback will help make another call updated with the token header.
Pseudo code / logic in this case would look something like below
RetryTemplate template = new RetryTemplate();
Foo foo = template.execute(new RetryCallback<Foo>() {
public Foo doWithRetry(RetryContext context) {
/*
* 1. Check if RetryContext contains the token via hasAttribute. If available set the header else proceed
* 2. Call the transmission API
* 3.a. If API responds with 409, read the token
* 3.a.1. Store the token in RetryContext via setAttribute method
* 3.a.2. Throw a custom exception so that retry kicks in
* 3.b. If API response is non 409 handle according to business logic
* 4. Return result
*/
}
});
Make sure to configure the RetryTemplate with reasonable retry & backoff policies so as to avoid any resource contention / surprises.
Let know in comments in case of any queries / roadblock.
N.B.: RetryContext's implementation RetryContextSupport has the hasAttribute & setAttribute method inherited from Spring core AttributeAccessor
Assuming you are using Apache CXF JAX RS Client it is easy to do by just creating a custom Runtime Exception and ResponseExceptionMapper for it. So the idea is to manually convert 409 outcomes to some exception and then handle them correctly (in your case retry the service call).
See following code snipped for fully working example.
#SpringBootApplication
#EnableJaxRsProxyClient
public class SpringBootClientApplication {
// This can e stored somewhere in db or elsewhere
private static String lastToken = "";
public static void main(String[] args) {
SpringApplication.run(SpringBootClientApplication.class, args);
}
#Bean
CommandLineRunner initWebClientRunner(final TransmissionService service) {
return new CommandLineRunner() {
#Override
public void run(String... runArgs) throws Exception {
try {
System.out.println(service.sayHello(1, lastToken));
// catch the TokenExpiredException get the new token and retry
} catch (TokenExpiredException ex) {
lastToken = ex.getNewToken();
System.out.println(service.sayHello(1, lastToken));
}
}
};
}
public static class TokenExpiredException extends RuntimeException {
private String newToken;
public TokenExpiredException(String token) {
newToken = token;
}
public String getNewToken() {
return newToken;
}
}
/**
* This is where the magic is done !!!!
*/
#Provider
public static class TokenExpiredExceptionMapper implements ResponseExceptionMapper<TokenExpiredException> {
#Override
public TokenExpiredException fromResponse(Response r) {
if (r.getStatus() == 409) {
return new TokenExpiredException(r.getHeaderString("X-Transmission-Session-Id"));
}
return null;
}
}
#Path("/post")
public interface TransmissionService {
#GET
#Path("/{a}")
#Produces(MediaType.APPLICATION_JSON_VALUE)
String sayHello(#PathParam("a") Integer a, #HeaderParam("X-Transmission-Session-Id") String sessionId)
throws TokenExpiredException;
}
}

What are the methods testing POST to prevent status=405?

I was trying to build a RESTful web service using Jersey.
In my server side code, there is a path with name "domain" which I use to display content. The content of the page the "domain" refers to is accessible only correct username and password are input.
#POST
#Produces(MediaType.APPLICATION_JSON)
#Path("domain")
public ArrayList<String> domainList(#Context HttpServletRequest req) throws Exception{
Environments environments = new DefaultConfigurationBuilder().build();
final ALMProfile profile = new ALMProfile();
profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
profile.setUsername((String) req.getSession().getAttribute("username"));
//Set username from input, HTML form
profile.setPassword((String) req.getSession().getAttribute("password"));
//Set password from input, HTML form
try (ALMConnection connection = new ALMConnection(profile);) {
if (connection.getOtaConnector().connected()) {
Multimap<String, String> domain = connection.getDomains();
ArrayList<String> domain_names = new ArrayList<String>();
for(String key : domain.keys()){
if(domain_names.contains(key)) domain_names.add(key);
}
return domain_names; //return the content
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
When I attempted to test if correct content was returned, I got an error (status=405, reason=Method Not Allowed). Below is my client side test.
public static void main(String[] args){
Environments environments = new DefaultConfigurationBuilder().build();
final ALMProfile profile = new ALMProfile();
profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
profile.setUsername("username"); //Creating a profile with username and password
profile.setPassword("password");
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
String response = target.path("domain").request().accept
(MediaType.APPLICATION_JSON).get(Response.class).toString();
//Above is the GET method I see from an example,
//probably is the reason why 405 error comes from.
System.out.println(response);
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/qa-automation-console").build();
}
The servlet configuration is good. We have other paths succesfully running.
I suspect the reason might come from I used a GET method to do the job that is supposed to be POST.
But I am not familiar to Jersey methods I can use.
Does anyone know any methods that I can use to test the functionality?
See 405 Status Code
405 Method Not Allowed
The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.
Your endpoint is for a #POST request. In your client you are trying to get().
See the Client API documentation for information on how to make a POST request. If it is supposed to be a GET request, then simply change the method annotation to #GET.
Also note, for your #POST resource methods, you should always put a #Consumes annotation with the media types the method supports. If the client send a media type not supported, then they will get a 415 not supported as expected. I would have posted an example of the client post, but I have no idea what type are you are expecting because of the missing annotation, also you don't even have a post object as a method parameter so I am not even sure if your method is really even supposed to be for POST.
See Also:
How to send json object from REST client using javax.ws.rs.client.WebTarget

GET request using GWT to retrieve XML data?

Oh hello there, fellow SO members,
I have a web service that returns XML data using a simple get request that goes like this :
http://my-service:8082/qc/getData?paramX=0169&paramY=2
the service returns raw xml in the page according to the parameters' values.
I am trying to retrieve this data from a GET request in GWT using RequestBuilder, Request, etc.
However, the response gives me empty text, a Status code of ZERO (which doesn't mean anything and isn't supposed to happen), and so on.
Here's the simplified code that doesn't work.
public class SimpleXML implements EntryPoint {
public void onModuleLoad() {
this.doGet("http://my-service:8082/qc/getData", "0169", "2");
}
public void doGet(String serviceURL, String paramX, String paramY) {
final String getUrl = serviceURL + "?paramX=" + paramX + "&idTarification=" + paramY;
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, getUrl);
try {
Request response = builder.sendRequest(null, new RequestCallback() {
#Override
public void onResponseReceived(Request request, Response response) {
response.getStatusCode(); // Gives me 0 (zero) :(
}
#Override
public void onError(Request request, Throwable exception) {
// ... doesn't matter for this example
}
});
} catch (RequestException e) {
// ... doesn't matter for this example
}
}
}
I don't get why this wouldn't work, since this is REALLY simple, I've seen tutorials and they all show me this way of doing things..
Thanks in advance
The reason is, that browsers do not allow cross-site requests with AJAX (see Same Origin Policy).
This means, that you can only call a service on the same server, same port (using the same protocol) as your HTML page. If you want to perform cross-site requests, you can use JSONP, as explained in http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html.

Categories

Resources