Home » tiles-2.2.1-src » org.apache.tiles.velocity.context » [javadoc | source]

    1   /*
    2    * $Id: VelocityTilesRequestContextFactory.java 765774 2009-04-16 21:43:00Z apetrelli $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    * http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   package org.apache.tiles.velocity.context;
   22   
   23   import java.io.Writer;
   24   import java.util.Map;
   25   
   26   import javax.servlet.http.HttpServletRequest;
   27   import javax.servlet.http.HttpServletResponse;
   28   
   29   import org.apache.tiles.TilesApplicationContext;
   30   import org.apache.tiles.awareness.TilesRequestContextFactoryAware;
   31   import org.apache.tiles.context.TilesRequestContext;
   32   import org.apache.tiles.context.TilesRequestContextFactory;
   33   import org.apache.tiles.servlet.context.ServletTilesRequestContext;
   34   import org.apache.velocity.context.Context;
   35   
   36   /**
   37    * The implementation of the Tiles request context factory specific for Velocity.
   38    *
   39    * @version $Rev: 765774 $ $Date: 2009-04-16 23:43:00 +0200 (gio, 16 apr 2009) $
   40    * @since 2.2.0
   41    */
   42   public class VelocityTilesRequestContextFactory implements TilesRequestContextFactory, TilesRequestContextFactoryAware {
   43   
   44       /**
   45        * The position of the writer in the request objects.
   46        */
   47       private static final int WRITER_POSITION = 3;
   48   
   49       /**
   50        * The size of the array of the request objects with the writer.
   51        */
   52       private static final int SIZE_WITH_WRITER = 4;
   53   
   54       /**
   55        * The size of the array of the request objects without the writer.
   56        */
   57       private static final int SIZE_WITHOUT_WRITER = 3;
   58       /**
   59        * Parent Tiles context factory.
   60        */
   61       private TilesRequestContextFactory parent;
   62   
   63       /** {@inheritDoc} */
   64       public TilesRequestContext createRequestContext(TilesApplicationContext context, Object... requestItems) {
   65           if ((requestItems.length == SIZE_WITHOUT_WRITER || requestItems.length == SIZE_WITH_WRITER)
   66                   && requestItems[0] instanceof Context
   67                   && requestItems[1] instanceof HttpServletRequest
   68                   && requestItems[2] instanceof HttpServletResponse
   69                   && ((requestItems.length == SIZE_WITH_WRITER
   70                           && requestItems[WRITER_POSITION] instanceof Writer)
   71                           || requestItems.length == SIZE_WITHOUT_WRITER)) {
   72               Context ctx = (Context) requestItems[0];
   73               HttpServletRequest request = (HttpServletRequest) requestItems[1];
   74               HttpServletResponse response = (HttpServletResponse) requestItems[2];
   75               Writer writer = null;
   76               if (requestItems.length == SIZE_WITH_WRITER) {
   77                   writer = (Writer) requestItems[WRITER_POSITION];
   78               }
   79               TilesRequestContext enclosedRequest;
   80               if (parent != null) {
   81                   enclosedRequest = parent.createRequestContext(context, request, response);
   82               } else {
   83                   enclosedRequest = new ServletTilesRequestContext(context, request, response);
   84               }
   85               return new VelocityTilesRequestContext(enclosedRequest,
   86                       ctx, writer);
   87           } else if (requestItems.length == 1
   88               && requestItems[0] instanceof VelocityTilesRequestContext) {
   89               // FIXME is it necessary?
   90   
   91               VelocityTilesRequestContext ctx = (VelocityTilesRequestContext) requestItems[0];
   92               return ctx;
   93           }
   94           return null;
   95       }
   96   
   97       /** {@inheritDoc} */
   98       public void init(Map<String, String> configurationParameters) {
   99           // Nothing to initialize.
  100       }
  101   
  102       /** {@inheritDoc} */
  103       public void setRequestContextFactory(
  104               TilesRequestContextFactory contextFactory) {
  105           this.parent = contextFactory;
  106       }
  107   }

Home » tiles-2.2.1-src » org.apache.tiles.velocity.context » [javadoc | source]