List of usage examples for java.net URL getQuery
public String getQuery()
From source file:org.codice.ddf.catalog.content.monitor.DavEntry.java
static String getLocation(String initialLocation, DavEntry parent) { String location = initialLocation; if (parent != null && !location.startsWith(HTTP)) { String parentLocation = parent.getLocation(); if (parentLocation.endsWith(FORSLASH) && location.startsWith(FORSLASH)) { location = location.replaceFirst(FORSLASH, ""); }/*from w w w . j a v a 2 s . c om*/ if (!parentLocation.endsWith(FORSLASH) && !location.startsWith(FORSLASH)) { location = FORSLASH + location; } location = parentLocation + location; } try { // URL class performs structural decomposition of location for us // URI class performs character encoding, but ONLY via multipart constructors // Finally, we have a fully qualified and escaped location for future manipulation URL url = new URL(location); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); location = uri.toASCIIString(); } catch (MalformedURLException | URISyntaxException e) { throw new RuntimeException(e); } return location; }
From source file:com.dmsl.anyplace.utils.NetworkUtils.java
public static String encodeURL(String urlStr) throws URISyntaxException, MalformedURLException { URL url = new URL(urlStr); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL();/*from w w w . j a v a2s .c o m*/ return url.toString(); }
From source file:in.flipbrain.Utils.java
public static String getVideoId(String url) throws MalformedURLException { URL u = new URL(url); String host = u.getHost();//from ww w . j av a 2 s . c o m if (host.equals("youtu.be")) { return u.getPath(); } else if (host.equals("www.youtube.com")) { String path = u.getPath(); if (path.contains("embed")) { return path.substring(path.indexOf("/")); } else { String query = u.getQuery(); String[] parts = query.split("&"); for (String p : parts) { if (p.startsWith("v=")) { return p.substring(2); } } } } return null; }
From source file:com.mashape.unirest.android.http.HttpClientHelper.java
private static HttpRequestBase prepareRequest(HttpRequest request, boolean async) { Object defaultHeaders = Options.getOption(Option.DEFAULT_HEADERS); if (defaultHeaders != null) { @SuppressWarnings("unchecked") Set<Entry<String, String>> entrySet = ((Map<String, String>) defaultHeaders).entrySet(); for (Entry<String, String> entry : entrySet) { request.header(entry.getKey(), entry.getValue()); }//from w w w . ja v a 2 s. c om } if (!request.getHeaders().containsKey(USER_AGENT_HEADER)) { request.header(USER_AGENT_HEADER, USER_AGENT); } if (!request.getHeaders().containsKey(ACCEPT_ENCODING_HEADER)) { request.header(ACCEPT_ENCODING_HEADER, "gzip"); } HttpRequestBase reqObj = null; String urlToRequest = null; try { URL url = new URL(request.getUrl()); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), URLDecoder.decode(url.getPath(), "UTF-8"), "", url.getRef()); urlToRequest = uri.toURL().toString(); if (url.getQuery() != null && !url.getQuery().trim().equals("")) { if (!urlToRequest.substring(urlToRequest.length() - 1).equals("?")) { urlToRequest += "?"; } urlToRequest += url.getQuery(); } else if (urlToRequest.substring(urlToRequest.length() - 1).equals("?")) { urlToRequest = urlToRequest.substring(0, urlToRequest.length() - 1); } } catch (Exception e) { throw new RuntimeException(e); } switch (request.getHttpMethod()) { case GET: reqObj = new HttpGet(urlToRequest); break; case POST: reqObj = new HttpPost(urlToRequest); break; case PUT: reqObj = new HttpPut(urlToRequest); break; case DELETE: //reqObj = new HttpDeleteWithBody(urlToRequest); break; case PATCH: //reqObj = new HttpPatchWithBody(urlToRequest); break; case OPTIONS: reqObj = new HttpOptions(urlToRequest); break; case HEAD: reqObj = new HttpHead(urlToRequest); break; } Set<Entry<String, List<String>>> entrySet = request.getHeaders().entrySet(); for (Entry<String, List<String>> entry : entrySet) { List<String> values = entry.getValue(); if (values != null) { for (String value : values) { reqObj.addHeader(entry.getKey(), value); } } } // Set body if (!(request.getHttpMethod() == HttpMethod.GET || request.getHttpMethod() == HttpMethod.HEAD)) { if (request.getBody() != null) { HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType()); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) { throw new RuntimeException(e); } } else { ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity); } } } return reqObj; }
From source file:com.seajas.search.utilities.tags.URLTag.java
/** * Protect a given URL by starring out the password-portion. * //from w w w .j a v a 2 s . co m * @param url * @return String */ public static String protectUrl(final String url) { try { URL actualUrl = new URL(url); return actualUrl.getProtocol() + "://" + (StringUtils.isEmpty(actualUrl.getUserInfo()) ? "" : "****@") + actualUrl.getHost() + (actualUrl.getPort() == -1 ? "" : ':' + actualUrl.getPort()) + actualUrl.getPath() + (StringUtils.isEmpty(actualUrl.getQuery()) ? "" : '?' + actualUrl.getQuery()); } catch (MalformedURLException e) { return url; } }
From source file:org.springframework.cloud.config.server.support.AwsCodeCommitCredentialProvider.java
/** * This provider can handle uris like//from ww w .jav a2s .com * https://git-codecommit.$AWS_REGION.amazonaws.com/v1/repos/$REPO . * @param uri uri to parse * @return {@code true} if the URI can be handled */ public static boolean canHandle(String uri) { if (!hasText(uri)) { return false; } try { URL url = new URL(uri); URI u = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); if (u.getScheme().equals("https")) { String host = u.getHost(); if (host.endsWith(".amazonaws.com") && host.startsWith("git-codecommit.")) { return true; } } } catch (Throwable t) { // ignore all, we can't handle it } return false; }
From source file:org.apache.jmeter.protocol.http.util.ConversionUtils.java
/** * Checks a URL and encodes it if necessary, * i.e. if it is not currently correctly encoded. * Warning: it may not work on all unencoded URLs. * @param url non-encoded URL// w ww.j a v a2 s . c o m * @return URI which has been encoded as necessary * @throws URISyntaxException if parts of the url form a non valid URI */ public static URI sanitizeUrl(URL url) throws URISyntaxException { try { return url.toURI(); // Assume the URL is already encoded } catch (URISyntaxException e) { // it's not, so encode it return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); // anchor or fragment } }
From source file:Models.Geographic.Repository.RepositoryGoogle.java
/** * //from w ww .j a v a 2 s. com * @param latitude * @param longitude * @return */ public static HashMap reverse(double latitude, double longitude) { HashMap a = new HashMap(); try { //URL url=new URL(Configuration.getParameter("geocoding_google_url_send_json") + "latlng=" + String.valueOf(latitude) + "," + String.valueOf(longitude)); URL url = new URL(Configuration.getParameter("geocoding_google_url_send_json") + "latlng=" + String.valueOf(latitude) + "," + String.valueOf(longitude)); URL file_url = new URL( url.getProtocol() + "://" + url.getHost() + signRequest(url.getPath(), url.getQuery())); //BufferedReader lector=new BufferedReader(new InputStreamReader(url.openStream())); BufferedReader lector = new BufferedReader(new InputStreamReader(file_url.openStream())); String textJson = "", tempJs; while ((tempJs = lector.readLine()) != null) textJson += tempJs; if (textJson == null) throw new Exception("Don't found item"); JSONObject google = ((JSONObject) JSONValue.parse(textJson)); a.put("status", google.get("status").toString()); if (a.get("status").toString().equals("OK")) { JSONArray results = (JSONArray) google.get("results"); JSONArray address_components = (JSONArray) ((JSONObject) results.get(2)).get("address_components"); for (int i = 0; i < address_components.size(); i++) { JSONObject items = (JSONObject) address_components.get(i); //if(((JSONObject)types.get(0)).get("").toString().equals("country")) if (items.get("types").toString().contains("country")) { a.put("country", items.get("long_name").toString()); a.put("iso", items.get("short_name").toString()); break; } } } } catch (Exception ex) { a = null; System.out.println("Error Google Geocoding: " + ex); } return a; }
From source file:sce.ElasticJob.java
public static URL convertToURLEscapingIllegalCharacters(String string) { try {/*from w ww. j av a 2s. c o m*/ String decodedURL = URLDecoder.decode(string, "UTF-8"); URL url = new URL(decodedURL); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return uri.toURL(); } catch (MalformedURLException | URISyntaxException | UnsupportedEncodingException e) { e.printStackTrace(System.out); return null; } }
From source file:com.db.comserv.main.utilities.HttpCaller.java
private static void logExtAccess(final String type, final URL url, final String method, final int responseStatus, final int responseBytes, final long takenMs) { LogUtil.logExtAccess(url.getHost(), type, method, responseStatus, responseBytes, takenMs, ServletUtil.filterUrl(url.getPath() + (url.getQuery() == null ? "" : "?" + url.getQuery()))); }