Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.webbasics.resource;
 
 
 
 
Special servlet to stream JS and CSS bundles.
Default usage:
  <servlet>
     <servlet-name>ResourceBundleServlet</servlet-name>
     <servlet-class>com.helger.webbasics.resource.ResourceBundleServlet</servlet-class>
     <init-param>
       <param-name>allowedExtensions</param-name>
       <param-value>js,css</param-value>
     </init-param>
   </servlet>
   <servlet-mapping>
     <servlet-name>ResourceBundleServlet</servlet-name>
     <url-pattern>/resbundle/*</url-pattern>
   </servlet-mapping>
 

Author(s):
philip
 
 {
   public static final String SERVLET_DEFAULT_NAME = "resbundle";
   public static final String SERVLET_DEFAULT_PATH = '/' + ;
 
   private static final Logger s_aLogger = LoggerFactory.getLogger (ResourceBundleServlet.class);
 
   @Override
   @Nonnull
   @Nonempty
   protected String getApplicationID ()
   {
     return .;
   }
 
   @Override
   @Nonnull
   {
     return ;
   }
 
   @Nonnull
   private static String _getBundleIDFromFilename (@Nonnull final String sFilename)
   {
     // Cut leading path ("/") and file extension
     return FilenameHelper.getBaseName (sFilename);
   }
 
   @Override
   protected EContinue initRequestState (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                         @Nonnull final UnifiedResponse aUnifiedResponse)
   {
     if (super.initRequestState (aRequestScopeaUnifiedResponse).isBreak ())
       return .;
 
    // Allow only valid bundle IDs
    if (!MetaSystemManager.getWebSiteResourceBundleMgr ().containsResourceBundleOfID (sBundleID))
    {
      .info ("Failed to resolve resource bundle with ID '" + sBundleID + "'");
      aUnifiedResponse.setStatus (.);
      return .;
    }
    return .;
  }

  

Returns:
The number of days to cache the result.
  protected int getCachingDays ()
  {
    return 10;
  }
  protected void onDeliverResource (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                    @Nonnull final UnifiedResponse aUnifiedResponse,
                                    @Nonnull final String sFilenamethrows IOException
  {
    final String sBundleID = _getBundleIDFromFilename (sFilename);
    final WebSiteResourceBundleSerialized aBundle = MetaSystemManager.getWebSiteResourceBundleMgr ()
                                                                     .getResourceBundleOfID (sBundleID);
    final int nCachingDays = getCachingDays ();
    aUnifiedResponse.enableCaching (. * nCachingDays)
                    .setMimeType (aBundle.getMimeType ())
                    .setContent (aBundle)
                    .setCharset (.);
  }
New to GrepCode? Check out our FAQ X