URL percent encoding in Java - java

URL url = new URL("http://www.example.com/data.php?q=%FD");
logger.info("url: " + url);
URI uri = url.toURI();
logger.info("uri ASCII: " + uri.toASCIIString());
logger.info("uri str : " + uri.toString());
logger.info("query : " + uri.getQuery());
logger.info("decoded : " + URLDecoder.decode(ur.getRawQuery(), "WINDOWS-1252"));
String scheme = uri.getScheme();
String auth = uri.getAuthority();
String path = uri.getPath();
String query = uri.getQuery();
URI cleanedURI = new URI(scheme, auth, path, query, null);
logger.info("cleaned uri ASCII: " + cleanedURI.toASCIIString());
logger.info("cleaned uri str : " + cleanedURI.toString());
The output is:
url: http://www.example.com/data.php?q=%FD
uri ASCII: http://www.example.com/data.php?q=%FD
uri str : http://www.example.com/data.php?q=%FD
query: q=�
decoded: q=ý
cleaned uri ASCII: http://www.example.com/data.php?q=%EF%BF%BD
cleaned uri str : http://www.example.com/data.php?q=�
So, when I split the URI into parts, and then construct again, I cannot get back the original URL. How can I get back the original URL, which is a correctly percent-encoded, valid URL.
Instead of getting %EF%BF%BD I need to get the original %3F.
(Actually what I am trying to achieve is to manipulate certain parts of the URL in a clean way, such as removing the fragment, but this has not much relation to my question.)

The URL http://www.example.com/data.php?q=? is same as http://www.example.com/data.php?q=%3F
%3F (or numeric 63) is nothing but the ascii code for character '?'.
Check it here: http://grox.net/utils/encoding.html
So, if you hit a browser with URL %3f or '?'; it should behave the same.
If you are much concerned with the how it displays on the console, you could try this.
String query = uri.getQuery();
char charData = query.charAt(0); // fetch the character from String
int asciiValue = (int)charData;
or
you could look into String's getByte() method. A short tutorial is here - http://www.tutorialspoint.com/java/java_string_getbytes.htm

Related

Array type found error when try to call from an array of string urls

I just have a question in running a Get request based on string urls.
The code below determines the string urls and puts each urls in an array for a table row.
final String URL_CORE = “/test/platform/auth”;
List<String> urls = new ArrayList<>();
rows.forEach(row -> {
final String clientValue = row.getCell("client");
final String uriValue = row.getCell("uri");
final String typeValue = row.getCell("type"));
urls.add(URL_CORE + "?" +
(clientValue.isEmpty ? "" : CLIENT + "=" + clientValue + "&") +
(uriValue.isEmpty ? "" : URI + "=" + uriValue + "&") +
(typeValue.isEmpty ? "" : TYPE + "=" + typeValue);
});
Within the forEach, I have this code where I just want to run the get request for the url string (for each row at a time). It is giving me an erray type expected found and wondering what am I doing wrong?
getRequest(queryParamsList[row]);
It seems by the name that queryParamsList is a List, not an array, so you have to access element by list.get(index) method. If you dont have the index, you can get it by list.indexOf(element)

Insert text in middle of String (link)

I am getting a URL from a message that is in the form https://example.com/eUjKSv, however I need to insert a "tag" /raw/ in between .com/ and eUjKSv.
I was wondering what would be the easiest way to do it, currently I have a very "hacky" way to achieve it, new URL("https://example.com/raw" + new URL(link).getPath()), I know it's pretty awful and only works if I know exactly the URL. Any suggestions on how to make this better? I thought about regex but couldn't think of a good one to capture it.
You can use either the URL class or the URI class. They both work for this.
URL baseUrl = new URL("https://example.com/eUjKSv");
URL rawUrl = new URL(baseUrl, "/raw" + baseUrl.getPath());
System.out.println("baseUrl = " + baseUrl);
System.out.println("rawUrl = " + rawUrl);
URI baseUri = new URI("https://example.com/eUjKSv");
URI rawUri = baseUri.resolve("/raw" + baseUri.getPath());
System.out.println("baseUri = " + baseUri);
System.out.println("rawUri = " + rawUri);
Output
baseUrl = https://example.com/eUjKSv
rawUrl = https://example.com/raw/eUjKSv
baseUri = https://example.com/eUjKSv
rawUri = https://example.com/raw/eUjKSv

