Building an absolute URL from a relative URL in Java - java

I'm having trouble building an absolute URL from a relative URL without resorting to String hackery...
Given
http://localhost:8080/myWebApp/someServlet
Inside the method:
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
What's the most "correct" way of building :
http://localhost:8080/myWebApp/someImage.jpg
(Note, must be absolute, not relative)
Currently, I'm doing it through building the string, but there MUST be a better way.
I've looked at various combinations of new URI / URL, and I end up with
http://localhost:8080/someImage.jpg
Help greatly appreciated

Using java.net.URL
URL baseUrl = new URL("http://www.google.com/someFolder/");
URL url = new URL(baseUrl, "../test.html");

How about:
String s = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/someImage.jpg";

Looks like you already figured out the hard part, which is what host your are running on. The rest is easy,
String url = host + request.getContextPath() + "/someImage.jpg";
Should give you what you need.

this code work will on linux, it can just combine the path, if you want more, constructor of URI could be helpful.
URL baseUrl = new URL("http://example.com/first");
URL targetUrl = new URL(baseUrl, Paths.get(baseUrl.getPath(), "second", "/third", "//fourth//", "fifth").toString());
if you path contain something need to escape, use URLEncoder.encode to escape it at first.
URL baseUrl = new URL("http://example.com/first");
URL targetUrl = new URL(baseUrl, Paths.get(baseUrl.getPath(), URLEncoder.encode(relativePath, StandardCharsets.UTF_8), URLEncoder.encode(filename, StandardCharsets.UTF_8)).toString());
example:
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
try {
URL baseUrl = new URL("http://example.com/first");
Path relativePath = Paths.get(baseUrl.getPath(), "second", "/third", "//fourth//", "fifth");
URL targetUrl = new URL(baseUrl, relativePath.toString());
System.out.println(targetUrl.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
output
http://example.com/first/second/third/fourth/fifth
baseUrl.getPath() are very important, don't forget it.
a wrong example:
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
try {
URL baseUrl = new URL("http://example.com/first");
Path relativePath = Paths.get("second", "/third", "//fourth//", "fifth");
URL targetUrl = new URL(baseUrl, relativePath.toString());
System.out.println(targetUrl.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
output
http://example.com/second/third/fourth/fifth
we have lost our /first in baseurl.

Related

Convert File object to URI object where the path contains spaces

In my program I have the conversion as illustrated by the test.
Path->File->URI->URL->File.
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
#RunWith(JUnit4.class)
public class UrlStuffTest {
#Test
public void testFileToUriToUrlWithCreateFile() throws MalformedURLException, IOException {
Path p = Paths.get("testfolder", "xmls");
File f = p.toAbsolutePath().toFile();
f.mkdirs();
System.out.println(f);
URI uri = f.toURI();
System.out.println(uri);
URL url = uri.toURL();
System.out.println(url);
File aXmlFile = new File(url.getPath(), "test.xml");
System.out.println(aXmlFile);
aXmlFile.createNewFile();
}
#Test
public void testFileToUriToUrlWithCreateFileAndSpaceInPath() throws MalformedURLException, IOException {
Path p = Paths.get("test folder", "xmls");
File f = p.toAbsolutePath().toFile();
f.mkdirs();
System.out.println(f);
URI uri = f.toURI();
System.out.println(uri);
URL url = uri.toURL();
System.out.println(url);
File aXmlFile = new File(url.getPath(), "test.xml");
System.out.println(aXmlFile);
aXmlFile.createNewFile();
}
}
If you run the methods you will see that the upper one succeeds. The last one has a space in the path and fails on the last line basically saying "System can not find path...".
Output of the first method is
C:\Development\Workspace\spielwiese\testfolder\xmls
file:/C:/Development/Workspace/spielwiese/testfolder/xmls/
file:/C:/Development/Workspace/spielwiese/testfolder/xmls/
C:\Development\Workspace\spielwiese\testfolder\xmls\test.xml
Output of the second method is
C:\Development\Workspace\spielwiese\test folder\xmls
file:/C:/Development/Workspace/spielwiese/test%20folder/xmls/
file:/C:/Development/Workspace/spielwiese/test%20folder/xmls/
C:\Development\Workspace\spielwiese\test%20folder\xmls\test.xml
So when converting from File to URI the space becomes a %20. I guess this is what makes the final XML file creation fail.
I solved this issue in my program by skipping the conversion from File to URI by using File.toURL() method. This method is deprecated though.
What would be a better solution?
You need to decode the URL string before using it as File path. Something like this
String decodedUrlPath = java.net.URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8.name());
Really weird. With Path-File and without URL/URI it runs fine. Was trying to find other hints, but stuck on this one also:
Path path = Paths.get("test folder\\xmls", "test1.xml");
File aXmlFile = path.toFile();
System.out.println(aXmlFile);
aXmlFile.createNewFile();
So I guess in your URI you have to replace %20 pack to spaces " ".
That code works:
aXmlFile = new File(url.getPath().replaceAll("%20", " "), "test.xml");
System.out.println(aXmlFile);
aXmlFile.createNewFile();

WebDriver getCurrentUrl() returning malformed URI

I'm involved in writing a (Java/Groovy) browser-automation app with Selenium 2 and FireFox driver.
Currently there is an issue with some URLs we find in the wild that are apparently using bad URI syntax. (specifically curly braces ({}), |'s and ^'s).
String url = driver.getCurrentUrl(); // http://example.com/foo?key=val|with^bad{char}acters
When trying to construct a java.net.URI from the string returned by driver.getCurrentUrl() a URISyntaxException is thrown.
new URI(url); // java.net.URISyntaxException: Illegal character in query at index ...
Encoding the whole url before constructing the URI will not work (as I understand it).
The whole url is encoded, and it doesn't preseve any pieces of it that I can parse in any normal fashion. For example, with this uri-safe string, URI can't know the difference between a & as the query-string-param delimeter or %26 (its encoded value) in the content of a single qs-param.
String encoded = URLEncoder.encode(url, "UTF-8") // http%3A%2F%2Fexample.com%2Ffoo%3Fkey%3Dval%7Cwith%5E%7Cbad%7Ccharacters
URI uri = new URI(encoded)
URLEncodedUtils.parse(uri, "UTF-8") // []
Currently the solution is, before constructing the URI, running the following (groovy) code:
["|", "^", "{", "}"].each {
url = url.replace(it, URLEncoder.encode(it, "UTF-8"))
}
But this seems dirty and wrong.
I guess my question is multi-part:
Why does FirefoxDriver return a String rather than a URI?
Why is this String malformed?
What is best practice for dealing with this kind of thing?
We can partially encode query string parameters, as discussed in comments, it should work.
Other way is to use galimatias library:
import io.mola.galimatias.GalimatiasParseException;
import io.mola.galimatias.URL;
import java.net.URI;
import java.net.URISyntaxException;
public class Main {
public static void main(String[] args) throws URISyntaxException {
String example1 = "http://example.com/foo?key=val-with-a-|-in-it";
String example2 = "http://example.com?foo={bar}";
try {
URL url1 = URL.parse(example1);
URI uri1 = url1.toJavaURI();
System.out.println(url1);
System.out.println(uri1);
URL url2 = URL.parse(example2);
URI uri2 = url2.toJavaURI();
System.out.println(url2);
System.out.println(uri2);
} catch (GalimatiasParseException ex) {
// Do something with non-recoverable parsing error
}
}
}
Output:
http://example.com/foo?key=val-with-a-|-in-it
http://example.com/foo?key=val-with-a-%7C-in-it
http://example.com/?foo={bar}
http://example.com/?foo=%7Bbar%7D
driver.getCurrentUrl() gets a string from the browser and before making it into an URL, you should URL encode the string.
See Java URL encoding of query string parameters for an example of this in Java.
Will this work for you?
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
public class Sample {
public static void main(String[] args) throws UnsupportedEncodingException {
String urlInString="http://example.com/foo?key=val-with-a-{-in-it";
String encodedURL=URLEncoder.encode(urlInString, "UTF-8");
URI encodedURI=URI.create(encodedURL);
System.out.println("Actual URL:"+urlInString);
System.out.println("Encoded URL:"+encodedURL);
System.out.println("Encoded URI:"+encodedURI);
}
}
Output:
Actual URL:http://example.com/foo?key=val-with-a-{-in-it
Encoded URL:http%3A%2F%2Fexample.com%2Ffoo%3Fkey%3Dval-with-a-%7B-in-it
Encoded URI:http%3A%2F%2Fexample.com%2Ffoo%3Fkey%3Dval-with-a-%7B-in-it
Another Solution is to split the URL fetched and then use them to create the URL you want. This will ensure that you get all the features of URL class.
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
public class Sample {
public static void main(String[] args) throws UnsupportedEncodingException,
URISyntaxException, MalformedURLException {
String uri1 = "http://example.com/foo?key=val-with-a-{-in-it";
String scheme=uri1.split(":")[0];
String authority=uri1.split("//")[1].split("/")[0];
String path=uri1.split("//")[1].split("/")[1].split("\\?")[0];
String query=uri1.split("\\?")[1];
URI uri = null;
uri = new URI(scheme, authority, "/"+path, query,null);
URL url = null;
url = uri.toURL();
System.out.println("URI's Query:"+uri.getQuery());
System.out.println("URL's Query:"+url.getQuery());
}
}

Cannot access azure blobs through rest api

I was able to create a Container in Storage Account and upload a blob to it through the Client Side Code.
I was able to make the blob available for Public access as well , such that when I hit the following query from my browser, I am able to see the image which I uploaded.
https://MYACCOUNT.blob.core.windows.net/MYCONTAINER/MYBLOB
I now have a requirement to use the rest service to retrieve the contents of the blob. I wrote down the following java code.
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class GetBlob {
public static void main(String[] args) {
String url="https://MYACCOUNT.blob.core.windows.net/MYCONTAINER/MYBLOB";
try {
System.out.println("RUNNIGN");
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("Authorization", createQuery());
connection.setRequestProperty("x-ms-version", "2009-09-19");
InputStream response = connection.getInputStream();
System.out.println("SUCCESSS");
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static String createQuery()
{
String dateFormat="EEE, dd MMM yyyy hh:mm:ss zzz";
SimpleDateFormat dateFormatGmt = new SimpleDateFormat(dateFormat);
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));
String date=dateFormatGmt.format(new Date());
String Signature="GET\n\n\n\n\n\n\n\n\n\n\n\n" +
"x-ms-date:" +date+
"\nx-ms-version:2009-09-19" ;
// I do not know CANOCALIZED RESOURCE
//WHAT ARE THEY??
// +"\n/myaccount/myaccount/mycontainer\ncomp:metadata\nrestype:container\ntimeout:20";
String SharedKey="SharedKey";
String AccountName="MYACCOUNT";
String encryptedSignature=(encrypt(Signature));
String auth=""+SharedKey+" "+AccountName+":"+encryptedSignature;
return auth;
}
public static String encrypt(String clearTextPassword) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(clearTextPassword.getBytes());
return new sun.misc.BASE64Encoder().encode(md.digest());
} catch (NoSuchAlgorithmException e) {
}
return "";
}
}
However , I get the following error when I run this main class...
RUNNIGN
java.io.IOException: Server returned HTTP response code: 403 for URL: https://klabs.blob.core.windows.net/delete/Blob_1
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at main.MainClass.main(MainClass.java:61)
Question1: Why this error, did I miss any header/parameter?
Question2: Do I need to add headers in the first place, because I am able to hit the request from the browser without any issues.
Question3: Can it be an SSL issue? What is the concept of certificates, and how and where to add them? Do I really need them? Will I need them later, when I do bigger operations on my blob storage(I want to manage a thousand blobs)?
Will be thankful for any reference as well, within Azure and otherwise that could help me understand better.
:D
AFTER A FEW DAYS
Below is my new code for PutBlob I azure. I believe I have fully resolved all header and parameter issues and my request is perfect. However I am still getting the same 403. I do not know what the issue is. Azure is proving to be pretty difficult.
A thing to note is that the containers name is delete, and I want to create a blob inside it, say newBlob. I tried to initialize the urlPath in the code below with both "delete" and "delete/newBlob".
Does not work..
package main;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
public class Internet {
static String key="password";
static String account="klabs";
private static Base64 base64 ;
private static String createAuthorizationHeader(String canonicalizedString) throws InvalidKeyException, Base64DecodingException, NoSuchAlgorithmException, IllegalStateException, UnsupportedEncodingException {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(base64.decode(key), "HmacSHA256"));
String authKey = new String(base64.encode(mac.doFinal(canonicalizedString.getBytes("UTF-8"))));
String authStr = "SharedKey " + account + ":" + authKey;
return authStr;
}
public static void main(String[] args) {
System.out.println("INTERNET");
String key="password";
String account="klabs";
long blobLength="Dipanshu Verma wrote this".getBytes().length;
File f = new File("C:\\Users\\Dipanshu\\Desktop\\abc.txt");
String requestMethod = "PUT";
String urlPath = "delete";
String storageServiceVersion = "2009-09-19";
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:sss");
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = fmt.format(Calendar.getInstance().getTime()) + " UTC";
String blobType = "BlockBlob";
String canonicalizedHeaders = "x-ms-blob-type:"+blobType+"\nx-ms-date:"+date+"\nx-ms-version:"+storageServiceVersion;
String canonicalizedResource = "/"+account+"/"+urlPath;
String stringToSign = requestMethod+"\n\n\n"+blobLength+"\n\n\n\n\n\n\n\n\n"+canonicalizedHeaders+"\n"+canonicalizedResource;
try {
String authorizationHeader = createAuthorizationHeader(stringToSign);
URL myUrl = new URL("https://klabs.blob.core.windows.net/" + urlPath);
HttpURLConnection connection=(HttpURLConnection)myUrl.openConnection();
connection.setRequestProperty("x-ms-blob-type", blobType);
connection.setRequestProperty("Content-Length", String.valueOf(blobLength));
connection.setRequestProperty("x-ms-date", date);
connection.setRequestProperty("x-ms-version", storageServiceVersion);
connection.setRequestProperty("Authorization", authorizationHeader);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
System.out.println(String.valueOf(blobLength));
System.out.println(date);
System.out.println(storageServiceVersion);
System.out.println(stringToSign);
System.out.println(authorizationHeader);
System.out.println(connection.getDoOutput());
DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
// Send request
outStream.writeBytes("Dipanshu Verma wrote this");
outStream.flush();
outStream.close();
DataInputStream inStream = new DataInputStream(connection.getInputStream());
System.out.println("BULLA");
String buffer;
while((buffer = inStream.readLine()) != null) {
System.out.println(buffer);
}
// Close I/O streams
inStream.close();
outStream.close();
} catch (InvalidKeyException | Base64DecodingException | NoSuchAlgorithmException | IllegalStateException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I know only a proper code reviewer might be able to help me, please do it if you can.
Thanks
Question1: Why this error, did I miss any header/parameter?
Most likely you're getting this error is because of incorrect signature. Please refer to MSDN documentation for creating correct signature: http://msdn.microsoft.com/en-us/library/azure/dd179428.aspx. Unless your signature is correct you'll not be able to perform operations using REST API.
Question2: Do I need to add headers in the first place, because I am
able to hit the request from the browser without any issues.
In your current scenario, because you can access the blob directly (which in turn means the container in which the blob exist has Public or Blob ACL) you don't really need to use REST API. You can simply make a HTTP request using Java and read the response stream which will have blob contents. You would need to go down this route if the container ACL is Private because in this case your requests need to be authenticated and the code above creates an authenticated request.
Question3: Can it be an SSL issue? What is the concept of
certificates, and how and where to add them? Do I really need them?
Will I need them later, when I do bigger operations on my blob
storage(I want to manage a thousand blobs)?
No, it is not an SSL issue. Its an issue with incorrect signature.
Finally found the mistake!!
In the code above , I was using a String "password" as key for my SHA2
base64.decode(key)
It should have been the key associated with my account with AZURE.
Silly One!! Took me 2 weeks to find.

Creating a directory wherever jar file is located through java

I have already surveyed SO for an answer, and could not find an appropriate one.
When I launch my program from a jar I need to create a folder in the directory where the jar file is located. It should not matter where the user saves the jar file.
Here is the newest code I was playing with: A System.out.println will print out the correct directory but the folder will not be created. In contrast,everything is being saved to my System32 folder as of now.
public static String getProgramPath() throws IOException{
String currentdir = System.getProperty("user.dir");
currentdir = currentdir.replace( "\\", "/" );
return currentdir;
}
File dir = new File(getProgramPath() + "Comics/");//The name of the directory to create
dir.mkdir();//Creates the directory
To get a Jar's path can be a little trickier than simply getting the user.dir directory. I can't remember the details why, but user.dir does not return this path reliably in all situations. If you absolutely must get the jar's path, then you need to do a little black magic and first get the class's protectionDomain. Something like:
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import javax.swing.JOptionPane;
public class MkDirForMe {
public static void main(String[] args) {
try {
String path = getProgramPath2();
String fileSeparator = System.getProperty("file.separator");
String newDir = path + fileSeparator + "newDir2" + fileSeparator;
JOptionPane.showMessageDialog(null, newDir);
File file = new File(newDir);
file.mkdir();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProgramPath2() throws UnsupportedEncodingException {
URL url = MkDirForMe.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = URLDecoder.decode(url.getFile(), "UTF-8");
String parentPath = new File(jarPath).getParentFile().getPath();
return parentPath;
}
}
Even this isn't guaranteed to work, and you'll have to resign yourself to the fact that there are just some times (for instance for security reasons) when you won't be able to get a Jar's path.
With some changes (such as adding a "/" before Comics), I managed to create the directory where you expected it to. Here is the full code I used.
import java.io.*;
public class TestClass {
public static String getProgramPath() throws IOException{
String currentdir = System.getProperty("user.dir");
currentdir = currentdir.replace( "\\", "/" );
return currentdir;
}
public static void main(String[] argv) {
try {
String d = getProgramPath() + "/Comics/";
System.out.println("Making directory at " + d);
File dir = new File(d);//The name of the directory to create
dir.mkdir();//Creates the directory
}
catch (Exception e) { System.out.println("Exception occured" + e);}
}
}
In the future, please don't hard code things like "/" and such. Use built-in libraries which will ask the OS what is right in this case. This ensures the functionality doesn't break (as easily) cross platform.
Of course, catch the exception properly etc. This is just quick and dirty attempt to mold your code into something that works.

incompatible type and can not find symbol in twitter4j?

this is what is the code and i get the problem in marked lines(BOLD), i think it is because of the jar version but i am not sure about this. if this is because of jar version please do let me know the right one.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
public class NamexTweet {
private final static String CONSUMER_KEY = "xxxxxxxxxxxxx";
private final static String CONSUMER_KEY_SECRET = "yyyyyyyyyyyyyyy";
public void start() throws TwitterException, IOException {
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
**RequestToken requestToken = twitter.getOAuthRequestToken();**
System.out.println("Authorization URL: \n"
+ requestToken.getAuthorizationURL());
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (null == accessToken) {
try {
System.out.print("Input PIN here: ");
String pin = br.readLine();
**accessToken = twitter.getOAuthAccessToken(requestToken, pin);**
} catch (TwitterException te) {
System.out.println("Failed to get access token, caused by: "
+ te.getMessage());
System.out.println("Retry input PIN");
}
}
System.out.println("Access Token: " + accessToken.getToken());
System.out.println("Access Token Secret: "
+ accessToken.getTokenSecret());
twitter.updateStatus("hi.. im updating this using Namex Tweet for Demo");
}
public static void main(String[] args) throws Exception {
new NamexTweet().start();// run the Twitter client
}
}
Make sure the jar is actually in the build path (if I knew your IDE I might have given more concrete instructions).
If this doesn't solve the problem, search these classes and methods in that jar. If they're there - try to perform step 1 above better... If it's not there - then you have the wrong jar.
twitter4j 2.2.4 is the reliable version, which can be used.
You have to attach following libraries:
twitter4j-core-4.0.4.jar
twitter4j-stream-4.0.4.jar

Categories