List of usage examples for org.apache.http.client ClientProtocolException ClientProtocolException
public ClientProtocolException(final Throwable cause)
From source file:org.wikipedia.vlsergey.secretary.jwpf.HttpBot.java
private void onPostResponse(final ContentProcessable action, final HttpPost postMethod, HttpResponse response) throws IOException { try {// ww w . j a va2s. c o m int statuscode = response.getStatusLine().getStatusCode(); if (action.followRedirects() && (statuscode == HttpStatus.SC_MOVED_TEMPORARILY || statuscode == HttpStatus.SC_MOVED_PERMANENTLY || statuscode == HttpStatus.SC_SEE_OTHER || statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { /* * Usually a successful form-based login results in a redicrect * to another url */ Header header = response.getFirstHeader("location"); if (header != null) { String newuri = header.getValue(); if ((newuri == null) || (newuri.equals(""))) { newuri = "/"; } log.debug("Redirect target: " + newuri); HttpPost redirect = new HttpPost(newuri); redirect.setEntity(postMethod.getEntity()); redirect.setHeader("Accept-Encoding", GZIP_CONTENT_ENCODING); log.trace("GET: " + redirect.getURI()); httpClient.execute(redirect, new ResponseHandler<Object>() { @Override public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException { // no more redirects? onPostResponse(action, postMethod, response); return null; } }); return; } } if (statuscode == HttpStatus.SC_INTERNAL_SERVER_ERROR) { throw new ServerErrorException(response.getStatusLine()); } if (statuscode != HttpStatus.SC_OK) { throw new ClientProtocolException(response.getStatusLine().toString()); } final Header databaseLag = response.getFirstHeader("X-Database-Lag"); final Header retryAfter = response.getFirstHeader("Retry-After"); if (databaseLag != null) { throw new DatabaseLagException(databaseLag, retryAfter); } InputStream inputStream = response.getEntity().getContent(); String out; try { String encoding = response.getFirstHeader("Content-Encoding") != null ? response.getFirstHeader("Content-Encoding").getValue() : ""; if (GZIP_CONTENT_ENCODING.equalsIgnoreCase(encoding)) { inputStream = new GZIPInputStream(inputStream); } Header charsetHeader = response.getFirstHeader("Content-Type"); String charset; if (charsetHeader == null) charset = MediaWikiBot.ENCODING; else charset = getContentCharSet(charsetHeader); out = IoUtils.readToString(inputStream, charset); } finally { inputStream.close(); } action.processReturningText(postMethod, out); action.validateReturningCookies(httpClient.getCookieStore().getCookies(), postMethod); log.trace(postMethod.getURI() + " || " + "POST: " + response.getStatusLine().toString()); } catch (CookieException exc) { throw new ClientProtocolException(exc.getMessage(), exc); } catch (ProcessException exc) { throw new ClientProtocolException(exc.getMessage(), exc); } }
From source file:nl.opengeogroep.filesetsync.client.FilesetSyncer.java
private void retrieveFilesetList() throws IOException { final boolean cachedFileList = state.getFileListDate() != null && state.getFileListRemotePath().equals(fs.getRemote()) && (!fs.isHash() || state.isFileListHashed()) && SyncJobState.haveCachedFileList(fs.getName()); String s = "Retrieving file list"; if (cachedFileList) { s += String.format(" (last file list cached at %s)", FormatUtil.dateToString(state.getFileListDate())); }/*from w w w. ja v a 2 s.com*/ action(s); try (CloseableHttpClient httpClient = HttpClientUtil.get()) { HttpUriRequest get = RequestBuilder.get().setUri(serverUrl + "list/" + fs.getRemote()) .addParameter("hash", fs.isHash() + "").addParameter("regexp", fs.getRegexp()).build(); if (cachedFileList) { get.addHeader(HttpHeaders.IF_MODIFIED_SINCE, HttpUtil.formatDate(state.getFileListDate())); } addExtraHeaders(get); // Request poorly encoded text format get.addHeader(HttpHeaders.ACCEPT, "text/plain"); ResponseHandler<List<FileRecord>> rh = new ResponseHandler<List<FileRecord>>() { @Override public List handleResponse(HttpResponse hr) throws ClientProtocolException, IOException { log.info("< " + hr.getStatusLine()); int status = hr.getStatusLine().getStatusCode(); if (status == SC_NOT_MODIFIED) { return null; } else if (status >= SC_OK && status < 300) { HttpEntity entity = hr.getEntity(); if (entity == null) { throw new ClientProtocolException("No response entity, invalid server URL?"); } try (InputStream in = entity.getContent()) { return Protocol.decodeFilelist(in); } } else { if (log.isTraceEnabled()) { String entity = hr.getEntity() == null ? null : EntityUtils.toString(hr.getEntity()); log.trace("Response body: " + entity); } else { EntityUtils.consumeQuietly(hr.getEntity()); } throw new ClientProtocolException("Server error: " + hr.getStatusLine()); } } }; log.info("> " + get.getRequestLine()); fileList = httpClient.execute(get, rh); if (fileList == null) { log.info("Cached file list is up-to-date"); fileList = SyncJobState.readCachedFileList(fs.getName()); } else { log.info("Filelist returned " + fileList.size() + " files"); /* Calculate last modified client-side, requires less server * memory */ long lastModified = -1; for (FileRecord fr : fileList) { lastModified = Math.max(lastModified, fr.getLastModified()); } if (lastModified != -1) { state.setFileListRemotePath(fs.getRemote()); state.setFileListDate(new Date(lastModified)); state.setFileListHashed(fs.isHash()); SyncJobState.writeCachedFileList(fs.getName(), fileList); SyncJobStatePersistence.persist(); } } } }
From source file:org.sonatype.nexus.repository.httpclient.FilteredHttpClient.java
private static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException { HttpHost target = null;/*from www . ja v a2 s .co m*/ final URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) { throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI); } } return target; }
From source file:sample.ui.mvc.MessageController.java
private String getBidId(Message message) { try {//ww w. ja v a 2 s . com BasicCookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); doLogin(cookieStore, httpclient, ZHANGDAIYIXIAN); // String bidName = message.getBidName(); // time // String mainUrl = "http://www.wujinsuo.cn:80/index.php"; HttpGet httpget = new HttpGet(mainUrl); httpget.addHeader("Accept", ACCEPT); httpget.addHeader("User-Agent", AGENT); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String resultString = httpclient.execute(httpget, responseHandler); // parse html Document doc = Jsoup.parse(resultString); Elements links = doc.select("a[href]"); Element aElement = null; for (Element e : links) { List<Node> childNode = e.childNodes(); if (childNode.size() != 1) continue; Node node = childNode.get(0); if ("span".equals(node.nodeName())) { String html = node.outerHtml(); logger.info(html); if (html.contains(bidName)) { // okle aElement = e; } } } if (aElement == null) { // retry return ""; } else { String href = aElement.attr("href"); String bidId = StringUtils.substringAfter(href, "id="); logger.info(bidId); return bidId; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:org.grycap.gpf4med.DownloadService.java
/** * Uses a group of URIs to retrieve objects and writes them to the same number of files. This method will do * its best effort to optimally handle the downloads, opening a pool of connections to the servers and reusing * them as much as possible. Also, it will create several concurrent threads in the JVM in order to perform * simultaneous downloads./*from w w w .j av a 2 s. c om*/ * @param requests a key-value map with the list of requests to handle. The source of the object is the key of * the map, while the value is the destination file. * @param validator checks the file for correctness. * @param config download settings. * @param encryptionProvider an optional encryption provider that, when available, is used to encrypt the * files after download. * @param task an optional task that will be executed passing each individual file as parameter, when the download * of the file ends. * @return the requests that could not be served after exhausting the individual retries. * @throws IOException if an error occurs in the execution of the operation. */ public ImmutableMap<URI, File> download(final ImmutableMap<URI, File> requests, final @Nullable FileValidator validator, final DownloadConfiguration config, final @Nullable FileEncryptionProvider encryptionProvider, final @Nullable PostProcessTask<File> task) throws IOException { checkArgument(requests != null, "Uninitialized request"); checkArgument(config != null, "Uninitialized configuration"); ImmutableMap<URI, File> pending = ImmutableMap.copyOf(requests); final List<URI> cancelled = new ArrayList<URI>(); try { for (int attempt = 0; attempt < config.getRetries() && !pending.isEmpty() && pending.size() > cancelled.size(); attempt++) { LOGGER.info("Attempt " + (attempt + 1) + " to download " + requests.size() + " files"); // create connection manager final PoolingNHttpClientConnectionManager connectionManager = createConnectionManager(); // create HTTP asynchronous client int eSoTimeoutMs = config.soToMs + (int) (config.soToMs * attempt * (config.toIncPercent >= 0.0d && config.toIncPercent <= 1.0d ? config.toIncPercent : 0.0d)); int eConnTimeoutMs = config.connToMs + (int) (config.connToMs * attempt * (config.toIncPercent >= 0.0d && config.toIncPercent <= 1.0d ? config.toIncPercent : 0.0d)); final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(eConnTimeoutMs) .setConnectionRequestTimeout(eConnTimeoutMs).setSocketTimeout(eSoTimeoutMs).build(); final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom() .setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build(); httpclient.start(); // attempt to perform download try { final CountDownLatch latch = new CountDownLatch(pending.size()); for (final Map.Entry<URI, File> entry : pending.entrySet()) { final URI uri = entry.getKey(); if (cancelled.contains(uri)) { continue; } final File file = entry.getValue(); FileUtils.forceMkdir(file.getParentFile()); final HttpGet request = new HttpGet(uri); final HttpAsyncRequestProducer producer = new BasicAsyncRequestProducer( new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request); final ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(file) { @Override protected File process(final HttpResponse response, final File file, final ContentType contentType) throws Exception { releaseResources(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { FileUtils.deleteQuietly(file); throw new ClientProtocolException( "Download failed: " + response.getStatusLine()); } if (validator != null && !validator.isValid(file)) { FileUtils.deleteQuietly(file); cancelled.add(uri); throw new IOException( file.getCanonicalPath() + " not recognised as a supported file format"); } if (encryptionProvider != null) { try { final File cipherFile = File .createTempFile(RandomStringUtils.random(8, true, true), ".tmp"); encryptionProvider.encrypt(new FileInputStream(file), new FileOutputStream(cipherFile)); FileUtils.deleteQuietly(file); FileUtils.moveFile(cipherFile, file); LOGGER.info("File encrypted: " + file.getCanonicalPath()); } catch (Exception e) { FileUtils.deleteQuietly(file); cancelled.add(uri); LOGGER.warn("Failed to encrypt: " + file.getCanonicalPath(), e); throw new IOException("File encryption failed"); } } LOGGER.info("Download succeed to file: " + file.getCanonicalPath()); return file; } }; httpclient.execute(producer, consumer, new FutureCallback<File>() { @Override public void completed(final File result) { request.releaseConnection(); latch.countDown(); if (task != null) { task.apply(result); } LOGGER.info("Request succeed: " + request.getRequestLine() + " => Response file length: " + result.length()); } @Override public void failed(final Exception ex) { request.releaseConnection(); FileUtils.deleteQuietly(file); latch.countDown(); LOGGER.error("Request failed: " + request.getRequestLine() + "=>" + ex); } @Override public void cancelled() { request.releaseConnection(); FileUtils.deleteQuietly(file); latch.countDown(); LOGGER.error("Request cancelled: " + request.getRequestLine()); } }); } latch.await(); } finally { try { httpclient.close(); } catch (Exception ignore) { } try { shutdown(connectionManager, 0l); } catch (Exception ignore) { } } // populate the pending list with the files that does not exist final ImmutableMap.Builder<URI, File> builder = new ImmutableMap.Builder<URI, File>(); for (final Map.Entry<URI, File> entry : requests.entrySet()) { if (!entry.getValue().exists()) { builder.put(entry.getKey(), entry.getValue()); } } pending = builder.build(); if ((attempt + 1) < config.retries && !pending.isEmpty() && pending.size() > cancelled.size()) { final long waitingTime = (long) (config.soToMs * 0.1d); LOGGER.info("Waiting " + waitingTime + " ms before attempt " + (attempt + 2) + " to download " + requests.size() + " pending files"); Thread.sleep(waitingTime); } } } catch (IOException ioe) { throw ioe; } catch (Exception e) { throw new IOException("Download has failed", e); } return pending; }
From source file:co.paralleluniverse.fibers.httpclient.FiberHttpClient.java
private static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException { // A null target may be acceptable if there is a default target. // Otherwise, the null target is detected in the director. HttpHost target = null;/* w w w .ja v a 2 s . com*/ final URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI); } return target; }
From source file:org.muhia.app.psi.config.http.CustomHttpClientUtilities.java
public String doGetWithResponseHandler(String url, Map<String, String> allRequestParams) { AtomicReference<String> result = new AtomicReference<>(""); // CloseableHttpResponse response = null; CloseableHttpClient client = null;/*w w w . j a va 2s . co m*/ try { HttpGet httpGet = new HttpGet(url); URIBuilder uriBuilder = new URIBuilder(httpGet.getURI()); allRequestParams.entrySet().forEach((entry) -> { String key = entry.getKey(); String value = entry.getValue(); if (value != null) { uriBuilder.setParameter(key, value); } }); httpGet.setURI(uriBuilder.build()); RequestConfig config = RequestConfig.custom().setConnectTimeout(hcp.getConnectionTimeout()) .setConnectionRequestTimeout(hcp.getConnectionRequestTimeout()) .setSocketTimeout(hcp.getSockectTimeout()).build(); client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); ResponseHandler<String> responseHandler = (final HttpResponse response) -> { int status = response.getStatusLine().getStatusCode(); if (status >= hcp.getLowerStatusLimit() && status <= hcp.getUpperStatusLimit()) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException(hcp.getUnexpectedStatus() + status); } }; result.set(client.execute(httpGet, responseHandler)); client.close(); } catch (IOException | URISyntaxException ex) { // LoggerFactory.getLogger(CustomHttpClientUtil.class).error(ex.getMessage(), // ex); Logger.getLogger(CustomHttpClientUtilities.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (client != null) { client.close(); } } catch (IOException ex) { // LoggerFactory.getLogger(CustomHttpClientUtil.class).error(ex.getMessage(), // ex); result.set(hcp.getIoExceptionMessage()); Logger.getLogger(CustomHttpClientUtilities.class.getName()).log(Level.SEVERE, null, ex); } } return result.get(); }
From source file:com.klarna.checkout.stubs.HttpClientStub.java
/** * Stubbed Execute implementation./*from www .j a v a2 s . c o m*/ * * @param <T> The class ResponseHandler operates on * @param hur HttpUriRequest object * @param rh ResponseHandler object * @param hc HttpContext holder * * @return ResponseHandler result * * @throws IOException never */ @Override public <T> T execute(final HttpUriRequest hur, final ResponseHandler<? extends T> rh, final HttpContext hc) throws IOException { this.httpUriReq = hur; List<Integer> redirects = new ArrayList(); redirects.add(301); redirects.add(302); redirects.add(303); this.visited.clear(); if (this.httpUriReq instanceof HttpEntityEnclosingRequest) { try { this.httpUriReq = new EntityEnclosingRequestWrapper((HttpEntityEnclosingRequest) hur); } catch (ProtocolException ex) { throw new IOException(ex); } } int status; do { try { for (HttpRequestInterceptor hri : requestInterceptors) { hri.process(this.httpUriReq, hc); } } catch (HttpException ex) { throw new ClientProtocolException(ex); } if (!this.visited.add(this.httpUriReq.getURI())) { throw new ClientProtocolException(new CircularRedirectException()); } this.lastResponse = this.getResponse(); if (this.lastResponse.getStatusLine().getStatusCode() < 400) { fixData(); } try { for (HttpResponseInterceptor hri : responseInterceptors) { hri.process(this.lastResponse, hc); } } catch (HttpException ex) { throw new ClientProtocolException(ex); } status = this.lastResponse.getStatusLine().getStatusCode(); Header location = this.lastResponse.getLastHeader("Location"); if (location != null) { this.httpUriReq = new HttpGet(location.getValue()); } } while (redirects.contains(status)); if (this.data.containsKey("test")) { ByteArrayInputStream bis = new ByteArrayInputStream(JSONObject.toJSONString(data).getBytes()); this.lastResponse.setEntity(new InputStreamEntity(bis, bis.available())); } return rh.handleResponse(this.lastResponse); }
From source file:com.thed.zapi.cloud.sample.FetchExecuteUpdate.java
public static void addTestsToCycle(String uriStr, ZFJCloudRestClient client, String accessKey, StringEntity addTestsJSON) throws URISyntaxException, JSONException { URI uri = new URI(uriStr); int expirationInSec = 360; JwtGenerator jwtGenerator = client.getJwtGenerator(); String jwt = jwtGenerator.generateJWT("POST", uri, expirationInSec); // System.out.println(uri.toString()); // System.out.println(jwt); HttpResponse response = null;//from w w w . j ava 2 s. com HttpClient restClient = new DefaultHttpClient(); HttpPost addTestsReq = new HttpPost(uri); addTestsReq.addHeader("Content-Type", "application/json"); addTestsReq.addHeader("Authorization", jwt); addTestsReq.addHeader("zapiAccessKey", accessKey); addTestsReq.setEntity(addTestsJSON); try { response = restClient.execute(addTestsReq); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } int statusCode = response.getStatusLine().getStatusCode(); // System.out.println(statusCode); // System.out.println(response.toString()); if (statusCode >= 200 && statusCode < 300) { System.out.println("Tests added Successfully"); } else { try { throw new ClientProtocolException("Unexpected response status: " + statusCode); } catch (ClientProtocolException e) { e.printStackTrace(); } } }
From source file:com.apigee.sdk.apm.http.impl.client.cache.ResponseProtocolCompliance.java
private void requestDidNotExpect100ContinueButResponseIsOne(HttpRequest request, HttpResponse response) throws ClientProtocolException { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CONTINUE) { return;/*from w ww . j a v a2s. c o m*/ } if (!requestWasWrapped(request)) { return; } ProtocolVersion originalProtocol = getOriginalRequestProtocol((RequestWrapper) request); if (originalProtocol.compareToVersion(HttpVersion.HTTP_1_1) >= 0) { return; } if (originalRequestDidNotExpectContinue((RequestWrapper) request)) { throw new ClientProtocolException("The incoming request did not contain a " + "100-continue header, but the response was a Status 100, continue."); } }