List of usage examples for org.apache.http.client.methods HttpRequestBase isAborted
public boolean isAborted()
From source file:org.musicmount.io.server.dav.DAVResourceProvider.java
protected Sardine createSardine(final ServerFileSystem fileSystem) { /*/* w w w .j av a 2 s . c om*/ * extract user/password */ String user = null; String password = null; if (fileSystem.getUserInfo() != null) { String[] userAndPassword = fileSystem.getUserInfo().split(":"); user = userAndPassword[0]; password = userAndPassword.length > 1 ? userAndPassword[1] : null; } /* * create customized sardine */ return new SardineImpl(user, password, null) { @Override protected Registry<ConnectionSocketFactory> createDefaultSchemeRegistry() { ConnectionSocketFactory socketFactory; if ("https".equalsIgnoreCase(fileSystem.getScheme())) { socketFactory = createDefaultSecureSocketFactory(); } else { socketFactory = createDefaultSocketFactory(); } return RegistryBuilder.<ConnectionSocketFactory>create() .register(fileSystem.getScheme(), socketFactory).build(); } @Override protected ConnectionSocketFactory createDefaultSecureSocketFactory() { try { // trust anybody... SSLContext context = SSLContext.getInstance("TLS"); X509TrustManager trustManager = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; context.init(null, new TrustManager[] { trustManager }, null); return new SSLConnectionSocketFactory(context, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } catch (NoSuchAlgorithmException | KeyManagementException e) { // should not happen... } return super.createDefaultSecureSocketFactory(); } @Override protected <T> T execute(HttpRequestBase request, ResponseHandler<T> responseHandler) throws IOException { /* * Sardine re-executes a PUT request after a org.apache.http.NoHttpResponseException without resetting it... */ if (request.isAborted()) { request.reset(); } return super.execute(request, responseHandler); } @Override public ContentLengthInputStream get(String url, Map<String, String> headers) throws IOException { /* * abort rather than consume entity for better performance */ final HttpGet get = new HttpGet(url); for (String header : headers.keySet()) { get.addHeader(header, headers.get(header)); } // Must use #execute without handler, otherwise the entity is consumed already after the handler exits. final HttpResponse response = this.execute(get); VoidResponseHandler handler = new VoidResponseHandler(); try { handler.handleResponse(response); // Will consume or abort the entity when the stream is closed. PositionInputStream positionInputStream = new PositionInputStream( response.getEntity().getContent()) { public void close() throws IOException { if (getPosition() == response.getEntity().getContentLength()) { EntityUtils.consume(response.getEntity()); } else { // partial read or unknown content length get.abort(); } } }; return new ContentLengthInputStream(positionInputStream, response.getEntity().getContentLength()); } catch (IOException ex) { get.abort(); throw ex; } } }; }
From source file:com.gsma.mobileconnect.utils.RestClient.java
/** * Execute the given request./*from ww w. j av a2 s . c om*/ * <p> * Abort the request if it exceeds the specified timeout. * * @param httpClient The client to use. * @param request The request to make. * @param context The context to use. * @param timeout The timeout to use. * @return A Http Response. * @throws RestException Thrown if the request fails or times out. */ private CloseableHttpResponse executeRequest(CloseableHttpClient httpClient, final HttpRequestBase request, HttpClientContext context, int timeout) throws RestException { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { request.abort(); } }, timeout); RequestConfig localConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout) .setConnectTimeout(timeout).setSocketTimeout(timeout).setCookieSpec(CookieSpecs.STANDARD).build(); request.setConfig(localConfig); try { return httpClient.execute(request, context); } catch (IOException ex) { String requestUri = request.getURI().toString(); if (request.isAborted()) { throw new RestException("Rest end point did not respond", requestUri); } throw new RestException("Rest call failed", requestUri, ex); } }
From source file:com.mirth.connect.client.core.ServerConnection.java
/** * Allows multiple simultaneous requests. */// w w w. j av a 2s. co m private ClientResponse executeAsync(ClientRequest request) throws ClientException { HttpRequestBase requestBase = null; CloseableHttpResponse response = null; boolean shouldClose = true; try { requestBase = setupRequestBase(request, ExecuteType.ASYNC); response = client.execute(requestBase); ClientResponse responseContext = handleResponse(request, requestBase, response); if (responseContext.hasEntity()) { shouldClose = false; } return responseContext; } catch (Exception e) { if (requestBase != null && requestBase.isAborted()) { throw new RequestAbortedException(e); } else if (e instanceof ClientException) { throw (ClientException) e; } throw new ClientException(e); } finally { if (shouldClose) { HttpClientUtils.closeQuietly(response); } } }
From source file:com.mirth.connect.client.core.ServerConnection.java
/** * Allows one request at a time.//from w ww . jav a2 s . c om */ private synchronized ClientResponse executeSync(ClientRequest request, Operation operation) throws ClientException { synchronized (currentOp) { currentOp.setName(operation.getName()); currentOp.setDisplayName(operation.getDisplayName()); currentOp.setAuditable(operation.isAuditable()); } HttpRequestBase requestBase = null; CloseableHttpResponse response = null; boolean shouldClose = true; try { requestBase = setupRequestBase(request, ExecuteType.SYNC); response = client.execute(requestBase); ClientResponse responseContext = handleResponse(request, requestBase, response, true); if (responseContext.hasEntity()) { shouldClose = false; } return responseContext; } catch (Exception e) { if (requestBase != null && requestBase.isAborted()) { throw new RequestAbortedException(e); } else if (e instanceof ClientException) { throw (ClientException) e; } throw new ClientException(e); } finally { if (shouldClose) { HttpClientUtils.closeQuietly(response); synchronized (currentOp) { currentOp.setName(null); currentOp.setDisplayName(null); currentOp.setAuditable(false); } } } }
From source file:com.mirth.connect.client.core.ServerConnection.java
/** * The requests sent through this channel will be aborted on the client side when a new request * arrives. Currently there is no guarantee of the order that pending requests will be sent. *//*from www. ja va 2 s . co m*/ private ClientResponse executeAbortPending(ClientRequest request) throws ClientException { // TODO: Make order sequential abortTask.incrementRequestsInQueue(); synchronized (abortExecutor) { if (!abortExecutor.isShutdown() && !abortTask.isRunning()) { abortExecutor.execute(abortTask); } HttpRequestBase requestBase = null; CloseableHttpResponse response = null; boolean shouldClose = true; try { abortPendingClientContext = HttpClientContext.create(); abortPendingClientContext.setRequestConfig(requestConfig); requestBase = setupRequestBase(request, ExecuteType.ABORT_PENDING); abortTask.setAbortAllowed(true); response = client.execute(requestBase, abortPendingClientContext); abortTask.setAbortAllowed(false); ClientResponse responseContext = handleResponse(request, requestBase, response); if (responseContext.hasEntity()) { shouldClose = false; } return responseContext; } catch (Exception e) { if (requestBase != null && requestBase.isAborted()) { return new ClientResponse(Status.NO_CONTENT, request); } else if (e instanceof ClientException) { throw (ClientException) e; } throw new ClientException(e); } finally { abortTask.decrementRequestsInQueue(); if (shouldClose) { HttpClientUtils.closeQuietly(response); } } } }