1 /**
2 * .
3 * Copyright � 1999 Erich P G.
4 *
5 */
6
7 package autohit.verify;
8
9 import java.io.ByteArrayInputStream;
10 import java.io.InputStreamReader;
11
12 import autohit.transport.Response;
13 import autohit.utils.CRC;
14
15
16 /**
17 * A simple HTTP Verify implementation.
18 * <p>
19 * Current issues: <br>
20 * 1- Doesn't handle 'serious' encodings.<br>
21 *
22 * @author Erich P. Gatejen
23 * @version 1.0
24 * <i>Version History</i>
25 * <code>EPG - Initial - 5Feb99</code>
26 *
27 */
28 public class HTTPVerify implements Verify {
29
30 // --- FINAL FIELDS ------------------------------------------------------
31
32 // --- FIELDS ------------------------------------------------------------
33
34 /**
35 * Current response context.
36 */
37 private Response rContext;
38
39 /**
40 * Reader for the content.
41 */
42 private InputStreamReader cin;
43
44 /**
45 * CRC machine.
46 */
47 private CRC crcMachine;
48
49 /**
50 * Last delta.
51 */
52 private int delta;
53
54
55 // --- PUBLIC METHODS ----------------------------------------------------
56
57 /**
58 * Default constructor.
59 */
60 public HTTPVerify() {
61
62 rContext = null; // be paranoid
63 crcMachine = new CRC();
64 }
65
66 /**
67 * Create a fresh verification context.
68 *
69 * @param address Address specification.
70 *
71 * @throws autohit.transport.VerifyException
72 */
73 public void fresh(Response context) throws VerifyException {
74
75 rContext = context;
76 delta = 0;
77 this.reset();
78 }
79
80 /**
81 * Reset the current verification context.
82 *
83 * @param address Address specification.
84 *
85 * @throws autohit.transport.VerifyException
86 */
87 public void reset() throws VerifyException {
88
89 if (rContext == null) throw new VerifyException(Verify.psVE_NO_CONTEXT);
90
91 // Build a reader for the content.
92 cin = new InputStreamReader(new ByteArrayInputStream(rContext.content));
93 }
94
95 /**
96 * A seek operation.
97 * <p>
98 * This will always return true if the expected string is empty.
99 *
100 * @param expected the string to seek.
101 * @return true if it passes verification,otherwise false.
102 *
103 * @throws autohit.transport.VerifyException
104 */
105 public boolean seek(String expected) throws VerifyException {
106
107 int rover = 0;
108 int end = expected.length();
109
110 if (rContext == null) throw new VerifyException(Verify.psVE_NO_CONTEXT);
111 if (end == 0) return true;
112
113 // Trap all exceptions. Any will return a verification fail.
114 // This will most certainly happen if the reader is depleted.
115 try {
116
117 // Bleh. Sometimes this throws an expection, sometimes
118 // it doesnt. anyways, lookf or EOF.
119 int curr = cin.read();
120 while (curr != -1) {
121
122 if (curr == expected.charAt(rover)) {
123 // So far, so good. Look at the next.
124 rover++;
125 } else {
126 // ACK! no good. Jump back to the start of the
127 // expected string.
128 rover = 0;
129 }
130
131 // See if we've roved the entire string. If it did, it
132 // passed verification.
133 if (rover == end) return true;
134
135 curr = cin.read();
136 }
137
138 } catch (Exception e) {
139
140 // Prolly got here cause we depleted the cin.
141 // Pass through and let this method return a fail.
142 }
143
144 // If for any reason the code gets here, verification failed.
145 return false;
146 }
147
148 /**
149 * A CRC check operation.
150 *
151 * @param expected the expected CRC value.
152 * @return true if it passes verification,otherwise false.
153 *
154 * @throws autohit.transport.VerifyException
155 */
156 public boolean crc(int expected) throws VerifyException {
157
158 if (rContext == null) throw new VerifyException(Verify.psVE_NO_CONTEXT);
159
160 int c = crcMachine.calc(rContext.content, 0, rContext.content.length);
161
162 // DEBUG
163 //System.out.println("CRC: c=" + c + " expected=" + expected);
164
165 delta = c - expected;
166
167 if (delta == 0) return true;
168 else return false;
169 }
170
171 /**
172 * A size check operation.
173 *
174 * @param expected the expected size.
175 * @return true if it passes verification,otherwise false.
176 *
177 * @throws autohit.transport.VerifyException
178 */
179 public boolean size(int expected) throws VerifyException {
180
181 if (rContext == null) throw new VerifyException(Verify.psVE_NO_CONTEXT);
182
183 delta = rContext.cLength - expected;
184
185 if (delta == 0) return true;
186 else return false;
187 }
188
189 /**
190 * Returns the numeric difference from the previous operation. Currently,
191 * if will return the diff between the expected and actual values calculated
192 * in crc and size.
193 * <p>
194 * It is calculated as actual - expected.
195 *
196 * @return numeric delta. It will be 0, if there was no difference.
197 *
198 * @throws autohit.transport.VerifyException
199 */
200 public int lastDelta() throws VerifyException {
201
202 if (rContext == null) throw new VerifyException(Verify.psVE_NO_CONTEXT);
203 return delta;
204 }
205
206 /**
207 * Run a sub-executable to perform a verification.
208 * <p>
209 * Scrubbed for now... I need to ponder this entire mechanism. It will
210 * always return TRUE.
211 *
212 * @param invocation an invocation string. used by the specific verification
213 * to determine what to run and how to run it.
214 * @param content passed to the sun-executable as content.
215 * @return true if it passes verification,otherwise false.
216 *
217 * @throws autohit.transport.VerifyException
218 */
219 public boolean exec(String invocation, String content) throws VerifyException {
220
221 if (rContext == null) throw new VerifyException(Verify.psVE_NO_CONTEXT);
222
223 return true;
224
225
226 }
227
228
229 // --- PRIVATE METHODS ---------------------------------------------------
230 }
|