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

org.kawanfw.sql.jdbc.http.JdbcHttpCallableStatementTransfer Maven / Gradle / Ivy

/*
 * 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.http;

import java.io.File;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import org.kawanfw.commons.api.client.InvalidLoginException;
import org.kawanfw.commons.client.http.HttpTransfer;
import org.kawanfw.commons.client.http.SimpleNameValuePair;
import org.kawanfw.commons.util.FrameworkDebug;
import org.kawanfw.commons.util.FrameworkFileUtil;
import org.kawanfw.commons.util.KeepTempFilePolicyParms;
import org.kawanfw.commons.util.Tag;
import org.kawanfw.file.util.parms.Parameter;
import org.kawanfw.file.util.parms.ReturnCode;
import org.kawanfw.sql.api.client.InvalidatedSessionException;
import org.kawanfw.sql.jdbc.ConnectionHttp;
import org.kawanfw.sql.jdbc.util.CallableStatementHolderListEncryptor;
import org.kawanfw.sql.json.ConnectionHolder;
import org.kawanfw.sql.json.ConnectionHolderTransport;
import org.kawanfw.sql.json.no_obfuscation.CallableStatementHolder;
import org.kawanfw.sql.json.no_obfuscation.CallableStatementHolderTransport;
import org.kawanfw.sql.util.ConnectionParms;
import org.kawanfw.sql.util.SqlAction;
import org.kawanfw.sql.util.SqlActionCallable;
import org.kawanfw.sql.util.SqlReturnCode;

public class JdbcHttpCallableStatementTransfer {

    /** Set to true to display/log debug info */
    private static boolean DEBUG = FrameworkDebug
	    .isSet(JdbcHttpCallableStatementTransfer.class);

    /** The Http Connection */
    private ConnectionHttp connectionHttp = null;

    /** The Authentication Token */
    private String authenticationToken = null;

    /**
     * Protected constructor, not callable
     */
    protected JdbcHttpCallableStatementTransfer() {
    }

    /**
     * Constructor
     * 
     * @param connectionHttp
     *            the http Connection
     * @param authenticationToken
     *            the Http Connection Authentication Token
     */

    public JdbcHttpCallableStatementTransfer(ConnectionHttp connectionHttp,
	    String authenticationToken) {
	this.connectionHttp = connectionHttp;
	this.authenticationToken = authenticationToken;

	debug(new java.util.Date() + " Uploading statement file...");
    }

    /**
     * Execute a remote sql execute query callable statement and get the the
     * file of the result
     * 
     * @param callableStatementHolder
     *            the callable statement to execute on server
     * @param action
     *            the action:
     *            SqlActionCallable.ACTION_SQL_CALLABLE_EXECUTE_QUERY or
     *            SqlActionCallable.ACTION_SQL_CALLABLE_EXECUTE_QUERY or
     * 
     * @return the received file
     */
    public File getFileFromExecuteOnServer(
	    CallableStatementHolder callableStatementHolder, String action)
	    throws SQLException {

	HttpTransfer httpTransfer = connectionHttp.getHttpTransfer();

	if (action == null) {
	    throw new IllegalArgumentException(Tag.PRODUCT_PRODUCT_FAIL
		    + "action can not be null");
	}

	if (!action.equals(SqlActionCallable.ACTION_SQL_CALLABLE_EXECUTE_QUERY)
		&& !action
			.equals(SqlActionCallable.ACTION_SQL_CALLABLE_EXECUTE_RAW)) {
	    throw new IllegalArgumentException(Tag.PRODUCT_PRODUCT_FAIL
		    + "Invalid action: " + action + ". Action must be "
		    + SqlActionCallable.ACTION_SQL_CALLABLE_EXECUTE_RAW
		    + " or "
		    + SqlActionCallable.ACTION_SQL_CALLABLE_EXECUTE_QUERY);
	}

	// Prepare the request parameters
	List requestParams = new Vector();
	requestParams.add(new SimpleNameValuePair(Parameter.ACTION, action));
	requestParams.add(new SimpleNameValuePair(Parameter.USERNAME,
		connectionHttp.getUsername()));
	requestParams.add(new SimpleNameValuePair(Parameter.TOKEN,
		authenticationToken));

	requestParams.add(new SimpleNameValuePair(ConnectionParms.CONNECTION_ID,
		connectionHttp.getConnectionId()));

	// Store the Connections commit mode / read only mode / transaction
	// isolation
	ConnectionHolder connectionHolder = new ConnectionHolder(connectionHttp);
	String jsonString = ConnectionHolderTransport.toJson(connectionHolder);

	requestParams.add(new SimpleNameValuePair(SqlAction.CONNECTION_HOLDER,
		jsonString));

	// Execute the statement waiting
	List statementHolderList = new Vector();
	statementHolderList.add(callableStatementHolder); // Only one
							  // StatementHolder

	CallableStatementHolderListEncryptor.encrypt(statementHolderList,
		connectionHttp);

	jsonString = CallableStatementHolderTransport
		.toJson(statementHolderList);

	debug("jsonString: " + jsonString);

	requestParams.add(new SimpleNameValuePair(SqlAction.STATEMENT_HOLDER,
		jsonString));

	httpTransfer.setReceiveInFile(true); // To say we get the result into a
					     // file
	File receiveFile = null;

	try {
	    httpTransfer.send(requestParams);

	    receiveFile = httpTransfer.getReceiveFile();
	    String receive = FrameworkFileUtil.getFirstLineOfFile(receiveFile);
	    debug("JdbcHttpCallableStatementTransfer.getReceiveFile(): "
		    + receiveFile + ":");

	    if (receive.startsWith(ReturnCode.INVALID_LOGIN_OR_PASSWORD)) {

		if (!DEBUG && !KeepTempFilePolicyParms.KEEP_TEMP_FILE) {
		    receiveFile.delete();
		}

		throw new InvalidLoginException();
	    }

	    if (receive.startsWith(SqlReturnCode.SESSION_INVALIDATED)) {
		throw new SQLException(new InvalidatedSessionException());
	    }

	} catch (Exception e) {
	    JdbcHttpTransferUtil.wrapExceptionAsSQLException(e);
	}

	// We are done: return the received string
	return receiveFile;

    }

    /**
     * Debug tool
     * 
     * @param s
     */

    // @SuppressWarnings("unused")
    private void debug(String s) {
	if (DEBUG) {
	    System.out.println(s);
	}
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy