You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Sebastiaan van Erk <se...@sebster.com> on 2008/02/24 18:00:08 UTC

Small shared resource question...

Hi,

I register some DynamicWebResources in my Application's init method, as 
well as mount them (in a loop, a different one for every locale):

	getSharedResources().add(name, resource);
	mountSharedResource("/downloads/" + name, new 
ResourceReference(name).getSharedResourceKey());

I was wondering if there was another way to get the shared resource key 
without having to construct a throwaway resource reference.

Futherhmore, in a web page I want to link to the shared resources, i.e, 
I thought of using a ResourceLink. However, I was wondering how I can 
make sure that the resource being linked to is the correct (localized) 
resource. (They're generated PDF's, one in English, one in Dutch, and I 
want the link to point to the correct one depending on the locale). My 
currentsolution is just to have 2 links and override isVisible() on each 
of them to check the locale, but I don't know if that's the "correct" 
way to go about it.

Regards,
Sebastiaan

Re: Small shared resource question...

Posted by Kent Tong <ke...@cpttm.org.mo>.
Try:

public class P1 extends WebPage {
	private ResourceReference resourceRef;

	static {
		WebApplication app = (WebApplication) Application.get();
		mount(app, Locale.ENGLISH);
		mount(app, Locale.CHINESE);
	}

	private static void mount(WebApplication app, Locale locale) {
		ResourceReference resourceRef = makeResourceReference();
		resourceRef.setLocale(locale);
		app.mountSharedResource("/download/cvs_" + locale + ".pdf", resourceRef
				.getSharedResourceKey());
	}

	@Override
	protected void onBeforeRender() {
		super.onBeforeRender();
		resourceRef.setLocale(getLocale());
	}

	public P1() {
		resourceRef = makeResourceReference();
		ResourceLink link = new ResourceLink("link", resourceRef);
		add(link);
		Link changeLocale = new Link("changeLocale") {
			private int n = 0;

			@Override
			public void onClick() {
				n++;
				Locale newLocale = (n % 2 == 0) ? Locale.ENGLISH
						: Locale.CHINESE;
				getSession().setLocale(newLocale);
			}
		};
		add(changeLocale);
	}

	private static ResourceReference makeResourceReference() {
		return new ResourceReference("cvs.pdf") {
			@Override
			protected Resource newResource() {
				return new PDFResource(getLocale());
			}
		};
	}

}
public class PDFResource extends DynamicWebResource {

	public PDFResource(Locale locale) {
		super(locale);
	}

	@Override
	protected ResourceState getResourceState() {
		ResourceState state = new ResourceState() {
		
			@Override
			public byte[] getData() {
				return readPDF();
			}
		
			@Override
			public String getContentType() {
				return "application/pdf";
			}
		
		};
		return state;
	}

