List of usage examples for org.apache.http.client.utils URIBuilder clearParameters
public URIBuilder clearParameters()
From source file:adalid.util.google.Translator.java
private static String translate(String arg, URIBuilder builder, CloseableHttpClient client) throws IOException, URISyntaxException { builder.clearParameters(); builder.setParameter("langpair", "en|es"); builder.setParameter("text", arg); URI uri = builder.build();/* w ww . j av a 2s .co m*/ HttpGet get = new HttpGet(uri); HttpUriRequest request = (HttpUriRequest) get; try (CloseableHttpResponse response = client.execute(request)) { StatusLine statusLine = response.getStatusLine(); if (statusLine.getReasonPhrase().equals("OK")) { logger.info(statusLine.toString()); } else { throw new RuntimeException(statusLine.toString()); } HttpEntity entity = response.getEntity(); if (entity != null) { logger.info(entity.getContentType()); return translate(entity); } } catch (ClientProtocolException ex) { logger.fatal(uri, ex); } return null; }
From source file:com.qwazr.crawler.web.manager.WebCrawlThread.java
/** * Remove any query parameter which match the parameters_matcher list * * @param uriBuilder/*w w w . j a v a 2s .c o m*/ */ private void checkRemoveParameter(URIBuilder uriBuilder) { if (parametersMatcherList == null || parametersMatcherList.isEmpty()) return; List<NameValuePair> oldParams = uriBuilder.getQueryParams(); if (oldParams == null || oldParams.isEmpty()) return; uriBuilder.clearParameters(); for (NameValuePair param : oldParams) if (!checkRegExpMatcher(param.getName() + "=" + param.getValue(), parametersMatcherList)) uriBuilder.addParameter(param.getName(), param.getValue()); }
From source file:eu.esdihumboldt.hale.io.gml.reader.internal.wfs.WfsBackedGmlInstanceCollection.java
/** * Create a GML instance collection based on the given WFS source. * //from w ww. ja v a 2s . c om * @param source the source * @param sourceSchema the source schema * @param restrictToFeatures if only instances that are GML features shall * be loaded * @param ignoreRoot if the root element should be ignored for creating * instances even if it is recognized as an allowed instance type * @param strict if associating elements with properties should be done * strictly according to the schema, otherwise a fall-back is * used trying to populate values also on invalid property paths * @param ignoreNamespaces if parsing of the XML instances should allow * types and properties with namespaces that differ from those * defined in the schema * @param crsProvider CRS provider in case no CRS is specified, may be * <code>null</code> * @param provider the I/O provider to get values * @param featuresPerRequest Number of features to retrieve at most with one * WFS GetFeature request, or {@value #UNLIMITED} to disable * pagination * @throws URISyntaxException thrown if the WFS request URL cannot be * generated from the source location URI */ public WfsBackedGmlInstanceCollection(LocatableInputSupplier<? extends InputStream> source, TypeIndex sourceSchema, boolean restrictToFeatures, boolean ignoreRoot, boolean strict, boolean ignoreNamespaces, CRSProvider crsProvider, IOProvider provider, int featuresPerRequest) throws URISyntaxException { this.sourceSchema = sourceSchema; this.restrictToFeatures = restrictToFeatures; this.ignoreRoot = ignoreRoot; this.strict = strict; this.crsProvider = crsProvider; this.ignoreNamespaces = ignoreNamespaces; this.ioProvider = provider; this.primordialUri = source.getLocation(); // Build base URI from original location by removing STARTINDEX and // MAXFEATURES/COUNT parameters if present URIBuilder builder = new URIBuilder(primordialUri); builder.getQueryParams() .forEach(qp -> primordialQueryParams.put(qp.getName().toUpperCase(), qp.getValue())); wfsVersion = primordialQueryParams.get("VERSION"); if (wfsVersion == null || wfsVersion.isEmpty()) { throw new IllegalArgumentException("WFS URL must contain VERSION parameter"); } List<NameValuePair> params = builder.getQueryParams(); params.removeIf(nvp -> nvp.getName().equalsIgnoreCase("STARTINDEX")); params.removeIf(nvp -> nvp.getName().equalsIgnoreCase(getMaxFeaturesParameterName(wfsVersion))); builder.clearParameters(); builder.addParameters(params); this.baseUri = builder.build(); // If a MAXFEATURES/COUNT parameter is present in the primordial URI, // set maxNumberOfFeatures accordingly if (primordialQueryParams.containsKey(getMaxFeaturesParameterName(wfsVersion))) { // Allow possible NumberFormatException to be thrown up to prevent // unintended retrieval of too many features maxNumberOfFeatures = Integer .parseInt(primordialQueryParams.get(getMaxFeaturesParameterName(wfsVersion))); if (maxNumberOfFeatures < 0) { throw new IllegalArgumentException( MessageFormat.format("Parameter \"{0}\" must be a non-negative integer.", getMaxFeaturesParameterName(wfsVersion))); } } // Use primordial URI and issue "hits" request to check if the WFS will // return anything at all int hits; try { hits = requestHits(primordialUri); } catch (WFSException e) { log.debug(MessageFormat.format("Failed to perform hits query (REQUESTTYPE=hits): {0}", e.getMessage()), e); hits = UNKNOWN_SIZE; } switch (wfsVersion) { case "1.1.0": // The "numberOfFeatures" reported by a 1.1.0 WFS may be smaller // than the actual number of features matches by the query if the // number of features returned per query is limited on the server // side. Therefore do not rely on it as a size information here. this.size = UNKNOWN_SIZE; break; case "2.0.0": case "2.0.2": // The "numberMatched" reported by a 2.0.0/2.0.2 WFS should be // number of features matched by the query. If hits equals // UNKNOWN_SIZE then size is also set to that value this.size = isLimited() ? Math.min(maxNumberOfFeatures, hits) : hits; break; default: this.size = UNKNOWN_SIZE; } if (featuresPerRequest != UNLIMITED && featuresPerRequest <= 0) { throw new IllegalArgumentException(MessageFormat.format( "featuresPerRequest must be a positive integer or {0} to disable pagination", UNLIMITED)); } this.featuresPerRequest = featuresPerRequest; }
From source file:org.apache.ignite.console.agent.handlers.RestHandler.java
/** * @param uri Url./*w ww . ja va 2s . com*/ * @param params Params. * @param demo Use demo node. * @param mtd Method. * @param headers Headers. * @param body Body. */ protected RestResult executeRest(String uri, Map<String, Object> params, boolean demo, String mtd, Map<String, Object> headers, String body) throws IOException, URISyntaxException { if (log.isDebugEnabled()) log.debug("Start execute REST command [method=" + mtd + ", uri=/" + (uri == null ? "" : uri) + ", parameters=" + params + "]"); final URIBuilder builder; if (demo) { // try start demo if needed. AgentClusterDemo.testDrive(cfg); // null if demo node not started yet. if (cfg.demoNodeUri() == null) return RestResult.fail("Demo node is not started yet.", 404); builder = new URIBuilder(cfg.demoNodeUri()); } else builder = new URIBuilder(cfg.nodeUri()); if (builder.getPort() == -1) builder.setPort(DFLT_NODE_PORT); if (uri != null) { if (!uri.startsWith("/") && !cfg.nodeUri().endsWith("/")) uri = '/' + uri; builder.setPath(uri); } if (params != null) { for (Map.Entry<String, Object> entry : params.entrySet()) { if (entry.getValue() != null) builder.addParameter(entry.getKey(), entry.getValue().toString()); } } HttpRequestBase httpReq = null; try { if ("GET".equalsIgnoreCase(mtd)) httpReq = new HttpGet(builder.build()); else if ("POST".equalsIgnoreCase(mtd)) { HttpPost post; if (body == null) { List<NameValuePair> nvps = builder.getQueryParams(); builder.clearParameters(); post = new HttpPost(builder.build()); if (!nvps.isEmpty()) post.setEntity(new UrlEncodedFormEntity(nvps)); } else { post = new HttpPost(builder.build()); post.setEntity(new StringEntity(body)); } httpReq = post; } else throw new IOException("Unknown HTTP-method: " + mtd); if (headers != null) { for (Map.Entry<String, Object> entry : headers.entrySet()) httpReq.addHeader(entry.getKey(), entry.getValue() == null ? null : entry.getValue().toString()); } try (CloseableHttpResponse resp = httpClient.execute(httpReq)) { ByteArrayOutputStream out = new ByteArrayOutputStream(); resp.getEntity().writeTo(out); Charset charset = Charsets.UTF_8; Header encodingHdr = resp.getEntity().getContentEncoding(); if (encodingHdr != null) { String encoding = encodingHdr.getValue(); charset = Charsets.toCharset(encoding); } return RestResult.success(resp.getStatusLine().getStatusCode(), new String(out.toByteArray(), charset)); } catch (ConnectException e) { log.info("Failed connect to node and execute REST command [uri=" + builder.build() + "]"); return RestResult.fail("Failed connect to node and execute REST command.", 404); } } finally { if (httpReq != null) httpReq.reset(); } }