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

org.kawanfw.sql.jdbc.http.JdbcHttpTransactionTransfer 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.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;

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.ClientLogger;
import org.kawanfw.commons.util.FrameworkDebug;
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.util.ConnectionParms;
import org.kawanfw.sql.util.SqlActionTransaction;
import org.kawanfw.sql.util.SqlReturnCode;

/**
 * 
 * For all transaction parameters get/set transfers...
 * 
 */
public class JdbcHttpTransactionTransfer {

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

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

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

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

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

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

    /**
     * Sends a setAutoCommit() to server
     * 
     * @param autoCommit
     *            true or false
     */
    public void setAutoCommit(boolean autoCommit) throws SQLException {
	setTransactionAction(SqlActionTransaction.ACTION_SQL_SET_AUTOCOMMIT,
		ConnectionParms.AUTOCOMMIT, autoCommit + "");
    }

    /**
     * Sends a setReadOnly() to server
     * 
     * @param readOnly
     *            true or false
     */
    public void setReadOnly(boolean readOnly) throws SQLException {
	setTransactionAction(SqlActionTransaction.ACTION_SQL_SET_READ_ONLY,
		ConnectionParms.READONLY, readOnly + "");
    }

    /**
     * Sends a setHoldability() to server
     * 
     * @param readOnly
     *            true or false
     */
    public void setHoldability(int holdability) throws SQLException {
	setTransactionAction(SqlActionTransaction.ACTION_SQL_SET_HOLDABILITY,
		ConnectionParms.HOLDABILITY, holdability + "");
    }

    /**
     * Sends a setHoldability() to server
     * 
     * @param readOnly
     *            true or false
     */
    public void setTransactionIsolation(int level) throws SQLException {
	setTransactionAction(
		SqlActionTransaction.ACTION_SQL_SET_TRANSACTION_ISOLATION,
		ConnectionParms.TRANSACTION_ISOLATION, level + "");
    }

    /**
     * Sets a transaction with a unique parm : setReadOnly(), setHoldability(),
     * setTransactionIsolation()
     * 
     * @param transactionAction
     *            the action
     * @param transactionParmName
     *            the parm name
     * @param transactionParmValue
     *            the parm value
     * @throws SQLException
     */
    private void setTransactionAction(String transactionAction,
	    String transactionParmName, String transactionParmValue)
	    throws SQLException {

	HttpTransfer httpTransfer = connectionHttp.getHttpTransfer();

	// Launch the Servlet

	List requestParams = new Vector();
	requestParams.add(new SimpleNameValuePair(Parameter.ACTION,
		transactionAction));
	requestParams.add(new SimpleNameValuePair(Parameter.USERNAME,
		connectionHttp.getUsername()));
	requestParams.add(new SimpleNameValuePair(Parameter.TOKEN,
		this.authenticationToken));

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

	try {
	    httpTransfer.send(requestParams);
	} catch (Exception e) {
	    JdbcHttpTransferUtil.wrapExceptionAsSQLException(e);
	}

	String receive = httpTransfer.recv();
	receive = receive.trim();

	debug("httpTransfer.recv(): " + receive + ":");

	if (receive.startsWith(ReturnCode.INVALID_LOGIN_OR_PASSWORD)) {
	    throw new SQLException(new InvalidLoginException());
	}

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

	return;
    }

    /**
     * Init the remote connection on the server
     */
    public void initRemoteConnection() throws SQLException {
	sendTransactionAction(SqlActionTransaction.ACTION_SQL_INIT_REMOTE_CONNECTION);
    }

    /**
     * Sends a Connection.commit() to server
     */
    public void commit() throws SQLException {
	sendTransactionAction(SqlActionTransaction.ACTION_SQL_COMMIT);
    }

    /**
     * Sends a Connection.rollback() to server
     */
    public void rollback() throws SQLException {
	sendTransactionAction(SqlActionTransaction.ACTION_SQL_ROLLBACK);
    }

    /**
     * Sends a Connection.rollback() to server
     */
    public void close() throws SQLException {
	sendTransactionAction(SqlActionTransaction.ACTION_SQL_CON_CLOSE);
    }

