/** * Normalize the URL so that it ends with '/'. * <p> * An attention is paid to preserve the query parameters in URL if any. */ protected static URL normalizeToEndWithSlash(URL url) { if(url.getPath().endsWith("/")) return url; // normalize String q = url.getQuery(); q = q!=null?('?'+q):""; try { return new URL(url,url.getPath()+'/'+q); } catch (MalformedURLException e) { // impossible throw new Error(e); } }
public String translate(URL uri) { StringBuilder sb = new StringBuilder(128); sb.append(uri.getProtocol()).append("://") .append(ip) .append(':') .append(port) .append(uri.getPath()); String query = uri.getQuery(); if (query != null) sb.append('?').append(query); return sb.toString(); }
/** * Gets object from Qiniu kodo. All requests are authenticated by default,default expires 3600s We * use okhttp as our HTTP client and support two main parameters in the external adjustment, MAX * request and timeout time. * @param key object key * @param startPos start index for object * @param endPos end index for object * @return inputstream * @param contentLength object file size * @throws IOException */ public InputStream getObject(String key, long startPos, long endPos, long contentLength) throws IOException { String baseUrl = String.format("http://%s/%s", mDownloadHost, key); String privateUrl = mAuth.privateDownloadUrl(baseUrl); URL url = new URL(privateUrl); String objectUrl = String.format("http://%s/%s?%s", mEndPoint, key, url.getQuery()); Request request = new Request.Builder().url(objectUrl) .addHeader("Range", "bytes=" + String.valueOf(startPos) + "-" + String.valueOf(endPos < contentLength ? endPos - 1 : contentLength - 1)) .addHeader("Host", mDownloadHost).get().build(); Response response = mOkHttpClient.newCall(request).execute(); if (response.code() != 200 && response.code() != 206) { throw new IOException(String.format("Qiniu kodo:get object failed errcode:%d,errmsg:%s", response.code(), response.message())); } return response.body().byteStream(); }
/** * Parse a URL query and fragment parameters into a key-value bundle. * * @param url the URL to parse * @return a dictionary bundle of keys and values */ public static Map<String, String> parseUrl(String url) { // hack to prevent MalformedURLException url = url.replace("fbconnect", "http"); try { URL u = new URL(url); Map<String, String> params = decodeUrl(u.getQuery()); params.putAll(decodeUrl(u.getRef())); return params; } catch (MalformedURLException e) { return new HashMap<>(); } }
public static void initQueryString(String urlQuery) throws MalformedURLException { URL url = new URL(urlQuery); if ("".equals(urlQuery) || "".equals(url.getHost())) { throw new MalformedURLException("empty URL"); } ConnectionUtil.setUrlByUser(urlQuery); // Parse url and GET query string ParameterUtil.setQueryString(new ArrayList<SimpleEntry<String, String>>()); Matcher regexSearch = Pattern.compile("(.*\\?)(.*)").matcher(urlQuery); if (regexSearch.find()) { ConnectionUtil.setUrlBase(regexSearch.group(1)); if (!"".equals(url.getQuery())) { ParameterUtil.setQueryString( Pattern.compile("&").splitAsStream(regexSearch.group(2)) .map(s -> Arrays.copyOf(s.split("="), 2)) .map(o -> new SimpleEntry<String, String>(o[0], o[1] == null ? "" : o[1])) .collect(Collectors.toList()) ); } } else { ConnectionUtil.setUrlBase(urlQuery); } }
assertTrue(graphic instanceof ExternalGraphic); assertEquals( ((ExternalGraphic) graphic).getLocation().getPath(), iconFile.toURI().toURL().getPath()); assertEquals("param1=1", ((ExternalGraphic) graphic).getLocation().getQuery()); assertEquals("textAfterHash", ((ExternalGraphic) graphic).getLocation().getRef());
if (input instanceof URL && (((URL) input).getProtocol().equalsIgnoreCase("http"))) { try { java.net.URLDecoder.decode(((URL) input).getQuery().intern(), "UTF-8");
private static URL buildGetUrl(String strUrl, String query) throws IOException { URL url = new URL(strUrl); if (StringUtils.isEmpty(query)) { return url; } if (StringUtils.isEmpty(url.getQuery())) { if (strUrl.endsWith("?")) { strUrl = strUrl + query; } else { strUrl = strUrl + "?" + query; } } else { if (strUrl.endsWith("&")) { strUrl = strUrl + query; } else { strUrl = strUrl + "&" + query; } } return new URL(strUrl); }
public static final String toRelativeURL(String urlStr, boolean withParam) { String uri = urlStr; if (isAbsoluteURL(urlStr) == false) { if (0 < urlStr.length() && urlStr.charAt(0) != '/') uri = "/" + urlStr; } else { try{ URL url = new URL(urlStr); uri = url.getPath(); if (withParam == true) { String queryStr = url.getQuery(); if (!queryStr.equals("")){ uri += "?" + queryStr; } } if (uri.endsWith("/")) uri = uri.substring(0,uri.length()-1); }catch(Exception e){} } return uri; }
/** * Parse a URL query and fragment parameters into a key-value bundle. * * @param url the URL to parse * @return a dictionary bundle of keys and values */ public static Map<String, String> parseUrl(String url) { // hack to prevent MalformedURLException url = url.replace("fbconnect", "http"); try { URL u = new URL(url); Map<String, String> params = decodeUrl(u.getQuery()); params.putAll(decodeUrl(u.getRef())); return params; } catch (MalformedURLException e) { return new HashMap<>(); } }
if (!"file".equals(url.getProtocol())) { return null; // not a File URL if (url.getQuery() != null) { string = string.substring(0, string.indexOf("?")); } else { String auth = url.getAuthority(); String path2 = url.getPath(); try { path2 = URLDecoder.decode(path2, java.nio.charset.StandardCharsets.UTF_8.name());
final String protocol = url.getProtocol(); if (protocol.equalsIgnoreCase("file")) pathname = URLs.urlToFile(url).getPath(); else { final String query; try { query = java.net.URLDecoder.decode(url.getQuery().intern(), "UTF-8"); } catch (UnsupportedEncodingException e) { if (LOGGER.isLoggable(Level.FINE))
private static URL buildGetUrl(String strUrl, String query) throws IOException { URL url = new URL(strUrl); if (StringUtils.isEmpty(query)) { return url; } if (StringUtils.isEmpty(url.getQuery())) { if (strUrl.endsWith("?")) { strUrl = strUrl + query; } else { strUrl = strUrl + "?" + query; } } else { if (strUrl.endsWith("&")) { strUrl = strUrl + query; } else { strUrl = strUrl + "&" + query; } } return new URL(strUrl); }
String href = element.text(); if (href.contains("//www.youtube.com/watch")) { URL aUrl = new URL(href); String query = aUrl.getQuery(); Map<String, Object> querys = StringUtil.formatParams(query); element.text(""); } else if (href.contains("//v.youku.com/v_show/")) { element.text(""); URL aUrl = new URL(href); String _href = "https://player.youku.com/embed/" + aUrl.getPath().replace("/v_show/id_", "").replace(".html", ""); element.addClass("embedded_video_wrapper"); element.append("<iframe class='embedded_video' src='" + _href + "' frameborder='0' allowfullscreen></iframe>");
protected RenderableSVG toRenderableSVG(String svgfile, URL svgUrl) throws SAXException, IOException { RenderableSVG svg; String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); String svgUri = svgfile; // Remove parameters from file URLs, as it is not supported by Windows if ("file".equals(svgUrl.getProtocol()) && svgUrl.getQuery() != null) { int idx = svgfile.indexOf('?'); if (idx > -1) { svgUri = svgfile.substring(0, idx); } } Document doc = f.createDocument(svgUri); Map<String, String> parameters = getParametersFromUrl(svgfile); if (!parameters.isEmpty() || hasParameters(doc.getDocumentElement())) { replaceParameters(doc.getDocumentElement(), parameters); } svg = new RenderableSVG(doc); return svg; }