Source Code Cross Referenced for ClientIHttpClient.java in  » Groupware » LibreSource » com » artenum » so6 » dataflow » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Groupware » LibreSource » com.artenum.so6.dataflow 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * LibreSource
003:         * Copyright (C) 2004-2008 Artenum SARL / INRIA
004:         * http://www.libresource.org - contact@artenum.com
005:         *
006:         * This file is part of the LibreSource software, 
007:         * which can be used and distributed under license conditions.
008:         * The license conditions are provided in the LICENSE.TXT file 
009:         * at the root path of the packaging that enclose this file. 
010:         * More information can be found at 
011:         * - http://dev.libresource.org/home/license
012:         *
013:         * Initial authors :
014:         *
015:         * Guillaume Bort / INRIA
016:         * Francois Charoy / Universite Nancy 2
017:         * Julien Forest / Artenum
018:         * Claude Godart / Universite Henry Poincare
019:         * Florent Jouille / INRIA
020:         * Sebastien Jourdain / INRIA / Artenum
021:         * Yves Lerumeur / Artenum
022:         * Pascal Molli / Universite Henry Poincare
023:         * Gerald Oster / INRIA
024:         * Mariarosa Penzi / Artenum
025:         * Gerard Sookahet / Artenum
026:         * Raphael Tani / INRIA
027:         *
028:         * Contributors :
029:         *
030:         * Stephane Bagnier / Artenum
031:         * Amadou Dia / Artenum-IUP Blois
032:         * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033:         */package com.artenum.so6.dataflow;
034:
035:        import com.artenum.so6.dataflow.util.ProxyLoginPasswordPanel;
036:
037:        import org.apache.commons.httpclient.HttpClient;
038:        import org.apache.commons.httpclient.HttpException;
039:        import org.apache.commons.httpclient.ProtocolException;
040:        import org.apache.commons.httpclient.UsernamePasswordCredentials;
041:        import org.apache.commons.httpclient.auth.AuthScope;
042:        import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
043:        import org.apache.commons.httpclient.methods.PostMethod;
044:
045:        import org.libresource.so6.core.StateMonitoring;
046:        import org.libresource.so6.core.client.AuthenticationException;
047:        import org.libresource.so6.core.client.ConnectionException;
048:        import org.libresource.so6.core.client.InvalideTicketException;
049:        import org.libresource.so6.core.client.LocalException;
050:        import org.libresource.so6.core.client.PatchNotFoundException;
051:        import org.libresource.so6.core.client.ServerException;
052:        import org.libresource.so6.core.client.UnableToContactServerException;
053:        import org.libresource.so6.core.engine.util.CryptUtil;
054:        import org.libresource.so6.core.engine.util.MonitoredInputStream;
055:        import org.libresource.so6.core.net.DataflowClientI;
056:
057:        import java.io.File;
058:        import java.io.FileInputStream;
059:        import java.io.FileOutputStream;
060:        import java.io.IOException;
061:        import java.io.InputStream;
062:
063:        import java.net.URL;
064:
065:        import java.util.ArrayList;
066:        import java.util.Properties;
067:        import java.util.StringTokenizer;
068:
069:        /**
070:         * @author smack (artenum)
071:         */
072:        public class ClientIHttpClient implements  DataflowClientI {
073:            // Default properties
074:            public final static String SO6_SERVICE_URL = "so6.service.url";
075:            public final static String SO6_DOWNLOAD_DIR = "so6.download.dir";
076:
077:            //
078:            public final static String SO6_ACTION = "so6.action";
079:
080:            // Internal parameters
081:            private static int BUFFER_SIZE = 1024 * 1024 * 2;
082:
083:            // HTTP header props
084:            protected final static String SO6_PATCH_TICKET = "so6.patch.ticket";
085:            protected final static String SO6_PATCH_FROM_TICKET = "so6.patch.from.ticket";
086:            protected final static String SO6_PATCH_TO_TICKET = "so6.patch.to.ticket";
087:            protected final static String SO6_BIN_EXT = "so6.bin.ext";
088:            public final static String SO6_SYNCHRONIZER_URI = "so6.synchronizer.uri";
089:            public final static String SO6_REPLICA_TICKET = "so6.replica.ticket";
090:            public final static String SO6_WORKSPACE_ID = "so6.ws.id";
091:            public final static String SO6_REPLICA_URI = "so6.replica.uri";
092:            public final static String SO6_WSC_PATH = "so6.wsc.path";
093:            public final static String SO6_WS_NAME = "so6.ws.name";
094:            public final static String SO6_VALIDATE_TICKET_NUMBER = "so6.validate.ticket.number";
095:
096:            // status code
097:            public final static int STATUS_CODE_OK = 200;
098:            public final static int STATUS_CODE_AUTH_ERROR = 402;
099:            public final static int STATUS_CODE_SERVER_ERROR = 403;
100:            public final static int STATUS_CODE_INVALIDE_TICKET = 404;
101:            public final static int STATUS_CODE_PATCH_NOT_FOUND = 405;
102:
103:            // Client http
104:            private HttpClient httpClient;
105:
106:            // props
107:            private String serviceUrl;
108:            private String login;
109:            private String password;
110:            private String synchronizerURI;
111:            private String replicaURI;
112:            private String downloadDir = System.getProperty("java.io.tmpdir")
113:                    + "/so6.tmp_" + System.getProperty("user.name");
114:
115:            public ClientIHttpClient(Properties props) {
116:                // Create tmp dir
117:                new File(downloadDir).mkdirs();
118:
119:                // init proxy
120:                httpClient = new HttpClient();
121:                this .initProxy(httpClient);
122:
123:                // init client
124:                serviceUrl = props.getProperty(SO6_SERVICE_URL);
125:                login = props.getProperty(SO6_LOGIN);
126:
127:                if (props.getProperty(SO6_PASSWORD) != null) {
128:                    password = CryptUtil
129:                            .decode(props.getProperty(SO6_PASSWORD));
130:                }
131:
132:                synchronizerURI = props.getProperty(SO6_QUEUE_ID);
133:                replicaURI = props.getProperty(SO6_CONNECTION_ID);
134:
135:                //
136:                if ((serviceUrl == null) || (synchronizerURI == null)) {
137:                    throw new RuntimeException(
138:                            "Properties are missing for ClientIHttpClient initilisation");
139:                }
140:            }
141:
142:            private void initProxy(HttpClient client) {
143:                String proxyHost = null;
144:                int proxyPort = -1;
145:
146:                // Default java 1.4 Web start props
147:                proxyHost = System.getProperty("proxyHost");
148:
149:                if (System.getProperty("proxyPort") != null) {
150:                    proxyPort = Integer.parseInt(System
151:                            .getProperty("proxyPort"));
152:                }
153:
154:                // Java Web start setting
155:                if ((proxyHost == null)
156:                        && (System.getProperty("deployment.javaws.proxy.http") != null)) {
157:                    proxyHost = System
158:                            .getProperty("deployment.javaws.proxy.http");
159:
160:                    if (proxyHost.startsWith("http:\\\\")) {
161:                        proxyHost = proxyHost.substring("http:\\\\".length());
162:                    }
163:
164:                    if (System.getProperty("deployment.javaws.proxy.httpport") != null) {
165:                        proxyPort = Integer
166:                                .parseInt(System
167:                                        .getProperty("deployment.javaws.proxy.httpport"));
168:                    }
169:                }
170:
171:                // Java 1.6 manual setting
172:                if ((proxyHost == null)
173:                        && (System.getProperty("deployment.proxy.http.host") != null)) {
174:                    proxyHost = System
175:                            .getProperty("deployment.proxy.http.host");
176:
177:                    if (proxyHost.startsWith("http:\\\\")) {
178:                        proxyHost = proxyHost.substring("http:\\\\".length());
179:                    }
180:
181:                    if (System.getProperty("deployment.proxy.http.port") != null) {
182:                        proxyPort = Integer.parseInt(System
183:                                .getProperty("deployment.proxy.http.port"));
184:                    }
185:                }
186:
187:                // Java 1.6 auto setting
188:                if ((proxyHost == null)
189:                        && (System
190:                                .getProperty("deployment.proxy.auto.config.url") != null)) {
191:                    try {
192:                        URL proxyURL = new URL(
193:                                System
194:                                        .getProperty("deployment.proxy.auto.config.url"));
195:                        proxyHost = proxyURL.getHost();
196:                        proxyPort = proxyURL.getPort();
197:                    } catch (Exception e) {
198:                        e.printStackTrace();
199:                    }
200:                }
201:
202:                // Init proxy
203:                if (proxyHost != null) {
204:                    if (proxyPort == -1) {
205:                        proxyPort = 80;
206:                    }
207:
208:                    client.getHostConfiguration()
209:                            .setProxy(proxyHost, proxyPort);
210:
211:                    // Ask if proxy need credential
212:                    ProxyLoginPasswordPanel panel = new ProxyLoginPasswordPanel(
213:                            "Proxy authentication");
214:
215:                    if (panel.isAuthentificationAvailable()) {
216:                        client.getState().setProxyCredentials(
217:                                AuthScope.ANY,
218:                                new UsernamePasswordCredentials(panel
219:                                        .getLogin(), panel.getPassword()));
220:                        client.getParams().setAuthenticationPreemptive(true);
221:                    }
222:                }
223:            }
224:
225:            private void basicHTTPLogin(String login, String password,
226:                    PostMethod post) {
227:                // String crypt = login + ":" + password;
228:                // post.addRequestHeader("auth", Base64.encode(crypt.getBytes()));
229:                post.addRequestHeader("login", login);
230:                post.addRequestHeader("password", password);
231:            }
232:
233:            private PostMethod createPost(String action) {
234:                PostMethod post = new PostMethod(serviceUrl);
235:                post.addRequestHeader(SO6_ACTION, action);
236:                post.addRequestHeader(SO6_SYNCHRONIZER_URI, synchronizerURI);
237:
238:                // Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
239:                if ((login != null) && (password != null)) {
240:                    basicHTTPLogin(login, password, post);
241:                }
242:
243:                return post;
244:            }
245:
246:            public void createWorkspace(String wsId) throws LocalException,
247:                    ServerException, AuthenticationException,
248:                    UnableToContactServerException {
249:                PostMethod post = createPost("createWorkspace");
250:                post.addRequestHeader(SO6_WORKSPACE_ID, wsId);
251:
252:                int statusCode;
253:
254:                try {
255:                    statusCode = httpClient.executeMethod(post);
256:                    post.getResponseBodyAsString();
257:                    post.releaseConnection();
258:                } catch (HttpException e) {
259:                    throw new UnableToContactServerException(e);
260:                } catch (IOException e) {
261:                    throw new LocalException(e);
262:                }
263:
264:                switch (statusCode) {
265:                case STATUS_CODE_OK:
266:
267:                    // post.disconnect();
268:                    break;
269:
270:                case STATUS_CODE_AUTH_ERROR:
271:                    throw new AuthenticationException();
272:
273:                case STATUS_CODE_SERVER_ERROR:
274:                default:
275:                    throw new ServerException(
276:                            "Server error on createWorkspace. Status code="
277:                                    + statusCode);
278:                }
279:            }
280:
281:            public void removeWorkspace(String wsId) throws LocalException,
282:                    ServerException, AuthenticationException,
283:                    UnableToContactServerException {
284:                PostMethod post = createPost("removeWorkspace");
285:                post.addRequestHeader(SO6_WORKSPACE_ID, wsId);
286:
287:                int statusCode;
288:
289:                try {
290:                    statusCode = httpClient.executeMethod(post);
291:                    post.getResponseBodyAsString();
292:                    post.releaseConnection();
293:                } catch (HttpException e) {
294:                    throw new UnableToContactServerException(e);
295:                } catch (IOException e) {
296:                    throw new LocalException(e);
297:                }
298:
299:                switch (statusCode) {
300:                case STATUS_CODE_OK:
301:
302:                    // post.disconnect();
303:                    break;
304:
305:                case STATUS_CODE_AUTH_ERROR:
306:                    throw new AuthenticationException();
307:
308:                case STATUS_CODE_SERVER_ERROR:
309:                default:
310:                    throw new ServerException(
311:                            "Server error on removeWorkspace. Status code="
312:                                    + statusCode);
313:                }
314:            }
315:
316:            public void notifyWorkspaceConnections(String wsId,
317:                    String[] queueIds) throws LocalException, ServerException,
318:                    AuthenticationException, UnableToContactServerException {
319:                // Not Yet implemented
320:                throw new ServerException(
321:                        "Not yet implemented (notifyWorkspaceConnections)");
322:            }
323:
324:            public String addWsConnection(String wsId, String queueId,
325:                    String wsName, String wscPath) throws LocalException,
326:                    ServerException, AuthenticationException,
327:                    UnableToContactServerException {
328:                PostMethod post = createPost("addWsConnection");
329:                post.addRequestHeader(SO6_WORKSPACE_ID, wsId);
330:                post.addRequestHeader(SO6_SYNCHRONIZER_URI, queueId);
331:                post.addRequestHeader(SO6_WS_NAME, wsName);
332:                post.addRequestHeader(SO6_WSC_PATH, wscPath);
333:
334:                int statusCode;
335:                String result = null;
336:
337:                try {
338:                    statusCode = httpClient.executeMethod(post);
339:                    result = post.getResponseBodyAsString();
340:                    post.releaseConnection();
341:                } catch (HttpException e) {
342:                    throw new UnableToContactServerException(e);
343:                } catch (IOException e) {
344:                    throw new LocalException(e);
345:                }
346:
347:                switch (statusCode) {
348:                case STATUS_CODE_OK:
349:                    return result;
350:
351:                case STATUS_CODE_AUTH_ERROR:
352:                    throw new AuthenticationException();
353:
354:                case STATUS_CODE_SERVER_ERROR:
355:                default:
356:                    throw new ServerException(
357:                            "Server error on addWsConnection. Status code="
358:                                    + statusCode);
359:                }
360:            }
361:
362:            public void removeWsConnection(String wsId, String queueId)
363:                    throws LocalException, ServerException,
364:                    AuthenticationException, UnableToContactServerException {
365:                PostMethod post = createPost("removeWsConnection");
366:                post.addRequestHeader(SO6_WORKSPACE_ID, wsId);
367:                post.addRequestHeader(SO6_SYNCHRONIZER_URI, queueId);
368:
369:                int statusCode;
370:
371:                try {
372:                    statusCode = httpClient.executeMethod(post);
373:                    post.getResponseBodyAsString();
374:                    post.releaseConnection();
375:                } catch (HttpException e) {
376:                    throw new UnableToContactServerException(e);
377:                } catch (IOException e) {
378:                    throw new LocalException(e);
379:                }
380:
381:                switch (statusCode) {
382:                case STATUS_CODE_OK:
383:                    break;
384:
385:                case STATUS_CODE_AUTH_ERROR:
386:                    throw new AuthenticationException();
387:
388:                case STATUS_CODE_SERVER_ERROR:
389:                default:
390:                    throw new ServerException(
391:                            "Server error on removeWsConnection. Status code="
392:                                    + statusCode);
393:                }
394:            }
395:
396:            public String getNetworkFromWorkspace(String wsId) throws Exception {
397:                PostMethod post = createPost("getNetworkFromWorkspace");
398:                post.addRequestHeader(SO6_WORKSPACE_ID, wsId);
399:
400:                int statusCode;
401:
402:                try {
403:                    statusCode = httpClient.executeMethod(post);
404:                } catch (HttpException e) {
405:                    throw new UnableToContactServerException(e);
406:                } catch (IOException e) {
407:                    throw new LocalException(e);
408:                }
409:
410:                switch (statusCode) {
411:                case STATUS_CODE_OK:
412:
413:                    try {
414:                        File tmpFile = File.createTempFile("wsNetwork", null,
415:                                new File(downloadDir));
416:                        InputStream is = post.getResponseBodyAsStream();
417:                        FileOutputStream fos = new FileOutputStream(tmpFile);
418:                        int length;
419:                        byte[] buffer = new byte[BUFFER_SIZE];
420:
421:                        while ((length = is.read(buffer)) != -1) {
422:                            fos.write(buffer, 0, length);
423:                        }
424:
425:                        fos.close();
426:                        is.close();
427:
428:                        // post.disconnect();
429:                        post.releaseConnection();
430:
431:                        return tmpFile.getPath();
432:                    } catch (Exception e) {
433:                        throw new LocalException(e);
434:                    }
435:
436:                case STATUS_CODE_AUTH_ERROR:
437:                    throw new AuthenticationException();
438:
439:                case STATUS_CODE_SERVER_ERROR:
440:                default:
441:                    throw new ServerException(
442:                            "Server error on getNetworkFromWorkspace. Status code="
443:                                    + statusCode);
444:                }
445:            }
446:
447:            public String getNetworkFromQueue(String queueId) throws Exception {
448:                PostMethod post = createPost("getNetworkFromQueue");
449:                post.addRequestHeader(SO6_QUEUE_ID, queueId);
450:
451:                int statusCode;
452:
453:                try {
454:                    statusCode = httpClient.executeMethod(post);
455:                } catch (HttpException e) {
456:                    throw new UnableToContactServerException(e);
457:                } catch (IOException e) {
458:                    throw new LocalException(e);
459:                }
460:
461:                switch (statusCode) {
462:                case STATUS_CODE_OK:
463:
464:                    try {
465:                        File tmpFile = File.createTempFile("queueNetwork",
466:                                null, new File(downloadDir));
467:                        InputStream is = post.getResponseBodyAsStream();
468:                        FileOutputStream fos = new FileOutputStream(tmpFile);
469:                        int length;
470:                        byte[] buffer = new byte[BUFFER_SIZE];
471:
472:                        while ((length = is.read(buffer)) != -1) {
473:                            fos.write(buffer, 0, length);
474:                        }
475:
476:                        fos.close();
477:                        is.close();
478:
479:                        // post.disconnect();
480:                        post.releaseConnection();
481:
482:                        return tmpFile.getPath();
483:                    } catch (Exception e) {
484:                        throw new LocalException(e);
485:                    }
486:
487:                case STATUS_CODE_AUTH_ERROR:
488:                    throw new AuthenticationException();
489:
490:                case STATUS_CODE_SERVER_ERROR:
491:                default:
492:                    throw new ServerException(
493:                            "Server error on getNetworkFromWorkspace. Status code="
494:                                    + statusCode);
495:                }
496:            }
497:
498:            public void notifyQueue(long lastTicket) throws Exception {
499:                PostMethod post = createPost("notifyQueue");
500:                post.addRequestHeader(SO6_REPLICA_URI, replicaURI);
501:                post.addRequestHeader(SO6_REPLICA_TICKET, Long
502:                        .toString(lastTicket));
503:
504:                int statusCode;
505:
506:                try {
507:                    statusCode = httpClient.executeMethod(post);
508:                    post.getResponseBodyAsString();
509:                    post.releaseConnection();
510:                } catch (HttpException e) {
511:                    throw new UnableToContactServerException(e);
512:                } catch (IOException e) {
513:                    throw new LocalException(e);
514:                }
515:
516:                switch (statusCode) {
517:                case STATUS_CODE_OK:
518:
519:                    // post.disconnect();
520:                    return;
521:
522:                case STATUS_CODE_AUTH_ERROR:
523:                    throw new AuthenticationException();
524:
525:                case STATUS_CODE_SERVER_ERROR:
526:                default:
527:                    throw new ServerException(
528:                            "Server error on notifyQueue. Status code="
529:                                    + statusCode);
530:                }
531:            }
532:
533:            public long getLastTicket() throws AuthenticationException,
534:                    UnableToContactServerException, ServerException,
535:                    ConnectionException, LocalException {
536:                PostMethod post = createPost("getLastTicket");
537:                int statusCode;
538:
539:                try {
540:                    statusCode = httpClient.executeMethod(post);
541:                } catch (HttpException e) {
542:                    throw new UnableToContactServerException(e);
543:                } catch (IOException e) {
544:                    throw new LocalException(e);
545:                }
546:
547:                switch (statusCode) {
548:                case STATUS_CODE_OK:
549:
550:                    long result = Long.parseLong(post.getResponseHeader(
551:                            "lastTicket").getValue());
552:                    post.releaseConnection();
553:
554:                    return result;
555:
556:                case STATUS_CODE_AUTH_ERROR:
557:                    throw new AuthenticationException();
558:
559:                case STATUS_CODE_SERVER_ERROR:
560:                default:
561:                    throw new ServerException(
562:                            "Server error on getLastTicket. Status code="
563:                                    + statusCode);
564:                }
565:            }
566:
567:            public String getPatch(long ticket) throws AuthenticationException,
568:                    PatchNotFoundException, UnableToContactServerException,
569:                    ServerException, ConnectionException, LocalException {
570:                PostMethod post = createPost("getPatch");
571:                post.addRequestHeader(SO6_PATCH_TICKET, Long.toString(ticket));
572:
573:                int statusCode;
574:
575:                try {
576:                    statusCode = httpClient.executeMethod(post);
577:                } catch (HttpException e) {
578:                    throw new UnableToContactServerException(e);
579:                } catch (IOException e) {
580:                    throw new LocalException(e);
581:                }
582:
583:                switch (statusCode) {
584:                case STATUS_CODE_OK:
585:                    StateMonitoring.getInstance().setXMLMonitoringStartSubCall(
586:                            1, "");
587:
588:                    String patchName = post.getResponseHeader("patchName")
589:                            .getValue();
590:                    String patchPathName = downloadDir + File.separator
591:                            + patchName;
592:                    MonitoredInputStream mis;
593:
594:                    try {
595:                        mis = new MonitoredInputStream(post
596:                                .getResponseBodyAsStream(), Long
597:                                .parseLong(post.getResponseHeader(
598:                                        "Content-Length").getValue()));
599:                    } catch (IOException e) {
600:                        throw new ConnectionException(e);
601:                    }
602:
603:                    mis.setComment("Download finished", "Downloading");
604:
605:                    try {
606:                        FileOutputStream fos = new FileOutputStream(
607:                                patchPathName);
608:                        int length;
609:                        byte[] buffer = new byte[BUFFER_SIZE];
610:
611:                        while ((length = mis.read(buffer)) != -1) {
612:                            fos.write(buffer, 0, length);
613:                        }
614:
615:                        fos.close();
616:                        mis.close();
617:                    } catch (Exception e) {
618:                        throw new LocalException(e);
619:                    }
620:
621:                    StateMonitoring.getInstance().setXMLMonitoringEndSubCall();
622:
623:                    post.releaseConnection();
624:
625:                    // post.disconnect();
626:                    return patchPathName;
627:
628:                case STATUS_CODE_AUTH_ERROR:
629:                    throw new AuthenticationException();
630:
631:                case STATUS_CODE_INVALIDE_TICKET:
632:                case STATUS_CODE_PATCH_NOT_FOUND:
633:                    throw new PatchNotFoundException();
634:
635:                case STATUS_CODE_SERVER_ERROR:
636:                default:
637:                    throw new ServerException(
638:                            "Server error on getPatch. Status code="
639:                                    + statusCode);
640:                }
641:            }
642:
643:            public String getPatch(long fromTicket, long toTicket)
644:                    throws AuthenticationException, PatchNotFoundException,
645:                    UnableToContactServerException, ServerException,
646:                    ConnectionException, LocalException {
647:                PostMethod post = createPost("getPatch2");
648:                post.addRequestHeader(SO6_PATCH_FROM_TICKET, Long
649:                        .toString(fromTicket));
650:                post.addRequestHeader(SO6_PATCH_TO_TICKET, Long
651:                        .toString(toTicket));
652:
653:                int statusCode;
654:
655:                try {
656:                    statusCode = httpClient.executeMethod(post);
657:                } catch (HttpException e) {
658:                    throw new UnableToContactServerException(e);
659:                } catch (IOException e) {
660:                    throw new LocalException(e);
661:                }
662:
663:                switch (statusCode) {
664:                case STATUS_CODE_OK:
665:                    StateMonitoring.getInstance().setXMLMonitoringStartSubCall(
666:                            1, "");
667:
668:                    String patchName = post.getResponseHeader("patchName")
669:                            .getValue();
670:                    String patchPathName = downloadDir + File.separator
671:                            + patchName;
672:                    MonitoredInputStream mis;
673:
674:                    try {
675:                        mis = new MonitoredInputStream(post
676:                                .getResponseBodyAsStream(), Long
677:                                .parseLong(post.getResponseHeader(
678:                                        "Content-Length").getValue()));
679:                    } catch (IOException e) {
680:                        throw new ConnectionException(e);
681:                    }
682:
683:                    mis.setComment("Download finished", "Downloading");
684:
685:                    try {
686:                        FileOutputStream fos = new FileOutputStream(
687:                                patchPathName);
688:                        int length;
689:                        byte[] buffer = new byte[BUFFER_SIZE];
690:
691:                        while ((length = mis.read(buffer)) != -1) {
692:                            fos.write(buffer, 0, length);
693:                        }
694:
695:                        fos.close();
696:                        mis.close();
697:                    } catch (Exception e) {
698:                        throw new LocalException(e);
699:                    }
700:
701:                    StateMonitoring.getInstance().setXMLMonitoringEndSubCall();
702:
703:                    post.releaseConnection();
704:
705:                    // post.disconnect();
706:                    return patchPathName;
707:
708:                case STATUS_CODE_AUTH_ERROR:
709:                    throw new AuthenticationException();
710:
711:                case STATUS_CODE_INVALIDE_TICKET:
712:                case STATUS_CODE_PATCH_NOT_FOUND:
713:                    throw new PatchNotFoundException();
714:
715:                case STATUS_CODE_SERVER_ERROR:
716:                default:
717:                    throw new ServerException(
718:                            "Server error on getPatch(2). Status code="
719:                                    + statusCode);
720:                }
721:            }
722:
723:            public void sendPatch(long ticket, long lastticket,
724:                    String patchfile, boolean validate)
725:                    throws AuthenticationException, InvalideTicketException,
726:                    UnableToContactServerException, ServerException,
727:                    ConnectionException, LocalException {
728:                int statusCode;
729:                PostMethod post = null;
730:
731:                try {
732:                    post = createPost("sendPatch");
733:                    post.addRequestHeader(SO6_PATCH_FROM_TICKET, Long
734:                            .toString(ticket));
735:                    post.addRequestHeader(SO6_PATCH_TO_TICKET, Long
736:                            .toString(lastticket));
737:                    post
738:                            .addRequestHeader(SO6_SYNCHRONIZER_URI,
739:                                    synchronizerURI);
740:                    post.addRequestHeader(SO6_VALIDATE_TICKET_NUMBER, Boolean
741:                            .toString(validate));
742:
743:                    final File patch = new File(patchfile);
744:                    MonitoredInputStream mis = null;
745:
746:                    try {
747:                        mis = new MonitoredInputStream(new FileInputStream(
748:                                patch), patch.length());
749:
750:                        // old way  post.setRequestContentLength((int) patch.length());  post.setRequestBody(mis);
751:                        post.setRequestEntity(new InputStreamRequestEntity(mis,
752:                                patch.length()));
753:                        statusCode = httpClient.executeMethod(post);
754:                        System.out.println("Ok the first time");
755:                    } catch (ProtocolException e) {
756:                        System.out.println("Failed the first time");
757:
758:                        // Try again
759:                        mis = new MonitoredInputStream(new FileInputStream(
760:                                patch), patch.length());
761:                        post.setRequestEntity(new InputStreamRequestEntity(mis,
762:                                patch.length()));
763:                        statusCode = httpClient.executeMethod(post);
764:                    }
765:
766:                    switch (statusCode) {
767:                    case 202:
768:                        post.getResponseBodyAsString();
769:
770:                        // good response
771:                        break;
772:
773:                    case STATUS_CODE_AUTH_ERROR:
774:                        throw new AuthenticationException();
775:
776:                    case STATUS_CODE_INVALIDE_TICKET:
777:                        throw new InvalideTicketException();
778:
779:                    case STATUS_CODE_SERVER_ERROR:
780:                    default:
781:                        throw new ServerException(
782:                                "Server error on sendPatch. Status code="
783:                                        + statusCode);
784:                    }
785:                } catch (HttpException e) {
786:                    throw new UnableToContactServerException(e);
787:                } catch (IOException e) {
788:                    throw new LocalException(e);
789:                } catch (NumberFormatException e) {
790:                    throw new LocalException(e);
791:                } finally {
792:                    post.releaseConnection();
793:                }
794:            }
795:
796:            public long[][] listPatch() throws AuthenticationException,
797:                    UnableToContactServerException, ServerException,
798:                    ConnectionException, LocalException {
799:                PostMethod post = createPost("listPatch");
800:                int statusCode;
801:                String patchArray = null;
802:
803:                try {
804:                    statusCode = httpClient.executeMethod(post);
805:                    patchArray = post.getResponseBodyAsString();
806:                } catch (HttpException e) {
807:                    throw new UnableToContactServerException(e);
808:                } catch (IOException e) {
809:                    throw new LocalException(e);
810:                }
811:
812:                switch (statusCode) {
813:                case STATUS_CODE_OK:
814:
815:                    StringTokenizer st = new StringTokenizer(patchArray, "\n");
816:                    ArrayList patchs = new ArrayList();
817:
818:                    while (st.hasMoreTokens()) {
819:                        patchs.add(st.nextToken());
820:                    }
821:
822:                    long[][] result = new long[patchs.size()][3];
823:
824:                    for (int i = 0; i < result.length; i++) {
825:                        result[i][0] = Long.parseLong(((String) patchs.get(i))
826:                                .split(" ")[0]);
827:                        result[i][1] = Long.parseLong(((String) patchs.get(i))
828:                                .split(" ")[1]);
829:                        result[i][2] = Long.parseLong(((String) patchs.get(i))
830:                                .split(" ")[2]);
831:                    }
832:
833:                    post.releaseConnection();
834:
835:                    return result;
836:
837:                case STATUS_CODE_AUTH_ERROR:
838:                    throw new AuthenticationException();
839:
840:                case STATUS_CODE_INVALIDE_TICKET:
841:                case STATUS_CODE_PATCH_NOT_FOUND:
842:                case STATUS_CODE_SERVER_ERROR:
843:                default:
844:                    throw new ServerException(
845:                            "Server error on listPatch. Status code="
846:                                    + statusCode);
847:                }
848:            }
849:
850:            public void addBinExt(String ext) throws AuthenticationException,
851:                    UnableToContactServerException, ServerException,
852:                    ConnectionException, LocalException {
853:                PostMethod post = createPost("addBinExt");
854:                post.addParameter(SO6_BIN_EXT, ext);
855:
856:                int statusCode;
857:
858:                try {
859:                    statusCode = httpClient.executeMethod(post);
860:                    post.getResponseBodyAsString();
861:                    post.releaseConnection();
862:                } catch (HttpException e) {
863:                    throw new UnableToContactServerException(e);
864:                } catch (IOException e) {
865:                    throw new LocalException(e);
866:                }
867:
868:                switch (statusCode) {
869:                case STATUS_CODE_OK:
870:                    break;
871:
872:                case STATUS_CODE_AUTH_ERROR:
873:                    throw new AuthenticationException();
874:
875:                case STATUS_CODE_SERVER_ERROR:
876:                default:
877:                    throw new ServerException(
878:                            "Server error on addBinExt. Status code="
879:                                    + statusCode);
880:                }
881:            }
882:
883:            public void removeBinExt(String ext)
884:                    throws AuthenticationException,
885:                    UnableToContactServerException, ServerException,
886:                    ConnectionException, LocalException {
887:                PostMethod post = createPost("removeBinExt");
888:                post.addParameter(SO6_BIN_EXT, ext);
889:
890:                int statusCode;
891:
892:                try {
893:                    statusCode = httpClient.executeMethod(post);
894:                    post.getResponseBodyAsString();
895:                    post.releaseConnection();
896:                } catch (HttpException e) {
897:                    throw new UnableToContactServerException(e);
898:                } catch (IOException e) {
899:                    throw new LocalException(e);
900:                }
901:
902:                switch (statusCode) {
903:                case STATUS_CODE_OK:
904:                    break;
905:
906:                case STATUS_CODE_AUTH_ERROR:
907:                    throw new AuthenticationException();
908:
909:                case STATUS_CODE_INVALIDE_TICKET:
910:                case STATUS_CODE_PATCH_NOT_FOUND:
911:                case STATUS_CODE_SERVER_ERROR:
912:                default:
913:                    throw new ServerException(
914:                            "Server error on removeBinExt. Status code="
915:                                    + statusCode);
916:                }
917:            }
918:
919:            public String getBinExt() throws AuthenticationException,
920:                    UnableToContactServerException, ServerException,
921:                    ConnectionException, LocalException {
922:                PostMethod post = createPost("getBinExt");
923:                int statusCode;
924:                String result = null;
925:
926:                try {
927:                    statusCode = httpClient.executeMethod(post);
928:                    result = post.getResponseBodyAsString();
929:                    post.releaseConnection();
930:                } catch (HttpException e) {
931:                    throw new UnableToContactServerException(e);
932:                } catch (IOException e) {
933:                    throw new LocalException(e);
934:                }
935:
936:                switch (statusCode) {
937:                case STATUS_CODE_OK:
938:                    return result;
939:
940:                case STATUS_CODE_AUTH_ERROR:
941:                    throw new AuthenticationException();
942:
943:                case STATUS_CODE_INVALIDE_TICKET:
944:                case STATUS_CODE_PATCH_NOT_FOUND:
945:                case STATUS_CODE_SERVER_ERROR:
946:                default:
947:                    throw new ServerException(
948:                            "Server error on getBinExt. Status code="
949:                                    + statusCode);
950:                }
951:            }
952:
953:            public static String[] getInternalPropertyList() {
954:                return new String[] { SO6_QUEUE_ID, SO6_LOGIN, SO6_PASSWORD,
955:                        SO6_SERVICE_URL, SO6_CONNECTION_ID, SO6_XML_DETECT };
956:            }
957:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.