org.kawanfw.sql.jdbc.util.JsonLineDecryptor.java Source code

Java tutorial

Introduction

Here is the source code for org.kawanfw.sql.jdbc.util.JsonLineDecryptor.java

Source

/*
 * This file is part of AceQL. 
 * AceQL: Remote JDBC access over HTTP.                                     
 * Copyright (C) 2015,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                                
 *                                                                               
 * AceQL is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * AceQL is distributed in the hope that it will be useful,               
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.kawanfw.sql.jdbc.util;

import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.kawanfw.commons.api.client.SessionParameters;
import org.kawanfw.commons.util.convert.Pbe;
import org.kawanfw.sql.jdbc.ConnectionHttp;

/**
 * 
 * Allows to decrypt a json line received from server side.
 * 
 * @author Nicolas de Pomereu
 * 
 */
public class JsonLineDecryptor {

    /**
     * Decrypt the json line if necessary
     * 
     * @param line
     *            the line to decrypt
     * @return the decrypted line
     * @throws SQLException
     *             if any Exception occurs
     */
    public static String decrypt(String line, ConnectionHttp connectionHttp) throws SQLException {

        try {
            if (!line.startsWith(Pbe.KAWANFW_ENCRYPTED)) {
                return line;
            }

            SessionParameters sessionParameters = connectionHttp.getSessionParameters();

            if (sessionParameters != null) {
                char[] password = sessionParameters.getEncryptionPassword();

                if (password != null) {
                    Pbe pbe = new Pbe();
                    line = StringUtils.substringAfter(line, Pbe.KAWANFW_ENCRYPTED);
                    line = pbe.decryptFromHexa(line, password);

                    return line;
                }

            }

            return line;
        } catch (Exception e) {
            throw new SQLException(e);
        }

    }

}