use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.
the class V13WireOperations method tryKnownServerKeys.
private void tryKnownServerKeys() throws IOException, SQLException {
boolean initializedEncryption = false;
SQLExceptionChainBuilder<SQLException> chainBuilder = new SQLExceptionChainBuilder<>();
for (KnownServerKey.PluginSpecificData pluginSpecificData : getPluginSpecificData()) {
EncryptionIdentifier encryptionIdentifier = pluginSpecificData.getEncryptionIdentifier();
EncryptionPluginSpi currentEncryptionSpi = SUPPORTED_ENCRYPTION_PLUGINS.get(encryptionIdentifier);
if (currentEncryptionSpi == null) {
continue;
}
try (CryptSessionConfig cryptSessionConfig = getCryptSessionConfig(encryptionIdentifier, pluginSpecificData.getSpecificData())) {
EncryptionPlugin encryptionPlugin = currentEncryptionSpi.createEncryptionPlugin(cryptSessionConfig);
EncryptionInitInfo encryptionInitInfo = encryptionPlugin.initializeEncryption();
if (encryptionInitInfo.isSuccess()) {
enableEncryption(encryptionInitInfo);
clearServerKeys();
initializedEncryption = true;
log.debug("Wire encryption established with " + encryptionIdentifier);
break;
} else {
chainBuilder.append(encryptionInitInfo.getException());
}
} catch (SQLException e) {
chainBuilder.append(e);
}
}
if (!initializedEncryption && getAttachProperties().getWireCryptAsEnum() == WireCrypt.REQUIRED) {
FbExceptionBuilder exceptionBuilder = new FbExceptionBuilder().nonTransientException(ISCConstants.isc_wirecrypt_incompatible);
if (chainBuilder.hasException()) {
exceptionBuilder.cause(chainBuilder.getException());
}
throw exceptionBuilder.toFlatSQLException();
}
if (chainBuilder.hasException()) {
SQLException current = chainBuilder.getException();
if (log.isWarnEnabled()) {
log.warn(initializedEncryption ? "Wire encryption established, but some plugins failed; see other loglines for details" : "No wire encryption established because of errors");
log.warn("Encryption plugin failed; see debug level for stacktraces:\n" + ExceptionHelper.collectAllMessages(current));
}
if (log.isDebugEnabled()) {
do {
log.debug("Encryption plugin failed", current);
} while ((current = current.getNextException()) != null);
}
}
}
use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.
the class V10AsynchronousChannel method cancelEvent.
@Override
public void cancelEvent(EventHandle eventHandle) throws SQLException {
if (!(eventHandle instanceof WireEventHandle))
throw new SQLNonTransientException("Invalid event handle type: " + eventHandle.getClass().getName());
final WireEventHandle wireEventHandle = (WireEventHandle) eventHandle;
removeChannelListener(wireEventHandle);
synchronized (database.getSynchronizationObject()) {
try {
final XdrOutputStream dbXdrOut = database.getXdrStreamAccess().getXdrOut();
dbXdrOut.writeInt(op_cancel_events);
dbXdrOut.writeInt(database.getHandle());
dbXdrOut.writeInt(wireEventHandle.getLocalId());
dbXdrOut.flush();
} catch (IOException e) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(e).toSQLException();
}
try {
database.readGenericResponse(null);
} catch (IOException e) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(e).toSQLException();
}
}
}
use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.
the class V10AsynchronousChannel method queueEvent.
@Override
public void queueEvent(EventHandle eventHandle) throws SQLException {
if (!(eventHandle instanceof WireEventHandle))
throw new SQLNonTransientException("Invalid event handle type: " + eventHandle.getClass().getName());
final WireEventHandle wireEventHandle = (WireEventHandle) eventHandle;
wireEventHandle.assignNewLocalId();
addChannelListener(wireEventHandle);
synchronized (database.getSynchronizationObject()) {
try {
if (log.isDebugEnabled()) {
log.debug("Queue event: " + wireEventHandle);
}
final XdrOutputStream dbXdrOut = database.getXdrStreamAccess().getXdrOut();
dbXdrOut.writeInt(op_que_events);
dbXdrOut.writeInt(auxHandle);
dbXdrOut.writeBuffer(wireEventHandle.toByteArray());
// AST info
dbXdrOut.writeLong(0);
dbXdrOut.writeInt(wireEventHandle.getLocalId());
dbXdrOut.flush();
} catch (IOException e) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(e).toSQLException();
}
try {
final GenericResponse response = database.readGenericResponse(null);
wireEventHandle.setEventId(response.getObjectHandle());
} catch (IOException e) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(e).toSQLException();
}
}
}
use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.
the class V10AsynchronousChannel method connect.
@Override
public void connect(String hostName, int portNumber, int auxHandle) throws SQLException {
if (isConnected())
throw new SQLException("Asynchronous channel already established");
this.auxHandle = auxHandle;
try {
socketChannel = SocketChannel.open();
socketChannel.socket().setTcpNoDelay(true);
SocketAddress socketAddress = new InetSocketAddress(hostName, portNumber);
socketChannel.connect(socketAddress);
socketChannel.configureBlocking(false);
} catch (IOException ex) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
}
}
use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.
the class V10InputBlob method getSegment.
@Override
public byte[] getSegment(final int sizeRequested) throws SQLException {
try {
if (sizeRequested <= 0) {
throw new FbExceptionBuilder().exception(jb_blobGetSegmentNegative).messageParameter(sizeRequested).toSQLException();
}
// TODO Is this actually a real limitation, or are larger sizes possible?
int actualSize = 2 + Math.min(sizeRequested, getMaximumSegmentSize());
synchronized (getSynchronizationObject()) {
checkDatabaseAttached();
checkTransactionActive();
checkBlobOpen();
final GenericResponse response;
final FbWireDatabase database = getDatabase();
try {
final XdrOutputStream xdrOut = database.getXdrStreamAccess().getXdrOut();
xdrOut.writeInt(op_get_segment);
xdrOut.writeInt(getHandle());
xdrOut.writeInt(actualSize);
// length of segment send buffer (always 0 in get)
xdrOut.writeInt(0);
xdrOut.flush();
} catch (IOException e) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(e).toSQLException();
}
try {
response = database.readGenericResponse(null);
// TODO Meaning of 2
if (response.getObjectHandle() == 2) {
// TODO what if I seek on a stream blob?
setEof();
}
} catch (IOException e) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(e).toSQLException();
}
final byte[] responseBuffer = response.getData();
if (responseBuffer.length == 0) {
return responseBuffer;
}
final ByteArrayOutputStream bos = new ByteArrayOutputStream(actualSize);
int position = 0;
while (position < responseBuffer.length) {
final int segmentLength = iscVaxInteger2(responseBuffer, position);
position += 2;
bos.write(responseBuffer, position, segmentLength);
position += segmentLength;
}
return bos.toByteArray();
}
} catch (SQLException e) {
exceptionListenerDispatcher.errorOccurred(e);
throw e;
}
}
Aggregations