1 21 package org.apache.derby.client.net; 22 23 import org.apache.derby.client.am.Configuration; 24 import org.apache.derby.client.am.Section; 25 import org.apache.derby.client.am.SqlException; 26 import org.apache.derby.client.am.ClientMessageId; 27 import org.apache.derby.shared.common.reference.SQLState; 28 29 30 public class NetPackageRequest extends NetConnectionRequest { 31 static final String COLLECTIONNAME = "NULLID"; 32 33 NetPackageRequest(NetAgent netAgent, CcsidManager ccsidManager, int bufferSize) { 34 super(netAgent, ccsidManager, bufferSize); 35 } 36 37 static final String collectionName = "NULLID"; 45 46 void buildCommonPKGNAMinfo(Section section) throws SqlException { 47 String collectionToFlow = COLLECTIONNAME; 48 int maxIdentifierLength = NetConfiguration.PKG_IDENTIFIER_MAX_LEN; 60 61 boolean scldtalenRequired = false; 62 scldtalenRequired = checkPKGNAMlengths(netAgent_.netConnection_.databaseName_, 63 maxIdentifierLength, 64 NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); 65 66 if (!scldtalenRequired) { 67 scldtalenRequired = checkPKGNAMlengths(collectionToFlow, 68 maxIdentifierLength, 69 NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); 70 } 71 72 if (!scldtalenRequired) { 73 scldtalenRequired = checkPKGNAMlengths(section.getPackageName(), 74 maxIdentifierLength, 75 NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); 76 } 77 78 if (!scldtalenRequired) { 80 writeScalarPaddedString(netAgent_.netConnection_.databaseName_, 81 NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); 82 writeScalarPaddedString(collectionToFlow, 83 NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); 84 writeScalarPaddedString(section.getPackageName(), 85 NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); 86 } else { 87 buildSCLDTA(netAgent_.netConnection_.databaseName_, NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); 88 buildSCLDTA(collectionToFlow, NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); 89 buildSCLDTA(section.getPackageName(), NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); 90 } 91 } 92 93 private void buildSCLDTA(String identifier, int minimumLength) throws SqlException { 94 if (identifier.length() <= minimumLength) { 95 write2Bytes(minimumLength); 96 writeScalarPaddedString(identifier, minimumLength); 97 } else { 98 write2Bytes(identifier.length()); 99 writeScalarPaddedString(identifier, identifier.length()); 100 } 101 } 102 103 104 void buildPKGNAMCSN(Section section) throws SqlException { 110 if (!canCommandUseDefaultPKGNAMCSN()) { 111 markLengthBytes(CodePoint.PKGNAMCSN); 112 if (section.getPKGNAMCBytes() != null) { 114 writeStoredPKGNAMCBytes(section); 115 } else { 116 markForCachingPKGNAMCSN(); 118 buildCommonPKGNAMinfo(section); 119 writeScalarPaddedBytes(Configuration.dncPackageConsistencyToken, 120 NetConfiguration.PKGCNSTKN_FIXED_LEN, 121 NetConfiguration.NON_CHAR_DDM_DATA_PAD_BYTE); 122 storePKGNAMCBytes(section); 124 } 125 write2Bytes(section.getSectionNumber()); 126 updateLengthBytes(); 127 } else { 128 writeScalar2Bytes(CodePoint.PKGSN, section.getSectionNumber()); 129 } 130 } 131 132 private void storePKGNAMCBytes(Section section) { 133 int startPos = popMarkForCachingPKGNAMCSN(); 135 int copyLength = offset_ - startPos; 136 byte[] b = new byte[copyLength]; 137 System.arraycopy(bytes_, 138 startPos, 139 b, 140 0, 141 copyLength); 142 section.setPKGNAMCBytes(b); 143 } 144 145 private void writeStoredPKGNAMCBytes(Section section) { 146 byte[] b = section.getPKGNAMCBytes(); 147 148 ensureLength(offset_ + b.length); 150 151 System.arraycopy(b, 152 0, 153 bytes_, 154 offset_, 155 b.length); 156 157 offset_ += b.length; 158 } 159 160 private boolean canCommandUseDefaultPKGNAMCSN() { 161 return false; 162 } 163 164 165 private boolean checkPKGNAMlengths(String identifier, 168 int maxIdentifierLength, 169 int lengthRequiringScldta) throws SqlException { 170 int length = identifier.length(); 171 if (length > maxIdentifierLength) { 172 throw new SqlException(netAgent_.logWriter_, 173 new ClientMessageId(SQLState.LANG_IDENTIFIER_TOO_LONG), 174 identifier, new Integer (maxIdentifierLength)); 175 } 176 177 return (length > lengthRequiringScldta); 178 } 179 180 private byte[] getBytes(String string, String encoding) throws SqlException { 181 try { 182 return string.getBytes(encoding); 183 } catch (java.lang.Exception e) { 184 throw new SqlException(netAgent_.logWriter_, 185 new ClientMessageId(SQLState.JAVA_EXCEPTION), 186 e.getClass().getName(), e.getMessage(), e); 187 } 188 } 189 190 private void buildNOCMorNOCS(String string) throws SqlException { 191 if (string == null) { 192 write2Bytes(0xffff); 193 } else { 194 byte[] sqlBytes = null; 195 196 if (netAgent_.typdef_.isCcsidMbcSet()) { 197 sqlBytes = getBytes(string, netAgent_.typdef_.getCcsidMbcEncoding()); 198 write1Byte(0x00); 199 write4Bytes(sqlBytes.length); 200 writeBytes(sqlBytes, sqlBytes.length); 201 write1Byte(0xff); 202 } else { 203 sqlBytes = getBytes(string, netAgent_.typdef_.getCcsidSbcEncoding()); 204 write1Byte(0xff); 205 write1Byte(0x00); 206 write4Bytes(sqlBytes.length); 207 writeBytes(sqlBytes, sqlBytes.length); 208 } 209 } 210 } 211 212 private void buildSQLSTTGRP(String string) throws SqlException { 223 buildNOCMorNOCS(string); 224 return; 225 } 226 227 private void buildSQLSTT(String string) throws SqlException { 233 buildSQLSTTGRP(string); 234 } 235 236 protected void buildSQLSTTcommandData(String sql) throws SqlException { 237 createEncryptedCommandData(); 238 int loc = offset_; 239 markLengthBytes(CodePoint.SQLSTT); 240 buildSQLSTT(sql); 241 updateLengthBytes(); 242 if (netAgent_.netConnection_.getSecurityMechanism() == 243 NetConfiguration.SECMEC_EUSRIDDTA || 244 netAgent_.netConnection_.getSecurityMechanism() == 245 NetConfiguration.SECMEC_EUSRPWDDTA) { 246 encryptDataStream(loc); 247 } 248 249 } 250 251 252 protected void buildSQLATTRcommandData(String sql) throws SqlException { 253 createEncryptedCommandData(); 254 int loc = offset_; 255 markLengthBytes(CodePoint.SQLATTR); 256 buildSQLSTT(sql); 257 updateLengthBytes(); 258 if (netAgent_.netConnection_.getSecurityMechanism() == 259 NetConfiguration.SECMEC_EUSRIDDTA || 260 netAgent_.netConnection_.getSecurityMechanism() == 261 NetConfiguration.SECMEC_EUSRPWDDTA) { 262 encryptDataStream(loc); 263 } 264 265 } 266 267 268 public void encryptDataStream(int lengthLocation) throws SqlException { 269 byte[] clearedBytes = new byte[offset_ - lengthLocation]; 270 byte[] encryptedBytes; 271 for (int i = lengthLocation; i < offset_; i++) { 272 clearedBytes[i - lengthLocation] = bytes_[i]; 273 } 274 275 encryptedBytes = netAgent_.netConnection_.getEncryptionManager(). 276 encryptData(clearedBytes, 277 NetConfiguration.SECMEC_EUSRIDPWD, 278 netAgent_.netConnection_.getTargetPublicKey(), 279 netAgent_.netConnection_.getTargetPublicKey()); 280 281 int length = encryptedBytes.length; 282 283 if (bytes_.length >= lengthLocation + length) { 284 System.arraycopy(encryptedBytes, 0, bytes_, lengthLocation, length); 285 } else { 286 byte[] largeByte = new byte[lengthLocation + length]; 287 System.arraycopy(bytes_, 0, largeByte, 0, lengthLocation); 288 System.arraycopy(encryptedBytes, 0, largeByte, lengthLocation, length); 289 bytes_ = largeByte; 290 } 291 292 offset_ += length - clearedBytes.length; 293 294 296 bytes_[lengthLocation - 6] = (byte) ((length >>> 8) & 0xff); 297 bytes_[lengthLocation - 5] = (byte) (length & 0xff); 298 } 299 300 } 301 | Popular Tags |