	protected byte[] readPDF() {
		try {
			File f = new File("c:/tmp/cvs_"+getLocale()+".pdf");
			FileInputStream s = new FileInputStream(f);
			byte[] content = new byte[(int) f.length()];
			s.read(content);
			s.close();
			return content;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}


-----
--
Kent Tong
Wicket tutorials freely available at http://www.agileskills2.org/EWDW
Axis2 tutorials freely available at http://www.agileskills2.org/DWSAA
-- 
View this message in context: http://www.nabble.com/Re%3A-Small-shared-resource-question...-tp15663443p15737491.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Small shared resource question...

Posted by Sebastiaan van Erk <se...@sebster.com>.

Kent Tong wrote:
> 
> Sebastiaan van Erk wrote:
>> As far as I can tell, there's still only 1 resource, which has the 
>> locale of the page (at creation time).
>>
> 
> Every time the page is rendered, it will generate a different variant of
> the resource reference due to the setLocale() call.
> 
> 
> Sebastiaan van Erk wrote:
>> But I have a link on the page which allows you to change the locale. 
>> This link is stateful, so it just calls the callback on the current page 
>> and sets the locale.
>>
> 
> No problem. If the link is a normal link (not ajax), you'll render the
> page again and thus will have a new resource reference. If it's
> ajax, you need to put the setLocale() call into a callback and
> refresh the link.
> 

Your code was:

public class P1 extends WebPage {
     public P1() {
         ResourceReference resourceRef = new ResourceReference("cvs.pdf") {
             protected Resource newResource() {
                 return new PDFResource(getLocale());
             }
         };
         resourceRef.setLocale(getLocale()); //THIS IS THE LINE
         ResourceLink link = new ResourceLink("link", resourceRef);
         add(link);
     }
}

As far as I can see, the line marked "//THIS IS THE LINE" is only called 
in the constructor, which means it's called every time the page is 
*constructed* and not every time the page is *rendered*.

You're right that I could do the setLocale() on the reference manually 
in the callback (I'd have to do that for a non-ajax locale link as 
well). However, I don't really like that solution either, because my 
switch locale links are on a ChangeLocalePanel which knows nothing about 
my page or the resource links on that page (and shouldn't as far as I'm 
concerned).

The last problem, and the problem which might actually require me to 
have 2 different resources is that I want the shared resources for 
different locales to be mounted on *different* locations, 
"/dowloads/resourceX_localeY.pdf". I've tried variations on the code 
above, but I couldn't get it working (for one, the ResourceReference 
class seems to require that it's string argument is the name of the 
shared resource, and thus I can't have 2 different mount paths which 
always work regardless of the locale you're in: i.e., if you're in the 
Dutch locale and you request /downloads/cvs_en.pdf I want you to get the 
English version).

Regards,
Sebastiaan

> -----
> --
> Kent Tong
> Wicket tutorials freely available at http://www.agileskills2.org/EWDW
> Axis2 tutorials freely available at http://www.agileskills2.org/DWSAA

Re: Small shared resource question...

Posted by Kent Tong <ke...@cpttm.org.mo>.

Sebastiaan van Erk wrote:
> 
> As far as I can tell, there's still only 1 resource, which has the 
> locale of the page (at creation time).
> 

Every time the page is rendered, it will generate a different variant of
the resource reference due to the setLocale() call.


Sebastiaan van Erk wrote:
> 
> But I have a link on the page which allows you to change the locale. 
> This link is stateful, so it just calls the callback on the current page 
> and sets the locale.
> 

No problem. If the link is a normal link (not ajax), you'll render the
page again and thus will have a new resource reference. If it's
ajax, you need to put the setLocale() call into a callback and
refresh the link.


-----
--
Kent Tong
Wicket tutorials freely available at http://www.agileskills2.org/EWDW
Axis2 tutorials freely available at http://www.agileskills2.org/DWSAA
-- 
View this message in context: http://www.nabble.com/Re%3A-Small-shared-resource-question...-tp15663443p15727184.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Small shared resource question...

Posted by Sebastiaan van Erk <se...@sebster.com>.
Hi,

Thanks for the answer.

I don't quite understand it though!

As far as I can tell, there's still only 1 resource, which has the 
locale of the page (at creation time).

But I have a link on the page which allows you to change the locale. 
This link is stateful, so it just calls the callback on the current page 
and sets the locale.

Since one ResourceReference doesn't seem to have more than one (version 
of a) Resource associated with it, I don't see how the code below is 
going to help me...

Thus my situation is:

I have 2 versions of a resource, an english generated pdf and a dutch 
one. They represent the same content, but in different languages. I want 
to mount them respectively on /downloads/cvs_en.pdf and 
/downloads/cvs_nl.pdf (in general, /downloads/resourcename_locale.pdf).
And I want a resource link in the page which links to the proper one of 
the two depending on the current locale...

Currently I just have multiple links, one to each (version of the) 
resource, with only one link visible at a time, but that feels like a 
hack to me.

Regards,
Sebastiaan

