Solved

Custom protocol handler URL source

Posted on 2009-05-18
17
795 Views
Last Modified: 2012-05-07
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.
0
Comment
Question by:gobbo18uk
  • 7
  • 5
  • 5
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24415588
>>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
 
LVL 92

Expert Comment

by:objects
ID: 24416919
>  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
 
LVL 1

Author Comment

by:gobbo18uk
ID: 24424433
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24424514
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24424656
>>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
 
LVL 1

Author Comment

by:gobbo18uk
ID: 24424694
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24425004
Well in Windows you can set the current directory through, and in, the shortcut
0
 
LVL 1

Author Comment

by:gobbo18uk
ID: 24425058
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 40 total points
ID: 24425123
>>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
 
LVL 92

Expert Comment

by:objects
ID: 24427591
> 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
 
LVL 1

Accepted Solution

by:
gobbo18uk earned 0 total points
ID: 24465243
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
 
LVL 92

Expert Comment

by:objects
ID: 24465701
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
 
LVL 1

Author Comment

by:gobbo18uk
ID: 24467884
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
 
LVL 92

Expert Comment

by:objects
ID: 24469485
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
 
LVL 1

Author Comment

by:gobbo18uk
ID: 24474720
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
 
LVL 92

Assisted Solution

by:objects
objects earned 40 total points
ID: 24479501
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
 
LVL 1

Author Comment

by:gobbo18uk
ID: 24484602
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now