Package org.internna.iwebmvc.core.dwr

Source Code of org.internna.iwebmvc.core.dwr.EncryptedUUIDConverter

/*
* Copyright 2002-2007 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.core.dwr;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.convert.BaseV20Converter;
import org.directwebremoting.extend.InboundContext;
import org.directwebremoting.extend.InboundVariable;
import org.directwebremoting.extend.MarshallException;
import org.directwebremoting.extend.NonNestedOutboundVariable;
import org.directwebremoting.extend.OutboundContext;
import org.directwebremoting.extend.OutboundVariable;
import org.directwebremoting.util.LocalUtil;
import org.internna.iwebmvc.core.IWebMvcException;
import org.internna.iwebmvc.core.crypto.Cipherer;
import org.internna.iwebmvc.core.crypto.Decipherer;
import org.internna.iwebmvc.model.UUID;
import org.internna.iwebmvc.utils.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.support.RequestContextUtils;

/**
* DWR converter for UUID objects from/to an encrypted String
*
* @author Jose Noheda
*/
public class EncryptedUUIDConverter extends BaseV20Converter {

    private static Decipherer decipherer;
    private static Cipherer cipherer;
    private static Log log = LogFactory.getLog(EncryptedUUIDConverter.class);

    protected void initEncryption() {
        WebContext webCtx = WebContextFactory.get();
        WebApplicationContext springContext = RequestContextUtils.getWebApplicationContext(webCtx.getHttpServletRequest(), webCtx.getServletContext());
        decipherer = (Decipherer) springContext.getBean("cryptographyManager");
        cipherer = (Cipherer) decipherer;
        if (log.isInfoEnabled()) log.info("UUID converter found cipherer/decipherer: " + decipherer.getClass().getName());
    }

    @Override
    public Object convertInbound(Class<?> paramType, InboundVariable data, InboundContext ctx) throws MarshallException {
        if (decipherer == null) initEncryption();
        try {
            if (data != null) {
                String id = LocalUtil.decode(data.getValue());
                Assert.isEncrypted(decipherer, id);
                return new UUID(decipherer.decrypt(id));
            }
        } catch (Exception e) {
            throw new MarshallException(paramType, new IWebMvcException("Could not convert UUID from: " + data, e));
        }
        return null;
    }

    @Override
    public OutboundVariable convertOutbound(Object data, OutboundContext ctx) throws MarshallException {
        if (decipherer == null) initEncryption();
        try {
            return new NonNestedOutboundVariable("'" + cipherer.encrypt(((UUID) data).toString()) + "'");
        } catch (Exception e) {
            throw new MarshallException(data.getClass(), new IWebMvcException("Could not generate encrypted UUID from: " + data, e));
        }
    }

}
TOP

Related Classes of org.internna.iwebmvc.core.dwr.EncryptedUUIDConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.