views:

42

answers:

2

I have long text that identifies few things in my application

For example my code: U2Cd3c7a781856c69559539a78e9492e9772dfe1b67.2.nrg

As I am sharing this key in public, it is bit long and I would like to make short by transforming just like shorturl so that is shorter in public and internally i would like to map this long text as it includes few information such as encrypted record id, user id and etc..

I am looking for a java code that does above, I never mind using my database to store in case a short code generator needs database.

Thank you Rams

+2  A: 

You will have to store in a database, and it should be as simple as adding the file name to a table with an autoincrement ID column, and using the ID column to build the URL. Make sure to put a cache in there somewhere. You don't want to hit the database every time you need to render a link.

Marcelo Cantos
A: 

Marcelo's answer is good if the links are of temporary nature. If the links are long-lived, I'd add another column that used a short but dense randomly generated key (such as a 10-digit base 36 number A-Z0-9) and use that for the URL. The reason is that if you needed to do any kind of table maintenance (such as merging test and QA data, for example), you could do so without worrying too much about conflicts resulting from the same autokey value referring to two different URLs.

Where I worked previously, they thought nothing about hard-coding PK values for status and code tables. This meant that these tables in prod, QA, Test, and Dev had to be identical to the PK. What a pain!

Thus I don't like to give my PKs to users...

Tony Ennis