All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.swap.BeanInterceptor Maven / Gradle / Ivy

// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
// * with the License.  You may obtain a copy of the License at                                                              *
// *                                                                                                                         *
// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
// *                                                                                                                         *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
// * specific language governing permissions and limitations under the License.                                              *
// ***************************************************************************************************************************
package org.apache.juneau.swap;

/**
 * Bean interceptor.
 *
 * 

* Bean interceptors intercept calls to bean getters and setters to allow them to override values in transit. * *

Example:
*

* // Interceptor that strips out sensitive information on Address beans. * public class AddressInterceptor extends BeanInterceptor<Address> { * * @Override * public Object readProperty(Address bean, String name, Object value) { * if ("taxInfo".equals(name)) * return "redacted"; * return value; * } * * @Override * public Object writeProperty(Address bean, String name, Object value) { * if ("taxInfo".equals(name) && "redacted".equals(value)) * return TaxInfoUtils.lookup(bean.getStreet(), bean.getCity(), bean.getState()); * return value; * } * } *

* *

* Bean interceptors are registered in the following way: *

    *
  • {@link org.apache.juneau.annotation.Bean#interceptor() @Bean(interceptor)} *
  • {@link org.apache.juneau.BeanContext.Builder#beanInterceptor(Class,Class)} *
* *
Example:
*

* // Register interceptor on bean class. * @Bean(interceptor=AddressInterceptor.class) * public class Address { * public String getTaxInfo() {...} * public void setTaxInfo(String value) {...} * } *

* *
See Also:
* * @param The bean type. */ public class BeanInterceptor { /** Non-existent bean interceptor. */ public static final class Void extends BeanInterceptor {} /** * Default reusable property filter instance. */ public static final BeanInterceptor DEFAULT = new BeanInterceptor<>(); /** * Property read interceptor. * *

* Subclasses can override this property to convert property values to some other object just before serialization. * *

Example:
*

* // Address filter that strips out sensitive information. * public class AddressInterceptor extends BeanInterceptor<Address> { * * public Object readProperty(Address bean, String name, Object value) { * if ("taxInfo".equals(name)) * return "redacted"; * return value; * } * } *

* * @param bean The bean from which the property was read. * @param name The property name. * @param value The value just extracted from calling the bean getter. * @return The value to serialize. Default is just to return the existing value. */ public Object readProperty(T bean, String name, Object value) { return value; } /** * Property write interceptor. * *

* Subclasses can override this property to convert property values to some other object just before calling the * bean setter. * *

Example:
*

* // Address filter that strips out sensitive information. * public class AddressInterceptor extends BeanInterceptor<Address> { * * public Object writeProperty(Address bean, String name, Object value) { * if ("taxInfo".equals(name) && "redacted".equals(value)) * return TaxInfoUtils.lookup(bean.getStreet(), bean.getCity(), bean.getState()); * return value; * } * } *

* * @param bean The bean from which the property was read. * @param name The property name. * @param value The value just parsed. * @return The value to serialize. Default is just to return the existing value. */ public Object writeProperty(T bean, String name, Object value) { return value; } }