Is there a clean way to check if a String contains a char from an other String

I'm currently trying to catch special chars from an URL to change them to their hex value (for example : "This shop" should transform into "This%20shop").
I was wondering if there was some clean was of looking into the string to find each special chars and replace them to their ascii values.
I try to do it because I have to pass PHP arguments into the url using GET.
Here would be an example of an url :
www.mysite.com/page?adress=I am Living here!
My code actually reaplace '%' and ' ' from URLs, but I'd like to change any special chars I've defined.
This is what I've done so far :
private final String specialChars = "!#\\[]`#$%&'()*+-<>=?";
public URL getURL(){
String tempUrl = baseURL;
Set<String> keys = this.arguments.keySet();
for (Map.Entry<String, String> entry : arguments.entrySet()) {
String value = entry.getValue();
if(entry.getValue().contains(specialChars)){
Log.e("INFO", "THIS URL CONTAINS SPECIAL CHARS");
}
//Replacing % char
if(entry.getValue().contains("%")){
Log.i("URL BUILD INFO", "URL ARGS CONTAINS '%'");
value = entry.getValue().replace("%", "%25");
}
//Replacing spaces
if(entry.getValue().contains(" ")){
Log.i("URL BUILD INFO", "URL ARGS CONTAINS SPACE");
value = entry.getValue().replace(" ", "%20");
}
baseURL += entry.getKey() + "=" + value + "&";
}
try{
this.url = new URL(baseURL);
} catch(MalformedURLException e){
Log.e("URL MALFORMED", "YOUR IS MALFORMED");
e.printStackTrace();
}
Log.d("URL IS VALID", url.toString());
return url;
}
From what I understood from String documentation, matches method should only return true if my URL matches exactly my specialChars charSequence, which isn't what I want. But I can't find any other method to do what I'm trying to achieve on the String documentation.
If you're just trying to handle url encoding there are existing solutions for that - see URLEncoder and URLDecoder (good previous answer here)

How to send plus operator in url in java

I am fetching data from server in my project. In some condition there is a need to send + operator in url with parameter. How can i send "+" in url with parameter.
here is my url
http://www.needsthesupermarket.com/webservice/dp/addCart.php?cart_id=43530&cust_id=13936&pid=11303&qty=1&combination=2 ltr + 1 kg&guest_id=2509245
In blank space i replace with %20. but problem with + sign. How can i send it in url?
%26 -> &
%2B -> +
You can decode/encode here
You should encode your GET parameters:
Uri.encode(someParam)
For example if you have some Map paramsGet with GET parameters:
final StringBuilder url = new StringBuilder("http://example.com");
String delim = "?";
for (final Map.Entry<String, String> entry : paramsGet.entrySet()) {
url.append(delim).append(entry.getKey()).append("=").append(Uri.encode(entry.getValue()));
delim = "&";
}
To Encode use
String encodedInput = java.net.URLEncoder.encode(inputText, "UTF-8");
To Decode use
String decodedInput = java.net.URLDecoder.decode(encodedInput, "UTF-8");

How to obtain the last path segment of a URI

