View Javadoc

    1   //========================================================================
    2   //$Id: AjaxFilter.java,v 1.1 2005/11/14 17:45:56 gregwilkins Exp $
    3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15
    16  package org.mortbay.util.ajax;
    17
    18  import java.io.IOException;
    19  import java.io.PrintWriter;
    20  import java.io.StringWriter;
    21
    22  import javax.servlet.Filter;
    23  import javax.servlet.FilterChain;
    24  import javax.servlet.FilterConfig;
    25  import javax.servlet.ServletContext;
    26  import javax.servlet.ServletException;
    27  import javax.servlet.ServletRequest;
    28  import javax.servlet.ServletResponse;
    29  import javax.servlet.http.HttpServletRequest;
    30  import javax.servlet.http.HttpServletResponse;
    31
    32
    33  /**
    34   * @author gregw
    35   * @deprecated Use Cometd
    36   */
    37  public class AjaxFilter implements Filter
    38  {
    39      ServletContext context;
    40
    41      public void init(FilterConfig filterConfig) throws ServletException
    42      {
    43          context=filterConfig.getServletContext();
    44      }
    45
    46      /* ------------------------------------------------------------ */
    47      /**
    48       * @return Returns the context.
    49       */
    50      public ServletContext getContext()
    51      {
    52          return context;
    53      }
    54
    55      /* ------------------------------------------------------------ */
    56      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    57      {
    58          String[] method=request.getParameterValues("ajax");
    59          String[] message=request.getParameterValues("message");
    60
    61          if (method!=null && method.length>0)
    62          {
    63              HttpServletRequest srequest = (HttpServletRequest)request;
    64              HttpServletResponse sresponse = (HttpServletResponse) response;
    65
    66              StringWriter sout = new StringWriter();
    67              PrintWriter out = new PrintWriter(sout);
    68
    69              out.println("<ajax-response>");
    70              AjaxResponse aResponse =new AjaxResponse(srequest,out);
    71
    72              for (int i=0;i<method.length;i++)
    73              {
    74                  handle(method[i],message[i],srequest,aResponse);
    75              }
    76
    77              out.println("</ajax-response>");
    78              byte[] ajax = sout.toString().getBytes("UTF-8");
    79              sresponse.setHeader( "Pragma", "no-cache" );
    80              sresponse.addHeader( "Cache-Control", "must-revalidate,no-cache,no-store" );
    81              sresponse.setDateHeader("Expires", 0);
    82              sresponse.setContentType("text/xml; charset=UTF-8");
    83              sresponse.setContentLength(ajax.length);
    84              sresponse.getOutputStream().write(ajax);
    85              sresponse.flushBuffer();
    86          }
    87          else
    88              chain.doFilter(request, response);
    89      }
    90
    91      public void handle(String method, String message, HttpServletRequest request,AjaxResponse response)
    92      {
    93          response.elementResponse(null, "<span class=\"error\">No implementation for "+method+" "+request.getParameter("member")+"</span>");
    94      }
    95
    96      public void destroy()
    97      {
    98          context=null;
    99      }
    100
    101     public static String encodeText(String s)
    102     {
    103         StringBuffer buf=null;
    104         for (int i=0;i<s.length();i++)
    105         {
    106             char c=s.charAt(i);
    107             String r=null;
    108             switch(c)
    109             {
    110                 case '<': r="&lt;"; break;
    111                 case '>': r="&gt;"; break;
    112                 case '&': r="&amp;"; break;
    113             }
    114
    115             if (r!=null)
    116             {
    117                 if (buf==null)
    118                 {
    119                     buf=new StringBuffer(s.length()*2);
    120                     buf.append(s.subSequence(0,i));
    121                 }
    122                 buf.append(r);
    123             }
    124             else if (buf!=null)
    125                 buf.append(c);
    126         }
    127
    128         if (buf!=null)
    129             return buf.toString();
    130         return s;
    131     }
    132
    133     public static class AjaxResponse
    134     {
    135         private HttpServletRequest request;
    136         private PrintWriter out;
    137         private AjaxResponse(HttpServletRequest request,PrintWriter out)
    138         {this.out=out; this.request=request;}
    139
    140         public void elementResponse(String id,String element)
    141         {
    142             if (id==null)
    143                 id = request.getParameter("id");
    144             if (id==null)
    145                 id="unknown";
    146             out.println("<response type=\"element\" id=\""+id+"\">"+element+"</response>");
    147         }
    148
    149         public void objectResponse(String id,String element)
    150         {
    151             if (id==null)
    152                 id = request.getParameter("id");
    153             if (id==null)
    154                 id="unknown";
    155
    156             out.println("<response type=\"object\" id=\""+id+"\">"+element+"</response>");
    157         }
    158     }
    159 }