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:de.geeksfactory.opacclient.apis.TouchPoint.java
protected DetailledItem parse_result(String html) throws IOException { Document doc = Jsoup.parse(html); doc.setBaseUri(opac_url);/*from ww w.j a va 2s . c o m*/ DetailledItem result = new DetailledItem(); if (doc.select("#cover script").size() > 0) { String js = doc.select("#cover script").first().html(); String isbn = matchJSVariable(js, "isbn"); String ajaxUrl = matchJSVariable(js, "ajaxUrl"); if (ajaxUrl == null) { ajaxUrl = matchJSParameter(js, "url"); } if (ajaxUrl != null && !"".equals(ajaxUrl)) { if (!"".equals(isbn) && isbn != null) { String url = new URL(new URL(opac_url + "/"), ajaxUrl).toString(); String coverUrl = httpGet(url + "?isbn=" + isbn + "&size=medium", ENCODING); if (!"".equals(coverUrl)) { result.setCover(coverUrl.replace("\r\n", "").trim()); } } else { String url = new URL(new URL(opac_url + "/"), ajaxUrl).toString(); String coverJs = httpGet(url, ENCODING); result.setCover(matchHTMLAttr(coverJs, "src")); } } } result.setTitle(doc.select("h1").first().text()); for (Element tr : doc.select(".titleinfo tr")) { // Sometimes there is one th and one td, sometimes two tds String detailName = tr.select("th, td").first().text().trim(); String detailValue = tr.select("td").last().text().trim(); result.addDetail(new Detail(detailName, detailValue)); if (detailName.contains("ID in diesem Katalog")) { result.setId(detailValue); } } if (result.getDetails().size() == 0 && doc.select("#details").size() > 0) { // e.g. Bayreuth_Uni String dname = ""; String dval = ""; boolean in_value = true; for (Node n : doc.select("#details").first().childNodes()) { if (n instanceof Element && ((Element) n).tagName().equals("strong")) { if (in_value) { if (dname.length() > 0 && dval.length() > 0) { result.addDetail(new Detail(dname, dval)); } dname = ((Element) n).text(); in_value = false; } else { dname += ((Element) n).text(); } } else { String t = null; if (n instanceof TextNode) { t = ((TextNode) n).text(); } else if (n instanceof Element) { t = ((Element) n).text(); } if (t != null) { if (in_value) { dval += t; } else { in_value = true; dval = t; } } } } } // Copies String copiesParameter = doc.select("div[id^=ajax_holdings_url").attr("ajaxParameter").replace("&", ""); if (!"".equals(copiesParameter)) { String copiesHtml = httpGet(opac_url + "/" + copiesParameter, ENCODING); Document copiesDoc = Jsoup.parse(copiesHtml); List<String> table_keys = new ArrayList<>(); for (Element th : copiesDoc.select(".data tr th")) { if (th.text().contains("Zweigstelle")) { table_keys.add("branch"); } else if (th.text().contains("Status")) { table_keys.add("status"); } else if (th.text().contains("Signatur")) { table_keys.add("signature"); } else { table_keys.add(null); } } for (Element tr : copiesDoc.select(".data tr:has(td)")) { Copy copy = new Copy(); int i = 0; for (Element td : tr.select("td")) { if (table_keys.get(i) != null) { copy.set(table_keys.get(i), td.text().trim()); } i++; } result.addCopy(copy); } } // Reservation Info, only works if the code above could find a URL if (!"".equals(copiesParameter)) { String reservationParameter = copiesParameter.replace("showHoldings", "showDocument"); try { String reservationHtml = httpGet(opac_url + "/" + reservationParameter, ENCODING); Document reservationDoc = Jsoup.parse(reservationHtml); reservationDoc.setBaseUri(opac_url); if (reservationDoc.select("a").size() == 1) { result.setReservable(true); result.setReservation_info(reservationDoc.select("a").first().attr("abs:href")); } } catch (Exception e) { e.printStackTrace(); // fail silently } } // TODO: Volumes try { Element isvolume = null; Map<String, String> volume = new HashMap<>(); Elements links = doc.select(".data td a"); int elcount = links.size(); for (int eli = 0; eli < elcount; eli++) { List<NameValuePair> anyurl = URLEncodedUtils.parse(new URI(links.get(eli).attr("href")), "UTF-8"); for (NameValuePair nv : anyurl) { if (nv.getName().equals("methodToCall") && nv.getValue().equals("volumeSearch")) { isvolume = links.get(eli); } else if (nv.getName().equals("catKey")) { volume.put("catKey", nv.getValue()); } else if (nv.getName().equals("dbIdentifier")) { volume.put("dbIdentifier", nv.getValue()); } } if (isvolume != null) { volume.put("volume", "true"); result.setVolumesearch(volume); break; } } } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:com.google.maps.PlacesApiTest.java
private List<NameValuePair> parseQueryParamsFromRequestLine(String requestLine) throws Exception { // Extract the URL part from the HTTP request line String[] chunks = requestLine.split("\\s"); String url = chunks[1];//ww w . java2 s . c om return URLEncodedUtils.parse(new URI(url), "UTF-8"); }
From source file:com.screenslicer.core.util.Util.java
private static boolean isUrlFiltered(String currentUrl, String url, String[] whitelist, String[] patterns, UrlTransform[] transforms) {/*from w w w . j ava2 s.c o m*/ if (!isUrlFilteredHelper(currentUrl, url, whitelist, patterns, transforms)) { return false; } try { List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8"); for (NameValuePair pair : params) { String param = null; try { param = new URI(pair.getValue()).toString(); } catch (Throwable t) { continue; } if (param != null && (param.startsWith("http:") || param.startsWith("https:")) && !isUrlFilteredHelper(currentUrl, param.toString(), whitelist, patterns, transforms)) { return false; } } } catch (Throwable t) { } return true; }
From source file:de.geeksfactory.opacclient.apis.SISIS.java
protected DetailledItem parse_result(String html) throws IOException { Document doc = Jsoup.parse(html); doc.setBaseUri(opac_url);// w w w . j a v a 2 s . c om String html2 = httpGet(opac_url + "/singleHit.do?methodToCall=activateTab&tab=showTitleActive", ENCODING); Document doc2 = Jsoup.parse(html2); doc2.setBaseUri(opac_url); String html3 = httpGet(opac_url + "/singleHit.do?methodToCall=activateTab&tab=showAvailabilityActive", ENCODING); Document doc3 = Jsoup.parse(html3); doc3.setBaseUri(opac_url); DetailledItem result = new DetailledItem(); try { result.setId(doc.select("#bibtip_id").text().trim()); } catch (Exception ex) { ex.printStackTrace(); } List<String> reservationlinks = new ArrayList<>(); for (Element link : doc3.select("#vormerkung a, #tab-content a")) { String href = link.absUrl("href"); Map<String, String> hrefq = getQueryParamsFirst(href); if (result.getId() == null) { // ID retrieval String key = hrefq.get("katkey"); if (key != null) { result.setId(key); break; } } // Vormerken if (hrefq.get("methodToCall") != null) { if (hrefq.get("methodToCall").equals("doVormerkung") || hrefq.get("methodToCall").equals("doBestellung")) { reservationlinks.add(href.split("\\?")[1]); } } } if (reservationlinks.size() == 1) { result.setReservable(true); result.setReservation_info(reservationlinks.get(0)); } else if (reservationlinks.size() == 0) { result.setReservable(false); } else { // TODO: Multiple options - handle this case! } if (doc.select(".data td img").size() == 1) { result.setCover(doc.select(".data td img").first().attr("abs:src")); try { downloadCover(result); } catch (Exception e) { } } if (doc.select(".aw_teaser_title").size() == 1) { result.setTitle(doc.select(".aw_teaser_title").first().text().trim()); } else if (doc.select(".data td strong").size() > 0) { result.setTitle(doc.select(".data td strong").first().text().trim()); } else { result.setTitle(""); } if (doc.select(".aw_teaser_title_zusatz").size() > 0) { result.addDetail(new Detail("Titelzusatz", doc.select(".aw_teaser_title_zusatz").text().trim())); } String title = ""; String text = ""; boolean takeover = false; Element detailtrs = doc2.select(".box-container .data td").first(); for (Node node : detailtrs.childNodes()) { if (node instanceof Element) { if (((Element) node).tagName().equals("strong")) { title = ((Element) node).text().trim(); text = ""; } else { if (((Element) node).tagName().equals("a") && (((Element) node).text().trim().contains("hier klicken") || title.equals("Link:"))) { text = text + node.attr("href"); takeover = true; break; } } } else if (node instanceof TextNode) { text = text + ((TextNode) node).text(); } } if (!takeover) { text = ""; title = ""; } detailtrs = doc2.select("#tab-content .data td").first(); if (detailtrs != null) { for (Node node : detailtrs.childNodes()) { if (node instanceof Element) { if (((Element) node).tagName().equals("strong")) { if (!text.equals("") && !title.equals("")) { result.addDetail(new Detail(title.trim(), text.trim())); if (title.equals("Titel:")) { result.setTitle(text.trim()); } text = ""; } title = ((Element) node).text().trim(); } else { if (((Element) node).tagName().equals("a") && (((Element) node).text().trim().contains("hier klicken") || title.equals("Link:"))) { text = text + node.attr("href"); } else { text = text + ((Element) node).text(); } } } else if (node instanceof TextNode) { text = text + ((TextNode) node).text(); } } } else { if (doc2.select("#tab-content .fulltitle tr").size() > 0) { Elements rows = doc2.select("#tab-content .fulltitle tr"); for (Element tr : rows) { if (tr.children().size() == 2) { Element valcell = tr.child(1); String value = valcell.text().trim(); if (valcell.select("a").size() == 1) { value = valcell.select("a").first().absUrl("href"); } result.addDetail(new Detail(tr.child(0).text().trim(), value)); } } } else { result.addDetail(new Detail(stringProvider.getString(StringProvider.ERROR), stringProvider.getString(StringProvider.COULD_NOT_LOAD_DETAIL))); } } if (!text.equals("") && !title.equals("")) { result.addDetail(new Detail(title.trim(), text.trim())); if (title.equals("Titel:")) { result.setTitle(text.trim()); } } for (Element link : doc3.select("#tab-content a")) { Map<String, String> hrefq = getQueryParamsFirst(link.absUrl("href")); if (result.getId() == null) { // ID retrieval String key = hrefq.get("katkey"); if (key != null) { result.setId(key); break; } } } for (Element link : doc3.select(".box-container a")) { if (link.text().trim().equals("Download")) { result.addDetail( new Detail(stringProvider.getString(StringProvider.DOWNLOAD), link.absUrl("href"))); } } Map<String, Integer> copy_columnmap = new HashMap<>(); // Default values copy_columnmap.put("barcode", 1); copy_columnmap.put("branch", 3); copy_columnmap.put("status", 4); Elements copy_columns = doc.select("#tab-content .data tr#bg2 th"); for (int i = 0; i < copy_columns.size(); i++) { Element th = copy_columns.get(i); String head = th.text().trim(); if (head.contains("Status")) { copy_columnmap.put("status", i); } if (head.contains("Zweigstelle")) { copy_columnmap.put("branch", i); } if (head.contains("Mediennummer")) { copy_columnmap.put("barcode", i); } if (head.contains("Standort")) { copy_columnmap.put("location", i); } if (head.contains("Signatur")) { copy_columnmap.put("signature", i); } } Pattern status_lent = Pattern.compile( "^(entliehen) bis ([0-9]{1,2}.[0-9]{1,2}.[0-9]{2," + "4}) \\(gesamte Vormerkungen: ([0-9]+)\\)$"); Pattern status_and_barcode = Pattern.compile("^(.*) ([0-9A-Za-z]+)$"); Elements exemplartrs = doc.select("#tab-content .data tr").not("#bg2"); DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN); for (Element tr : exemplartrs) { try { Copy copy = new Copy(); Element status = tr.child(copy_columnmap.get("status")); Element barcode = tr.child(copy_columnmap.get("barcode")); String barcodetext = barcode.text().trim().replace(" Wegweiser", ""); // STATUS String statustext; if (status.getElementsByTag("b").size() > 0) { statustext = status.getElementsByTag("b").text().trim(); } else { statustext = status.text().trim(); } if (copy_columnmap.get("status").equals(copy_columnmap.get("barcode"))) { Matcher matcher1 = status_and_barcode.matcher(statustext); if (matcher1.matches()) { statustext = matcher1.group(1); barcodetext = matcher1.group(2); } } Matcher matcher = status_lent.matcher(statustext); if (matcher.matches()) { copy.setStatus(matcher.group(1)); copy.setReservations(matcher.group(3)); copy.setReturnDate(fmt.parseLocalDate(matcher.group(2))); } else { copy.setStatus(statustext); } copy.setBarcode(barcodetext); if (status.select("a[href*=doVormerkung]").size() == 1) { copy.setResInfo(status.select("a[href*=doVormerkung]").attr("href").split("\\?")[1]); } String branchtext = tr.child(copy_columnmap.get("branch")).text().trim().replace(" Wegweiser", ""); copy.setBranch(branchtext); if (copy_columnmap.containsKey("location")) { copy.setLocation( tr.child(copy_columnmap.get("location")).text().trim().replace(" Wegweiser", "")); } if (copy_columnmap.containsKey("signature")) { copy.setShelfmark( tr.child(copy_columnmap.get("signature")).text().trim().replace(" Wegweiser", "")); } result.addCopy(copy); } catch (Exception ex) { ex.printStackTrace(); } } try { Element isvolume = null; Map<String, String> volume = new HashMap<>(); Elements links = doc.select(".data td a"); int elcount = links.size(); for (int eli = 0; eli < elcount; eli++) { List<NameValuePair> anyurl = URLEncodedUtils.parse(new URI(links.get(eli).attr("href")), "UTF-8"); for (NameValuePair nv : anyurl) { if (nv.getName().equals("methodToCall") && nv.getValue().equals("volumeSearch")) { isvolume = links.get(eli); } else if (nv.getName().equals("catKey")) { volume.put("catKey", nv.getValue()); } else if (nv.getName().equals("dbIdentifier")) { volume.put("dbIdentifier", nv.getValue()); } } if (isvolume != null) { volume.put("volume", "true"); result.setVolumesearch(volume); break; } } } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:org.dasein.cloud.aws.AWSCloud.java
private String getV4CanonicalQueryString(URI endpoint) throws InternalException { // parse query params and translate to another form of tuple that is comparable on both key and value List<NameValuePair> parsedParams = URLEncodedUtils.parse(endpoint, "UTF-8"); List<KeyValuePair> queryParams = new ArrayList<KeyValuePair>(parsedParams.size()); for (NameValuePair param : parsedParams) { String key = encode(param.getName(), false); String value = param.getValue() != null ? encode(param.getValue(), false) : ""; queryParams.add(new KeyValuePair(key, value)); }/*from w w w. j a v a2s . c o m*/ // sort query parameters by key, then value Collections.sort(queryParams); StringBuilder sb = new StringBuilder(); for (KeyValuePair pair : queryParams) { if (sb.length() > 0) { sb.append("&"); } sb.append(pair.getKey()).append("=").append(pair.getValue()); } return sb.toString(); }
From source file:org.cloudfoundry.identity.uaa.scim.endpoints.ScimUserEndpointsMockMvcTests.java
private String getQueryStringParam(String query, String key) { List<NameValuePair> params = URLEncodedUtils.parse(query, Charset.defaultCharset()); for (NameValuePair pair : params) { if (key.equals(pair.getName())) { return pair.getValue(); }/* w w w .j ava2 s. c o m*/ } return null; }
From source file:org.apache.cloudstack.storage.resource.NfsSecondaryStorageResource.java
protected String parseCifsMountOptions(URI uri) { List<NameValuePair> args = URLEncodedUtils.parse(uri, "UTF-8"); boolean foundUser = false; boolean foundPswd = false; StringBuilder extraOpts = new StringBuilder(); for (NameValuePair nvp : args) { String name = nvp.getName(); if (name.equals("user")) { foundUser = true;//from w w w . j a va 2 s . c o m s_logger.debug("foundUser is" + foundUser); } else if (name.equals("password")) { foundPswd = true; s_logger.debug("password is present in uri"); } extraOpts.append(name + "=" + nvp.getValue() + ","); } if (s_logger.isDebugEnabled()) { s_logger.error("extraOpts now " + extraOpts); } if (!foundUser || !foundPswd) { String errMsg = "Missing user and password from URI. Make sure they" + "are in the query string and separated by '&'. E.g. " + "cifs://example.com/some_share?user=foo&password=bar"; s_logger.error(errMsg); throw new CloudRuntimeException(errMsg); } return extraOpts.toString(); }
From source file:org.activiti.app.rest.editor.AbstractModelsResource.java
public ResultListDataRepresentation getModels(String filter, String sort, Integer modelType, HttpServletRequest request) {//from w w w . j a va 2 s . co m // need to parse the filterText parameter ourselves, due to encoding issues with the default parsing. String filterText = null; List<NameValuePair> params = URLEncodedUtils.parse(request.getQueryString(), Charset.forName("UTF-8")); if (params != null) { for (NameValuePair nameValuePair : params) { if ("filterText".equalsIgnoreCase(nameValuePair.getName())) { filterText = nameValuePair.getValue(); } } } List<ModelRepresentation> resultList = new ArrayList<ModelRepresentation>(); List<Model> models = null; String validFilter = makeValidFilterText(filterText); User user = SecurityUtils.getCurrentUserObject(); if (validFilter != null) { models = modelRepository.findModelsCreatedBy(user.getId(), modelType, validFilter, getSort(sort, false)); } else { models = modelRepository.findModelsCreatedBy(user.getId(), modelType, getSort(sort, false)); } if (CollectionUtils.isNotEmpty(models)) { List<String> addedModelIds = new ArrayList<String>(); for (Model model : models) { if (addedModelIds.contains(model.getId()) == false) { addedModelIds.add(model.getId()); ModelRepresentation representation = createModelRepresentation(model); resultList.add(representation); } } } ResultListDataRepresentation result = new ResultListDataRepresentation(resultList); return result; }
From source file:org.activiti.app.rest.editor.FormsResource.java
@RequestMapping(method = RequestMethod.GET, produces = "application/json") public ResultListDataRepresentation getForms(HttpServletRequest request) { // need to parse the filterText parameter ourselves, due to encoding issues with the default parsing. String filter = null;/*w w w . ja v a 2 s . c o m*/ List<NameValuePair> params = URLEncodedUtils.parse(request.getQueryString(), Charset.forName("UTF-8")); if (params != null) { for (NameValuePair nameValuePair : params) { if ("filter".equalsIgnoreCase(nameValuePair.getName())) { filter = nameValuePair.getValue(); } } } String validFilter = makeValidFilterText(filter); List<Model> models = null; if (validFilter != null) { models = modelRepository.findByModelTypeAndFilter(AbstractModel.MODEL_TYPE_FORM, validFilter); } else { models = modelRepository.findByModelType(AbstractModel.MODEL_TYPE_FORM); } List<FormRepresentation> reps = new ArrayList<FormRepresentation>(); for (Model model : models) { reps.add(new FormRepresentation(model)); } Collections.sort(reps, new NameComparator()); ResultListDataRepresentation result = new ResultListDataRepresentation(reps); result.setTotal(Long.valueOf(models.size())); return result; }