I have as input a string that is a URI. how is it possible to get the last path segment (that in my case is an id)?
This is my input URL:
String uri = "http://base_path/some_segment/id"
and I have to obtain the id I have tried with this:
String strId = "http://base_path/some_segment/id";
strId = strId.replace(path);
strId = strId.replaceAll("/", "");
Integer id = new Integer(strId);
return id.intValue();
but it doesn't work, and surely there must be a better way to do it.
is that what you are looking for:
URI uri = new URI("http://example.com/foo/bar/42?param=true");
String path = uri.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
int id = Integer.parseInt(idStr);
alternatively
URI uri = new URI("http://example.com/foo/bar/42?param=true");
String[] segments = uri.getPath().split("/");
String idStr = segments[segments.length-1];
int id = Integer.parseInt(idStr);
import android.net.Uri;
Uri uri = Uri.parse("http://example.com/foo/bar/42?param=true");
String token = uri.getLastPathSegment();
Here's a short method to do it:
public static String getLastBitFromUrl(final String url){
// return url.replaceFirst("[^?]*/(.*?)(?:\\?.*)","$1);" <-- incorrect
return url.replaceFirst(".*/([^/?]+).*", "$1");
}
Test Code:
public static void main(final String[] args){
System.out.println(getLastBitFromUrl(
"http://example.com/foo/bar/42?param=true"));
System.out.println(getLastBitFromUrl("http://example.com/foo"));
System.out.println(getLastBitFromUrl("http://example.com/bar/"));
}
Output:
42
foo
bar
Explanation:
.*/ // find anything up to the last / character
([^/?]+) // find (and capture) all following characters up to the next / or ?
// the + makes sure that at least 1 character is matched
.* // find all following characters
$1 // this variable references the saved second group from above
// I.e. the entire string is replaces with just the portion
// captured by the parentheses above
I know this is old, but the solutions here seem rather verbose. Just an easily readable one-liner if you have a URL or URI:
String filename = new File(url.getPath()).getName();
Or if you have a String:
String filename = new File(new URL(url).getPath()).getName();
If you are using Java 8 and you want the last segment in a file path you can do.
Path path = Paths.get("example/path/to/file");
String lastSegment = path.getFileName().toString();
If you have a url such as http://base_path/some_segment/id you can do.
final Path urlPath = Paths.get("http://base_path/some_segment/id");
final Path lastSegment = urlPath.getName(urlPath.getNameCount() - 1);
In Android
Android has a built in class for managing URIs.
Uri uri = Uri.parse("http://base_path/some_segment/id");
String lastPathSegment = uri.getLastPathSegment()
If you have commons-io included in your project, you can do it without creating unecessary objects with org.apache.commons.io.FilenameUtils
String uri = "http://base_path/some_segment/id";
String fileName = FilenameUtils.getName(uri);
System.out.println(fileName);
Will give you the last part of the path, which is the id
In Java 7+ a few of the previous answers can be combined to allow retrieval of any path segment from a URI, rather than just the last segment. We can convert the URI to a java.nio.file.Path object, to take advantage of its getName(int) method.
Unfortunately, the static factory Paths.get(uri) is not built to handle the http scheme, so we first need to separate the scheme from the URI's path.
URI uri = URI.create("http://base_path/some_segment/id");
Path path = Paths.get(uri.getPath());
String last = path.getFileName().toString();
String secondToLast = path.getName(path.getNameCount() - 2).toString();
To get the last segment in one line of code, simply nest the lines above.
Paths.get(URI.create("http://base_path/some_segment/id").getPath()).getFileName().toString()
To get the second-to-last segment while avoiding index numbers and the potential for off-by-one errors, use the getParent() method.
String secondToLast = path.getParent().getFileName().toString();
Note the getParent() method can be called repeatedly to retrieve segments in reverse order. In this example, the path only contains two segments, otherwise calling getParent().getParent() would retrieve the third-to-last segment.
You can also use replaceAll:
String uri = "http://base_path/some_segment/id"
String lastSegment = uri.replaceAll(".*/", "")
System.out.println(lastSegment);
result:
id
You can use getPathSegments() function. (Android Documentation)
Consider your example URI:
String uri = "http://base_path/some_segment/id"
You can get the last segment using:
List<String> pathSegments = uri.getPathSegments();
String lastSegment = pathSegments.get(pathSegments.size() - 1);
lastSegment will be id.
I'm using the following in a utility class:
public static String lastNUriPathPartsOf(final String uri, final int n, final String... ellipsis)
throws URISyntaxException {
return lastNUriPathPartsOf(new URI(uri), n, ellipsis);
}
public static String lastNUriPathPartsOf(final URI uri, final int n, final String... ellipsis) {
return uri.toString().contains("/")
? (ellipsis.length == 0 ? "..." : ellipsis[0])
+ uri.toString().substring(StringUtils.lastOrdinalIndexOf(uri.toString(), "/", n))
: uri.toString();
}
you can get list of path segments from the Uri class
String id = Uri.tryParse("http://base_path/some_segment/id")?.pathSegments.last ?? "InValid URL";
It will return id if the url is valid, if it is invalid it returns "Invalid url"
Get URL from URI and use getFile() if you are not ready to use substring way of extracting file.

Categories