Kent Tong wrote:
> 
> Sebastiaan van Erk wrote:
>> In my web page with the resource link I do the following:
>>
>> 	fragment.add(new ResourceLink("cvEnglishLink", new 
>> ResourceReference("cvs_en.pdf")) {
>> 	@Override
>> 		public boolean isVisible() {
>> 			return "en".equals(getLocale().getLanguage());
>> 		}
>> 	});
>>
> 
> Try:
> 
> public class P1 extends WebPage {
> 	public P1() {
> 		ResourceReference resourceRef = new ResourceReference("cvs.pdf") {
> 			protected Resource newResource() {
> 				return new PDFResource(getLocale());
> 			}
> 		};
> 		resourceRef.setLocale(getLocale()); //THIS IS THE LINE
> 		ResourceLink link = new ResourceLink("link", resourceRef);
> 		add(link);
> 	}
> }
> 
> 
> 
> -----
> --
> Kent Tong
> Wicket tutorials freely available at http://www.agileskills2.org/EWDW
> Axis2 tutorials freely available at http://www.agileskills2.org/DWSAA

Re: Small shared resource question...

Posted by Kent Tong <ke...@cpttm.org.mo>.

Sebastiaan van Erk wrote:
> 
> In my web page with the resource link I do the following:
> 
> 	fragment.add(new ResourceLink("cvEnglishLink", new 
> ResourceReference("cvs_en.pdf")) {
> 	@Override
> 		public boolean isVisible() {
> 			return "en".equals(getLocale().getLanguage());
> 		}
> 	});
> 

Try:

public class P1 extends WebPage {
	public P1() {
		ResourceReference resourceRef = new ResourceReference("cvs.pdf") {
			protected Resource newResource() {
				return new PDFResource(getLocale());
			}
		};
		resourceRef.setLocale(getLocale()); //THIS IS THE LINE
		ResourceLink link = new ResourceLink("link", resourceRef);
		add(link);
	}
}



-----
--
Kent Tong
Wicket tutorials freely available at http://www.agileskills2.org/EWDW
Axis2 tutorials freely available at http://www.agileskills2.org/DWSAA
-- 
View this message in context: http://www.nabble.com/Re%3A-Small-shared-resource-question...-tp15663443p15714682.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Small shared resource question...

Posted by Sebastiaan van Erk <se...@sebster.com>.
Hi,

The resource key for a shared resource is generated by the following 
method which I'm not allowed to call.

	/**
	 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT.
	 *
	 * @param scope
	 *            The scope of the resource
	 * @param path
	 *            The resource path
	 * @param locale
	 *            The locale
	 * @param style
	 *            The style (see {@link org.apache.wicket.Session})
	 * @return The localized path
	 */
	public String resourceKey(final Class scope, final String path, final 
Locale locale,
			final String style)
	{
		String alias = (String)classAliasMap.get(scope);
		if (alias == null)
		{
			alias = scope.getName();
		}
		return alias + '/' + resourceKey(path, locale, style);
	}

I add the resource using the following method:

	/**
	 * Adds a resource.
	 *
	 * @param name
	 *            Logical name of resource
	 * @param resource
	 *            Resource to store
	 */
	public final void add(final String name, final Resource resource)
	{
		add(Application.class, name, null, null, resource);
	}

Which decides for me that the scope is Application.class. Now all this 
is fine, except I don't really like the fact that I don't get the 
resource key and have to make a new throwaway ResourceReference to get 
at it.

I was wondering, could add not just return the resource key of the added 
resource?

Regards,
Sebastiaan


