Custom protocol handler URL source

Hi All,

I have an interesting challenge. We have report documents of different formats (html, pdf, rtf etc). These reports should contain hyperlinks to external files, which open in our application. These files are distributed along with the report, in the same location as the report.

I have created a protocol handler (by defining the appropriate keys in the Windows registry editor) that passes a URL onto our application, like "ourprotocol://C:/absolute/path/to/file.ext". This works fine.

However, our reports and associated files need to be sent to other machines and should open in the same way when the hyperlink is clicked in the report, so the hyperlinks should be defined with relative paths like: "ourprotocol://relative/path/to/file.ext". The problem is that our application's working directory is the location it is installed to and subsequently it cannot find the file specified in the URL.

Is there a way to pass the source document of the URL event to our application as well? For example, if our HTML report is open then I would like to know the location of that HTML in order to calculate where the additional files are.

Our application is Java based, so any suggestions or solutions available in Java would be appreciated.
LVL 1
Stephen ChamberlainSenior Software Engineer/System DesignerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>The problem is that our application's working directory is the location it is installed to and subsequently it cannot find the file specified in the URL

Why would that be a problem if you're using relative urls?
0
objectsCommented:
>  so the hyperlinks should be defined with relative paths like: "ourprotocol://relative/path/to/file.ext".

thats actually an absolute url. if you want to use relative urls then just specify the path.
Then append that path to a base url to get the absolute location

0
Stephen ChamberlainSenior Software Engineer/System DesignerAuthor Commented:
Thanks for the response! Allow me to reply.

CEHJ:

">>The problem is that our application's working directory is the location it is installed to and subsequently it cannot find the file specified in the URL

Why would that be a problem if you're using relative urls?"

The problem is that the main file to open could be on the desktop (for example C:\Documents and Settings\Home\Desktop). If the application is installed in C:\Program Files\Our Company\Our App and it gets a relative path of "ourFile.ext", it does not know that that file exists on the desktop.

objects:

"thats actually an absolute url. if you want to use relative urls then just specify the path.
Then append that path to a base url to get the absolute location"

How should this base URL be defined? Where should it be defined?

After some testing, it seems that a URL such as "ourprotocol://relative/path/to/file.ext" will work under certain circumstances. For example if the link is activated from an RTF version of our report in Word 2007, our application sometimes starts with the report file's parent directory as the working directory. The same test with PDF in Adobe Reader however produces different results, with the working directory being the desktop.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

CEHJCommented:
OK. I assumed if you're using relative urls, you were dealing with fixed reference points. That's the only context in which relative urls can be used.