    /**
     * Sends a transaction action (commit, rollback or close) to the server
     */
    private void sendTransactionAction(String transactionAction)
	    throws SQLException {

	HttpTransfer httpTransfer = connectionHttp.getHttpTransfer();

	// Launch the Servlet

	List requestParams = new Vector();
	requestParams.add(new SimpleNameValuePair(Parameter.ACTION,
		transactionAction));
	requestParams.add(new SimpleNameValuePair(Parameter.USERNAME,
		connectionHttp.getUsername()));
	requestParams.add(new SimpleNameValuePair(Parameter.TOKEN,
		this.authenticationToken));

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

	try {
	    httpTransfer.send(requestParams);
	} catch (Exception e) {
	    JdbcHttpTransferUtil.wrapExceptionAsSQLException(e);
	}

	String receive = httpTransfer.recv();
	receive = receive.trim();

	debug("httpTransfer.recv(): " + receive + ":");

	if (receive.startsWith(ReturnCode.INVALID_LOGIN_OR_PASSWORD)) {
	    throw new SQLException(new InvalidLoginException());
	}

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

	return;
    }

    /**
     * Sends a getReadOnly to the server
     * 
     * @return the server getReadOnly()
     * @throws SQLException
     */
    public boolean isReadOnly() throws SQLException {
	String readOnlyStr = getTransactionAction(SqlActionTransaction.ACTION_SQL_IS_READ_ONLY);
	return Boolean.parseBoolean(readOnlyStr);
    }

    /**
     * Sends a getAutoCommit() the server
     * 
     * @return the server getAutoCommit()
     * @throws SQLException
     */
    public boolean getAutoCommit() throws SQLException {
	String autoCommitStr = getTransactionAction(SqlActionTransaction.ACTION_SQL_GET_AUTOCOMMIT);
	return Boolean.parseBoolean(autoCommitStr);
    }

    /**
     * Sends a getHoldability() to the server
     * 
     * @return
     * @throws SQLException
     */
    public int getHoldability() throws SQLException {
	String holdabilityStr = getTransactionAction(SqlActionTransaction.ACTION_SQL_GET_HOLDABILITY);

	int holdability = -1;

	try {
	    holdability = Integer.parseInt(holdabilityStr);
	} catch (NumberFormatException e) {
	    throw new SQLException(Tag.PRODUCT_PRODUCT_FAIL + e.getMessage(),
		    new IOException(e.getMessage(), e));
	}

	return holdability;
    }

    /**
     * Gets the server default Connection.getTransactionIsolation()
     * 
     * @return the server default Connection.getTransactionIsolation()
     */
    public int getTransactionIsolation() throws SQLException {
	String levelStr = getTransactionAction(SqlActionTransaction.ACTION_SQL_GET_TRANSACTION_ISOLATION);

	int level = -1;

	try {
	    level = Integer.parseInt(levelStr);
	} catch (NumberFormatException e) {
	    throw new SQLException(Tag.PRODUCT_PRODUCT_FAIL + e.getMessage(),
		    new IOException(e.getMessage(), e));
	}

	return level;
    }

    /**
     * Gets a transaction action (commit, rollback or close) to the server
     */
    private String getTransactionAction(String transactionAction)
	    throws SQLException {

	HttpTransfer httpTransfer = connectionHttp.getHttpTransfer();

	// Launch the Servlet

	List requestParams = new Vector();
	requestParams.add(new SimpleNameValuePair(Parameter.ACTION,
		transactionAction));
	requestParams.add(new SimpleNameValuePair(Parameter.USERNAME,
		connectionHttp.getUsername()));
	requestParams.add(new SimpleNameValuePair(Parameter.TOKEN,
		this.authenticationToken));

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

	try {
	    httpTransfer.send(requestParams);
	} catch (Exception e) {
	    JdbcHttpTransferUtil.wrapExceptionAsSQLException(e);
	}

	String receive = httpTransfer.recv();
	receive = receive.trim();

	debug("httpTransfer.recv(): " + receive + ":");

	if (receive.startsWith(ReturnCode.INVALID_LOGIN_OR_PASSWORD)) {
	    throw new SQLException(new InvalidLoginException());
	}

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

	return receive;
    }

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

    // @SuppressWarnings("unused")
    private void debug(String s) {
	if (DEBUG) {
	    ClientLogger.getLogger().log(Level.WARNING, s);
	}
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy