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;
}
}
}
--
View this message in context:
http://www.nabble.com/compressing-resources-which-are-not-in-the-classpath-tf4441543.html#a12691148
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]