Sebastiaan van Erk wrote:
> Johan Compagner wrote:
>> Isnt the getResourceKey() returning exactly the name? It should
>> because else the first call to shared resources with just the name
>> doesnt make much sense! Those should be the same.
> 
> Well I tried using the name first and it kept complaining that it 
> couldn't find the resource, so I just tried this ResourceReference thing 
> not expecting it to work but it did. I added a log line to the 
> application.init() and it says:
> 
> mounted resource: name=cvs_nl.pdf 
> key=org.apache.wicket.Application/cvs_nl.pdf
> 
> Thus the name and key are *not* the same, and mounting by name did not 
> work.
> 
>> Also doesnt ResourceLink uses LocalizedImageResource? That one takes
>> care of that. But i guess it needs a resource per locale also in the
>> shared resources. So that the shared resource key is there for all the
>> locales.
> 
> In my web page with the resource link I do the following:
> 
>     fragment.add(new ResourceLink("cvEnglishLink", new 
> ResourceReference("cvs_en.pdf")) {
>     @Override
>         public boolean isVisible() {
>             return "en".equals(getLocale().getLanguage());
>         }
>     });
> 
> It's a bit of a dirty hack, but it works. As you can see, I make a 
> ResourceLink with a ResourceReference. I don't even know *how* to make 
> the referenced resource different for two locales. The names of the 
> resource are just "cvs_" + language + ".pdf". I don't see that 
> ResourceLink uses LocalizedImageResource anywhere. There is this 
> somewhat cryptic comment though in ResourceLink:
> 
>     protected final CharSequence getURL()
>     {
>         if (resourceReference != null)
>         {
>             // TODO post 1.2: should we have support for locale changes 
> when the
>             // resource reference (or resource??) is set manually..
>             // We should get a new resource reference for the current 
> locale
>             // then
>             // that points to the same resource but with another locale 
> if it
>             // exists.
>             // something like
>             // 
> SharedResource.getResourceReferenceForLocale(resourceReference);
> 
>             resourceReference.bind(getApplication());
>             return getRequestCycle().urlFor(resourceReference, 
> resourceParameters);
>         }
>         return urlFor(IResourceListener.INTERFACE);
>     }
> 
> It seems that I need to register a resource with the same name for 
> different locales? But can I still mount them on different paths then? 
> And obviously, what the comment alludes to would need to be implemented 
> of course. :-)
> 
> So what I would want to do is mount the English resource on 
> "/downloads/cvs_en.pdf", the Dutch resource on "/downloads/cvs_nl.pdf" 
> and have a resource link on my page which links to the proper one 
> depending on the locale.
> 
> Regards,
> Sebastiaan
> 
> 
>>
>> On 2/24/08, Sebastiaan van Erk <se...@sebster.com> wrote:
>>> Hi,
>>>
>>> I register some DynamicWebResources in my Application's init method, as
>>> well as mount them (in a loop, a different one for every locale):
>>>
>>>     getSharedResources().add(name, resource);
>>>     mountSharedResource("/downloads/" + name, new
>>> ResourceReference(name).getSharedResourceKey());
>>>
>>> I was wondering if there was another way to get the shared resource key
>>> without having to construct a throwaway resource reference.
>>>
>>> Futherhmore, in a web page I want to link to the shared resources, i.e,
>>> I thought of using a ResourceLink. However, I was wondering how I can
>>> make sure that the resource being linked to is the correct (localized)
>>> resource. (They're generated PDF's, one in English, one in Dutch, and I
>>> want the link to point to the correct one depending on the locale). My
>>> currentsolution is just to have 2 links and override isVisible() on each
>>> of them to check the locale, but I don't know if that's the "correct"
>>> way to go about it.
>>>
>>> Regards,
>>> Sebastiaan
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>

Re: Small shared resource question...

Posted by Sebastiaan van Erk <se...@sebster.com>.
Johan Compagner wrote:
> Isnt the getResourceKey() returning exactly the name? It should
> because else the first call to shared resources with just the name
> doesnt make much sense! Those should be the same.

Well I tried using the name first and it kept complaining that it 
couldn't find the resource, so I just tried this ResourceReference thing 
not expecting it to work but it did. I added a log line to the 
application.init() and it says:

mounted resource: name=cvs_nl.pdf 
key=org.apache.wicket.Application/cvs_nl.pdf

Thus the name and key are *not* the same, and mounting by name did not work.

> Also doesnt ResourceLink uses LocalizedImageResource? That one takes
> care of that. But i guess it needs a resource per locale also in the
> shared resources. So that the shared resource key is there for all the
> locales.

