@Override public void onThrowable(Throwable t) { this.throwable = t; // Counting down to handle error cases too. // In "premature exceptions" cases, the onBodyPartReceived() and // onCompleted() // methods will never be invoked, leaving caller of getResponse() method // blocked forever. try { semaphore.acquire(); } catch (InterruptedException e) { // Ignore } finally { headersArrived.countDown(); semaphore.release(); } try { closeOut(); } catch (IOException e) { // ignore } }
@Override public Response onCompleted() throws IOException { if (!responseSet) { response = responseBuilder.build(); responseSet = true; } // Counting down to handle error cases too. // In "normal" cases, latch is already at 0 here // But in other cases, for example when because of some error // onBodyPartReceived() is never called, the caller // of getResponse() would remain blocked infinitely. // By contract, onCompleted() is always invoked, even in case of errors headersArrived.countDown(); closeOut(); try { semaphore.acquire(); if (throwable != null) { throw new IOException(throwable); } else { // sending out current response return responseBuilder.build(); } } catch (InterruptedException e) { return null; } finally { semaphore.release(); } }
public void onThrowable(Throwable t) { this.throwable = t; // Counting down to handle error cases too. // In "premature exceptions" cases, the onBodyPartReceived() and // onCompleted() // methods will never be invoked, leaving caller of getResponse() method // blocked forever. try { semaphore.acquire(); } catch (InterruptedException e) { // Ignore } finally { headersArrived.countDown(); semaphore.release(); } try { closeOut(); } catch (IOException e) { // ignore } }
public Response onCompleted() throws IOException { if (!responseSet) { response = responseBuilder.build(); responseSet = true; } // Counting down to handle error cases too. // In "normal" cases, latch is already at 0 here // But in other cases, for example when because of some error // onBodyPartReceived() is never called, the caller // of getResponse() would remain blocked infinitely. // By contract, onCompleted() is always invoked, even in case of errors headersArrived.countDown(); closeOut(); try { semaphore.acquire(); if (throwable != null) { IOException ioe = new IOException(throwable.getMessage()); ioe.initCause(throwable); throw ioe; } else { // sending out current response return responseBuilder.build(); } } catch (InterruptedException e) { return null; } finally { semaphore.release(); } }