In practice, with a java application file-based paths work relative to the directory from which the app is being run, the current directory. If you fix that (e.g. by specifying the working directory in the app's shortcut) then you'd be able to use relative urls.
0
CEHJCommented:
>>After some testing, it seems that a URL such as "ourprotocol://relative/path/to/file.ext" will work under certain circumstances.

It's actually *not* an absolute url. It's a url defining a relative path with the authority part of the URI. In a relative url, the authority is seldom seen but i don't know it's a mistake to include it. The following would make it absolute:

"ourprotocol:///xxxxx/path/to/file.ext"
0
Stephen ChamberlainSenior Software Engineer/System DesignerAuthor Commented:
Hi CEHJ,

Yeah, thats currently the problem we are having. If we can find a way that our application would always start in the directory where the report with the hyperlink is located, then relative paths would work. This sometimes works (for example with RTF from Word 2007) but sometimes it doesn't, so if there is a way to find out the URL source path in the open command in the protocol handler in the windows registry, it would work.

The handler defined something like this (taken from the example on MSDN http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx):

HKEY_CLASSES_ROOT
     alert
          (Default) = "URL:Alert Protocol"
          URL Protocol = ""
          DefaultIcon
               (Default) = "alert.exe"
          shell
               open
                    command
                         (Default) = "C:\Program Files\Alert\alert.exe" "%1"

The "C:\Program Files\Alert\alert.exe" "%1" line specifies that alert.exe should open with the URI as a commandline parameter. It is this point at which the current directory should be set somehow to the report's parent folder.
0
CEHJCommented:
Well in Windows you can set the current directory through, and in, the shortcut
0
Stephen ChamberlainSenior Software Engineer/System DesignerAuthor Commented:
How would that be defined in a protocol handler though? If the hyperlink is activated in a PDF document located in "C:\Documents and Settings\Home\Desktop\report.pdf", then I want the working directory of my java application to be "C:\Documents and Settings\Home\Desktop".
0
CEHJCommented:
>>How would that be defined in a protocol handler though?

Well it's not really a matter of defining it *in* a protocol handler. That's simply what the working directory would be as far as relativity is concerned
0
objectsCommented:
> How should this base URL be defined? Where should it be defined?

thats up to you

> After some testing, it seems that a URL such as "ourprotocol://relative/path/to/file.ext" will work under certain circumstances.

sounds like your handler is using the path relative to some location already

> How would that be defined in a protocol handler though?

you're already doing it my the sound of it. Check how your handler is building the path
If its using the path as if it a relative path then you need to change it (see below)

>hen I want the working directory of my java application to be "C:\Documents and Settings\Home\Desktop".

don't use the working directory, it could be anywhere

0
Stephen ChamberlainSenior Software Engineer/System DesignerAuthor Commented:
OK, what I am doing at the moment is using a relative location in my URL. My viewer application then uses the recent items list from Windows to find out what the last file opened was (should be the report). The parent folder of this reference file is then used to attempt to find the archive file. This works, but I would prefer it to be a back up method as the user can configure the recent items list to not store references to recent files if they so choose.

I am investigating Adobe Reader JavaScript to update my URLs dynamically from the reader application itself, but if this even works it will be an Adobe/PDF only solution, so the points are still up for grabs if someone has solved this before!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
Why can't you use absolute url's?  Using relative url's when the application using them doesn't know what they are relative to would seem error prone

0
Stephen ChamberlainSenior Software Engineer/System DesignerAuthor Commented:
Absolute URLs will only work if the resource they refer to is located on a server or something. This is a desktop application scenario. I have considered making a local web server which points to the localhost, but even so, if the user were to email the report and the external files to someone else (which is an extremely common use case) then the links would no longer work on their system, as we cannot control where they will save the files to.

It is acceptable that the external files must be in the same location as the main report and should not be renamed by the user, so the current solution works within these parameters.
0
objectsCommented:
If you use a standard location then they are really absolute urls :)'
Why can't the viewer application always know where reports are stored.
0
Stephen ChamberlainSenior Software Engineer/System DesignerAuthor Commented:
Thats the problem, there is no "standard" location for reports.

Take the following scenario:

1) I export a report in PDF form along with our external files to C:/Users/User/Desktop on my machine. The links (if absolute in the hyperlink URL) might look something like "C:/Users/User/Desktop/OurFile.ourExtension".
2) I then email a colleague the report and external files and he/she wants to open the report and navigate the links.
3) My colleague saves the report and external files to his Documents folder, "C:/User/OtherUser/Documents".
4) Upon clicking a hyperlink in the report, the viewer application is given the address "C:/Users/User/Desktop/OurFile.ourExtension", but the external file cannot be found there.

As you can see from this scenario, it is a common use case that people will save these reports to locations that are unknown at the point that the URL is created.
0
objectsCommented:
idwally you should be using relatives paths (not urls's) in the document and the application would combine the url of the report with the relativte path in the link.
Guess thats not possible in your case.

0
Stephen ChamberlainSenior Software Engineer/System DesignerAuthor Commented:
Unfortunately not. I already tried something like that, but in order for the link to work, Windows needs to recognise what the link means. I tried using a simple path without any other information in the link, but if your report is in Word and you click it, Word complains that it can't find the file (as Word will try to resolve the link itself, rather than pass it on to our application). I tried all kinds of combinations, for example using the file:// protocol, but at the end of the day most report viewing applications (be it Adobe Reader, Internet Explorer or Word) will attempt to resolve links (or Windows does it, I am not sure which unless you define your own protocol, in which case Windows will pass it to your application to deal with.

Thanks for all the suggestions though!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.