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  public class AjaxFilter implements Filter
34  {
35      ServletContext context;
36  
37      public void init(FilterConfig filterConfig) throws ServletException
38      {
39          context=filterConfig.getServletContext();
40      }
41  
42      /* ------------------------------------------------------------ */
43      /**
44       * @return Returns the context.
45       */
46      public ServletContext getContext()
47      {
48          return context;
49      }
50  
51      /* ------------------------------------------------------------ */
52      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
53      {
54          String[] method=request.getParameterValues("ajax");
55          String[] message=request.getParameterValues("message");
56          
57          if (method!=null && method.length>0)
58          {
59              HttpServletRequest srequest = (HttpServletRequest)request;
60              HttpServletResponse sresponse = (HttpServletResponse) response;
61              
62              StringWriter sout = new StringWriter();
63              PrintWriter out = new PrintWriter(sout);
64              
65              out.println("<ajax-response>");
66              AjaxResponse aResponse =new AjaxResponse(srequest,out);
67              
68              for (int i=0;i<method.length;i++)
69              {
70                  handle(method[i],message[i],srequest,aResponse);
71                  if (srequest.isSuspended())
72                      return;
73              }
74  
75              out.println("</ajax-response>");
76              byte[] ajax = sout.toString().getBytes("UTF-8");
77              sresponse.setHeader( "Pragma", "no-cache" );
78              sresponse.addHeader( "Cache-Control", "must-revalidate,no-cache,no-store" );
79              sresponse.setDateHeader("Expires", 0);
80              sresponse.setContentType("text/xml; charset=UTF-8");
81              sresponse.setContentLength(ajax.length);
82              sresponse.getOutputStream().write(ajax);
83              sresponse.flushBuffer();
84          }
85          else
86              chain.doFilter(request, response);
87      }
88  
89      public void handle(String method, String message, HttpServletRequest request,AjaxResponse response)
90      {    
91          response.elementResponse(null, "<span class=\"error\">No implementation for "+method+" "+request.getParameter("member")+"</span>");
92      }
93      
94      public void destroy()
95      {
96          context=null;
97      }
98  
99      public static String encodeText(String s)
100     {
101         StringBuffer buf=null;
102         for (int i=0;i<s.length();i++)
103         {
104             char c=s.charAt(i);
105             String r=null;
106             switch(c)
107             {
108                 case '<': r="&lt;"; break;
109                 case '>': r="&gt;"; break;
110                 case '&': r="&amp;"; break;
111             }
112             
113             if (r!=null)
114             {
115                 if (buf==null)
116                 {
117                     buf=new StringBuffer(s.length()*2);
118                     buf.append(s.subSequence(0,i));
119                 }
120                 buf.append(r);
121             }
122             else if (buf!=null)
123                 buf.append(c);
124         }
125         
126         if (buf!=null)
127             return buf.toString();
128         return s;
129     }
130     
131     public static class AjaxResponse
132     {
133         private HttpServletRequest request;
134         private PrintWriter out;
135         private AjaxResponse(HttpServletRequest request,PrintWriter out)
136         {this.out=out; this.request=request;}
137         
138         public void elementResponse(String id,String element)
139         {
140             if (id==null)
141                 id = request.getParameter("id");
142             if (id==null)
143                 id="unknown";
144             out.println("<response type=\"element\" id=\""+id+"\">"+element+"</response>");
145         }
146         
147         public void objectResponse(String id,String element)
148         {
149             if (id==null)
150                 id = request.getParameter("id");
151             if (id==null)
152                 id="unknown";
153             
154             out.println("<response type=\"object\" id=\""+id+"\">"+element+"</response>");
155         }
156     }
157 }