Hi Wolfgang,

On Thu, Apr 25, 2013 at 5:46 PM, Wolfgang <[email protected]> wrote:

> Hi heikki!
>
> I needed the same so I did following:
>
> extending AbstractResource
> implemented the needed:
>
> protected ResourceResponse newResourceResponse(Attributes attributes) {
>   ResourceResponse resourceResponse = new ResourceResponse();
>   final StringBuilder sb = new StringBuilder();
>   //do sb.append() here your json etc.
>   resourceResponse.**setWriteCallback(new WriteCallback() {
>             @Override
>             public void writeData(Attributes attributes) throws
> IOException {
>                 Response response = attributes.getResponse();
>                 response.write(sb.toString());
>             }
>         });
>
>         resourceResponse.**setContentType("application/**json");
>         return resourceResponse;
> }
>
> written a subclass of ResourceReference which takes a Class as constructor
> parameter.
> @Martin: why doesn't wicket hasn't something generic?
>

I guess because it is too simple.

Or maybe it is not ?! See below.


>
> public class GenericResourceReference extends ResourceReference {
>     private Class<? extends IResource> iResourceClass;
>

Problem 1) You keep a reference to a class!
- may lead to class loader leaks
- definitely a problem in OSGi environment


>
>     public GenericResourceReference(**Class<? extends IResource>
> iResourceClass) {
>         super(iResourceClass.getName()**);

        this.iResourceClass = iResourceClass;
>     }
>
>     @Override
>     public IResource getResource() {
>         try {
>             return iResourceClass.newInstance();
>

What if there is no default constructor ? Users will ask for a factory...
It is much easier to do:
mountResource("/my.json", new ResourceReference("someName") {
      @Override
    public IResource getResource() {
      return new MyResource(...);
    }
});

No usage of reflection. The application developer knows what constructor
arguments to pass, etc.

        } catch (InstantiationException e) {
>             throw new RuntimeException(e);
>         } catch (IllegalAccessException e) {
>             throw new RuntimeException(e);
>         }
>     }
>
> and mounted it in the WebApplication init() method like this:
> mountResource("/my.json", new GenericResourceReference(**
> MyResource.class));
>
> Greetings from sunny Austria.
> -Wolfgang
>
>
> On 04/25/2013 04:28 PM, heikki wrote:
>
>> thanks,
>>
>> that example does indeed work fine, but I'd rather have *no* markup file,
>> just generate the XML or JSON myself and send that back in the response.
>>
>> So I need a mounted IResource to do that ? Any tip how to go about it for
>> this use case, e.g. use a ByteArrayResource ?
>>
>>
>>
>>
>> --
>> View this message in context: http://apache-wicket.1842946.**
>> n4.nabble.com/Wicket-**returning-XML-or-JSON-**tp4658271p4658273.html<http://apache-wicket.1842946.n4.nabble.com/Wicket-returning-XML-or-JSON-tp4658271p4658273.html>
>> Sent from the Users forum mailing list archive at Nabble.com.
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: 
>> users-unsubscribe@wicket.**apache.org<[email protected]>
>> For additional commands, e-mail: [email protected]
>>
>>
>


-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>

Reply via email to