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

src-main.org.awakefw.sql.jdbc.http.JdbcHttpMiscTransfer Maven / Gradle / Ivy

/*
 * Awake File: Easy file upload & download through HTTP with Java
 * Awake SQL: Remote JDBC access through HTTP.                                    
 * Copyright (C) 2012, Kawan Softwares S.A.S.
 * (http://www.awakeframework.org). All rights reserved.                                
 *                                                                               
 * Awake File/SQL 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.            
 *                                                                               
 * Awake File/SQL 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.
 */

// 13/12/11 12:40 NDP : JdbcHttpMiscTransfer: creation

package org.awakefw.sql.jdbc.http;

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

import org.apache.http.message.BasicNameValuePair;
import org.awakefw.commons.api.client.InvalidLoginException;
import org.awakefw.file.api.util.AwakeDebug;
import org.awakefw.file.http.HttpTransfer;
import org.awakefw.file.http.HttpTransferOne;
import org.awakefw.file.util.AwakeLogger;
import org.awakefw.file.util.Tag;
import org.awakefw.file.util.parms.Parameter;
import org.awakefw.file.util.parms.ReturnCode;
import org.awakefw.sql.jdbc.ConnectionHttp;
import org.awakefw.sql.util.SqlAction;

/**
 * 
 * For all other transfers...
 * Fist case : get the server Connection.getTransactionIsolation().
 * 
 */
public class JdbcHttpMiscTransfer {
    /** Set to true to display/log debug info */
    private static boolean DEBUG = AwakeDebug
	    .isSet(JdbcHttpMiscTransfer.class);

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

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

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

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

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

    /**
     * Gets the server default Connection.getTransactionIsolation()
     * @return the server default Connection.getTransactionIsolation()
     */
    public int getServerConnectionTransactionIsolation() throws SQLException {

	// Launch the Servlet
	HttpTransfer httpTransfer = new HttpTransferOne(connectionHttp.getUrl(),
		connectionHttp.getHttpProxy(),
		connectionHttp.getHttpProtocolParameters(),
		connectionHttp.getAwakeProgressManager());

	List requestParams = new Vector();
	requestParams.add(new BasicNameValuePair(Parameter.ACTION,
		SqlAction.ACTION_SQL_GET_TRANSACTION_ISOLATION));
	requestParams.add(new BasicNameValuePair(Parameter.LOGIN,
		connectionHttp.getUsername()));
	requestParams.add(new BasicNameValuePair(Parameter.TOKEN,
		this.authenticationToken));

	try {
	    httpTransfer.send(requestParams);
	} catch (Exception e) {
	    JdbcHttpTransferUtil.wrapExceptionAsSQLException(e);
	}
	
	String receive = httpTransfer.recv();

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

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

	receive = receive.trim();
	
	int transactionIsolation = -1;
	// We are done: return the received string as integer
	try {
	    transactionIsolation = Integer.parseInt(receive);
	} catch (NumberFormatException e) {
	    throw new SQLException(
		    Tag.AWAKE_PRODUCT_FAIL + e.getMessage(), new IOException(
			    e.getMessage(), e));
	}

	return transactionIsolation;
    }

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

    // @SuppressWarnings("unused")
    private void debug(String s) {
	if (DEBUG) {
	    AwakeLogger.log(s);
	}
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy