List of usage examples for java.net URI getRawQuery
public String getRawQuery()
From source file:org.texai.torrent.Tracker.java
/** Handles the announce request. * * @param httpRequest the HTTP request/* ww w . j a v a 2s.co m*/ * @param channel the channel */ public void handleAnnounceRequest(final HttpRequest httpRequest, final Channel channel) { //Preconditions assert httpRequest != null : "httpRequest must not be null"; assert channel != null : "channel must not be null"; final URI uri; try { uri = new URI(httpRequest.getUri()); } catch (URISyntaxException ex) { throw new TexaiException(ex); } final Map<String, String> parameterDictionary = HTTPUtils.getQueryMap(uri.getRawQuery()); LOGGER.warn("tracker announce request: " + channel.getRemoteAddress() + " -> " + parameterDictionary); // info hash final String requestedUrlEncodedInfoHash = parameterDictionary.get("info_hash"); LOGGER.debug(" requestedUrlEncodedInfoHash: " + requestedUrlEncodedInfoHash); if (requestedUrlEncodedInfoHash == null) { failure(httpRequest, "No info_hash given", channel); return; } boolean found = false; LOGGER.debug("trackedPeerInfosDictionary: " + trackedPeerInfosDictionary); synchronized (trackedPeerInfosDictionary) { for (String urlEncodedInfoHash : trackedPeerInfosDictionary.keySet()) { if (urlEncodedInfoHash.equals(requestedUrlEncodedInfoHash)) { found = true; } } } if (!found) { failure(httpRequest, "Tracker doesn't handle given info_hash", channel); return; } // peer id byte[] peerIdBytes; final String peerIdValue = parameterDictionary.get("peer_id"); if (peerIdValue == null) { failure(httpRequest, "No peer_id given", channel); return; } try { peerIdBytes = (new URLCodec()).decode(peerIdValue.getBytes()); } catch (DecoderException ex) { failure(httpRequest, "cannot decode peer id value: " + peerIdValue, channel); return; } if (peerIdBytes.length != 20) { failure(httpRequest, "peer_id must be 20 bytes long", channel); return; } // port @SuppressWarnings("UnusedAssignment") int peerPort = 0; final String peerPortValue = parameterDictionary.get("port"); if (peerPortValue == null) { failure(httpRequest, "No port given", channel); return; } try { peerPort = Integer.parseInt(peerPortValue); } catch (NumberFormatException nfe) { failure(httpRequest, "port not a number: " + nfe, channel); return; } // ip final InetAddress inetAddress = ((InetSocketAddress) channel.getRemoteAddress()).getAddress(); InetAddress inetAddress1 = null; if (NetworkUtils.isPrivateNetworkAddress(inetAddress)) { // See http://wiki.theory.org/BitTorrentSpecification#Tracker_Response . // Handle the case where the peer and ourselves are both behind the same NAT router. The ip address from the // socket connection will be a private address which indicates the peer is on our private network behind the // internet-facing router. The optional ip parameter contains what the peer found as its local host address. final String peerSuppliedIPAddress = parameterDictionary.get("ip"); if (peerSuppliedIPAddress == null) { failure(httpRequest, "No ip address given", channel); return; } else { try { inetAddress1 = InetAddress.getByName(peerSuppliedIPAddress); } catch (UnknownHostException ex) { LOGGER.warn("invalid ip parameter supplied by peer: '" + peerSuppliedIPAddress + "'"); } } if (inetAddress1 == null) { inetAddress1 = inetAddress; } } else { inetAddress1 = inetAddress; } final TrackedPeerInfo trackedPeerInfo = new TrackedPeerInfo(peerIdBytes, inetAddress1, peerPort); // downloaded final int downloaded; final String downloaded_value = parameterDictionary.get("downloaded"); if (downloaded_value == null) { failure(httpRequest, "No downloaded given", channel); return; } try { downloaded = Integer.parseInt(downloaded_value); } catch (NumberFormatException nfe) { failure(httpRequest, "downloaded not a number: " + nfe, channel); return; } // event final Map<String, Object> responseDictionary = new HashMap<>(); final Map<TrackedPeerInfo, TrackedPeerStatus> trackedPeerInfosMap = trackedPeerInfosDictionary .get(requestedUrlEncodedInfoHash); final String event = parameterDictionary.get("event"); if (event == null || event.isEmpty()) { synchronized (trackedPeerInfosMap) { final TrackedPeerStatus trackedPeerStatus = trackedPeerInfosMap.get(trackedPeerInfo); if (trackedPeerStatus == null) { failure(httpRequest, "peer never started", channel); return; } LOGGER.warn("updating peer " + trackedPeerInfo + " expiration"); trackedPeerStatus.updatePeerExpirationMilliseconds(); } } else { if (event.equals(COMPLETED_EVENT)) { final TrackedPeerStatus trackedPeerStatus = trackedPeerInfosMap.get(trackedPeerInfo); if (trackedPeerStatus == null) { failure(httpRequest, "peer never started", channel); return; } trackedPeerStatus.event = COMPLETED_EVENT; if (downloaded == 0) { LOGGER.warn("seeding without download: " + trackedPeerInfo); } else { LOGGER.warn("seeding after completed download: " + trackedPeerInfo); synchronized (completionsDictionary) { final Integer nbrDownloaded = completionsDictionary.get(requestedUrlEncodedInfoHash); if (nbrDownloaded == null) { completionsDictionary.put(requestedUrlEncodedInfoHash, 1); } else { completionsDictionary.put(requestedUrlEncodedInfoHash, nbrDownloaded + 1); } } } } else { synchronized (trackedPeerInfosMap) { switch (event) { case STOPPED_EVENT: LOGGER.warn("removing stopped peer " + trackedPeerInfo); trackedPeerInfosMap.remove(trackedPeerInfo); break; case STARTED_EVENT: LOGGER.warn("adding new peer " + trackedPeerInfo); trackedPeerInfosMap.put(trackedPeerInfo, new TrackedPeerStatus(trackedPeerInfo, event)); break; default: failure(httpRequest, "invalid event", channel); return; } } } } // compose tracker response responseDictionary.put("interval", REQUEST_INTERVAL_SECONDS); final List<Map<String, Object>> peerList = new ArrayList<>(); final Iterator<TrackedPeerStatus> trackedPeerInfos_iter = trackedPeerInfosMap.values().iterator(); try { LOGGER.warn("client peer id " + new String(peerIdBytes, "US-ASCII")); } catch (UnsupportedEncodingException ex) { throw new TexaiException(ex); } LOGGER.warn("tracked peers ..."); while (trackedPeerInfos_iter.hasNext()) { final TrackedPeerStatus trackedPeerStatus = trackedPeerInfos_iter.next(); final TrackedPeerInfo trackedPeerInfo1 = trackedPeerStatus.trackedPeerInfo; LOGGER.warn(""); LOGGER.warn(" peer id " + trackedPeerInfo1.toIDString()); LOGGER.warn(" ip " + trackedPeerInfo1.getInetAddress().getHostAddress()); LOGGER.warn(" port " + trackedPeerInfo1.getPort()); if (trackedPeerStatus.peerExpirationMilliseconds < System.currentTimeMillis()) { LOGGER.warn("expiring peer " + trackedPeerInfo1); trackedPeerInfos_iter.remove(); } else if (trackedPeerInfo1.equals(trackedPeerInfo)) { LOGGER.warn("omitting self-peer from the peer list"); } else { final Map<String, Object> map = new HashMap<>(); map.put("peer id", trackedPeerInfo1.getPeerIdBytes()); map.put("ip", trackedPeerInfo1.getInetAddress().getHostAddress()); map.put("port", trackedPeerInfo1.getPort()); peerList.add(map); } } responseDictionary.put("peers", peerList); LOGGER.log(Level.DEBUG, "Tracker response: " + responseDictionary); NettyHTTPUtils.writeBinaryResponse(httpRequest, BEncoder.bencode(responseDictionary), channel, null); // sessionCookie }
From source file:org.zaproxy.zap.spider.URLCanonicalizer.java
/** * Gets the canonical url, starting from a relative or absolute url found in a given context (baseURL). * //w ww. j a va 2 s . c o m * @param url the url string defining the reference * @param baseURL the context in which this url was found * @return the canonical url */ public static String getCanonicalURL(String url, String baseURL) { try { /* Build the absolute URL, from the url and the baseURL */ String resolvedURL = URLResolver.resolveUrl(baseURL == null ? "" : baseURL, url); log.debug("Resolved URL: " + resolvedURL); URI canonicalURI; try { canonicalURI = new URI(resolvedURL); } catch (Exception e) { canonicalURI = new URI(URIUtil.encodeQuery(resolvedURL)); } /* Some checking. */ if (canonicalURI.getScheme() == null) { throw new MalformedURLException("Protocol could not be reliably evaluated from uri: " + canonicalURI + " and base url: " + baseURL); } if (canonicalURI.getRawAuthority() == null) { log.debug("Ignoring URI with no authority (host[\":\"port]): " + canonicalURI); return null; } if (canonicalURI.getHost() == null) { throw new MalformedURLException("Host could not be reliably evaluated from: " + canonicalURI); } /* * Normalize: no empty segments (i.e., "//"), no segments equal to ".", and no segments equal to * ".." that are preceded by a segment not equal to "..". */ String path = canonicalURI.normalize().getRawPath(); /* Convert '//' -> '/' */ int idx = path.indexOf("//"); while (idx >= 0) { path = path.replace("//", "/"); idx = path.indexOf("//"); } /* Drop starting '/../' */ while (path.startsWith("/../")) { path = path.substring(3); } /* Trim */ path = path.trim(); /* Process parameters and sort them. */ final SortedMap<String, String> params = createParameterMap(canonicalURI.getRawQuery()); final String queryString; String canonicalParams = canonicalize(params); queryString = (canonicalParams.isEmpty() ? "" : "?" + canonicalParams); /* Add starting slash if needed */ if (path.length() == 0) { path = "/" + path; } /* Drop default port: example.com:80 -> example.com */ int port = canonicalURI.getPort(); if (port == 80) { port = -1; } /* Lowercasing protocol and host */ String protocol = canonicalURI.getScheme().toLowerCase(); String host = canonicalURI.getHost().toLowerCase(); String pathAndQueryString = normalizePath(path) + queryString; URL result = new URL(protocol, host, port, pathAndQueryString); return result.toExternalForm(); } catch (Exception ex) { log.warn("Error while Processing URL in the spidering process (on base " + baseURL + "): " + ex.getMessage()); return null; } }
From source file:com.fatwire.dta.sscrawler.util.PackedArgsSSUriHelper.java
public Link createLink(final URI uri) { Link link = super.createLink(uri); if (link == null) return link; if (link.has("childpagename")) { final String packedargs = link.remove("packedargs"); // special case if there is a childpagename, in that case packedargs // contain all the non c/cid/p/rendermode args. // decode them correctly if (packedargs != null) { log.trace("packedargs with childpagename: " + packedargs); String[] pv;/* w ww. j a va 2s. co m*/ try { pv = decode(packedargs).split("&"); for (String v : pv) { String[] nv = v.split("="); if (nv.length == 2) { link.addParameter(nv[0], nv[1]); } else if (nv.length == 1) { link.addParameter(nv[0], ""); } } } catch (DecoderException e) { log.warn(e + " for packedargs: " + packedargs + " on " + uri.getRawQuery(), e); return null; } } } return link; }
From source file:io.curly.artifact.web.remote.PaperclipLinkCatcher.java
private String reconstructURI(String host, String href) { URI original; try {/*from w ww .ja v a 2 s. c om*/ original = new URI(href); } catch (URISyntaxException e) { throw new IllegalArgumentException("Cannot create URI from: " + href); } int port = 80; if ("https".equals(original.getScheme())) { port = 443; } if (host.contains(":")) { String[] pair = host.split(":"); host = pair[0]; port = Integer.valueOf(pair[1]); } if (host.equals(original.getHost()) && port == original.getPort()) { return href; } String scheme = original.getScheme(); if (scheme == null) { scheme = port == 443 ? "https" : "http"; } StringBuilder sb = new StringBuilder(); sb.append(scheme).append("://"); if (StringUtils.hasText(original.getRawUserInfo())) { sb.append(original.getRawUserInfo()).append("@"); } sb.append(host); if (port >= 0) { sb.append(":").append(port); } sb.append(original.getPath()); if (StringUtils.hasText(original.getRawQuery())) { sb.append("?").append(original.getRawQuery()); } if (StringUtils.hasText(original.getRawFragment())) { sb.append("#").append(original.getRawFragment()); } return sb.toString(); }
From source file:com.nesscomputing.service.discovery.client.ServiceURI.java
public ServiceURI(final URI uri) throws URISyntaxException { if (!"srvc".equals(uri.getScheme())) { throw new URISyntaxException(uri.toString(), "ServiceURI only supports srvc:// URIs"); }/*from w w w . ja v a 2 s . c o m*/ if (!StringUtils.startsWith(uri.getSchemeSpecificPart(), "//")) { throw new URISyntaxException(uri.toString(), "ServiceURI only supports srvc:// URIs"); } final String schemeSpecificPart = uri.getSchemeSpecificPart().substring(2); final int slashIndex = schemeSpecificPart.indexOf('/'); if (slashIndex == -1) { throw new URISyntaxException(uri.toString(), "ServiceURI requires a slash at the end of the service!"); } final int colonIndex = schemeSpecificPart.indexOf(':'); if (colonIndex == -1 || colonIndex > slashIndex) { serviceName = schemeSpecificPart.substring(0, slashIndex); serviceType = null; } else { serviceName = schemeSpecificPart.substring(0, colonIndex); serviceType = schemeSpecificPart.substring(colonIndex + 1, slashIndex); } path = uri.getRawPath(); query = uri.getRawQuery(); fragment = uri.getRawFragment(); }
From source file:io.curly.commons.web.hateoas.ZuulAwareMappingResolver.java
/** * <a href="https://github.com/spring-cloud-samples/customers-stores/blob/master/rest-microservices-customers/src/main/java/example/customers/integration/StoreIntegration.java#L89">Github Spring Cloud Sample implementation<a/> * * @param host the current request host//www . ja v a2 s . c o m * @param href the #resolve builder returns * @return reconstructed URI based on the current request host and port comparing them with the service host and port */ public String reconstructURI(String host, String href) { URI original; try { original = new URI(href); } catch (URISyntaxException e) { throw new IllegalArgumentException("Cannot create URI from: " + href); } int port = 80; if ("https".equals(original.getScheme())) { port = 443; } if (host.contains(":")) { String[] pair = host.split(":"); host = pair[0]; port = Integer.valueOf(pair[1]); } if (host.equals(original.getHost()) && port == original.getPort()) { return href; } String scheme = original.getScheme(); if (scheme == null) { scheme = port == 443 ? "https" : "http"; } StringBuilder sb = new StringBuilder(); sb.append(scheme).append("://"); if (StringUtils.hasText(original.getRawUserInfo())) { sb.append(original.getRawUserInfo()).append("@"); } sb.append(host); if (port >= 0) { sb.append(":").append(port); } sb.append(original.getPath()); if (StringUtils.hasText(original.getRawQuery())) { sb.append("?").append(original.getRawQuery()); } if (StringUtils.hasText(original.getRawFragment())) { sb.append("#").append(original.getRawFragment()); } return sb.toString(); }
From source file:net.billylieurance.azuresearch.AbstractAzureSearchQuery.java
/** * Run the query that has been set up in this instance. * Next step would be to get the results with {@link getQueryResult()} *///from w ww .j ava2 s . co m public void doQuery() { DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials( new AuthScope(_targetHost.getHostName(), _targetHost.getPort()), new UsernamePasswordCredentials(this.getAppid(), this.getAppid())); URI uri; try { String full_path = getQueryPath(); String full_query = getUrlQuery(); uri = new URI(AZURESEARCH_SCHEME, AZURESEARCH_AUTHORITY, full_path, full_query, null); // Bing and java URI disagree about how to represent + in query // parameters. This is what we have to do instead... uri = new URI(uri.getScheme() + "://" + uri.getAuthority() + uri.getPath() + "?" + uri.getRawQuery().replace("+", "%2b").replace("'", "%27")); // System.out.println(uri.toString()); // log.log(Level.WARNING, uri.toString()); } catch (URISyntaxException e1) { e1.printStackTrace(); return; } HttpGet get = new HttpGet(uri); get.addHeader("Accept", "application/xml"); get.addHeader("Content-Type", "application/xml"); try { _responsePost = client.execute(get); _resEntity = _responsePost.getEntity(); if (this.getProcessHTTPResults()) { _rawResult = loadXMLFromStream(_resEntity.getContent()); this.loadResultsFromRawResults(); } // Adding an automatic HTTP Result to String really requires // Apache Commons IO. That would break // Android compatibility. I'm not going to do that unless I // re-implement IOUtils. } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } }
From source file:com.cisco.oss.foundation.http.AbstractHttpClient.java
protected S updateRequestUri(S request, InternalServerProxy serverProxy) { URI origUri = request.getUri(); String host = serverProxy.getHost(); String scheme = origUri.getScheme() == null ? (request.isHttpsEnabled() ? "https" : "http") : origUri.getScheme();/*w w w. j a v a 2 s. co m*/ int port = serverProxy.getPort(); String urlPath = ""; if (origUri.getRawPath() != null && origUri.getRawPath().startsWith("/")) { urlPath = origUri.getRawPath(); } else { urlPath = "/" + origUri.getRawPath(); } URI newURI = null; try { if (autoEncodeUri) { String query = origUri.getQuery(); newURI = new URI(scheme, origUri.getUserInfo(), host, port, urlPath, query, origUri.getFragment()); } else { String query = origUri.getRawQuery(); if (query != null) { newURI = new URI(scheme + "://" + host + ":" + port + urlPath + "?" + query); } else { newURI = new URI(scheme + "://" + host + ":" + port + urlPath); } } } catch (URISyntaxException e) { throw new ClientException(e.toString()); } S req = (S) request.replaceUri(newURI); // try { // req = (S) this.clone(); // } catch (CloneNotSupportedException e) { // throw new IllegalArgumentException(e); // } // req.uri = newURI; return req; }
From source file:org.opensolaris.opengrok.web.PageConfig.java
/** * Get all data required to create a diff view wrt. to this request in one * go./* w ww . j ava 2 s. c o m*/ * * @return an instance with just enough information to render a sufficient * view. If not all required parameters were given either they are * supplemented with reasonable defaults if possible, otherwise the related * field(s) are {@code null}. {@link DiffData#errorMsg} * {@code != null} indicates, that an error occured and one should not try * to render a view. */ public DiffData getDiffData() { DiffData data = new DiffData(); data.path = getPath().substring(0, path.lastIndexOf('/')); data.filename = Util.htmlize(getResourceFile().getName()); String srcRoot = getSourceRootPath(); String context = req.getContextPath(); String[] filepath = new String[2]; data.rev = new String[2]; data.file = new String[2][]; data.param = new String[2]; /* * Basically the request URI looks like this: * http://$site/$webapp/diff/$resourceFile?r1=$fileA@$revA&r2=$fileB@$revB * The code below extracts file path and revision from the URI. */ for (int i = 1; i <= 2; i++) { String p = req.getParameter("r" + i); if (p != null) { int j = p.lastIndexOf("@"); if (j != -1) { filepath[i - 1] = p.substring(0, j); data.rev[i - 1] = p.substring(j + 1); } } } if (data.rev[0] == null || data.rev[1] == null || data.rev[0].length() == 0 || data.rev[1].length() == 0 || data.rev[0].equals(data.rev[1])) { data.errorMsg = "Please pick two revisions to compare the changed " + "from the <a href=\"" + context + Prefix.HIST_L + getUriEncodedPath() + "\">history</a>"; return data; } data.genre = AnalyzerGuru.getGenre(getResourceFile().getName()); if (data.genre == null || txtGenres.contains(data.genre)) { InputStream[] in = new InputStream[2]; try { // Get input stream for both older and newer file. for (int i = 0; i < 2; i++) { File f = new File(srcRoot + filepath[i]); in[i] = HistoryGuru.getInstance().getRevision(f.getParent(), f.getName(), data.rev[i]); if (in[i] == null) { data.errorMsg = "Unable to get revision " + Util.htmlize(data.rev[i]) + " for file: " + Util.htmlize(getPath()); return data; } } /* * If the genre of the older revision cannot be determined, * (this can happen if the file was empty), try with newer * version. */ for (int i = 0; i < 2 && data.genre == null; i++) { try { data.genre = AnalyzerGuru.getGenre(in[i]); } catch (IOException e) { data.errorMsg = "Unable to determine the file type: " + Util.htmlize(e.getMessage()); } } if (data.genre != Genre.PLAIN && data.genre != Genre.HTML) { return data; } ArrayList<String> lines = new ArrayList<>(); Project p = getProject(); for (int i = 0; i < 2; i++) { try (BufferedReader br = new BufferedReader( ExpandTabsReader.wrap(new InputStreamReader(in[i]), p))) { String line; while ((line = br.readLine()) != null) { lines.add(line); } data.file[i] = lines.toArray(new String[lines.size()]); lines.clear(); } in[i] = null; } } catch (Exception e) { data.errorMsg = "Error reading revisions: " + Util.htmlize(e.getMessage()); } finally { for (int i = 0; i < 2; i++) { IOUtils.close(in[i]); } } if (data.errorMsg != null) { return data; } try { data.revision = Diff.diff(data.file[0], data.file[1]); } catch (DifferentiationFailedException e) { data.errorMsg = "Unable to get diffs: " + Util.htmlize(e.getMessage()); } for (int i = 0; i < 2; i++) { try { URI u = new URI(null, null, null, filepath[i] + "@" + data.rev[i], null); data.param[i] = u.getRawQuery(); } catch (URISyntaxException e) { LOGGER.log(Level.WARNING, "Failed to create URI: ", e); } } data.full = fullDiff(); data.type = getDiffType(); } return data; }
From source file:com.Upwork.api.OAuthClient.java
/** * Send signed GET OAuth request/*from w w w. j a va 2 s.co m*/ * * @param url Relative URL * @param type Type of HTTP request (HTTP method) * @param params Hash of parameters * @throws JSONException If JSON object is invalid or request was abnormal * @return {@link JSONObject} JSON Object that contains data from response * */ private JSONObject sendGetRequest(String url, Integer type, HashMap<String, String> params) throws JSONException { String fullUrl = getFullUrl(url); HttpGet request = new HttpGet(fullUrl); if (params != null) { URI uri; String query = ""; try { URIBuilder uriBuilder = new URIBuilder(request.getURI()); // encode values and add them to the request for (Map.Entry<String, String> entry : params.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); // to prevent double encoding, we need to create query string ourself // uriBuilder.addParameter(key, URLEncoder.encode(value).replace("%3B", ";")); query = query + key + "=" + value.replace("&", "&") + "&"; // what the hell is going on in java - no adequate way to encode query string // lets temporary replace "&" in the value, to encode it manually later } // this routine will encode query string uriBuilder.setCustomQuery(query); uri = uriBuilder.build(); // re-create request to have validly encoded ampersand request = new HttpGet(fullUrl + "?" + uri.getRawQuery().replace("&", "%26")); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { mOAuthConsumer.sign(request); } catch (OAuthException e) { e.printStackTrace(); } return UpworkRestClient.getJSONObject(request, type); }