
src-main.org.awakefw.sql.jdbc.http.JdbcHttpExecuteRawTransfer 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.
*/
// 16/11/11 19:00 NDP : JdbcHttpExecuteRawTransfer: creation
// 23/11/11 14:50 NDP : JdbcHttpExecuteRawTransfer: encrypt statement holder list
// 30/11/11 19:35 NDP : JdbcHttpExecuteRawTransfer: delete receiveFile if there is an Exception
// 13/03/12 14:05 NDP : JdbcHttpExecuteRawTransfer: uses now StatementHolderFileList to store statements holders for batchs
package org.awakefw.sql.jdbc.http;
import java.io.File;
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.AwakeFileUtil;
import org.awakefw.file.util.AwakeLogger;
import org.awakefw.file.util.KeepTempFilePolicyParms;
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.jdbc.util.StatementHolderFileList;
import org.awakefw.sql.jdbc.util.crypto.StatementHolderCrypto;
import org.awakefw.sql.json.ConnectionHolder;
import org.awakefw.sql.json.ConnectionHolderTransport;
import org.awakefw.sql.json.StatementHolder;
import org.awakefw.sql.json.StatementHolderTransport;
import org.awakefw.sql.util.SqlAction;
import org.awakefw.sql.util.TransportConverter;
/**
*
* Class that execute all http transfer for execute() to the server, and gets
* back the results
*
*/
public class JdbcHttpExecuteRawTransfer {
/** Set to true to display/log debug info */
private static boolean DEBUG = AwakeDebug
.isSet(JdbcHttpExecuteRawTransfer.class);
/** The Http Connection */
private ConnectionHttp connectionHttp = null;
/** The Authentication Token */
private String authenticationToken = null;
/**
* Protected constructor, not callable
*/
protected JdbcHttpExecuteRawTransfer() {
}
/**
* Constructor
*
* @param connectionHttp
* the http Connection
* @param authenticationToken
* the Http Connection Authentication Token
*/
public JdbcHttpExecuteRawTransfer(ConnectionHttp connectionHttp,
String authenticationToken) {
this.connectionHttp = connectionHttp;
this.authenticationToken = authenticationToken;
}
/**
* Execute a remote sql execute() statement and get the the file of the
* result
*
* @param statementHolder
* the statement to execute on server
*
* @return the received file
*/
public File getFileFromExecuteRaw(StatementHolder statementHolder)
throws SQLException {
HttpTransfer httpTransfer = null;
httpTransfer = new HttpTransferOne(connectionHttp.getUrl(),
connectionHttp.getHttpProxy(),
connectionHttp.getHttpProtocolParameters(),
connectionHttp.getAwakeProgressManager());
// Prepare the request parameters
List requestParams = new Vector();
requestParams.add(new BasicNameValuePair(Parameter.ACTION,
SqlAction.ACTION_SQL_EXECUTE_RAW));
requestParams.add(new BasicNameValuePair(Parameter.LOGIN,
connectionHttp.getUsername()));
requestParams.add(new BasicNameValuePair(Parameter.TOKEN,
authenticationToken));
// Store the Connections commit mode / read only mode / transaction
// isolation
ConnectionHolder connectionHolder = new ConnectionHolder(connectionHttp);
String jsonString = ConnectionHolderTransport.toJson(connectionHolder);
requestParams.add(new BasicNameValuePair(SqlAction.CONNECTION_HOLDER,
jsonString));
// Execute the statement waiting
List statementHolderList = new Vector();
statementHolderList.add(statementHolder); // Only one StatementHolder
StatementHolderCrypto.encrypt(statementHolderList, connectionHttp);
jsonString = StatementHolderTransport.toJson(statementHolderList);
requestParams.add(new BasicNameValuePair(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 = AwakeFileUtil.getFirstLineOfFile(receiveFile);
debug("httpTransfer.getReceiveFile(): " + receiveFile + ":");
if (receive.startsWith(ReturnCode.INVALID_LOGIN_OR_PASSWORD)) {
if (!DEBUG && !KeepTempFilePolicyParms.KEEP_TEMP_FILE) {
receiveFile.delete();
}
throw new InvalidLoginException();
}
} catch (Exception e) {
JdbcHttpTransferUtil.wrapExceptionAsSQLException(e);
}
// We are done: return the received string
return receiveFile;
}
/**
* Execute a remote sql execute update statement and get the string
*
* @param statementHolderList
* the list of statement + parameters to execute
* @return the result of the execute update
*/
public String getStringFromExecuteUpdateListOnServer(
StatementHolderFileList statementHolderFileList ) throws SQLException {
// First, we close the file!
statementHolderFileList.close();
if (statementHolderFileList.isEmpty()) {
String message = Tag.AWAKE_PRODUCT_FAIL
+ "Nothing to execute: statementHolderList is empty!";
throw new SQLException(message, new IOException(message));
}
//StatementHolderCrypto.encrypt(statementHolderList, connectionHttp);
HttpTransfer httpTransfer = null;
debug("this.connectionHttp.getHost() : " + connectionHttp.getUrl());
// Launch the Servlet
httpTransfer = new HttpTransferOne(connectionHttp.getUrl(),
connectionHttp.getHttpProxy(),
connectionHttp.getHttpProtocolParameters(),
connectionHttp.getAwakeProgressManager());
List requestParams = new Vector();
requestParams.add(new BasicNameValuePair(Parameter.ACTION,
SqlAction.ACTION_SQL_STATEMENT));
requestParams.add(new BasicNameValuePair(Parameter.LOGIN,
connectionHttp.getUsername()));
requestParams.add(new BasicNameValuePair(Parameter.TOKEN,
this.authenticationToken));
// Store the Connections commit mode / read only mode / transaction
// isolation
ConnectionHolder connectionHolder = new ConnectionHolder(
this.connectionHttp);
String jsonString = ConnectionHolderTransport.toJson(connectionHolder);
requestParams.add(new BasicNameValuePair(SqlAction.CONNECTION_HOLDER,
jsonString));
// Execute all the statements waiting
if (statementHolderFileList.size() < connectionHttp
.getMaxStatementsForMemoryTransport()) {
jsonString = StatementHolderTransport.toJson(statementHolderFileList.getStatementHolderList());
// Delete the associated file
statementHolderFileList.delete();
requestParams.add(new BasicNameValuePair(
SqlAction.STATEMENT_HOLDER, jsonString));
try {
httpTransfer.send(requestParams);
} catch (Exception e) {
JdbcHttpTransferUtil.wrapExceptionAsSQLException(e);
}
} else {
debug("statementHolderList.size() >= maxStatementsForMemoryTransport)");
//StatementHolderFileCreator statementHolderFileCreator = new StatementHolderFileCreator(
// statementHolderList);
JdbcHttpFileTransfer jdbcHttpFileTransfer = new JdbcHttpFileTransfer(
this.connectionHttp);
jdbcHttpFileTransfer.uploadFile(
statementHolderFileList.getLocalFile(),
statementHolderFileList.getRemoteFileName());
String statementHolderParam = TransportConverter.AWAKE_STREAM_FILE
+ statementHolderFileList.getRemoteFileName();
requestParams.add(new BasicNameValuePair(
SqlAction.STATEMENT_HOLDER, statementHolderParam));
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());
}
// We are done: return the received string
return receive;
}
/**
* Debug tool
*
* @param s
*/
// @SuppressWarnings("unused")
private void debug(String s) {
if (DEBUG) {
AwakeLogger.log(s);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy