private String encode(String s) throws BuildMethodException { try { return URIUtil.encodePathQuery(s, "UTF-8"); } catch (URIException e) { throw new BuildMethodException("Cannot create URI"); } }
public static String encodeUri(String uri) { try { return URIUtil.encodePathQuery(uri); } catch (URIException ex) { throw new EsHadoopIllegalArgumentException("Cannot escape uri" + uri); } }
public static String decodePath(String path) { try { return URIUtil.decode(path, "UTF-8"); } catch (URIException ex) { throw new EsHadoopIllegalArgumentException("Cannot encode path" + path, ex); } }
private static String queryBattle(String url) { StringBuilder builder = new StringBuilder(); HttpClient client = HttpClientBuilder.create().build(); try { HttpGet httpGet = new HttpGet(URIUtil.encodeQuery(url)); HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content, "UTF-8")); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } else { LOGGER.error("Failed to download file"); return null; } } catch (IOException e) { LOGGER.error(e); } return builder.toString(); }
/** * Call recurly API. * * @param <T> the generic type * @param <E> the element type * @param path the path * @param payload the payload * @param responseClass the response class * @param method the method * @param headers the headers * @return the t * @throws RecurlyException the recurly exception */ protected <T, E> T call( String path, E payload, Class<T> responseClass, HttpMethod method, HttpHeaders headers) throws RecurlyException { URI uri = null; ResponseEntity<T> responseEntity = null; T reponse = null; HttpEntity<?> entity = null; try { if (headers == null) { entity = new HttpEntity<>(payload, recurly.getRecurlyHeaders()); } else { entity = new HttpEntity<>(payload, headers); } uri = new URI(URIUtil.encodeQuery(recurly.getRecurlyServerURL() + path, "UTF-8")); getLogger().debug("Calling Recurly URL {}, method: {}", uri.toString(), method.toString()); responseEntity = restTemplate.exchange(uri, method, entity, responseClass); if (responseEntity != null) reponse = responseEntity.getBody(); } catch (URIException | UnsupportedEncodingException | URISyntaxException e) { throw new RecurlyException("Not able to reach recurly. Please check the URL.", e); } catch (RestClientException e) { String err = ((HttpStatusCodeException) e).getResponseBodyAsString(); int code = ((HttpStatusCodeException) e).getStatusCode().value(); publishError(uri.toString(), err, code); RecurlyException ex = handleError(err, code, e); throw ex; } return reponse; }
private String getFormDirUri(Path formPath) { String fileName = formPath.getFileName(); try { fileName = URIUtil.encodeQuery(fileName); } catch (Exception e) { } return formPath.toURI().substring(0, formPath.toURI().lastIndexOf(fileName) - 1); }
/** * Replace a "$var" within a path string with a value, properly encoding it. * * @param path the URL path to substitute the var within * @param var the name of the var in the string * @param value the value to substitute */ public static String substitutePathVariable( final String path, final String var, final String value) throws CentralDispatcherException { final String encoded; try { encoded = URIUtil.encodeWithinPath(value); } catch (URIException e) { throw new CentralDispatcherException(e); } return path.replace("$" + var, encoded); }
// Accessors private String getProxyURL(HttpServletRequest httpServletRequest) { // Get the proxy host String stringProxyHostNew = Config.getInstance().getProxyHost(); this.setProxyHost(stringProxyHostNew); // Get the proxy port if specified String stringProxyPortNew = "" + Config.getInstance().getProxyPort(); this.setProxyPort(Integer.parseInt(stringProxyPortNew)); // Get the proxy path if specified String stringProxyPathNew = Config.getInstance().getProxyPath(); if (stringProxyPathNew != null && stringProxyPathNew.length() > 0) { this.setProxyPath(stringProxyPathNew); } // Get the maximum file upload size if specified String stringMaxFileUploadSize = "" + Config.getInstance().getMaxFileUploadSize(); if (stringMaxFileUploadSize != null && stringMaxFileUploadSize.length() > 0) { this.setMaxFileUploadSize(Integer.parseInt(stringMaxFileUploadSize)); } // Set the protocol to HTTP String stringProxyURL = "http://" + this.getProxyHostAndPort(); // Check if we are proxying to a path other that the document root if (!this.getProxyPath().equalsIgnoreCase("")) { stringProxyURL += this.getProxyPath(); } try { // Handle the path given to the servlet stringProxyURL += URIUtil.encodePath(httpServletRequest.getPathInfo()); // Handle the query string if (httpServletRequest.getQueryString() != null) { stringProxyURL += "?" + URIUtil.encodeQuery(httpServletRequest.getQueryString()); } LOG.debug("Proxying " + httpServletRequest.getRequestURL() + " to " + stringProxyURL); return stringProxyURL; } catch (Throwable t) { throw new RuntimeException(t); } }
// 对Map内所有value作utf8编码,拼接返回结果 public static String toQueryString(Map<?, ?> data) throws UnsupportedEncodingException, URIException { StringBuffer queryString = new StringBuffer(); for (Entry<?, ?> pair : data.entrySet()) { queryString.append(pair.getKey() + "="); queryString.append(URIUtil.encodeQuery((String) pair.getValue(), "UTF-8") + "&"); } if (queryString.length() > 0) { queryString.deleteCharAt(queryString.length() - 1); } return queryString.toString(); }
/** * Checks request URL against properly resolved URL and returns null if url is proper or * redirection string if not. * * @param request - request that contains current URL * @param response response to write "301 Moved Permanently" status to if redirected * @param resolvedUrlPath - properly resolved URL * @param responseStatusAttributeName - response attribute name to which write the "301 Moved * Permanently" status * @return null if url is properly resolved or redirection string if not * @throws UnsupportedEncodingException */ protected String checkRequestUrl( final HttpServletRequest request, final HttpServletResponse response, final String resolvedUrlPath, final String responseStatusAttributeName) throws UnsupportedEncodingException { try { final String resolvedUrl = response.encodeURL(request.getContextPath() + resolvedUrlPath); final String requestURI = URIUtil.decode(request.getRequestURI(), "utf-8"); final String decoded = URIUtil.decode(resolvedUrl, "utf-8"); if (StringUtils.isNotEmpty(requestURI) && requestURI.endsWith(decoded)) { return null; } else { request.setAttribute(responseStatusAttributeName, HttpStatus.MOVED_PERMANENTLY); final String queryString = request.getQueryString(); if (queryString != null && !queryString.isEmpty()) { return "redirect:" + resolvedUrlPath + "?" + queryString; } return "redirect:" + resolvedUrlPath; } } catch (final URIException e) { throw new UnsupportedEncodingException(); } }
/** * Returns list with all stations with their ID's from the server. * * @return List<Station> * @throws HttpException * @throws IOException */ public List<Station> getAllStations() { if (stations != null && !stations.isEmpty()) { return stations; } String url = "http://booking.uz.gov.ua/purchase/station/"; char letter; GetMethod get = null; String jsonResp = null; List<Station> myStations = new ArrayList<>(); try { for (int i = 0; i < 32; i++) { letter = (char) (1072 + i); // 1072 - rus 'a' in ASCII String currentUrl = URIUtil.encodeQuery(url + letter); get = new GetMethod(currentUrl); int statusCode = client.executeMethod(get); if (statusCode != HttpStatus.SC_OK) { logger.error("getAllStations method failed. Get method failed: " + get.getStatusLine()); return null; } jsonResp = IOUtils.toString( get.getResponseBodyAsStream(), "UTF-8"); // get.getResponseBodyAsString(); StationsListJson resp = new ObjectMapper().readValue(jsonResp, StationsListJson.class); myStations.addAll(resp.getStations()); } } catch (HttpException e) { logger.error("Could not retrieve stations." + e.getMessage(), e); throw new RuntimeException("Could not retrieve stations", e); } catch (IOException e) { logger.error("Could not retrieve stations." + e.getMessage(), e); throw new RuntimeException("Could not retrieve stations", e); } Collections.sort(myStations); return stations = myStations; }