View Javadoc

    1   // ========================================================================
    2   // Copyright 2007 Mort Bay Consulting Pty. Ltd.
    3   // ------------------------------------------------------------------------
    4   // Licensed under the Apache License, Version 2.0 (the "License");
    5   // you may not use this file except in compliance with the License.
    6   // You may obtain a copy of the License at
    7   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.cometd.filter;
    16
    17  import java.lang.reflect.Array;
    18  import java.util.Collection;
    19  import java.util.Iterator;
    20  import java.util.List;
    21  import java.util.Map;
    22
    23  import org.cometd.Channel;
    24  import org.cometd.Client;
    25  import org.cometd.DataFilter;
    26  import org.mortbay.cometd.ClientImpl;
    27  import org.mortbay.log.Log;
    28  import org.mortbay.util.ajax.JSON;
    29
    30  /**
    31   * JSON DataFilter This {@link DataFilter} walks an Object as if it was a call
    32   * to {@link JSON#toString(Object)} and calls the protected methods
    33   * {@link #filterString(String)}, {@link #filterNumber(Number)},
    34   * {@link #filterBoolean(Boolean)}, {@link #filterArray(Object, ClientImpl)} or
    35   * {@link #filterMap(Map, ClientImpl)} appropriate. Derived filters may override
    36   * one or more of these methods to provide filtering of specific types.
    37   *
    38   * @author gregw
    39   *
    40   */
    41  public class JSONDataFilter implements DataFilter
    42  {
    43      public void init(Object init)
    44      {
    45      }
    46
    47      public Object filter(Client from, Channel to, Object data) throws IllegalStateException
    48      {
    49          if (data == null)
    50              return null;
    51
    52          if (data instanceof Map)
    53              return filterMap(from,to,(Map)data);
    54          if (data instanceof List)
    55              return filterArray(from,to,((List)data).toArray());
    56          if (data instanceof Collection)
    57              return filterArray(from,to,((Collection)data).toArray());
    58          if (data.getClass().isArray())
    59              return filterArray(from,to,data);
    60          if (data instanceof Number)
    61              return filterNumber((Number)data);
    62          if (data instanceof Boolean)
    63              return filterBoolean((Boolean)data);
    64          if (data instanceof String)
    65              return filterString((String)data);
    66          if (data instanceof JSON.Literal)
    67              return filterJSON(from,to,(JSON.Literal)data);
    68          if (data instanceof JSON.Generator)
    69              return filterJSON(from,to,(JSON.Generator)data);
    70          return filterObject(from,to,data);
    71      }
    72
    73      protected Object filterString(String string)
    74      {
    75          return string;
    76      }
    77
    78      protected Object filterBoolean(Boolean bool)
    79      {
    80          return bool;
    81      }
    82
    83      protected Object filterNumber(Number number)
    84      {
    85          return number;
    86      }
    87
    88      protected Object filterArray(Client from, Channel to, Object array)
    89      {
    90          if (array == null)
    91              return null;
    92
    93          int length=Array.getLength(array);
    94
    95          for (int i=0; i < length; i++)
    96              Array.set(array,i,filter(from,to,Array.get(array,i)));
    97
    98          return array;
    99      }
    100
    101     protected Object filterMap(Client from, Channel to, Map object)
    102     {
    103         if (object == null)
    104             return null;
    105
    106         Iterator iter=object.entrySet().iterator();
    107         while(iter.hasNext())
    108         {
    109             Map.Entry entry=(Map.Entry)iter.next();
    110             entry.setValue(filter(from,to,entry.getValue()));
    111         }
    112
    113         return object;
    114     }
    115
    116     protected Object filterJSON(Client from, Channel to, JSON.Generator generator)
    117     {
    118         String json=JSON.toString(generator);
    119         Object data=JSON.parse(json);
    120         return filter(from,to,data);
    121     }
    122
    123     protected Object filterJSON(Client from, Channel to, JSON.Literal json)
    124     {
    125         Object data=JSON.parse(json.toString());
    126         return filter(from,to,data);
    127     }
    128
    129     protected Object filterObject(Client from, Channel to, Object obj)
    130     {
    131         Log.warn(this + ": Cannot Filter " + obj.getClass());
    132         return obj;
    133     }
    134
    135 }