Femto Web Server V1.4-F

dfischer.femtowebserver.filter
Class ContentCompressionFilter

java.lang.Object
  |
  +--dfischer.femtowebserver.httpd.HttpdFilter
        |
        +--dfischer.femtowebserver.filter.ContentCompressionFilter

public class ContentCompressionFilter
extends HttpdFilter

This filter compresses the response content of some MIME types to save network bandwidth.

 Configuration Example:
 ...
 ...
 Httpd httpdThread = new Httpd();

 // configure femto web server
 String serverName = "127.0.0.1";
 try { serverName = InetAddress.getLocalHost().getHostName(); } catch (Exception ex) {}
 httpdThread.setServerName(serverName);
 httpdThread.setServerPort(80);

 String[] compressedMimeTypes = { "text/html", "text/css", "text/javascript" };
 httpdThread.addFilter("dfischer.femtowebserver.filter.ContentCompressionFilter", compressedMimeTypes);
 ...
 ...


 Sourcecode of Filter:

 package dfischer.femtowebserver.filter;
 import dfischer.femtowebserver.httpd.*;

 public class ContentCompressionFilter extends HttpdFilter
 {
      public void filterRequest(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception
      {
      }

      public void filterResponse(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception
      {
          String contentType = httpResponse.getContentType();
          if (contentType == null)
              return;

          byte[] content = httpResponse.getContent();
          if (content == null)
              return;

          String[] filterArguments = getFilterArguments();
          for (int x = 0; x < filterArguments.length; x++)
          {
              // check if data of MIME content type must be compressed
              if (filterArguments[x].equalsIgnoreCase(contentType))
              {
                  // compress content
                  ByteArrayOutputStream bout = new ByteArrayOutputStream();
                  GZIPOutputStream zipOut = new GZIPOutputStream(bout);
                  zipOut.write(content);
                  zipOut.close();
                  bout.close();

                  // set new compressed content
                  httpResponse.setContent(bout.toByteArray());
                  httpResponse.addHeaderField("Content-Encoding", "gzip");
                  break;
              }
          }
     }
 }
 

See Also:
RestrictedInetAddressFilter, HttpDebugFilter, HttpSessionCookieHandler, HttpdFilter

Constructor Summary
ContentCompressionFilter()
           
 
Method Summary
 void filterRequest(HttpRequest httpRequest, HttpResponse httpResponse)
          Allows filtering tasks on the HTTP request and to preset the HTTP response (at his time blank).
 void filterResponse(HttpRequest httpRequest, HttpResponse httpResponse)
          Allows filtering tasks on the HTTP response and to access the HTTP request.
 
Methods inherited from class dfischer.femtowebserver.httpd.HttpdFilter
abortRequest, getFilterArguments, getHttpdProperties
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ContentCompressionFilter

public ContentCompressionFilter()
Method Detail

filterRequest

public void filterRequest(HttpRequest httpRequest,
                          HttpResponse httpResponse)
                   throws java.lang.Exception
Description copied from class: HttpdFilter
Allows filtering tasks on the HTTP request and to preset the HTTP response (at his time blank). Consider that some or all parts of the HTTP response will later be overwritten when the (modified) request is processed by the Femto Web Server.
Overrides:
filterRequest in class HttpdFilter
Following copied from class: dfischer.femtowebserver.httpd.HttpdFilter
Parameters:
httpRequest - The HTTP request data (browser request)
HttpResponse - The HTTP response data (server response)
See Also:
HttpRequest.getSession(), HttpRequest.setSession(dfischer.femtowebserver.httpd.HttpdSession), HttpRequest.getRemoteAddress(), HttpRequest.setRemoteAddress(java.lang.String), HttpRequest.getVirtualHost(), HttpRequest.setVirtualHost(java.lang.String), HttpRequest.getVirtualPort(), HttpRequest.setVirtualPort(int), HttpRequest.getUserAgent(), HttpRequest.setUserAgent(java.lang.String), HttpRequest.getHeaderField(java.lang.String), HttpRequest.addHeaderField(java.lang.String, java.lang.String), HttpRequest.updateHeaderField(java.lang.String, java.lang.String), HttpRequest.removeHeaderField(java.lang.String), HttpRequest.getMethod(), HttpRequest.setMethod(java.lang.String), HttpRequest.getRequest(), HttpRequest.setRequest(java.lang.String), HttpRequest.getParameter(String), HttpRequest.addParameter(java.lang.String, java.lang.String), HttpRequest.updateParameter(java.lang.String, java.lang.String), HttpRequest.removeParameter(java.lang.String), HttpRequest.getContent(), HttpRequest.setContent(byte[]), HttpRequest.clearContent(), HttpRequest.getContentType(), HttpRequest.setContentType(java.lang.String), HttpRequest.getContentSubtype(), HttpRequest.setContentSubtype(java.lang.String)

filterResponse

public void filterResponse(HttpRequest httpRequest,
                           HttpResponse httpResponse)
                    throws java.lang.Exception
Description copied from class: HttpdFilter
Allows filtering tasks on the HTTP response and to access the HTTP request. Consider that modifying the HTTP request will have no impact to the HTTP response result because the request has been already processed by the Femto Web Server.
Overrides:
filterResponse in class HttpdFilter
Following copied from class: dfischer.femtowebserver.httpd.HttpdFilter
Parameters:
httpRequest - The HTTP request data (browser request)
HttpResponse - The HTTP response data (server response)
See Also:
HttpResponse.getSession(), HttpResponse.setSession(dfischer.femtowebserver.httpd.HttpdSession), HttpResponse.getStatus(), HttpResponse.setStatus(int, java.lang.String), HttpResponse.getContentType(), HttpResponse.getContentSubtype(), HttpResponse.setContentType(java.lang.String), HttpResponse.getHeaderField(java.lang.String), HttpResponse.getHeaderFieldNames(), HttpResponse.updateHeaderField(java.lang.String, java.lang.String), HttpResponse.addHeaderField(java.lang.String, java.lang.String), HttpResponse.removeHeaderField(java.lang.String), HttpResponse.getContent(), HttpResponse.getContentAsString(), HttpResponse.setContent(byte[]), HttpResponse.clearContent(), HttpResponse.print(String), HttpResponse.println(String)

Femto Web Server V1.4-F

Copyright 2002, 2003, 2006 by Ingenieurbüro David Fischer GmbH, Switzerland. All rights reserved.