In my web page with the resource link I do the following:

	fragment.add(new ResourceLink("cvEnglishLink", new 
ResourceReference("cvs_en.pdf")) {
	@Override
		public boolean isVisible() {
			return "en".equals(getLocale().getLanguage());
		}
	});

It's a bit of a dirty hack, but it works. As you can see, I make a 
ResourceLink with a ResourceReference. I don't even know *how* to make 
the referenced resource different for two locales. The names of the 
resource are just "cvs_" + language + ".pdf". I don't see that 
ResourceLink uses LocalizedImageResource anywhere. There is this 
somewhat cryptic comment though in ResourceLink:

	protected final CharSequence getURL()
	{
		if (resourceReference != null)
		{
			// TODO post 1.2: should we have support for locale changes when the
			// resource reference (or resource??) is set manually..
			// We should get a new resource reference for the current locale
			// then
			// that points to the same resource but with another locale if it
			// exists.
			// something like
			// SharedResource.getResourceReferenceForLocale(resourceReference);

			resourceReference.bind(getApplication());
			return getRequestCycle().urlFor(resourceReference, resourceParameters);
		}
		return urlFor(IResourceListener.INTERFACE);
	}

It seems that I need to register a resource with the same name for 
different locales? But can I still mount them on different paths then? 
And obviously, what the comment alludes to would need to be implemented 
of course. :-)

So what I would want to do is mount the English resource on 
"/downloads/cvs_en.pdf", the Dutch resource on "/downloads/cvs_nl.pdf" 
and have a resource link on my page which links to the proper one 
depending on the locale.

Regards,
Sebastiaan


> 
> On 2/24/08, Sebastiaan van Erk <se...@sebster.com> wrote:
>> Hi,
>>
>> I register some DynamicWebResources in my Application's init method, as
>> well as mount them (in a loop, a different one for every locale):
>>
>> 	getSharedResources().add(name, resource);
>> 	mountSharedResource("/downloads/" + name, new
>> ResourceReference(name).getSharedResourceKey());
>>
>> I was wondering if there was another way to get the shared resource key
>> without having to construct a throwaway resource reference.
>>
>> Futherhmore, in a web page I want to link to the shared resources, i.e,
>> I thought of using a ResourceLink. However, I was wondering how I can
>> make sure that the resource being linked to is the correct (localized)
>> resource. (They're generated PDF's, one in English, one in Dutch, and I
>> want the link to point to the correct one depending on the locale). My
>> currentsolution is just to have 2 links and override isVisible() on each
>> of them to check the locale, but I don't know if that's the "correct"
>> way to go about it.
>>
>> Regards,
>> Sebastiaan
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 

Re: Small shared resource question...

Posted by Johan Compagner <jc...@gmail.com>.
Isnt the getResourceKey() returning exactly the name? It should
because else the first call to shared resources with just the name
doesnt make much sense! Those should be the same.

Also doesnt ResourceLink uses LocalizedImageResource? That one takes
care of that. But i guess it needs a resource per locale also in the
shared resources. So that the shared resource key is there for all the
locales.

On 2/24/08, Sebastiaan van Erk <se...@sebster.com> wrote:
> Hi,
>
> I register some DynamicWebResources in my Application's init method, as
> well as mount them (in a loop, a different one for every locale):
>
> 	getSharedResources().add(name, resource);
> 	mountSharedResource("/downloads/" + name, new
> ResourceReference(name).getSharedResourceKey());
>
> I was wondering if there was another way to get the shared resource key
> without having to construct a throwaway resource reference.
>
> Futherhmore, in a web page I want to link to the shared resources, i.e,
> I thought of using a ResourceLink. However, I was wondering how I can
> make sure that the resource being linked to is the correct (localized)
> resource. (They're generated PDF's, one in English, one in Dutch, and I
> want the link to point to the correct one depending on the locale). My
> currentsolution is just to have 2 links and override isVisible() on each
> of them to check the locale, but I don't know if that's the "correct"
> way to go about it.
>
> Regards,
> Sebastiaan
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org