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

org.kawanfw.sql.jdbc.http.JdbcHttpSavepointTransfer 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.sql.SQLException;
import java.sql.Savepoint;
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.HtmlConverter;
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.transport.SavepointHttp;
import org.kawanfw.sql.util.ConnectionParms;
import org.kawanfw.sql.util.SqlActionTransaction;
import org.kawanfw.sql.util.SqlReturnCode;

/**
 * 
 * For all Savepoint actions get/set transfers...
 * 
 */
public class JdbcHttpSavepointTransfer {

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

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

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

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

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

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

    /*
     * setSavepoint setSavepoint(name) rollback(Savepoint savepoint)
     * releaseSavepoint(Savepoint savepoint)
     */

    /**
     * Sends a setSavepoint() to server
     * 
     * @return the Savepoint get from server
     */
    public Savepoint setSavepoint() throws SQLException {

	String savepointStr = null;

	savepointStr = setSavepointAction(
		SqlActionTransaction.ACTION_SQL_SET_SAVEPOINT,
		ConnectionParms.NO_PARM, "NO_PARM");

	savepointStr = HtmlConverter.fromHtml(savepointStr);

	Savepoint savepointHttp = SavepointHttp.buildFromString(savepointStr);
	return savepointHttp;
    }

    /**
     * Sends a setSavepoint(String name) to server
     * 
     * @return the Savepoint get from server
     */
    public Savepoint setSavepoint(String name) throws SQLException {

	String savepointStr = null;

	savepointStr = setSavepointAction(
		SqlActionTransaction.ACTION_SQL_SET_SAVEPOINT_NAME,
		ConnectionParms.NAME, HtmlConverter.toHtml(name));

	savepointStr = HtmlConverter.fromHtml(savepointStr);

	Savepoint savepointHttp = SavepointHttp.buildFromString(savepointStr);
	return savepointHttp;
    }

    /**
     * Sends a rollback(Savepoint savepoint) to server
     * 
     */
    public void rollback(Savepoint savepoint) throws SQLException {

	String savepointStr = savepoint.toString();
	savepointStr = HtmlConverter.toHtml(savepointStr);

	setSavepointAction(
		SqlActionTransaction.ACTION_SQL_SET_ROLLBACK_SAVEPOINT,
		ConnectionParms.SAVEPOINT, savepointStr);

    }

    /**
     * Sends a releaseSavepoin(tSavepoint savepoint) to server
     * 
     * @return the Savepoint get from server
     */
    public void releaseSavepoint(Savepoint savepoint) throws SQLException {

	String savepointStr = savepoint.toString();
	savepointStr = HtmlConverter.toHtml(savepointStr);

	setSavepointAction(
		SqlActionTransaction.ACTION_SQL_SET_RELEASE_SAVEPOINT,
		ConnectionParms.SAVEPOINT, savepointStr);
    }

    /**
     * Sets a Savepoint action
     * 
     * @param savepointAction
     *            the action
     * @param savepointParmName
     *            the parm name
     * @param savepointParmValue
     *            the parm value
     * 
     * @throws SQLException
     */
    private String setSavepointAction(String savepointAction,
	    String savepointParmName, String savepointParmValue)
	    throws SQLException {

	HttpTransfer httpTransfer = connectionHttp.getHttpTransfer();

	// Launch the Servlet

	List requestParams = new Vector();
	requestParams.add(new SimpleNameValuePair(Parameter.ACTION,
		savepointAction));
	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(savepointParmName,
		savepointParmValue));

	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