KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > net > NetPackageRequest


1 /*
2
3    Derby - Class org.apache.derby.client.net.NetPackageRequest
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

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 JavaDoc COLLECTIONNAME = "NULLID";
32
33     NetPackageRequest(NetAgent netAgent, CcsidManager ccsidManager, int bufferSize) {
34         super(netAgent, ccsidManager, bufferSize);
35     }
36
37     // RDB Package Name, Consistency Token
38
// Scalar Object specifies the fully qualified name of a relational
39
// database package and its consistency token.
40
//
41
// To accomodate larger lengths, the Scalar Data Length
42
// (SCLDTALEN) Field is used to specify the length of the instance
43
// variable which follows.
44
static final String JavaDoc collectionName = "NULLID";
45
46     void buildCommonPKGNAMinfo(Section section) throws SqlException {
47         String JavaDoc collectionToFlow = COLLECTIONNAME;
48         // the scalar data length field may or may not be required. it depends
49
// on the level of support and length of the data.
50
// check the lengths of the RDBNAM, RDBCOLID, and PKGID.
51
// Determine if the lengths require an SCLDTALEN object.
52
// Note: if an SQLDTALEN is required for ONE of them,
53
// it is needed for ALL of them. This is why this check is
54
// up front.
55
// the SQLAM level dictates the maximum size for
56
// RDB Collection Identifier (RDBCOLID)
57
// Relational Database Name (RDBNAM)
58
// RDB Package Identifier (PKGID)
59
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         // the format is different depending on if an SCLDTALEN is required.
79
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 JavaDoc 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     // this specifies the fully qualified package name,
105
// consistency token, and section number within the package being used
106
// to execute the SQL. If the connection supports reusing the previous
107
// package information and this information is the same except for the section
108
// number then only the section number needs to be sent to the server.
109
void buildPKGNAMCSN(Section section) throws SqlException {
110         if (!canCommandUseDefaultPKGNAMCSN()) {
111             markLengthBytes(CodePoint.PKGNAMCSN);
112             // If PKGNAMCBytes is already available, copy the bytes to the request buffer directly.
113
if (section.getPKGNAMCBytes() != null) {
114                 writeStoredPKGNAMCBytes(section);
115             } else {
116                 // Mark the beginning of PKGNAMCSN bytes.
117
markForCachingPKGNAMCSN();
118                 buildCommonPKGNAMinfo(section);
119                 writeScalarPaddedBytes(Configuration.dncPackageConsistencyToken,
120                         NetConfiguration.PKGCNSTKN_FIXED_LEN,
121                         NetConfiguration.NON_CHAR_DDM_DATA_PAD_BYTE);
122                 // store the PKGNAMCbytes
123
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         // Get the locaton where we started writing PKGNAMCSN
134
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         // Mare sure request buffer has enough space to write this byte array.
149
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     // throws an exception if lengths exceed the maximum.
166
// returns a boolean indicating if SLCDTALEN is required.
167
private boolean checkPKGNAMlengths(String JavaDoc 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 JavaDoc(maxIdentifierLength));
175         }
176
177         return (length > lengthRequiringScldta);
178     }
179
180     private byte[] getBytes(String JavaDoc string, String JavaDoc encoding) throws SqlException {
181         try {
182             return string.getBytes(encoding);
183         } catch (java.lang.Exception JavaDoc 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 JavaDoc 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     // SQLSTTGRP : FDOCA EARLY GROUP
213
// SQL Statement Group Description
214
//
215
// FORMAT FOR SQLAM <= 6
216
// SQLSTATEMENT_m; PROTOCOL TYPE LVCM; ENVLID 0x40; Length Override 32767
217
// SQLSTATEMENT_s; PROTOCOL TYPE LVCS; ENVLID 0x34; Length Override 32767
218
//
219
// FORMAT FOR SQLAM >= 7
220
// SQLSTATEMENT_m; PROTOCOL TYPE NOCM; ENVLID 0xCF; Length Override 4
221
// SQLSTATEMENT_s; PROTOCOL TYPE NOCS; ENVLID 0xCB; Length Override 4
222
private void buildSQLSTTGRP(String JavaDoc string) throws SqlException {
223         buildNOCMorNOCS(string);
224         return;
225     }
226
227     // SQLSTT : FDOCA EARLY ROW
228
// SQL Statement Row Description
229
//
230
// FORMAT FOR ALL SQLAM LEVELS
231
// SQLSTTGRP; GROUP LID 0x5C; ELEMENT TAKEN 0(all); REP FACTOR 1
232
private void buildSQLSTT(String JavaDoc string) throws SqlException {
233         buildSQLSTTGRP(string);
234     }
235
236     protected void buildSQLSTTcommandData(String JavaDoc 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 JavaDoc 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         //we need to update the length in DSS header here.
295

296         bytes_[lengthLocation - 6] = (byte) ((length >>> 8) & 0xff);
297         bytes_[lengthLocation - 5] = (byte) (length & 0xff);
298     }
299
300 }
301
Popular Tags