shared resources and refenences to it are for basic use pretty simple,
SharedResourece.add() adds a resource under a specific name, and then
you make a ResourceReference with that same name in your component
that has a src or href that wants to show the resource
On 9/17/07, Andrew Klochkov <[EMAIL PROTECTED]> wrote:
> Wow, Kent, thanks a lot for the code!!
>
> I wonder shouldn't something like this be in the framework core?
>
> Also I tried hard to find some docs about shared resources at all but
> didn't find any. The interface of the SharedResoruces class is quite
> confusing - almost all of its methods contain javadoc warning "THIS
> METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT.". Am I
> missing something?
>
>
> Kent Tong wrote:
> >
> > Andrew Klochkov wrote:
> >
> >> Hi!
> >>
> >> How to compress resources (css, java scripts) which are lying not in
> >> the classpath but in /css and /js in the webapp context folder? How can
> >> I create a CompressedResourceReference to such a resource?
> >>
> >>
> >
> > Try:
> >
> > public class MyApp extends WebApplication {
> > protected void init() {
> > getSharedResources().add(
> > "r1",
> > new CompressingWebResource(new
> > ContextRelativeResource(
> > "/css/f1.css")));
> > mountSharedResource("foo", "org.apache.wicket.Application/r1");
> > }
> > }
> >
> > public class CompressingWebResource extends WebResource {
> > private abstract class CompressingResourceStream implements
> IResourceStream
> > {
> > private static final long serialVersionUID = 1L;
> > /** Cache for compressed data */
> > private SoftReference cache = new SoftReference(null);
> > /** Timestamp of the cache */
> > private Time timeStamp = null;
> >
> > /**
> > * @see org.apache.wicket.util.resource.IResourceStream#close()
> > */
> > public void close() throws IOException {
> > }
> > /**
> > * @see
> > org.apache.wicket.util.resource.IResourceStream#getContentType()
> > */
> > public String getContentType() {
> > return getOriginalResourceStream().getContentType();
> > }
> > /**
> > * @see
> > org.apache.wicket.util.resource.IResourceStream#getInputStream()
> > */
> > public InputStream getInputStream()
> > throws ResourceStreamNotFoundException {
> > if (supportsCompression()) {
> > return new
> > ByteArrayInputStream(getCompressedContent());
> > } else {
> > return
> > getOriginalResourceStream().getInputStream();
> > }
> > }
> > /**
> > * @see
> > org.apache.wicket.util.resource.IResourceStream#getLocale()
> > */
> > public Locale getLocale() {
> > return getOriginalResourceStream().getLocale();
> > }
> > /**
> > * @see
> > org.apache.wicket.util.watch.IModifiable#lastModifiedTime()
> > */
> > public Time lastModifiedTime() {
> > return getOriginalResourceStream().lastModifiedTime();
> > }
> > /**
> > * @see org.apache.wicket.util.resource.IResourceStream#length()
> > */
> > public long length() {
> > if (supportsCompression()) {
> > return getCompressedContent().length;
> > } else {
> > return getOriginalResourceStream().length();
> > }
> > }
> > /**
> > * @see
> >
> org.apache.wicket.util.resource.IResourceStream#setLocale(java.util.Locale)
> > */
> > public void setLocale(Locale locale) {
> > getOriginalResourceStream().setLocale(locale);
> > }
> > /**
> > * @return compressed content
> > */
> > private byte[] getCompressedContent() {
> > IResourceStream stream = getOriginalResourceStream();
> > try {
> > byte ret[] = (byte[]) cache.get();
> > if (ret != null && timeStamp != null) {
> > if
> > (timeStamp.equals(stream.lastModifiedTime())) {
> > return ret;
> > }
> > }
> > ByteArrayOutputStream out = new
> > ByteArrayOutputStream();
> > GZIPOutputStream zout = new
> > GZIPOutputStream(out);
> > Streams.copy(stream.getInputStream(), zout);
> > zout.close();
> > stream.close();
> > ret = out.toByteArray();
> > timeStamp = stream.lastModifiedTime();
> > cache = new SoftReference(ret);
> > return ret;
> > } catch (IOException e) {
> > throw new RuntimeException(e);
> > } catch (ResourceStreamNotFoundException e) {
> > throw new RuntimeException(e);
> > }
> > }
> > protected abstract IResourceStream getOriginalResourceStream();
> > }
> >
> > private final WebResource wrappedResource;
> > private final IResourceStream resourceStream;
> >
> > public CompressingWebResource(WebResource wrappedResource) {
> > this.wrappedResource = wrappedResource;
> > this.resourceStream = newResourceStream();
> > }
> > public IResourceStream getResourceStream() {
> > return resourceStream;
> > }
> > public IResourceStream newResourceStream() {
> > return new CompressingResourceStream() {
> > protected IResourceStream getOriginalResourceStream() {
> > return wrappedResource.getResourceStream();
> > }
> > };
> > }
> > protected void setHeaders(WebResponse response) {
> > super.setHeaders(response);
> > if (supportsCompression()) {
> > response.setHeader("Content-Encoding", "gzip");
> > }
> > }
> > private boolean supportsCompression() {
> > if
> > (Application.get().getResourceSettings().getDisableGZipCompression())
> {
> > return false;
> > }
> > WebRequest request = (WebRequest)
> > RequestCycle.get().getRequest();
> > String s =
> > request.getHttpServletRequest().getHeader("Accept-Encoding");
> > if (s == null) {
> > return false;
> > } else {
> > return s.indexOf("gzip") >= 0;
> > }
> > }
> > }
> >
> >
>
>
> --
> Andrew Klochkov
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]