Felice Pollano
Felice Pollano

Reputation: 33272

Restlet, can't handle ConnectionException

I have the following code:

private Job[] synchro(URI u)  {
        try{
            Client client = new Client(new Context(),Protocol.HTTP);
            Reference samplesUri = new Reference(u+"/synchrojobs/");

            ClientResource cr = new ClientResource(client.getContext(),Method.POST,samplesUri);
            cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "xxx", "uxxx");
            Job[] back = persistence.all().toArray(new Job[persistence.all().size()]);
            for(Job trace : back){
                logger.debug(String.format("Sending synchro for %s status:%s time:%s",trace.getId().toString(),trace.getStatus(),trace.getLastStatusChangeDate().toString()));
            }
            Representation res = cr.post(new JacksonRepresentation<Job[]>(back));
            Job[] result = this.mapper.readValue(res.getStream(),Job[].class);
            cr.release();
            res.release();

            logger.info(String.format("Found %s jobs from %s",result.length,u));

            return result;
        }catch(Exception e){
            logger.error("Error synchronizing jobs:"+e.getMessage());
            return new Job[0];
        }
    }

if the other party is not connected, an exception is raised, but is not caught in my block, it pop up externally, why could this happen?

Upvotes: 1

Views: 953

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202326

In fact, such error is directly thrown by the client connector and wrapper within a restlet exception.

I did some tests and a Restlet ResourceException is thrown with cause java.net.ConnectException. But I can catch it using a try / catch(Exception). See below:

mai 12, 2015 7:18:54 PM org.restlet.engine.connector.HttpClientHelper start
INFOS: Starting the internal HTTP client
mai 12, 2015 7:18:54 PM org.restlet.resource.ClientResource retry
INFOS: A recoverable error was detected (1000), attempting again in 2000 ms.
mai 12, 2015 7:18:56 PM org.restlet.resource.ClientResource retry
INFOS: A recoverable error was detected (1000), attempting again in 2000 ms.
Connection Error (1000) - The connector failed to connect to the server

at org.restlet.resource.ClientResource.doError(ClientResource.java:590)
    at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1153)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1048)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1023)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:928)
    at org.restlet.resource.ClientResource.get(ClientResource.java:636)
    at test.TestClient.main(TestClient.java:9)
Caused by: java.net.ConnectException: Connexion refusée
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
    at sun.net.www.http.HttpClient.New(HttpClient.java:308)
    at sun.net.www.http.HttpClient.New(HttpClient.java:326)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:997)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:851)
    at org.restlet.engine.connector.HttpUrlConnectionCall.sendRequest(HttpUrlConnectionCall.java:356)

Perhaps can you try to use a try / catch(Throwable) to see which exception is actually thrown?

Moreover could you give us the version of Restlet and the client connector you use? For my test I use version 2.3.1 of Restlet...

Hope it helps you, Thierry

Upvotes: 1

Related Questions