Source Code Cross Referenced for ClientIServletImpl.java in  » Groupware » LibreSource » org » libresource » so6 » core » client » 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 » org.libresource.so6.core.client 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * LibreSource
0003:         * Copyright (C) 2004-2008 Artenum SARL / INRIA
0004:         * http://www.libresource.org - contact@artenum.com
0005:         *
0006:         * This file is part of the LibreSource software, 
0007:         * which can be used and distributed under license conditions.
0008:         * The license conditions are provided in the LICENSE.TXT file 
0009:         * at the root path of the packaging that enclose this file. 
0010:         * More information can be found at 
0011:         * - http://dev.libresource.org/home/license
0012:         *
0013:         * Initial authors :
0014:         *
0015:         * Guillaume Bort / INRIA
0016:         * Francois Charoy / Universite Nancy 2
0017:         * Julien Forest / Artenum
0018:         * Claude Godart / Universite Henry Poincare
0019:         * Florent Jouille / INRIA
0020:         * Sebastien Jourdain / INRIA / Artenum
0021:         * Yves Lerumeur / Artenum
0022:         * Pascal Molli / Universite Henry Poincare
0023:         * Gerald Oster / INRIA
0024:         * Mariarosa Penzi / Artenum
0025:         * Gerard Sookahet / Artenum
0026:         * Raphael Tani / INRIA
0027:         *
0028:         * Contributors :
0029:         *
0030:         * Stephane Bagnier / Artenum
0031:         * Amadou Dia / Artenum-IUP Blois
0032:         * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0033:         */package org.libresource.so6.core.client;
0034:
0035:        import org.libresource.so6.core.StateMonitoring;
0036:        import org.libresource.so6.core.engine.util.MonitoredInputStream;
0037:        import org.libresource.so6.core.net.DataflowClientI;
0038:
0039:        import java.io.BufferedReader;
0040:        import java.io.File;
0041:        import java.io.FileInputStream;
0042:        import java.io.FileNotFoundException;
0043:        import java.io.FileOutputStream;
0044:        import java.io.IOException;
0045:        import java.io.InputStream;
0046:        import java.io.InputStreamReader;
0047:        import java.io.LineNumberReader;
0048:        import java.io.OutputStream;
0049:        import java.io.OutputStreamWriter;
0050:
0051:        import java.net.HttpURLConnection;
0052:        import java.net.MalformedURLException;
0053:        import java.net.Socket;
0054:        import java.net.SocketException;
0055:        import java.net.URL;
0056:        import java.net.UnknownHostException;
0057:
0058:        import java.util.ArrayList;
0059:        import java.util.Properties;
0060:        import java.util.StringTokenizer;
0061:
0062:        /**
0063:         * @author smack
0064:         */
0065:        public class ClientIServletImpl implements  DataflowClientI {
0066:            // Default properties
0067:            public final static String SO6_SERVICE_URL = "so6.service.url";
0068:            public final static String SO6_DOWNLOAD_DIR = "so6.download.dir";
0069:
0070:            //
0071:            public final static String SO6_ACTION = "so6.action";
0072:
0073:            // Internal parameters
0074:            private static int BUFFER_SIZE = 1024 * 1024 * 2;
0075:
0076:            // HTTP header props
0077:            protected final static String SO6_PATCH_TICKET = "so6.patch.ticket";
0078:            protected final static String SO6_PATCH_FROM_TICKET = "so6.patch.from.ticket";
0079:            protected final static String SO6_PATCH_TO_TICKET = "so6.patch.to.ticket";
0080:            protected final static String SO6_BIN_EXT = "so6.bin.ext";
0081:            public final static String SO6_SYNCHRONIZER_URI = "so6.synchronizer.uri";
0082:            public final static String SO6_REPLICA_TICKET = "so6.replica.ticket";
0083:            public final static String SO6_WORKSPACE_ID = "so6.ws.id";
0084:            public final static String SO6_REPLICA_URI = "so6.replica.uri";
0085:            public final static String SO6_WSC_PATH = "so6.wsc.path";
0086:            public final static String SO6_WS_NAME = "so6.ws.name";
0087:            public final static String SO6_VALIDATE_TICKET_NUMBER = "so6.validate.ticket.number";
0088:
0089:            // status code
0090:            public final static int STATUS_CODE_OK = 200;
0091:            public final static int STATUS_CODE_AUTH_ERROR = 402;
0092:            public final static int STATUS_CODE_SERVER_ERROR = 403;
0093:            public final static int STATUS_CODE_INVALIDE_TICKET = 404;
0094:            public final static int STATUS_CODE_PATCH_NOT_FOUND = 405;
0095:
0096:            // Client http
0097:            private HttpURLConnection httpUrlConnection;
0098:
0099:            // props
0100:            private String serviceUrl;
0101:            private String login;
0102:            private String password;
0103:            private String synchronizerURI;
0104:            private String replicaURI;
0105:            private String downloadDir = System.getProperty("java.io.tmpdir")
0106:                    + "/so6.tmp_" + System.getProperty("user.name");
0107:
0108:            public ClientIServletImpl() {
0109:            }
0110:
0111:            public ClientIServletImpl(Properties props) throws Exception {
0112:                // init client
0113:                serviceUrl = props.getProperty(SO6_SERVICE_URL);
0114:                login = props.getProperty(SO6_LOGIN);
0115:
0116:                if (props.getProperty(SO6_PASSWORD) != null) {
0117:                    password = props.getProperty(SO6_PASSWORD);
0118:
0119:                    // CryptUtil.decode(props.getProperty(SO6_PASSWORD));
0120:                }
0121:
0122:                synchronizerURI = props.getProperty(SO6_QUEUE_ID);
0123:                replicaURI = props.getProperty(SO6_CONNECTION_ID);
0124:
0125:                //
0126:                if ((serviceUrl == null) || (synchronizerURI == null)) {
0127:                    throw new RuntimeException(
0128:                            "Properties are missing for ClientIServletImpl initilisation");
0129:                }
0130:
0131:                //
0132:                URL url = new URL(serviceUrl);
0133:                httpUrlConnection = (HttpURLConnection) url.openConnection();
0134:                this .initProxy(httpUrlConnection);
0135:
0136:                File download = new File(downloadDir);
0137:
0138:                if (download.exists()) {
0139:                    download.mkdirs();
0140:                }
0141:            }
0142:
0143:            private void initProxy(HttpURLConnection httpUrlConnection) {
0144:                if (System.getProperty("proxyHost") != null) {
0145:                    // System.out.println("Proxy host: " +
0146:                    // System.getProperty("proxyHost"));
0147:                    // System.out.println("Proxy port " +
0148:                    // System.getProperty("proxyPort"));
0149:                    // Init proxy
0150:                    // httpUrlConnection.getHostConfiguration().setProxy(System.getProperty("proxyHost"),
0151:                    // Integer.parseInt(System.getProperty("proxyPort")));
0152:                }
0153:            }
0154:
0155:            private void basicHTTPLogin(String login, String password,
0156:                    HttpURLConnection httpUrlConnection) {
0157:                // String crypt = login + ":" + password;
0158:                // post.addRequestHeader("auth",
0159:                // Base64.encode(crypt.getBytes("UTF-8")));
0160:                httpUrlConnection.addRequestProperty("login", login);
0161:                httpUrlConnection.addRequestProperty("password", password);
0162:            }
0163:
0164:            private HttpURLConnection createPost(String action)
0165:                    throws UnableToContactServerException {
0166:                try {
0167:                    URL url = new URL(serviceUrl);
0168:                    httpUrlConnection = (HttpURLConnection) url
0169:                            .openConnection();
0170:                    httpUrlConnection.setUseCaches(false);
0171:                    httpUrlConnection.setRequestProperty("Connection",
0172:                            "Keep-Alive");
0173:                    httpUrlConnection.setRequestMethod("POST");
0174:                    httpUrlConnection.addRequestProperty(SO6_ACTION, action);
0175:                    httpUrlConnection.addRequestProperty(SO6_SYNCHRONIZER_URI,
0176:                            synchronizerURI);
0177:                    httpUrlConnection.addRequestProperty("Content-Length", "0");
0178:
0179:                    // Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
0180:                    if ((login != null) && (password != null)) {
0181:                        basicHTTPLogin(login, password, httpUrlConnection);
0182:                    }
0183:
0184:                    return httpUrlConnection;
0185:                } catch (Exception e) {
0186:                    throw new UnableToContactServerException();
0187:                }
0188:            }
0189:
0190:            private int executePost(HttpURLConnection connection)
0191:                    throws ServerException {
0192:                try {
0193:                    // connection.connect();
0194:                    return connection.getResponseCode();
0195:                } catch (IOException e) {
0196:                    throw new ServerException(e);
0197:                }
0198:            }
0199:
0200:            public long getLastTicket() throws AuthenticationException,
0201:                    UnableToContactServerException, ServerException,
0202:                    ConnectionException, LocalException {
0203:                HttpURLConnection post = createPost("getLastTicket");
0204:                int statusCode = executePost(post);
0205:
0206:                switch (statusCode) {
0207:                case STATUS_CODE_OK:
0208:
0209:                    long result = Long.parseLong(post
0210:                            .getHeaderField("lastTicket"));
0211:
0212:                    // post.disconnect();
0213:                    return result;
0214:
0215:                case STATUS_CODE_AUTH_ERROR:
0216:                    throw new AuthenticationException();
0217:
0218:                case STATUS_CODE_SERVER_ERROR:
0219:                default:
0220:                    throw new ServerException(
0221:                            "Server error on getLastTicket. Status code="
0222:                                    + statusCode);
0223:                }
0224:            }
0225:
0226:            public String getPatch(long ticket) throws AuthenticationException,
0227:                    PatchNotFoundException, UnableToContactServerException,
0228:                    ServerException, ConnectionException, LocalException {
0229:                HttpURLConnection post = createPost("getPatch");
0230:                post
0231:                        .addRequestProperty(SO6_PATCH_TICKET, Long
0232:                                .toString(ticket));
0233:
0234:                int statusCode = executePost(post);
0235:
0236:                switch (statusCode) {
0237:                case STATUS_CODE_OK:
0238:                    StateMonitoring.getInstance().setXMLMonitoringStartSubCall(
0239:                            1, "");
0240:
0241:                    String patchName = post.getHeaderField("patchName");
0242:
0243:                    if (!new File(downloadDir).exists()) {
0244:                        new File(downloadDir).mkdirs();
0245:                    }
0246:
0247:                    String patchPathName = downloadDir + File.separator
0248:                            + patchName;
0249:                    MonitoredInputStream mis;
0250:
0251:                    try {
0252:                        mis = new MonitoredInputStream(post.getInputStream(),
0253:                                post.getContentLength());
0254:                    } catch (IOException e) {
0255:                        throw new ConnectionException(e);
0256:                    }
0257:
0258:                    mis.setComment("Download finished", "Downloading");
0259:
0260:                    try {
0261:                        FileOutputStream fos = new FileOutputStream(
0262:                                patchPathName);
0263:                        int length;
0264:                        byte[] buffer = new byte[BUFFER_SIZE];
0265:
0266:                        while ((length = mis.read(buffer)) != -1) {
0267:                            fos.write(buffer, 0, length);
0268:                        }
0269:
0270:                        fos.close();
0271:                        mis.close();
0272:                    } catch (Exception e) {
0273:                        throw new LocalException(e);
0274:                    }
0275:
0276:                    StateMonitoring.getInstance().setXMLMonitoringEndSubCall();
0277:
0278:                    // post.disconnect();
0279:                    return patchPathName;
0280:
0281:                case STATUS_CODE_AUTH_ERROR:
0282:                    throw new AuthenticationException();
0283:
0284:                case STATUS_CODE_INVALIDE_TICKET:
0285:                case STATUS_CODE_PATCH_NOT_FOUND:
0286:                    throw new PatchNotFoundException();
0287:
0288:                case STATUS_CODE_SERVER_ERROR:
0289:                default:
0290:                    throw new ServerException(
0291:                            "Server error on getPatch. Status code="
0292:                                    + statusCode);
0293:                }
0294:            }
0295:
0296:            public void sendPatch(long fromTicket, long toTicket,
0297:                    String patchFileName, boolean validate)
0298:                    throws AuthenticationException, InvalideTicketException,
0299:                    UnableToContactServerException, ServerException,
0300:                    ConnectionException, LocalException {
0301:                sendPatch_1_4(fromTicket, toTicket, patchFileName, validate);
0302:            }
0303:
0304:            //    private void sendPatch_1_5(long fromTicket, long toTicket, String patchFileName, boolean validate)
0305:            //        throws AuthenticationException, InvalideTicketException, UnableToContactServerException, ServerException, ConnectionException, LocalException {
0306:            //        final File patch = new File(patchFileName);
0307:            //
0308:            //        try {
0309:            //            final MonitoredInputStream mis = new MonitoredInputStream(new FileInputStream(patch), patch.length());
0310:            //            URL url = new URL(serviceUrl);
0311:            //
0312:            //            HttpURLConnection post = (HttpURLConnection) url.openConnection();
0313:            //
0314:            //            // FIXME Only valid in JDK 1.5 (Fix the bug with client being behind a proxy)
0315:            //            post.setFixedLengthStreamingMode((int) patch.length());
0316:            //
0317:            //            post.setDoInput(true);
0318:            //            post.setDoOutput(true);
0319:            //            post.setUseCaches(false);
0320:            //            post.setRequestProperty("Connection", "Keep-Alive");
0321:            //            post.setRequestMethod("POST");
0322:            //            post.addRequestProperty(SO6_ACTION, "sendPatch");
0323:            //            post.addRequestProperty(SO6_SYNCHRONIZER_URI, synchronizerURI);
0324:            //
0325:            //            // Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
0326:            //            if ((login != null) && (password != null)) {
0327:            //                basicHTTPLogin(login, password, post);
0328:            //            }
0329:            //
0330:            //            try {
0331:            //                post.addRequestProperty(SO6_PATCH_FROM_TICKET, Long.toString(fromTicket));
0332:            //                post.addRequestProperty(SO6_PATCH_TO_TICKET, Long.toString(toTicket));
0333:            //                post.addRequestProperty(SO6_VALIDATE_TICKET_NUMBER, Boolean.toString(validate));
0334:            //                post.addRequestProperty("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
0335:            //                post.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
0336:            //                post.addRequestProperty("Content-Length", String.valueOf(patch.length()));
0337:            //
0338:            //                OutputStreamWriter writer = new OutputStreamWriter(post.getOutputStream());
0339:            //                int length;
0340:            //                byte[] buffer = new byte[BUFFER_SIZE];
0341:            //
0342:            //                while ((length = mis.read(buffer)) != -1) {
0343:            //                    String s = new String(buffer, "UTF-8");
0344:            //                    writer.write(s.toCharArray(), 0, length);
0345:            //                    writer.flush();
0346:            //                }
0347:            //
0348:            //                writer.close();
0349:            //            } catch (IOException e) {
0350:            //                throw new LocalException(e);
0351:            //            }
0352:            //
0353:            //            // response
0354:            //            int statusCode = executePost(post);
0355:            //
0356:            //            switch (statusCode) {
0357:            //            case 202:
0358:            //
0359:            //                // good response
0360:            //                break;
0361:            //
0362:            //            case STATUS_CODE_AUTH_ERROR:
0363:            //                throw new AuthenticationException();
0364:            //
0365:            //            case STATUS_CODE_INVALIDE_TICKET:
0366:            //                throw new InvalideTicketException();
0367:            //
0368:            //            case STATUS_CODE_SERVER_ERROR:default:
0369:            //                throw new ServerException("Server error on sendPatch. Status code=" + statusCode);
0370:            //            }
0371:            //        } catch (NumberFormatException e) {
0372:            //            throw new LocalException(e);
0373:            //        } catch (FileNotFoundException e) {
0374:            //            throw new LocalException(e);
0375:            //        } catch (MalformedURLException e) {
0376:            //            throw new UnableToContactServerException();
0377:            //        } catch (IOException e) {
0378:            //            throw new LocalException(e);
0379:            //        }
0380:            //
0381:            //        // post.disconnect();
0382:            //    }
0383:            private void sendPatch_1_4(long fromTicket, long toTicket,
0384:                    String patchFileName, boolean validate)
0385:                    throws AuthenticationException, InvalideTicketException,
0386:                    UnableToContactServerException, ServerException,
0387:                    ConnectionException, LocalException {
0388:                final File patch = new File(patchFileName);
0389:
0390:                try {
0391:                    final MonitoredInputStream mis = new MonitoredInputStream(
0392:                            new FileInputStream(patch), patch.length());
0393:
0394:                    //
0395:                    URL url = new URL(serviceUrl);
0396:
0397:                    //
0398:                    Socket socket = new Socket(url.getHost(), url.getPort());
0399:                    socket.setKeepAlive(true);
0400:
0401:                    OutputStream out = socket.getOutputStream();
0402:
0403:                    //
0404:                    OutputStreamWriter writer = new OutputStreamWriter(out);
0405:                    int responseStatus = Math.abs((int) System
0406:                            .currentTimeMillis());
0407:                    writer.write("POST " + url.getFile() + " HTTP/1.1\r\n");
0408:                    writer.write("Host: " + url.getHost() + ":" + url.getPort()
0409:                            + "\r\n");
0410:                    writer.write("Content-Length: " + patch.length() + "\r\n");
0411:                    writer.write(SO6_ACTION + ": " + "sendPatch" + "\r\n");
0412:                    writer.write(SO6_PATCH_FROM_TICKET + ": "
0413:                            + Long.toString(fromTicket) + "\r\n");
0414:                    writer.write(SO6_PATCH_TO_TICKET + ": "
0415:                            + Long.toString(toTicket) + "\r\n");
0416:                    writer.write(SO6_VALIDATE_TICKET_NUMBER + ": "
0417:                            + Boolean.toString(validate) + "\r\n");
0418:
0419:                    // writer.write("ResponseStatus: " + responseStatus + "\r\n");
0420:                    writer.write("login: " + login + "\r\n");
0421:                    writer.write("password: " + password + "\r\n");
0422:                    writer.write(SO6_SYNCHRONIZER_URI + ": " + synchronizerURI
0423:                            + "\r\n");
0424:                    writer.write("Cache-Control: no-cache\r\n");
0425:                    writer.write("Connection: Keep-Alive\r\n");
0426:                    writer.write("Pragma: no-cache\r\n");
0427:                    writer.write("User-Agent: Java/1.4.2_03\r\n");
0428:                    writer
0429:                            .write("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n");
0430:                    writer
0431:                            .write("Content-Type: application/x-www-form-urlencoded\r\n");
0432:                    writer.write("\r\n");
0433:                    writer.flush();
0434:
0435:                    int length;
0436:                    byte[] buffer = new byte[BUFFER_SIZE];
0437:
0438:                    while ((length = mis.read(buffer)) != -1) {
0439:                        out.write(buffer, 0, length);
0440:                        out.flush();
0441:                    }
0442:
0443:                    // out.close();
0444:                    // mis.close();
0445:                    // response
0446:                    InputStream in = socket.getInputStream();
0447:                    InputStreamReader reader = new InputStreamReader(in);
0448:                    LineNumberReader lnr = new LineNumberReader(reader);
0449:                    String line = lnr.readLine();
0450:                    socket.close();
0451:
0452:                    int statusCode = Integer.parseInt(line.split(" ")[1]);
0453:
0454:                    switch (statusCode) {
0455:                    case 202:
0456:
0457:                        // good response
0458:                        break;
0459:
0460:                    case STATUS_CODE_AUTH_ERROR:
0461:                        throw new AuthenticationException();
0462:
0463:                    case STATUS_CODE_INVALIDE_TICKET:
0464:                        throw new InvalideTicketException();
0465:
0466:                    case STATUS_CODE_SERVER_ERROR:
0467:                    default:
0468:                        throw new ServerException(
0469:                                "Server error on sendPatch. Status code="
0470:                                        + statusCode);
0471:                    }
0472:                } catch (NumberFormatException e) {
0473:                    throw new LocalException(e);
0474:                } catch (FileNotFoundException e) {
0475:                    throw new LocalException(e);
0476:                } catch (MalformedURLException e) {
0477:                    throw new UnableToContactServerException();
0478:                } catch (UnknownHostException e) {
0479:                    throw new UnableToContactServerException();
0480:                } catch (SocketException e) {
0481:                    throw new ConnectionException(e);
0482:                } catch (IOException e) {
0483:                    throw new LocalException(e);
0484:                }
0485:
0486:                // post.disconnect();
0487:            }
0488:
0489:            public void addBinExt(String ext) throws AuthenticationException,
0490:                    UnableToContactServerException, ServerException,
0491:                    LocalException {
0492:                HttpURLConnection post = createPost("addBinExt");
0493:
0494:                try {
0495:                    OutputStreamWriter writer = new OutputStreamWriter(post
0496:                            .getOutputStream());
0497:                    writer.write(SO6_BIN_EXT);
0498:                    writer.write("=");
0499:                    writer.write(ext);
0500:                    writer.close();
0501:                } catch (IOException e) {
0502:                    throw new LocalException(e);
0503:                }
0504:
0505:                // post.addParameter(SO6_BIN_EXT, ext);
0506:                int statusCode = executePost(post);
0507:
0508:                switch (statusCode) {
0509:                case STATUS_CODE_OK:
0510:                    break;
0511:
0512:                case STATUS_CODE_AUTH_ERROR:
0513:                    throw new AuthenticationException();
0514:
0515:                case STATUS_CODE_SERVER_ERROR:
0516:                default:
0517:                    throw new ServerException(
0518:                            "Server error on addBinExt. Status code="
0519:                                    + statusCode);
0520:                }
0521:
0522:                // post.disconnect();
0523:            }
0524:
0525:            public void removeBinExt(String ext)
0526:                    throws AuthenticationException,
0527:                    UnableToContactServerException, ServerException,
0528:                    LocalException {
0529:                HttpURLConnection post = createPost("removeBinExt");
0530:
0531:                try {
0532:                    OutputStreamWriter writer = new OutputStreamWriter(post
0533:                            .getOutputStream());
0534:                    writer.write(SO6_BIN_EXT);
0535:                    writer.write("=");
0536:                    writer.write(ext);
0537:                    writer.close();
0538:                } catch (IOException e) {
0539:                    throw new LocalException(e);
0540:                }
0541:
0542:                // post.addParameter(SO6_BIN_EXT, ext);
0543:                int statusCode = executePost(post);
0544:
0545:                switch (statusCode) {
0546:                case STATUS_CODE_OK:
0547:                    break;
0548:
0549:                case STATUS_CODE_AUTH_ERROR:
0550:                    throw new AuthenticationException();
0551:
0552:                case STATUS_CODE_INVALIDE_TICKET:
0553:                case STATUS_CODE_PATCH_NOT_FOUND:
0554:                case STATUS_CODE_SERVER_ERROR:
0555:                default:
0556:                    throw new ServerException(
0557:                            "Server error on removeBinExt. Status code="
0558:                                    + statusCode);
0559:                }
0560:
0561:                // post.disconnect();
0562:            }
0563:
0564:            private String getResponseAsString(InputStream input)
0565:                    throws ServerException {
0566:                BufferedReader reader = new BufferedReader(
0567:                        new InputStreamReader(input));
0568:
0569:                // return reader.readLine(); // assume response is a single-line
0570:                // response ;d
0571:                String s;
0572:                StringBuffer buf = new StringBuffer();
0573:
0574:                try {
0575:                    while ((s = reader.readLine()) != null) {
0576:                        buf.append(s);
0577:                        buf.append("\n");
0578:                    }
0579:
0580:                    return buf.toString();
0581:                } catch (IOException e) {
0582:                    throw new ServerException(e);
0583:                }
0584:            }
0585:
0586:            public String getBinExt() throws AuthenticationException,
0587:                    LocalException, UnableToContactServerException,
0588:                    ServerException {
0589:                HttpURLConnection post = createPost("getBinExt");
0590:                int statusCode = executePost(post);
0591:
0592:                switch (statusCode) {
0593:                case STATUS_CODE_OK:
0594:
0595:                    String result;
0596:
0597:                    try {
0598:                        result = getResponseAsString(post.getInputStream());
0599:                    } catch (IOException e) {
0600:                        throw new LocalException(e);
0601:                    }
0602:
0603:                    // post.disconnect();
0604:                    return result;
0605:
0606:                case STATUS_CODE_AUTH_ERROR:
0607:                    throw new AuthenticationException();
0608:
0609:                case STATUS_CODE_INVALIDE_TICKET:
0610:                case STATUS_CODE_PATCH_NOT_FOUND:
0611:                case STATUS_CODE_SERVER_ERROR:
0612:                default:
0613:                    throw new ServerException(
0614:                            "Server error on getBinExt. Status code="
0615:                                    + statusCode);
0616:                }
0617:            }
0618:
0619:            public long[][] listPatch() throws AuthenticationException,
0620:                    LocalException, UnableToContactServerException,
0621:                    ServerException {
0622:                HttpURLConnection post = createPost("listPatch");
0623:                int statusCode = executePost(post);
0624:
0625:                switch (statusCode) {
0626:                case STATUS_CODE_OK:
0627:
0628:                    String patchArray = null;
0629:
0630:                    try {
0631:                        patchArray = getResponseAsString(post.getInputStream());
0632:                    } catch (IOException e) {
0633:                        throw new LocalException(e);
0634:                    }
0635:
0636:                    // post.disconnect();
0637:                    StringTokenizer st = new StringTokenizer(patchArray, "\n");
0638:                    ArrayList patchs = new ArrayList();
0639:
0640:                    while (st.hasMoreTokens()) {
0641:                        patchs.add(st.nextToken());
0642:                    }
0643:
0644:                    long[][] result = new long[patchs.size()][3];
0645:
0646:                    for (int i = 0; i < result.length; i++) {
0647:                        result[i][0] = Long.parseLong(((String) patchs.get(i))
0648:                                .split(" ")[0]);
0649:                        result[i][1] = Long.parseLong(((String) patchs.get(i))
0650:                                .split(" ")[1]);
0651:                        result[i][2] = Long.parseLong(((String) patchs.get(i))
0652:                                .split(" ")[2]);
0653:                    }
0654:
0655:                    return result;
0656:
0657:                case STATUS_CODE_AUTH_ERROR:
0658:                    throw new AuthenticationException();
0659:
0660:                case STATUS_CODE_INVALIDE_TICKET:
0661:                case STATUS_CODE_PATCH_NOT_FOUND:
0662:                case STATUS_CODE_SERVER_ERROR:
0663:                default:
0664:                    throw new ServerException(
0665:                            "Server error on listPatch. Status code="
0666:                                    + statusCode);
0667:                }
0668:            }
0669:
0670:            public String ping() throws ServerException,
0671:                    UnableToContactServerException, IOException {
0672:                HttpURLConnection post = createPost("ping");
0673:                int statusCode = executePost(post);
0674:
0675:                switch (statusCode) {
0676:                case STATUS_CODE_OK:
0677:
0678:                    String result = getResponseAsString(post.getInputStream());
0679:
0680:                    // post.disconnect();
0681:                    return result;
0682:
0683:                case STATUS_CODE_AUTH_ERROR:
0684:                case STATUS_CODE_INVALIDE_TICKET:
0685:                case STATUS_CODE_PATCH_NOT_FOUND:
0686:                case STATUS_CODE_SERVER_ERROR:
0687:                default:
0688:                    throw new ServerException(
0689:                            "Server error on ping. Status code=" + statusCode);
0690:                }
0691:            }
0692:
0693:            public void createWorkspace(String workspaceId)
0694:                    throws AuthenticationException, ServerException,
0695:                    UnableToContactServerException {
0696:                HttpURLConnection post = createPost("createWorkspace");
0697:                post.addRequestProperty(SO6_WORKSPACE_ID, workspaceId);
0698:
0699:                int statusCode = executePost(post);
0700:
0701:                switch (statusCode) {
0702:                case STATUS_CODE_OK:
0703:
0704:                    // post.disconnect();
0705:                    break;
0706:
0707:                case STATUS_CODE_AUTH_ERROR:
0708:                    throw new AuthenticationException();
0709:
0710:                case STATUS_CODE_SERVER_ERROR:
0711:                default:
0712:                    throw new ServerException(
0713:                            "Server error on createWorkspace. Status code="
0714:                                    + statusCode);
0715:                }
0716:            }
0717:
0718:            public void removeWorkspace(String workspaceId)
0719:                    throws AuthenticationException, ServerException,
0720:                    UnableToContactServerException {
0721:                HttpURLConnection post = createPost("removeWorkspace");
0722:                post.addRequestProperty(SO6_WORKSPACE_ID, workspaceId);
0723:
0724:                int statusCode = executePost(post);
0725:
0726:                switch (statusCode) {
0727:                case STATUS_CODE_OK:
0728:
0729:                    // post.disconnect();
0730:                    break;
0731:
0732:                case STATUS_CODE_AUTH_ERROR:
0733:                    throw new AuthenticationException();
0734:
0735:                case STATUS_CODE_SERVER_ERROR:
0736:                default:
0737:                    throw new ServerException(
0738:                            "Server error on removeWorkspace. Status code="
0739:                                    + statusCode);
0740:                }
0741:            }
0742:
0743:            public void notifyWorkspaceConnections(String wsId,
0744:                    String[] queueIds) throws AuthenticationException,
0745:                    ServerException, UnableToContactServerException {
0746:                // Not Yet implemented
0747:                throw new ServerException(
0748:                        "Not yet implemented (notifyWorkspaceConnections)");
0749:            }
0750:
0751:            public String addWsConnection(String wsId, String queueURI,
0752:                    String wscName, String wscPath) throws LocalException,
0753:                    AuthenticationException, ServerException,
0754:                    UnableToContactServerException {
0755:                HttpURLConnection post = createPost("addWsConnection");
0756:                post.addRequestProperty(SO6_WORKSPACE_ID, wsId);
0757:                post.addRequestProperty(SO6_SYNCHRONIZER_URI, queueURI);
0758:                post.addRequestProperty(SO6_WS_NAME, wscName);
0759:                post.addRequestProperty(SO6_WSC_PATH, wscPath);
0760:
0761:                int statusCode = executePost(post);
0762:
0763:                switch (statusCode) {
0764:                case STATUS_CODE_OK:
0765:
0766:                    try {
0767:                        return getResponseAsString(post.getInputStream());
0768:                    } catch (IOException e) {
0769:                        throw new LocalException(e);
0770:                    }
0771:
0772:                    // post.disconnect();
0773:                case STATUS_CODE_AUTH_ERROR:
0774:                    throw new AuthenticationException();
0775:
0776:                case STATUS_CODE_SERVER_ERROR:
0777:                default:
0778:                    throw new ServerException(
0779:                            "Server error on addWsConnection. Status code="
0780:                                    + statusCode);
0781:                }
0782:            }
0783:
0784:            public void removeWsConnection(String wsId, String queueId)
0785:                    throws AuthenticationException, ServerException,
0786:                    UnableToContactServerException {
0787:                HttpURLConnection post = createPost("removeWsConnection");
0788:                post.addRequestProperty(SO6_WORKSPACE_ID, wsId);
0789:                post.addRequestProperty(SO6_SYNCHRONIZER_URI, queueId);
0790:
0791:                int statusCode = executePost(post);
0792:
0793:                switch (statusCode) {
0794:                case STATUS_CODE_OK:
0795:
0796:                    // post.disconnect();
0797:                    break;
0798:
0799:                case STATUS_CODE_AUTH_ERROR:
0800:                    throw new AuthenticationException();
0801:
0802:                case STATUS_CODE_SERVER_ERROR:
0803:                default:
0804:                    throw new ServerException(
0805:                            "Server error on removeWsConnection. Status code="
0806:                                    + statusCode);
0807:                }
0808:            }
0809:
0810:            // DataflowReaderClientI
0811:            public String getNetworkFromWorkspace(String wsId)
0812:                    throws LocalException, AuthenticationException,
0813:                    ServerException, UnableToContactServerException {
0814:                HttpURLConnection post = createPost("getNetworkFromWorkspace");
0815:                post.addRequestProperty(SO6_WORKSPACE_ID, wsId);
0816:
0817:                int statusCode = executePost(post);
0818:
0819:                switch (statusCode) {
0820:                case STATUS_CODE_OK:
0821:
0822:                    try {
0823:                        File tmpFile = File.createTempFile("wsNetwork", null,
0824:                                new File(downloadDir));
0825:                        InputStream is = post.getInputStream();
0826:                        FileOutputStream fos = new FileOutputStream(tmpFile);
0827:                        int length;
0828:                        byte[] buffer = new byte[BUFFER_SIZE];
0829:
0830:                        while ((length = is.read(buffer)) != -1) {
0831:                            fos.write(buffer, 0, length);
0832:                        }
0833:
0834:                        fos.close();
0835:                        is.close();
0836:
0837:                        // post.disconnect();
0838:                        return tmpFile.getPath();
0839:                    } catch (Exception e) {
0840:                        throw new LocalException(e);
0841:                    }
0842:
0843:                case STATUS_CODE_AUTH_ERROR:
0844:                    throw new AuthenticationException();
0845:
0846:                case STATUS_CODE_SERVER_ERROR:
0847:                default:
0848:                    throw new ServerException(
0849:                            "Server error on getNetworkFromWorkspace. Status code="
0850:                                    + statusCode);
0851:                }
0852:            }
0853:
0854:            public String getNetworkFromQueue(String queueId)
0855:                    throws LocalException, AuthenticationException,
0856:                    ServerException, UnableToContactServerException {
0857:                HttpURLConnection post = createPost("getNetworkFromWorkspace");
0858:                post.addRequestProperty(SO6_QUEUE_ID, queueId);
0859:
0860:                int statusCode = executePost(post);
0861:
0862:                switch (statusCode) {
0863:                case STATUS_CODE_OK:
0864:
0865:                    try {
0866:                        File tmpFile = File.createTempFile("queueNetwork",
0867:                                null, new File(downloadDir));
0868:                        InputStream is = post.getInputStream();
0869:                        FileOutputStream fos = new FileOutputStream(tmpFile);
0870:                        int length;
0871:                        byte[] buffer = new byte[BUFFER_SIZE];
0872:
0873:                        while ((length = is.read(buffer)) != -1) {
0874:                            fos.write(buffer, 0, length);
0875:                        }
0876:
0877:                        fos.close();
0878:                        is.close();
0879:
0880:                        // post.disconnect();
0881:                        return tmpFile.getPath();
0882:                    } catch (Exception e) {
0883:                        throw new LocalException(e);
0884:                    }
0885:
0886:                case STATUS_CODE_AUTH_ERROR:
0887:                    throw new AuthenticationException();
0888:
0889:                case STATUS_CODE_SERVER_ERROR:
0890:                default:
0891:                    throw new ServerException(
0892:                            "Server error on getNetworkFromWorkspace. Status code="
0893:                                    + statusCode);
0894:                }
0895:            }
0896:
0897:            // Workspace listener
0898:            public void notifyQueue(long ticket)
0899:                    throws AuthenticationException, ServerException,
0900:                    UnableToContactServerException {
0901:                HttpURLConnection post = createPost("notifyQueue");
0902:                post.addRequestProperty(SO6_REPLICA_URI, replicaURI);
0903:                post.addRequestProperty(SO6_REPLICA_TICKET, Long
0904:                        .toString(ticket));
0905:
0906:                int statusCode = executePost(post);
0907:
0908:                switch (statusCode) {
0909:                case STATUS_CODE_OK:
0910:
0911:                    // post.disconnect();
0912:                    return;
0913:
0914:                case STATUS_CODE_AUTH_ERROR:
0915:                    throw new AuthenticationException();
0916:
0917:                case STATUS_CODE_SERVER_ERROR:
0918:                default:
0919:                    throw new ServerException(
0920:                            "Server error on notifyQueue. Status code="
0921:                                    + statusCode);
0922:                }
0923:            }
0924:
0925:            public String getPatch(long fromTicket, long toTicket)
0926:                    throws PatchNotFoundException, LocalException,
0927:                    AuthenticationException, UnableToContactServerException,
0928:                    ServerException, ConnectionException {
0929:                HttpURLConnection post = createPost("getPatch2");
0930:                post.addRequestProperty(SO6_PATCH_FROM_TICKET, Long
0931:                        .toString(fromTicket));
0932:                post.addRequestProperty(SO6_PATCH_TO_TICKET, Long
0933:                        .toString(toTicket));
0934:
0935:                int statusCode = executePost(post);
0936:
0937:                switch (statusCode) {
0938:                case STATUS_CODE_OK:
0939:                    StateMonitoring.getInstance().setXMLMonitoringStartSubCall(
0940:                            1, "");
0941:
0942:                    String patchName = post.getHeaderField("patchName");
0943:
0944:                    // TODO check
0945:                    String patchPathName = downloadDir + File.separator
0946:                            + System.getProperty("user.name") + "_" + patchName;
0947:                    MonitoredInputStream mis;
0948:
0949:                    try {
0950:                        mis = new MonitoredInputStream(post.getInputStream(),
0951:                                post.getContentLength());
0952:                    } catch (IOException e) {
0953:                        throw new ConnectionException(e);
0954:                    }
0955:
0956:                    mis.setComment("Download finished", "Downloading");
0957:
0958:                    try {
0959:                        FileOutputStream fos = new FileOutputStream(
0960:                                patchPathName);
0961:                        int length;
0962:                        byte[] buffer = new byte[BUFFER_SIZE];
0963:
0964:                        while ((length = mis.read(buffer)) != -1) {
0965:                            fos.write(buffer, 0, length);
0966:                        }
0967:
0968:                        fos.close();
0969:                        mis.close();
0970:                    } catch (Exception e) {
0971:                        throw new LocalException(e);
0972:                    }
0973:
0974:                    StateMonitoring.getInstance().setXMLMonitoringEndSubCall();
0975:
0976:                    // post.disconnect();
0977:                    return patchPathName;
0978:
0979:                case STATUS_CODE_AUTH_ERROR:
0980:                    throw new AuthenticationException();
0981:
0982:                case STATUS_CODE_INVALIDE_TICKET:
0983:                case STATUS_CODE_PATCH_NOT_FOUND:
0984:                    throw new PatchNotFoundException();
0985:
0986:                case STATUS_CODE_SERVER_ERROR:
0987:                default:
0988:                    throw new ServerException(
0989:                            "Server error on getPatch(2). Status code="
0990:                                    + statusCode);
0991:                }
0992:            }
0993:
0994:            public static String[] getInternalPropertyList() {
0995:                return new String[] { SO6_QUEUE_ID, SO6_LOGIN, SO6_PASSWORD,
0996:                        SO6_SERVICE_URL, SO6_CONNECTION_ID, SO6_XML_DETECT };
0997:            }
0998:
0999:            // /////////////////////////////////////////
1000:            public class HttpConnection {
1001:                // Holds target port number for Connection.
1002:                private int HTTP_PORT = 80;
1003:
1004:                // Holds target url.
1005:                private String resource;
1006:
1007:                // Holds target server address.
1008:                private String host;
1009:
1010:                // Holds target file address.
1011:                private String file;
1012:
1013:                // Setting defalt HTTP request method to Get.
1014:                private String method = "GET";
1015:
1016:                // Holds body length of request.
1017:                private String contentLen = "";
1018:
1019:                // Setting default content Type to text/html.
1020:                private String type = "text/html";
1021:                private String contentType;
1022:
1023:                // Socket object for connection.
1024:                private Socket httpSocket = null;
1025:
1026:                // Holds all HTTP headers that our class will initialize.
1027:                private StringBuffer defaultHTTPHeaders;
1028:
1029:                // Holds all HTTP headers that user will initialize.
1030:                private StringBuffer userDefinedHTTPHeaders = new StringBuffer();
1031:                private InputStream in = null;
1032:                private OutputStream out = null;
1033:
1034:                // Constructor with URL and Port number as argument.
1035:                // It opens a socket connection at provided URL.
1036:                public HttpConnection(URL url) throws UnknownHostException,
1037:                        IOException {
1038:                    if (url.getPort() > 0) {
1039:                        HTTP_PORT = url.getPort();
1040:                    }
1041:
1042:                    // Parses the passed URL e.g. "http://www.xyz.com/tariqspage/soap "
1043:                    // and extract Host address i.e. "www.xyz.com"
1044:                    // and File name i.e. "/tariqspage/soap" from URL.
1045:                    file = url.getPath();
1046:                    host = url.getHost();
1047:                    httpSocket = new Socket(host, HTTP_PORT);
1048:                    defaultHTTPHeaders = new StringBuffer();
1049:                }
1050:
1051:                // Returns input stream for received data.
1052:                public InputStream getInputStream() throws IOException {
1053:                    return in = httpSocket.getInputStream();
1054:                }
1055:
1056:                // Returns output stream for data to be sent.
1057:                public OutputStream getOutputStream() throws IOException {
1058:                    return out = httpSocket.getOutputStream();
1059:                }
1060:
1061:                // Set request method for http request.
1062:                public void setRequestMethod(String method) {
1063:                    this .method = method;
1064:                }
1065:
1066:                // Set Content Type Header for request.
1067:                public void setContentType(String type) {
1068:                    this .type = type;
1069:                }
1070:
1071:                // Sets content length of the request data.
1072:                public void setContentLength(int length) {
1073:                    contentLen = "Content-length: " + String.valueOf(length)
1074:                            + "\r\n";
1075:                }
1076:
1077:                // Returs Content Type Header.
1078:                public String getContentType() {
1079:                    return this .contentType;
1080:                }
1081:
1082:                // Returs Request Method Header.
1083:                public String getRequestMethod() {
1084:                    return this .method;
1085:                }
1086:
1087:                public void setRequestProperty(String name, String value) {
1088:                    userDefinedHTTPHeaders.append(name + ": " + value + "\r\n");
1089:                } // setRequestProperty()
1090:
1091:                // Concatenating headers from buffer strings
1092:                // into a single String and Returns it.
1093:                public String getHeaders() {
1094:                    defaultHTTPHeaders.append(method + " " + file
1095:                            + " HTTP/1.1\r\n");
1096:                    defaultHTTPHeaders.append("Host: " + host + "\r\n");
1097:                    defaultHTTPHeaders.append("Content-type: " + type
1098:                            + "; charset=utf-8\r\n");
1099:                    defaultHTTPHeaders.append(contentLen);
1100:
1101:                    // Appending empty line at end of headers.
1102:                    userDefinedHTTPHeaders.append("\r\n");
1103:
1104:                    // Concatenating two string buffers containing headers
1105:                    // into single String named "Headers".
1106:                    String headers = defaultHTTPHeaders.toString();
1107:                    headers += userDefinedHTTPHeaders.toString();
1108:
1109:                    return headers;
1110:                } // getHeaders()
1111:            } // class
1112:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.