/*
* Copyright 2009-2010 the original author or authors.
*
* Licensed 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.internna.iwebmvc.dwr;
import java.util.Map;
import java.util.Map.Entry;
import org.directwebremoting.extend.Property;
import org.directwebremoting.ConversionException;
import org.directwebremoting.convert.BeanConverter;
import org.directwebremoting.extend.InboundVariable;
import org.directwebremoting.extend.OutboundContext;
import org.directwebremoting.extend.OutboundVariable;
import org.directwebremoting.extend.NonNestedOutboundVariable;
import org.internna.iwebmvc.utils.StringUtils;
import org.internna.iwebmvc.model.security.UserImpl;
import org.springframework.beans.factory.InitializingBean;
/**
* DWR converter for User model class.
*
* @author Jose Noheda
* @since 2.0
*/
public final class UserConverter extends BeanConverter implements InitializingBean {
private String salt;
public void setSalt(String salt) {
this.salt = salt;
}
/**
* Remove some problematic properties like the user password or the global server salt.
*
*/
@Override public Map<String, Property> getPropertyMapFromObject(Object example, boolean readRequired, boolean writeRequired) throws ConversionException {
Map<String, Property> map = super.getPropertyMapFromObject(example, readRequired, writeRequired);
if (map != null) {
map.remove("salt");
map.remove("password");
}
return map;
}
/**
* Remove some problematic properties like the user password or the global server salt.
*
*/
@Override public Map<String, Property> getPropertyMapFromClass(Class<?> type, boolean readRequired, boolean writeRequired) throws ConversionException {
Map<String, Property> map = super.getPropertyMapFromClass(type, readRequired, writeRequired);
if (map != null) {
map.remove("salt");
map.remove("password");
}
return map;
}
@Override public void afterPropertiesSet() throws Exception {
setJavascript("User");
}
@Override public Object convertInbound(Class<?> paramType, InboundVariable data) throws ConversionException {
if (!data.isNull()) {
String value = data.getValue();
if (StringUtils.hasText(value)) {
value = value.substring(1, value.length() - 1);
UserImpl user = new UserImpl();
user.setSalt(salt);
data.getContext().addConverted(data, instanceType, user);
Map<String, Property> properties = getPropertyMapFromObject(user, false, true);
for (Entry<String, String> entry : extractInboundTokens(paramType, value).entrySet()) {
String key = entry.getKey();
if (!key.startsWith("%24dwr") && !"salt".equals(key)) {
Property property = properties.get(key);
Object output = convert(entry.getValue(), property.getPropertyType(), data.getContext(), property);
property.setValue(user, output);
}
}
return user;
}
}
return null;
}
/**
* Remove some problematic properties like the user password or the global server salt. Yes, yet again.
*
*/
@Override public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws ConversionException {
try {
if (data != null) {
UserImpl user = (UserImpl) data;
user.setSalt(null);
user.setPassword(null);
}
} catch (Exception ex) {
// Better safe than sorry
return new NonNestedOutboundVariable("null");
}
return super.convertOutbound(data, outctx);
}
}