All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wicketstuff.select2.JsonResourceReference Maven / Gradle / Ivy

There is a newer version: 10.1.1
Show newest version
package org.wicketstuff.select2;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.request.resource.ResourceStreamResource;
import org.apache.wicket.util.resource.StringResourceStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * A resource that serves JSON for stateless . You have to mount at application init like. E.g.
 *
 * 

*

 * 	mountResource(CATEGORIES_JSON, new JsonResourceReference("categories") {
 *
 * 			private static final long serialVersionUID = 1L;
 *
 * 			protected ChoiceProvider getChoiceProvider() {
 * 				return CategoriesTextChoiceProvider.getInstance();
 * 			}
 * 		});
 *
 * 
*

* * and then on component you add * *

*

 *  	Select2MultiChoice c = new Select2MultiChoice("categories", new PropertyModel<Collection<CategoryTranslation>>(searchBean, "translations"), CategoriesTextChoiceProvider.getInstance());
 *  	c.getSettings().setStateless(true);
 *  	c.getSettings().setMountPath(Application.CATEGORIES_JSON);
 *  
*

* * @author Ernesto Reinaldo Barreiro ([email protected]). */ public abstract class JsonResourceReference extends ResourceReference { private static final long serialVersionUID = 1L; private static Logger logger = LoggerFactory.getLogger(JsonResourceReference.class); public JsonResourceReference(String name) { super(JsonResourceReference.class, name); } @Override public IResource getResource() { ByteArrayOutputStream webResponse = new ByteArrayOutputStream(); AbstractSelect2Choice.generateJSON(getChoiceProvider(), webResponse); StringResourceStream resourceStream; try { resourceStream = new StringResourceStream(webResponse.toString("UTF-8"), "application/json"); } catch (UnsupportedEncodingException e) { logger.error("getResource", e); throw new WicketRuntimeException(e); } return new ResourceStreamResource(resourceStream); } /** * The choice provider. * * @return The ChoiceProvider */ protected abstract ChoiceProvider getChoiceProvider(); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy