List of usage examples for org.apache.http.client.utils URLEncodedUtils parse
public static List<NameValuePair> parse(final String s, final Charset charset)
From source file:com.hubrick.vertx.s3.util.UrlEncodingUtils.java
public static String addParamsSortedToUrl(String url, Map<String, String> params) { checkNotNull(url, "url must not be null"); checkNotNull(!url.isEmpty(), "url must not be empty"); checkNotNull(params, "params must not be null"); checkNotNull(!params.isEmpty(), "params must not be empty"); final String baseUrl = extractBaseUrl(url); final String urlParams = baseUrl.equals(url) ? "" : url.replace(baseUrl + "?", ""); final List<NameValuePair> nameValuePairs = URLEncodedUtils.parse(urlParams, Charsets.UTF_8); for (Map.Entry<String, String> paramToUrlEncode : params.entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)).collect(Collectors.toList())) { nameValuePairs.add(new BasicNameValuePair(paramToUrlEncode.getKey(), paramToUrlEncode.getValue())); }/*w w w . ja va 2s . c o m*/ return baseUrl + "?" + URLEncodedUtils.format(nameValuePairs, Charsets.UTF_8); }
From source file:jobhunter.api.infojobs.OfferRequest.java
/** * Generates a OfferRequest after parsing the given URL for the * InfoJobs' key.//from w w w.j a va2 s .c om * FTM I know of two URL formats where we can get the key from: * - Using the "of_codigo" query param * - Using the last 30 chars from a path param * This method handles both cases. * @param url * @return OfferRequest * @throws InfoJobsAPIException */ public static OfferRequest of(String url) throws InfoJobsAPIException { List<NameValuePair> params = new ArrayList<>(); try { params = URLEncodedUtils.parse(new URI(url), "UTF-8"); } catch (URISyntaxException e) { l.error("Failed to build URI", e); throw new InfoJobsAPIException("Invalid URL " + url); } NameValuePair key = params.stream().filter(nvp -> nvp.getName().equalsIgnoreCase("of_codigo")).findFirst() .orElse(new BasicNameValuePair("of_codigo", StringUtils.right(url, 30))); return new OfferRequest(key); }
From source file:com.magnet.plugin.r2m.helpers.UrlParser.java
public static ParsedUrl parseUrl(String url) { List<PathPart> pathParts = new ArrayList<PathPart>(); List<Query> queries = new ArrayList<Query>(); ParsedUrl parsedUrl;// w w w . j a va 2 s. co m String base; try { URL aURL = new URL(url); base = aURL.getAuthority(); String protocol = aURL.getProtocol(); parsedUrl = new ParsedUrl(); parsedUrl.setPathWithEndingSlash(aURL.getPath().endsWith("/")); parsedUrl.setBaseUrl(protocol + "://" + base); List<NameValuePair> pairs = URLEncodedUtils.parse(aURL.getQuery(), Charset.defaultCharset()); for (NameValuePair pair : pairs) { Query query = new Query(pair.getName(), pair.getValue()); queries.add(query); } parsedUrl.setQueries(queries); String[] pathStrings = aURL.getPath().split("/"); for (String pathPart : pathStrings) { Matcher m = PATH_PARAM_PATTERN.matcher(pathPart); if (m.find()) { String paramDef = m.group(1); String[] paramParts = paramDef.split(":"); if (paramParts.length > 1) { pathParts.add(new PathPart(paramParts[1].trim(), paramParts[0].trim())); } else { pathParts.add(new PathPart(paramParts[0].trim())); } } else { if (!pathPart.isEmpty()) { pathParts.add(new PathPart(pathPart)); } } } parsedUrl.setPathParts(pathParts); } catch (Exception ex) { Logger.error(UrlParser.class, R2MMessages.getMessage("CANNOT_PARSE_URL", url)); return null; } return parsedUrl; }
From source file:com.github.tddts.jet.oauth.impl.QueryParserImpl.java
@Override public Map<String, String> parseQuery(String query) { return toMap(URLEncodedUtils.parse((query), StandardCharsets.UTF_8)); }
From source file:org.epics.archiverappliance.utils.ui.URIUtils.java
/** * If you do expect a param to have multiple values, use this method to get all the possible values for a name. * @param uri URI /* w ww. j a v a 2 s. c o m*/ * @param paramName   * @return multiple values of a param * @throws IOException   */ public static List<String> getMultiValuedParamFromQueryString(URI uri, String paramName) throws IOException { LinkedList<String> ret = new LinkedList<String>(); if (uri == null) return ret; List<NameValuePair> nvs = URLEncodedUtils.parse(uri, "UTF-8"); if (nvs == null) return ret; for (NameValuePair nv : nvs) { if (nv.getName().equals(paramName)) { ret.add(nv.getValue()); } } return ret; }
From source file:com.hp.application.automation.tools.octane.configuration.ConfigurationParser.java
public static MqmProject parseUiLocation(String uiLocation) throws FormValidation { try {//w w w .j a va2 s . co m URL url = new URL(uiLocation); String location; int contextPos = uiLocation.indexOf("/ui"); if (contextPos < 0) { throw FormValidation.errorWithMarkup(markup("red", Messages.ApplicationContextNotFound())); } else { location = uiLocation.substring(0, contextPos); } List<NameValuePair> params = URLEncodedUtils.parse(url.toURI(), "UTF-8"); for (NameValuePair param : params) { if (param.getName().equals(PARAM_SHARED_SPACE)) { String[] sharedSpaceAndWorkspace = param.getValue().split("/"); // we are relaxed and allow parameter without workspace in order not to force user to makeup // workspace value when configuring manually or via config API and not via copy & paste if (sharedSpaceAndWorkspace.length < 1 || StringUtils.isEmpty(sharedSpaceAndWorkspace[0])) { throw FormValidation.errorWithMarkup(markup("red", Messages.UnexpectedSharedSpace())); } return new MqmProject(location, sharedSpaceAndWorkspace[0]); } } throw FormValidation.errorWithMarkup(markup("red", Messages.MissingSharedSpace())); } catch (MalformedURLException e) { throw FormValidation.errorWithMarkup(markup("red", Messages.ConfigurationUrInvalid())); } catch (URISyntaxException e) { throw FormValidation.errorWithMarkup(markup("red", Messages.ConfigurationUrInvalid())); } }
From source file:com.bazaarvoice.seo.sdk.util.BVUtility.java
public static String getPageNumber(String queryString) { if (queryString != null && queryString.length() > 0) { List<NameValuePair> parameters = URLEncodedUtils.parse(queryString, Charset.forName("UTF-8")); for (NameValuePair parameter : parameters) { if (parameter.getName().equals("bvrrp") || parameter.getName().equals("bvqap")) { final Pattern p = Pattern.compile("^[^/]+/\\w+/\\w+/(\\d+)/[^/]+\\.htm$"); return matchPageNumber(p, parameter.getValue()); } else if (parameter.getName().equals("bvsyp")) { final Pattern p = Pattern.compile("^[^/]+/\\w+/\\w+/(\\d+)[[/\\w+]|[^/]]+\\.htm$"); return matchPageNumber(p, parameter.getValue()); } else if (parameter.getName().equals("bvpage")) { final Pattern p = Pattern.compile("^\\w+/(\\d+)$"); return matchPageNumber(p, parameter.getValue()); }// ww w. j a v a2 s . c o m } } return "1"; }
From source file:com.cooksys.httpserver.ParsedHttpRequest.java
private static List<String> parseUrlParameters(URI uri) { List<String> parameterList = new ArrayList<>(); List<NameValuePair> params = URLEncodedUtils.parse(uri, "UTF-8"); for (NameValuePair param : params) { parameterList.add(param.getName() + " " + param.getValue()); }//from w ww. ja va2s. c o m return parameterList; }
From source file:net.majorkernelpanic.streaming.misc.UriParser.java
/** * Configures a Session according to the given URI. * Here are some examples of URIs that can be used to configure a Session: * <ul><li>rtsp://xxx.xxx.xxx.xxx:8086?h264&flash=on</li> * <li>rtsp://xxx.xxx.xxx.xxx:8086?h263&camera=front&flash=on</li> * <li>rtsp://xxx.xxx.xxx.xxx:8086?h264=200-20-320-240</li> * <li>rtsp://xxx.xxx.xxx.xxx:8086?aac</li></ul> * @param uri The URI// ww w . j a va 2s .c o m * @param session The Session that will be configured * @throws IllegalStateException * @throws IOException */ public static void parse(String uri, Session session) throws IllegalStateException, IOException { boolean flash = false; int camera = CameraInfo.CAMERA_FACING_BACK; List<NameValuePair> params = URLEncodedUtils.parse(URI.create(uri), "UTF-8"); if (params.size() > 0) { // Those parameters must be parsed first or else they won't necessarily be taken into account for (Iterator<NameValuePair> it = params.iterator(); it.hasNext();) { NameValuePair param = it.next(); // FLASH ON/OFF if (param.getName().equals("flash")) { if (param.getValue().equals("on")) flash = true; else flash = false; } // CAMERA -> the client can choose between the front facing camera and the back facing camera else if (param.getName().equals("camera")) { if (param.getValue().equals("back")) camera = CameraInfo.CAMERA_FACING_BACK; else if (param.getValue().equals("front")) camera = CameraInfo.CAMERA_FACING_FRONT; } // MULTICAST -> the stream will be sent to a multicast group // The default mutlicast address is 228.5.6.7, but the client can specify one else if (param.getName().equals("multicast")) { session.setRoutingScheme(Session.MULTICAST); if (param.getValue() != null) { try { InetAddress addr = InetAddress.getByName(param.getValue()); if (!addr.isMulticastAddress()) { throw new IllegalStateException("Invalid multicast address !"); } session.setDestination(addr); } catch (UnknownHostException e) { throw new IllegalStateException("Invalid multicast address !"); } } else { // Default multicast address session.setDestination(InetAddress.getByName("228.5.6.7")); } } // UNICAST -> the client can use this so specify where he wants the stream to be sent else if (param.getName().equals("unicast")) { if (param.getValue() != null) { try { InetAddress addr = InetAddress.getByName(param.getValue()); session.setDestination(addr); } catch (UnknownHostException e) { throw new IllegalStateException("Invalid destination address !"); } } } // TTL -> the client can modify the time to live of packets // By default ttl=64 else if (param.getName().equals("ttl")) { if (param.getValue() != null) { try { int ttl = Integer.parseInt(param.getValue()); if (ttl < 0) throw new IllegalStateException("The TTL must be a positive integer !"); session.setTimeToLive(ttl); } catch (Exception e) { throw new IllegalStateException("The TTL must be a positive integer !"); } } } // No tracks will be added to the session else if (param.getName().equals("stop")) { return; } } for (Iterator<NameValuePair> it = params.iterator(); it.hasNext();) { NameValuePair param = it.next(); // H264 if (param.getName().equals("h264")) { VideoQuality quality = VideoQuality.parseQuality(param.getValue()); session.addVideoTrack(Session.VIDEO_H264, camera, quality, flash); } // H263 else if (param.getName().equals("h263")) { VideoQuality quality = VideoQuality.parseQuality(param.getValue()); session.addVideoTrack(Session.VIDEO_H263, camera, quality, flash); } // AMRNB else if (param.getName().equals("amrnb") || param.getName().equals("amr")) { session.addAudioTrack(Session.AUDIO_AMRNB); } // AAC else if (param.getName().equals("aac")) { session.addAudioTrack(Session.AUDIO_AAC); } // Generic Audio Stream -> make use of api level 12 // TODO: Doesn't work :/ else if (param.getName().equals("testnewapi")) { session.addAudioTrack(Session.AUDIO_ANDROID_AMR); } } // The default behavior is to only add one video track if (session.getTrackCount() == 0) { session.addVideoTrack(); session.addAudioTrack(); } } // Uri has no parameters: the default behavior is to only add one video track else { session.addVideoTrack(); session.addAudioTrack(); } }