/** * <p>Closes the underlying {@link ContentChannel}. If {@link CompletionHandler#failed(Throwable)} is called, * either of the get() methods will rethrow that Throwable.</p> */ @Override public void close() { numPendingCompletions.incrementAndGet(); closed.set(true); try { out.close(completionHandler); } catch (Throwable t) { future.setException(t); throw t; } }
@Override public void close(CompletionHandler handler) { try { requestContentChannel.close(handler); } finally { removeTask(requestContentChannelCloseTask); } } };
public boolean sendRequest(Request request, ResponseHandler responseHandler) { request.connect(responseHandler).close(null); return true; }
@Override public void close(CompletionHandler handler) { ContentChannel content; synchronized (lock) { if (closed) { throw new IllegalStateException(); } if (queue != null) { closed = true; closeCompletion = handler; return; } try { while (this.content == null) { lock.wait(); // waiting for connecTo() } } catch (InterruptedException e) { throw new IllegalStateException(e); } if (closed) { throw new IllegalStateException(); } closed = true; content = this.content; } content.close(handler); }
@Override public void close(CompletionHandler completionHandler) { if ( closed ) return; try { httpRequest.getData().close(); } catch (IOException e) {}; if (channel == null) channel = handleResponse(); try { channel.close(completionHandler); } catch (IllegalStateException e) { // Ignore: Known to be thrown when the other party closes } closed = true; }
@Override public void close(CompletionHandler completionHandler) { final CompletionHandler referenceCountingCompletionHandler = new ReferenceCountingCompletionHandler(request, completionHandler); try (final ResourceReference ref = requestReference) { delegate.close(referenceCountingCompletionHandler); } catch (Throwable t) { try { referenceCountingCompletionHandler.failed(t); } catch (AlreadyCompletedException ignored) { } catch (Throwable failFailure) { log.log(Level.WARNING, "Failure during call to CompletionHandler.failed()", failFailure); } throw t; } }
@Override public ContentChannel handle(Request request, ResponseHandler handler) { ContentChannel out = handler.handleResponse(new Response(Response.Status.OK)); out.close(EXCEPTION_COMPLETION_HANDLER); return null; } });
protected final void closeResponse(final ContentChannel out) { try { out.close(newCompletionHandler()); } finally { responseClosed.happened(); } }
@Override public void render(OutputStream output, ContentChannel networkChannel, CompletionHandler handler) throws IOException { if (rendererCopy instanceof AsynchronousSectionedRenderer) { AsynchronousSectionedRenderer<Result> renderer = (AsynchronousSectionedRenderer<Result>) rendererCopy; renderer.setNetworkWiring(networkChannel, handler); } try { try { waitableRender(output); } finally { if (!(rendererCopy instanceof AsynchronousSectionedRenderer)) { output.flush(); } } } finally { if (networkChannel != null && !(rendererCopy instanceof AsynchronousSectionedRenderer)) { networkChannel.close(handler); } } }
@Override public void close(CompletionHandler completionHandler) { final ProxyCompletionHandler proxyCompletionHandler = new ProxyCompletionHandler(request, completionHandler); try (final ResourceReference ref = requestReference) { delegate.close(proxyCompletionHandler); } catch (Throwable t) { try { proxyCompletionHandler.failed(t); } catch (Throwable ignored) { // empty } throw t; } }
/** * Closes the underlying {@link ContentChannel} and waits for the operation to complete. * * @throws InterruptedException If the thread was interrupted while waiting. * @throws RuntimeException If the operation failed to complete, see cause for details. */ public void close() throws InterruptedException { try { FutureCompletion future = new FutureCompletion(); channel.close(future); future.get(); } catch (ExecutionException e) { Throwable t = e.getCause(); if (t instanceof RuntimeException) { throw (RuntimeException)t; } if (t instanceof Error) { throw (Error)t; } throw new RuntimeException(t); } }
@Override public ContentChannel handle(Request request, ResponseHandler handler) { ContentChannel out = handler.handleResponse(new Response(Response.Status.OK)); writeResponse(out); out.close(EXCEPTION_COMPLETION_HANDLER); return null; } });
private void writeRequestBody(Request request, ContentChannel inputRequestChannel) { List<BlockingCompletionHandler> completionHandlers = new ArrayList<>(); if (request.getBody().length > 0) { BlockingCompletionHandler w = new BlockingCompletionHandler(); try { inputRequestChannel.write(ByteBuffer.wrap(request.getBody()), w); completionHandlers.add(w); } finally { BlockingCompletionHandler c = new BlockingCompletionHandler(); inputRequestChannel.close(c); completionHandlers.add(c); } } else { BlockingCompletionHandler c = new BlockingCompletionHandler(); inputRequestChannel.close(c); completionHandlers.add(c); } for (BlockingCompletionHandler completionHandler : completionHandlers) { completionHandler.waitUntilCompleted(); } }
@Override public void handleReply(final Reply reply) { final MbusResponse response = new MbusResponse(StatusCodes.fromMbusReply(reply), reply); handler.handleResponse(response).close(IgnoringCompletionHandler.INSTANCE); } }
public MockResponseHandler sendRequest(String uri, HttpRequest.Method method, ByteBuffer body) { responseHandler = new MockResponseHandler(); Request request = HttpRequest.newServerRequest(driver, URI.create(uri), method); request.context().put("contextVariable", 37); // TODO: Add a method for accepting a Request instead ContentChannel requestContent = request.connect(responseHandler); requestContent.write(body, null); requestContent.close(null); request.release(); return responseHandler; }
@SuppressWarnings("try") @Override public void close(final CompletionHandler completionHandler) { try (final ResourceReference ref = requestReference) { final byte[] requestContentBytes = accumulatedRequestContent.toByteArray(); final String content = new String(requestContentBytes, contentCharset); completionHandler.completed(); final Map<String, List<String>> parameterMap = parseFormParameters(content); mergeParameters(parameterMap, request.parameters()); final ContentChannel contentChannel = delegateHandler.handleRequest(request, responseHandler); if (contentChannel != null) { if (!removeBody) { final ByteBuffer byteBuffer = ByteBuffer.wrap(requestContentBytes); contentChannel.write(byteBuffer, NOOP_COMPLETION_HANDLER); } contentChannel.close(NOOP_COMPLETION_HANDLER); } } }
public MockResponseHandler sendRequest(String uri, HttpRequest.Method method, ByteBuffer body, String contentType) { responseHandler = new MockResponseHandler(); Request request = HttpRequest.newServerRequest(driver, URI.create(uri), method); request.context().put("contextVariable", 37); // TODO: Add a method for accepting a Request instead request.headers().put(com.yahoo.jdisc.http.HttpHeaders.Names.CONTENT_TYPE, contentType); ContentChannel requestContent = request.connect(responseHandler); requestContent.write(body, null); requestContent.close(null); request.release(); return responseHandler; }
@Override public void handleReply(final Reply reply) { reply.getTrace().trace(6, "Reply received by MbusClient."); final ResponseHandler handler = (ResponseHandler)reply.getContext(); reply.popHandler(); // restore user context try { handler.handleResponse(new MbusResponse(StatusCodes.fromMbusReply(reply), reply)) .close(IgnoredCompletionHandler.INSTANCE); } catch (final Exception e) { log.log(LogLevel.WARNING, "Ignoring exception thrown by ResponseHandler.", e); } }
@Override public ContentChannel handleRequest(Request request, ResponseHandler responseHandler) { try (final ResourceReference requestReference = request.refer()) { ContentChannel contentChannel; final ResponseHandler proxyResponseHandler = new ProxyResponseHandler( request, new NullContentResponseHandler(responseHandler)); try { contentChannel = delegate.handleRequest(request, proxyResponseHandler); Objects.requireNonNull(contentChannel, "contentChannel"); } catch (Throwable t) { try { proxyResponseHandler .handleResponse(new Response(Response.Status.INTERNAL_SERVER_ERROR, t)) .close(IGNORED_COMPLETION); } catch (Throwable ignored) { // empty } throw t; } return new ProxyContentChannel(request, contentChannel); } }
@Override public void handleMessage(Message msg) { if (!running.get()) { dispatchErrorReply(msg, ErrorCode.SESSION_BUSY, "Session temporarily closed."); return; } if (msg.getTrace().shouldTrace(6)) { msg.getTrace().trace(6, "Message received by MbusServer."); } Request request = null; ContentChannel content = null; try { request = new MbusRequest(container, uri, msg); content = request.connect(new ServerResponseHandler(msg)); } catch (RuntimeException e) { dispatchErrorReply(msg, ErrorCode.APP_FATAL_ERROR, e.toString()); } finally { if (request != null) { request.release(); } } if (content != null) { content.close(IgnoredCompletionHandler.INSTANCE); } }