    View Javadoc
    1   /*
    2    * Copyright (c) 2003
    3    * Information Desire GmbH
    4    * All rights reserved.
    5    */
    6   package com.infodesire.infobit.servlet;
    7
    8   import javax.servlet.ServletConfig;
    9   import javax.servlet.ServletContext;
    10  import javax.servlet.ServletException;
    11  import javax.servlet.ServletOutputStream;
    12  import javax.servlet.http.HttpServlet;
    13  import javax.servlet.http.HttpServletRequest;
    14  import javax.servlet.http.HttpServletResponse;
    15
    16  import com.infodesire.infobit.InfobitConfiguration;
    17  import com.infodesire.infobit.InfobitPool;
    18  import com.infodesire.infobit.InfobitManager;
    19  import com.infodesire.infobit.render.InfobitRenderer;
    20  import com.infodesire.infobit.render.velocity.ServletVelocityRenderer;
    21  import com.infodesire.infobit.util.FormatTool;
    22
    23  import org.apache.commons.logging.Log;
    24  import org.apache.commons.logging.LogFactory;
    25
    26  import java.io.IOException;
    27  import java.io.Writer;
    28
    29  import java.util.Properties;
    30  import java.util.Map;
    31  import java.util.HashMap;
    32  import java.util.Iterator;
    33  /***
    34   * servlet capable to render infobits. delegates work to renderer. output is
    35   * intendet to be transformed by xslt filter. Accepts 2 parameters: "poolName"
    36   * defined infobit pool name ( from pool configured in web application ).
    37   * "binaryPrefix" tells which servlet is going to serve out our binary content.
    38   *
    39   * @author    konstantin
    40   * @created   August 28, 2003
    41   * @version   $Revision: 1.8 $
    42   * @todo      improve ocnfiguration handling
    43   */
    44  public class InfobitServlet extends HttpServlet {
    45
    46      /***
    47       * Description of the Field
    48       */
    49      public final static String CONTENT_TYPE = "default.contentType";
    50
    51      /***
    52       * The default content type for the response
    53       */
    54      public final static String DEFAULT_CONTENT_TYPE = "text/html";
    55      /***
    56       * Default encoding for the output stream
    57       */
    58      public final static String DEFAULT_OUTPUT_ENCODING = "ISO-8859-1";
    59
    60      /***
    61       * parameter specifying which infobit pool is to be used
    62       */
    63      public final static String POOL_NAME = "poolName";
    64      /***
    65       * parameter specifying prefix for binary infobit servlet shall start with /
    66       * but without trailing one.
    67       */
    68      public final static String BINARY_PREFIX = "binaryPrefix";
    69      /***
    70       * parameter to pass to context for binary prefix
    71       */
    72      public final static String BINARY_PREFIX_NAME = "_infobit_binary_prefix";
    73      /***
    74       * parameter to pass to context for binary prefix
    75       */
    76      public final static String SESSION_NAME = "_session_id";
    77      /***
    78       * reference name in context for format tool
    79       */
    80      public final static String FORMAT_TOOL = "formatter";
    81
    82      private static Log _log = LogFactory.getLog(InfobitServlet.class);
    83
    84      InfobitPool     _pool;
    85      InfobitManager  _manager;
    86      ServletVelocityRenderer _renderer;
    87      String          _poolName;
    88      String          _binaryPrefix;
    89      FormatTool      _format = new FormatTool();
    90
    91
    92      /***
    93       * setup parameters from request. shall be overriden by implementing classes
    94       * in case they wish something special.
    95       *
    96       * @param request  Description of Parameter
    97       * @return         Description of the Returned Value
    98       */
    99      public Map setupContext(HttpServletRequest request) {
    100         HashMap parameters = new HashMap();
    101         Map reqParams = request.getParameterMap();
    102
    103         String key;
    104         String[] values;
    105
    106         for (Iterator iter = reqParams.keySet().iterator(); iter.hasNext(); ) {
    107             key = (String) iter.next();
    108             values = (String[]) reqParams.get(key);
    109             if (values.length == 1) {
    110                 parameters.put(key, values[0]);
    111                 if (_log.isDebugEnabled()) {
    112                     _log.debug("passing single value param: " + key + "=" + values[0]);
    113                 }
    114             }
    115             else {
    116                 parameters.put(key, values);
    117                 if (_log.isDebugEnabled()) {
    118                     _log.debug("passing single value array for: " + key);
    119                 }
    120             }
    121         }
    122
    123         // and also pass session ID and prefix to binary infobits
    124         parameters.put(BINARY_PREFIX_NAME, request.getContextPath() + _binaryPrefix);
    125         parameters.put(SESSION_NAME, request.getSession().getId());
    126         parameters.put(FORMAT_TOOL, _format);
    127         return parameters;
    128     }
    129
    130
    131     /***
    132      * initialize servlet for infobit rendering
    133      *
    134      * @param config                Description of Parameter
    135      * @exception ServletException  Description of Exception
    136      */
    137     public void init(ServletConfig config) throws ServletException {
    138         super.init(config);
    139         _poolName = config.getInitParameter(POOL_NAME);
    140         _binaryPrefix = config.getInitParameter(BINARY_PREFIX);
    141         try {
    142             _log.debug("******* loading infobit pool ***************");
    143             _pool = InfobitConfiguration.getInstance().getPool(_poolName);
    144             _log.debug("******* retriving infobit manager ***********");
    145             _manager = _pool.getInfobitManager();
    146             _log.debug("******* preparing velocity renderer ***********");
    147             _renderer = new ServletVelocityRenderer();
    148             _renderer.setManager(_manager);
    149             _renderer.init(new Properties());
    150             _log.debug("************ renderer ready ************* ");
    151             // flish infobit pool - our resource loader opens connection
    152             // in case your resource loader opened connection
    153             _pool.flush();
    154         } catch (Exception ex) {
    155             _log.error("exception while acquiring infobit pool: " + _poolName, ex);
    156             throw new ServletException("exception while acquiring infobit pool: " + _poolName, ex);
    157         }
    158     }
    159
    160
    161     /***
    162      * Handles GET - calls doRequest()
    163      *
    164      * @param request               Description of Parameter
    165      * @param response              Description of Parameter
    166      * @exception ServletException  Description of Exception
    167      * @exception IOException       Description of Exception
    168      */
    169     public void doGet(HttpServletRequest request, HttpServletResponse response)
    170          throws ServletException, IOException {
    171         doRequest(request, response);
    172     }
    173
    174
    175     /***
    176      * Handle a POST request - calls doRequest()
    177      *
    178      * @param request               Description of Parameter
    179      * @param response              Description of Parameter
    180      * @exception ServletException  Description of Exception
    181      * @exception IOException       Description of Exception
    182      */
    183     public void doPost(HttpServletRequest request, HttpServletResponse response)
    184          throws ServletException, IOException {
    185         doRequest(request, response);
    186     }
    187
    188
    189     /***
    190      * Handles with both GET and POST requests. we just render infobit out using
    191      * renderer
    192      *
    193      * @param request               HttpServletRequest object containing client
    194      *      request
    195      * @param response              HttpServletResponse object for the response
    196      * @exception ServletException  Description of Exception
    197      * @exception IOException       Description of Exception
    198      */
    199     protected void doRequest(HttpServletRequest request,
    200         HttpServletResponse response)
    201          throws ServletException, IOException {
    202
    203         String pathInfo = (String) request.getAttribute("javax.servlet.include.path_info");
    204         if (pathInfo == null) {
    205             // cool, we are original request. so we set up out pure response
    206             response.setContentType("text/html");
    207             // and grab path info out of request
    208             pathInfo = request.getPathInfo();
    209         }
    210
    211         String infobitName = pathInfo.substring(1);
    212         if (_log.isDebugEnabled()) {
    213             _log.debug("rendering infobit : " + infobitName);
    214         }
    215         Map parameters = setupContext(request);
    216
    217         try {
    218             Writer servletWriter = response.getWriter();
    219             _renderer.render(infobitName, servletWriter, parameters);
    220             servletWriter.flush();
    221             servletWriter.close();
    222         } catch (Exception ex) {
    223             _log.error("error occured while rendering infobit", ex);
    224
    225         }
    226     }
    227 }

This page was automatically generated by Maven