001: package vicazh.hyperpool.stream.net.http.html;
002:
003: import java.io.*;
004: import java.net.*;
005: import java.text.*;
006: import java.util.*;
007: import java.util.logging.*;
008: import vicazh.hyperpool.Start;
009: import vicazh.hyperpool.stream.*;
010: import vicazh.hyperpool.stream.net.http.*;
011: import vicazh.hyperpool.stream.Stream;
012:
013: public class AntivirusStream extends ServerStream {
014:
015: public AntivirusStream() {
016: }
017:
018: private String name;
019:
020: public String getName() {
021: return name;
022: }
023:
024: public void setName(String name) {
025: this .name = name;
026: }
027:
028: private String ver;
029:
030: public String getVer() {
031: return ver;
032: }
033:
034: public void setVer(String ver) {
035: this .ver = ver;
036: }
037:
038: private String time;
039:
040: public String getTime() {
041: return time;
042: }
043:
044: public void setTime(String time) {
045: this .time = time;
046: }
047:
048: private File file;
049:
050: private Socket socket;
051:
052: private OutputStream av;
053:
054: private InputStream in;
055:
056: private OutputStream out;
057:
058: private Date start;
059:
060: AntivirusStream(Session session, OutputStream outputstream) {
061: super (session, outputstream);
062: }
063:
064: private AntivirusClamAVConnection c;
065:
066: private boolean status;
067:
068: public void write(int b) throws IOException {
069: if (!status) {
070: status = true;
071: if (session.getClient().getMethod() != null
072: && !session.getClient().getMethod()
073: .equalsIgnoreCase("CONNECT")) {
074: try {
075: c = (AntivirusClamAVConnection) ((Stream) ((AntivirusService) connection.element).clamservice
076: .get(null)).connection;
077: // c.getClient().write("PING\n".getBytes());
078: c.getClient().write("STREAM\n".getBytes());
079: c.getClient().flush();
080: synchronized (c) {
081: if (c.result == null)
082: try {
083: c.wait();
084: } catch (InterruptedException e) {
085: }
086: }
087: StringTokenizer st = new StringTokenizer(c.result);
088: st.nextToken();
089: socket = new Socket(
090: ((AntivirusService) connection.element).outservice
091: .getHost(), Integer.parseInt(st
092: .nextToken()));
093:
094: socket
095: .setSoTimeout(((AntivirusService) connection.element).timeout);
096: av = new BufferedOutputStream(socket
097: .getOutputStream());
098: c.result = null;
099: file = File
100: .createTempFile(
101: ((AntivirusService) connection.element).prefix,
102: null);
103: out = new BufferedOutputStream(
104: new FileOutputStream(file));
105: in = new BufferedInputStream(new FileInputStream(
106: file));
107: outputstream = new NullStream(outputstream);
108: } catch (Exception e) {
109: try {
110: c.getClient().flush();
111: } catch (Exception ex) {
112: }
113: try {
114: c.getClient().close();
115: } catch (Exception ex) {
116: }
117: throw new IOException("ClamAV connection error");
118: }
119: }
120: }
121: if (out != null)
122: out.write(b);
123: super .write(b);
124: }
125:
126: private long len;
127:
128: public void content(int i) throws IOException {
129: if (out != null) {
130: av.write(i);
131: if (len == ((AntivirusService) connection.element)
132: .getBuffer()) {
133: while (in.available() > ((AntivirusService) connection.element)
134: .getBuffer()) {
135: Thread.yield();
136: ((NullStream) outputstream).outputstream.write(in
137: .read());
138: }
139: } else
140: len++;
141: }
142: super .content(i);
143: }
144:
145: public void flush() throws IOException {
146: super .flush();
147: if (out == null)
148: return;
149: av.flush();
150: out.flush();
151: }
152:
153: private String text;
154:
155: public String getText() {
156: return text;
157: }
158:
159: public void setText(String text) {
160: this .text = text;
161: }
162:
163: private String title;
164:
165: public String getTitle() {
166: return title;
167: }
168:
169: public void setTitle(String title) {
170: this .title = title;
171: }
172:
173: private String message;
174:
175: public String getMessage() {
176: return message;
177: }
178:
179: public void setMessage(String message) {
180: this .message = message;
181: }
182:
183: public void end() {
184: super .end();
185: if (out == null)
186: return;
187: try {
188: av.flush();
189: } catch (Exception e) {
190: }
191: try {
192: av.close();
193: } catch (Exception e) {
194: }
195: try {
196: socket.close();
197: } catch (Exception e) {
198: }
199: try {
200: out.flush();
201: } catch (Exception e) {
202: }
203: try {
204: out.close();
205: } catch (Exception e) {
206: }
207: outputstream = ((NullStream) outputstream).outputstream;
208: synchronized (c) {
209: if (c.result == null && !c.c)
210: try {
211: c.wait();
212: } catch (InterruptedException e) {
213: }
214: }
215: try {
216: c.getClient().flush();
217: } catch (Exception e) {
218: }
219: try {
220: c.getClient().close();
221: } catch (Exception e) {
222: }
223: if (c.result == null) {
224: try {
225: in.close();
226: } catch (Exception e) {
227: }
228: file.delete();
229: throw new BreakException();
230: } else if (c.result.contains("ERROR")) {
231: try {
232: in.close();
233: } catch (Exception e) {
234: }
235: file.delete();
236: throw new RuntimeException(c.result);
237: } else if (c.result.equals("stream: OK")) {
238: try {
239: int i;
240: while ((i = in.read()) != -1) {
241: Thread.yield();
242: outputstream.write(i);
243: }
244: } catch (IOException e) {
245: }
246: try {
247: in.close();
248: } catch (Exception e) {
249: }
250: file.delete();
251: } else {
252: try {
253: in.close();
254: } catch (Exception e) {
255: }
256: file.delete();
257: start = new Date();
258: StringTokenizer st = new StringTokenizer(c.result);
259: st.nextToken();
260: text = st.nextToken();
261: try {
262: ((AntivirusService) connection.element).store(session,
263: start);
264: } catch (FileNotFoundException e) {
265: Start.logger.log(Level.SEVERE, e.getMessage(), e);
266: }
267: if (len < ((AntivirusService) connection.element)
268: .getBuffer()) {
269: name = Start.NAME;
270: ver = Start.VERSION;
271: Locale locale = session.getClient().getLocale();
272: time = DateFormat.getDateTimeInstance(
273: DateFormat.DEFAULT, DateFormat.DEFAULT, locale)
274: .format(start);
275: title = ResourceBundle.getBundle(
276: ((AntivirusService) connection.element).base,
277: locale,
278: ((AntivirusService) connection.element).loader)
279: .getString("antivirus");
280: message = ResourceBundle.getBundle(
281: ((AntivirusService) connection.element).base,
282: locale,
283: ((AntivirusService) connection.element).loader)
284: .getString("virus");
285: try {
286: XMLStream cs = (XMLStream) ((Stream) new XMLService(
287: this ).get(outputstream)).outputstream;
288: cs.head("", ((AntivirusService) connection.element)
289: .getUrl(), "");
290: cs.header();
291: synchronized (cs.session.getServer()) {
292: if (!cs.session.getServer().isEnd)
293: try {
294: cs.session.getServer().wait();
295: } catch (InterruptedException e) {
296: }
297: }
298: } catch (IOException e) {
299: Start.logger.log(Level.SEVERE, e.getMessage(), e);
300: }
301: } else {
302: throw new BreakException();
303: }
304: }
305: }
306: }
|