Source Code Cross Referenced for HttpSOAPConnection.java in  » 6.0-JDK-Modules » saaj » com » sun » xml » messaging » saaj » client » p2p » 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 » 6.0 JDK Modules » saaj » com.sun.xml.messaging.saaj.client.p2p 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * The contents of this file are subject to the terms
003:         * of the Common Development and Distribution License
004:         * (the "License").  You may not use this file except
005:         * in compliance with the License.
006:         *
007:         * You can obtain a copy of the license at
008:         * https://jwsdp.dev.java.net/CDDLv1.0.html
009:         * See the License for the specific language governing
010:         * permissions and limitations under the License.
011:         *
012:         * When distributing Covered Code, include this CDDL
013:         * HEADER in each file and include the License file at
014:         * https://jwsdp.dev.java.net/CDDLv1.0.html  If applicable,
015:         * add the following below this CDDL HEADER, with the
016:         * fields enclosed by brackets "[]" replaced with your
017:         * own identifying information: Portions Copyright [yyyy]
018:         * [name of copyright owner]
019:         */
020:        /*
021:         * $Id: HttpSOAPConnection.java,v 1.3 2007/09/11 07:43:48 kumarjayanti Exp $
022:         * $Revision: 1.3 $
023:         * $Date: 2007/09/11 07:43:48 $
024:         */
025:
026:        /*
027:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028:         * 
029:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030:         * 
031:         * The contents of this file are subject to the terms of either the GNU
032:         * General Public License Version 2 only ("GPL") or the Common Development
033:         * and Distribution License("CDDL") (collectively, the "License").  You
034:         * may not use this file except in compliance with the License. You can obtain
035:         * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036:         * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
037:         * language governing permissions and limitations under the License.
038:         * 
039:         * When distributing the software, include this License Header Notice in each
040:         * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041:         * Sun designates this particular file as subject to the "Classpath" exception
042:         * as provided by Sun in the GPL Version 2 section of the License file that
043:         * accompanied this code.  If applicable, add the following below the License
044:         * Header, with the fields enclosed by brackets [] replaced by your own
045:         * identifying information: "Portions Copyrighted [year]
046:         * [name of copyright owner]"
047:         * 
048:         * Contributor(s):
049:         * 
050:         * If you wish your version of this file to be governed by only the CDDL or
051:         * only the GPL Version 2, indicate your decision by adding "[Contributor]
052:         * elects to include this software in this distribution under the [CDDL or GPL
053:         * Version 2] license."  If you don't indicate a single choice of license, a
054:         * recipient has the option to distribute your version of this file under
055:         * either the CDDL, the GPL Version 2 or to extend the choice of license to
056:         * its licensees as provided above.  However, if you add GPL Version 2 code
057:         * and therefore, elected the GPL Version 2 license, then the option applies
058:         * only if the new code is made subject to such option by the copyright
059:         * holder.
060:         */
061:        package com.sun.xml.messaging.saaj.client.p2p;
062:
063:        import java.io.*;
064:        import java.lang.reflect.Method;
065:        import java.net.*;
066:        import java.security.*;
067:        import java.util.Iterator;
068:        import java.util.StringTokenizer;
069:        import java.util.logging.Level;
070:        import java.util.logging.Logger;
071:
072:        import javax.xml.soap.*;
073:
074:        import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
075:        import com.sun.xml.messaging.saaj.util.*;
076:
077:        /**
078:         * This represents a "connection" to the simple HTTP-based provider.
079:         *
080:         * @author Anil Vijendran (akv@eng.sun.com)
081:         * @author Rajiv Mordani (rajiv.mordani@sun.com)
082:         * @author Manveen Kaur (manveen.kaur@sun.com)
083:         *
084:         */
085:        public class HttpSOAPConnection extends SOAPConnection {
086:
087:            protected static Logger log = Logger.getLogger(
088:                    LogDomainConstants.HTTP_CONN_DOMAIN,
089:                    "com.sun.xml.messaging.saaj.client.p2p.LocalStrings");
090:
091:            public static String defaultProxyHost = null;
092:            public static int defaultProxyPort = -1;
093:
094:            MessageFactory messageFactory = null;
095:
096:            boolean closed = false;
097:
098:            public HttpSOAPConnection() throws SOAPException {
099:                proxyHost = defaultProxyHost;
100:                proxyPort = defaultProxyPort;
101:
102:                try {
103:                    messageFactory = MessageFactory
104:                            .newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL);
105:                } catch (NoSuchMethodError ex) {
106:                    //fallback to default SOAP 1.1 in this case for backward compatibility
107:                    messageFactory = MessageFactory.newInstance();
108:                } catch (Exception ex) {
109:                    log.log(Level.SEVERE,
110:                            "SAAJ0001.p2p.cannot.create.msg.factory", ex);
111:                    throw new SOAPExceptionImpl(
112:                            "Unable to create message factory", ex);
113:                }
114:            }
115:
116:            public void close() throws SOAPException {
117:                if (closed) {
118:                    log.severe("SAAJ0002.p2p.close.already.closed.conn");
119:                    throw new SOAPExceptionImpl("Connection already closed");
120:                }
121:
122:                messageFactory = null;
123:                closed = true;
124:            }
125:
126:            public SOAPMessage call(SOAPMessage message, Object endPoint)
127:                    throws SOAPException {
128:                if (closed) {
129:                    log.severe("SAAJ0003.p2p.call.already.closed.conn");
130:                    throw new SOAPExceptionImpl("Connection is closed");
131:                }
132:
133:                Class urlEndpointClass = null;
134:
135:                try {
136:                    urlEndpointClass = Class
137:                            .forName("javax.xml.messaging.URLEndpoint");
138:                } catch (Exception ex) {
139:                    //Do nothing. URLEndpoint is available only when JAXM is there.
140:                    log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM");
141:                }
142:
143:                if (urlEndpointClass != null) {
144:                    if (urlEndpointClass.isInstance(endPoint)) {
145:                        String url = null;
146:
147:                        try {
148:                            Method m = urlEndpointClass.getMethod("getURL",
149:                                    (Class[]) null);
150:                            url = (String) m.invoke(endPoint, (Object[]) null);
151:                        } catch (Exception ex) {
152:                            // TBD -- exception chaining
153:                            log.log(Level.SEVERE, "SAAJ0004.p2p.internal.err",
154:                                    ex);
155:                            throw new SOAPExceptionImpl("Internal error: "
156:                                    + ex.getMessage());
157:                        }
158:                        try {
159:                            endPoint = new URL(url);
160:                        } catch (MalformedURLException mex) {
161:                            log.log(Level.SEVERE, "SAAJ0005.p2p.", mex);
162:                            throw new SOAPExceptionImpl("Bad URL: "
163:                                    + mex.getMessage());
164:                        }
165:                    }
166:                }
167:
168:                if (endPoint instanceof  java.lang.String) {
169:                    try {
170:                        endPoint = new URL((String) endPoint);
171:                    } catch (MalformedURLException mex) {
172:                        log.log(Level.SEVERE, "SAAJ0006.p2p.bad.URL", mex);
173:                        throw new SOAPExceptionImpl("Bad URL: "
174:                                + mex.getMessage());
175:                    }
176:                }
177:
178:                if (endPoint instanceof  URL)
179:                    try {
180:                        PriviledgedPost pp = new PriviledgedPost(this , message,
181:                                (URL) endPoint);
182:                        SOAPMessage response = (SOAPMessage) AccessController
183:                                .doPrivileged(pp);
184:
185:                        return response;
186:                    } catch (Exception ex) {
187:                        // TBD -- chaining?
188:                        throw new SOAPExceptionImpl(ex);
189:                    }
190:                else {
191:                    log.severe("SAAJ0007.p2p.bad.endPoint.type");
192:                    throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
193:                }
194:            }
195:
196:            static class PriviledgedPost implements  PrivilegedExceptionAction {
197:
198:                HttpSOAPConnection c;
199:                SOAPMessage message;
200:                URL endPoint;
201:
202:                PriviledgedPost(HttpSOAPConnection c, SOAPMessage message,
203:                        URL endPoint) {
204:                    this .c = c;
205:                    this .message = message;
206:                    this .endPoint = endPoint;
207:                }
208:
209:                public Object run() throws Exception {
210:                    return c.post(message, endPoint);
211:                }
212:            }
213:
214:            // TBD
215:            //    Fix this to do things better.
216:
217:            private String proxyHost = null;
218:
219:            static class PriviledgedSetProxyAction implements 
220:                    PrivilegedExceptionAction {
221:
222:                String proxyHost = null;
223:                int proxyPort = 0;
224:
225:                PriviledgedSetProxyAction(String host, int port) {
226:                    this .proxyHost = host;
227:                    this .proxyPort = port;
228:                }
229:
230:                public Object run() throws Exception {
231:                    System.setProperty("http.proxyHost", proxyHost);
232:                    System.setProperty("http.proxyPort", new Integer(proxyPort)
233:                            .toString());
234:                    log.log(Level.FINE, "SAAJ0050.p2p.proxy.host",
235:                            new String[] { proxyHost });
236:                    log.log(Level.FINE, "SAAJ0051.p2p.proxy.port",
237:                            new String[] { new Integer(proxyPort).toString() });
238:                    return proxyHost;
239:                }
240:            }
241:
242:            public void setProxy(String host, int port) {
243:                try {
244:                    proxyPort = port;
245:                    PriviledgedSetProxyAction ps = new PriviledgedSetProxyAction(
246:                            host, port);
247:                    proxyHost = (String) AccessController.doPrivileged(ps);
248:                } catch (Exception e) {
249:                    throw new RuntimeException(e);
250:                }
251:            }
252:
253:            public String getProxyHost() {
254:                return proxyHost;
255:            }
256:
257:            private int proxyPort = -1;
258:
259:            public int getProxyPort() {
260:                return proxyPort;
261:            }
262:
263:            SOAPMessage post(SOAPMessage message, URL endPoint)
264:                    throws SOAPException {
265:                boolean isFailure = false;
266:
267:                URL url = null;
268:                HttpURLConnection httpConnection = null;
269:
270:                int responseCode = 0;
271:                try {
272:                    if (endPoint.getProtocol().equals("https"))
273:                        //if(!setHttps)
274:                        initHttps();
275:                    // Process the URL
276:                    JaxmURI uri = new JaxmURI(endPoint.toString());
277:                    String userInfo = uri.getUserinfo();
278:
279:                    url = endPoint;
280:
281:                    if (dL > 0)
282:                        d("uri: " + userInfo + " " + url + " " + uri);
283:
284:                    // TBD
285:                    //    Will deal with https later.
286:                    if (!url.getProtocol().equalsIgnoreCase("http")
287:                            && !url.getProtocol().equalsIgnoreCase("https")) {
288:                        log
289:                                .severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
290:                        throw new IllegalArgumentException("Protocol "
291:                                + url.getProtocol() + " not supported in URL "
292:                                + url);
293:                    }
294:                    httpConnection = (HttpURLConnection) createConnection(url);
295:
296:                    httpConnection.setRequestMethod("POST");
297:
298:                    httpConnection.setDoOutput(true);
299:                    httpConnection.setDoInput(true);
300:                    httpConnection.setUseCaches(false);
301:                    HttpURLConnection.setFollowRedirects(true);
302:
303:                    if (message.saveRequired())
304:                        message.saveChanges();
305:
306:                    MimeHeaders headers = message.getMimeHeaders();
307:
308:                    Iterator it = headers.getAllHeaders();
309:                    boolean hasAuth = false; // true if we find explicit Auth header
310:                    while (it.hasNext()) {
311:                        MimeHeader header = (MimeHeader) it.next();
312:
313:                        String[] values = headers.getHeader(header.getName());
314:
315:                        if (values.length == 1)
316:                            httpConnection.setRequestProperty(header.getName(),
317:                                    header.getValue());
318:                        else {
319:                            StringBuffer concat = new StringBuffer();
320:                            int i = 0;
321:                            while (i < values.length) {
322:                                if (i != 0)
323:                                    concat.append(',');
324:                                concat.append(values[i]);
325:                                i++;
326:                            }
327:
328:                            httpConnection.setRequestProperty(header.getName(),
329:                                    concat.toString());
330:                        }
331:
332:                        if ("Authorization".equals(header.getName())) {
333:                            hasAuth = true;
334:                            log.fine("SAAJ0091.p2p.https.auth.in.POST.true");
335:                        }
336:                    }
337:
338:                    if (!hasAuth && userInfo != null) {
339:                        initAuthUserInfo(httpConnection, userInfo);
340:                    }
341:
342:                    OutputStream out = httpConnection.getOutputStream();
343:                    message.writeTo(out);
344:
345:                    out.flush();
346:                    out.close();
347:
348:                    httpConnection.connect();
349:
350:                    try {
351:
352:                        responseCode = httpConnection.getResponseCode();
353:
354:                        // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
355:                        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
356:                            isFailure = true;
357:                        }
358:                        //else if (responseCode != HttpURLConnection.HTTP_OK)
359:                        //else if (!(responseCode >= HttpURLConnection.HTTP_OK && responseCode < 207))
360:                        else if ((responseCode / 100) != 2) {
361:                            log.log(Level.SEVERE, "SAAJ0008.p2p.bad.response",
362:                                    new String[] { httpConnection
363:                                            .getResponseMessage() });
364:                            throw new SOAPExceptionImpl("Bad response: ("
365:                                    + responseCode
366:                                    + httpConnection.getResponseMessage());
367:
368:                        }
369:                    } catch (IOException e) {
370:                        // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
371:                        responseCode = httpConnection.getResponseCode();
372:                        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
373:                            isFailure = true;
374:                        } else {
375:                            throw e;
376:                        }
377:
378:                    }
379:
380:                } catch (SOAPException ex) {
381:                    throw ex;
382:                } catch (Exception ex) {
383:                    log.severe("SAAJ0009.p2p.msg.send.failed");
384:                    throw new SOAPExceptionImpl("Message send failed", ex);
385:                }
386:
387:                SOAPMessage response = null;
388:                if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
389:                    try {
390:                        MimeHeaders headers = new MimeHeaders();
391:
392:                        String key, value;
393:
394:                        // Header field 0 is the status line so we skip it.
395:
396:                        int i = 1;
397:
398:                        while (true) {
399:                            key = httpConnection.getHeaderFieldKey(i);
400:                            value = httpConnection.getHeaderField(i);
401:
402:                            if (key == null && value == null)
403:                                break;
404:
405:                            if (key != null) {
406:                                StringTokenizer values = new StringTokenizer(
407:                                        value, ",");
408:                                while (values.hasMoreTokens())
409:                                    headers.addHeader(key, values.nextToken()
410:                                            .trim());
411:                            }
412:                            i++;
413:                        }
414:
415:                        InputStream httpIn = (isFailure ? httpConnection
416:                                .getErrorStream() : httpConnection
417:                                .getInputStream());
418:
419:                        byte[] bytes = readFully(httpIn);
420:
421:                        int length = httpConnection.getContentLength() == -1 ? bytes.length
422:                                : httpConnection.getContentLength();
423:
424:                        // If no reply message is returned,
425:                        // content-Length header field value is expected to be zero.
426:                        if (length == 0) {
427:                            response = null;
428:                            log.warning("SAAJ0014.p2p.content.zero");
429:                        } else {
430:                            ByteInputStream in = new ByteInputStream(bytes,
431:                                    length);
432:                            response = messageFactory
433:                                    .createMessage(headers, in);
434:                        }
435:
436:                        httpIn.close();
437:                        httpConnection.disconnect();
438:
439:                    } catch (SOAPException ex) {
440:                        throw ex;
441:                    } catch (Exception ex) {
442:                        log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp",
443:                                ex);
444:                        throw new SOAPExceptionImpl("Unable to read response: "
445:                                + ex.getMessage());
446:                    }
447:                }
448:                return response;
449:            }
450:
451:            // Object identifies where the request should be sent.
452:            // It is required to support objects of type String and java.net.URL.
453:
454:            public SOAPMessage get(Object endPoint) throws SOAPException {
455:                if (closed) {
456:                    log.severe("SAAJ0011.p2p.get.already.closed.conn");
457:                    throw new SOAPExceptionImpl("Connection is closed");
458:                }
459:                Class urlEndpointClass = null;
460:
461:                try {
462:                    urlEndpointClass = Class
463:                            .forName("javax.xml.messaging.URLEndpoint");
464:                } catch (Exception ex) {
465:                    //Do nothing. URLEndpoint is available only when JAXM is there.
466:                }
467:
468:                if (urlEndpointClass != null) {
469:                    if (urlEndpointClass.isInstance(endPoint)) {
470:                        String url = null;
471:
472:                        try {
473:                            Method m = urlEndpointClass.getMethod("getURL",
474:                                    (Class[]) null);
475:                            url = (String) m.invoke(endPoint, (Object[]) null);
476:                        } catch (Exception ex) {
477:                            log.severe("SAAJ0004.p2p.internal.err");
478:                            throw new SOAPExceptionImpl("Internal error: "
479:                                    + ex.getMessage());
480:                        }
481:                        try {
482:                            endPoint = new URL(url);
483:                        } catch (MalformedURLException mex) {
484:                            log.severe("SAAJ0005.p2p.");
485:                            throw new SOAPExceptionImpl("Bad URL: "
486:                                    + mex.getMessage());
487:                        }
488:                    }
489:                }
490:
491:                if (endPoint instanceof  java.lang.String) {
492:                    try {
493:                        endPoint = new URL((String) endPoint);
494:                    } catch (MalformedURLException mex) {
495:                        log.severe("SAAJ0006.p2p.bad.URL");
496:                        throw new SOAPExceptionImpl("Bad URL: "
497:                                + mex.getMessage());
498:                    }
499:                }
500:
501:                if (endPoint instanceof  URL)
502:                    try {
503:                        PriviledgedGet pg = new PriviledgedGet(this ,
504:                                (URL) endPoint);
505:                        SOAPMessage response = (SOAPMessage) AccessController
506:                                .doPrivileged(pg);
507:
508:                        return response;
509:                    } catch (Exception ex) {
510:                        throw new SOAPExceptionImpl(ex);
511:                    }
512:                else
513:                    throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
514:            }
515:
516:            static class PriviledgedGet implements  PrivilegedExceptionAction {
517:
518:                HttpSOAPConnection c;
519:                URL endPoint;
520:
521:                PriviledgedGet(HttpSOAPConnection c, URL endPoint) {
522:                    this .c = c;
523:                    this .endPoint = endPoint;
524:                }
525:
526:                public Object run() throws Exception {
527:                    return c.get(endPoint);
528:                }
529:            }
530:
531:            SOAPMessage get(URL endPoint) throws SOAPException {
532:                boolean isFailure = false;
533:
534:                URL url = null;
535:                HttpURLConnection httpConnection = null;
536:
537:                int responseCode = 0;
538:                try {
539:                    /// Is https GET allowed??
540:                    if (endPoint.getProtocol().equals("https"))
541:                        initHttps();
542:                    // Process the URL
543:                    JaxmURI uri = new JaxmURI(endPoint.toString());
544:                    String userInfo = uri.getUserinfo();
545:
546:                    url = endPoint;
547:
548:                    if (dL > 0)
549:                        d("uri: " + userInfo + " " + url + " " + uri);
550:
551:                    // TBD
552:                    //    Will deal with https later.
553:                    if (!url.getProtocol().equalsIgnoreCase("http")
554:                            && !url.getProtocol().equalsIgnoreCase("https")) {
555:                        log
556:                                .severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
557:                        throw new IllegalArgumentException("Protocol "
558:                                + url.getProtocol() + " not supported in URL "
559:                                + url);
560:                    }
561:                    httpConnection = (HttpURLConnection) createConnection(url);
562:
563:                    httpConnection.setRequestMethod("GET");
564:
565:                    httpConnection.setDoOutput(true);
566:                    httpConnection.setDoInput(true);
567:                    httpConnection.setUseCaches(false);
568:                    HttpURLConnection.setFollowRedirects(true);
569:
570:                    httpConnection.connect();
571:
572:                    try {
573:
574:                        responseCode = httpConnection.getResponseCode();
575:
576:                        // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
577:                        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
578:                            isFailure = true;
579:                        } else if ((responseCode / 100) != 2) {
580:                            log.log(Level.SEVERE, "SAAJ0008.p2p.bad.response",
581:                                    new String[] { httpConnection
582:                                            .getResponseMessage() });
583:                            throw new SOAPExceptionImpl("Bad response: ("
584:                                    + responseCode
585:                                    + httpConnection.getResponseMessage());
586:
587:                        }
588:                    } catch (IOException e) {
589:                        // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
590:                        responseCode = httpConnection.getResponseCode();
591:                        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
592:                            isFailure = true;
593:                        } else {
594:                            throw e;
595:                        }
596:
597:                    }
598:
599:                } catch (SOAPException ex) {
600:                    throw ex;
601:                } catch (Exception ex) {
602:                    log.severe("SAAJ0012.p2p.get.failed");
603:                    throw new SOAPExceptionImpl("Get failed", ex);
604:                }
605:
606:                SOAPMessage response = null;
607:                if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
608:                    try {
609:                        MimeHeaders headers = new MimeHeaders();
610:
611:                        String key, value;
612:
613:                        // Header field 0 is the status line so we skip it.
614:
615:                        int i = 1;
616:
617:                        while (true) {
618:                            key = httpConnection.getHeaderFieldKey(i);
619:                            value = httpConnection.getHeaderField(i);
620:
621:                            if (key == null && value == null)
622:                                break;
623:
624:                            if (key != null) {
625:                                StringTokenizer values = new StringTokenizer(
626:                                        value, ",");
627:                                while (values.hasMoreTokens())
628:                                    headers.addHeader(key, values.nextToken()
629:                                            .trim());
630:                            }
631:                            i++;
632:                        }
633:
634:                        InputStream httpIn = (isFailure ? httpConnection
635:                                .getErrorStream() : httpConnection
636:                                .getInputStream());
637:
638:                        byte[] bytes = readFully(httpIn);
639:
640:                        int length = httpConnection.getContentLength() == -1 ? bytes.length
641:                                : httpConnection.getContentLength();
642:
643:                        // If no reply message is returned,
644:                        // content-Length header field value is expected to be zero.
645:                        if (length == 0) {
646:                            response = null;
647:                            log.warning("SAAJ0014.p2p.content.zero");
648:                        } else {
649:
650:                            ByteInputStream in = new ByteInputStream(bytes,
651:                                    length);
652:                            response = messageFactory
653:                                    .createMessage(headers, in);
654:                        }
655:
656:                        httpIn.close();
657:                        httpConnection.disconnect();
658:
659:                    } catch (SOAPException ex) {
660:                        throw ex;
661:                    } catch (Exception ex) {
662:                        log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp",
663:                                ex);
664:                        throw new SOAPExceptionImpl("Unable to read response: "
665:                                + ex.getMessage());
666:                    }
667:                }
668:                return response;
669:            }
670:
671:            private byte[] readFully(InputStream istream) throws IOException {
672:                ByteArrayOutputStream bout = new ByteArrayOutputStream();
673:                byte[] buf = new byte[1024];
674:                int num = 0;
675:
676:                while ((num = istream.read(buf)) != -1) {
677:                    bout.write(buf, 0, num);
678:                }
679:
680:                byte[] ret = bout.toByteArray();
681:
682:                return ret;
683:            }
684:
685:            private static String SSL_PKG = "com.sun.net.ssl.internal.www.protocol";
686:            private static String SSL_PROVIDER = "com.sun.net.ssl.internal.ssl.Provider";
687:
688:            private void initHttps() {
689:                //if(!setHttps) {
690:                String pkgs = System.getProperty("java.protocol.handler.pkgs");
691:                log.log(Level.FINE, "SAAJ0053.p2p.providers",
692:                        new String[] { pkgs });
693:
694:                if (pkgs == null || pkgs.indexOf(SSL_PKG) < 0) {
695:                    if (pkgs == null)
696:                        pkgs = SSL_PKG;
697:                    else
698:                        pkgs = pkgs + "|" + SSL_PKG;
699:                    System.setProperty("java.protocol.handler.pkgs", pkgs);
700:                    log.log(Level.FINE, "SAAJ0054.p2p.set.providers",
701:                            new String[] { pkgs });
702:                    try {
703:                        Class c = Class.forName(SSL_PROVIDER);
704:                        Provider p = (Provider) c.newInstance();
705:                        Security.addProvider(p);
706:                        log.log(Level.FINE, "SAAJ0055.p2p.added.ssl.provider",
707:                                new String[] { SSL_PROVIDER });
708:                        //System.out.println("Added SSL_PROVIDER " + SSL_PROVIDER);
709:                        //setHttps = true;
710:                    } catch (Exception ex) {
711:                    }
712:                }
713:                //}
714:            }
715:
716:            private void initAuthUserInfo(HttpURLConnection conn,
717:                    String userInfo) {
718:                String user;
719:                String password;
720:                if (userInfo != null) { // get the user and password
721:                    //System.out.println("UserInfo= " + userInfo );
722:                    int delimiter = userInfo.indexOf(':');
723:                    if (delimiter == -1) {
724:                        user = ParseUtil.decode(userInfo);
725:                        password = null;
726:                    } else {
727:                        user = ParseUtil.decode(userInfo.substring(0,
728:                                delimiter++));
729:                        password = ParseUtil.decode(userInfo
730:                                .substring(delimiter));
731:                    }
732:
733:                    String plain = user + ":";
734:                    byte[] nameBytes = plain.getBytes();
735:                    byte[] passwdBytes = password.getBytes();
736:
737:                    // concatenate user name and password bytes and encode them
738:                    byte[] concat = new byte[nameBytes.length
739:                            + passwdBytes.length];
740:
741:                    System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length);
742:                    System.arraycopy(passwdBytes, 0, concat, nameBytes.length,
743:                            passwdBytes.length);
744:                    String auth = "Basic " + new String(Base64.encode(concat));
745:                    conn.setRequestProperty("Authorization", auth);
746:                    if (dL > 0)
747:                        d("Adding auth " + auth);
748:                }
749:            }
750:
751:            private static final int dL = 0;
752:
753:            private void d(String s) {
754:                log.log(Level.SEVERE, "SAAJ0013.p2p.HttpSOAPConnection",
755:                        new String[] { s });
756:                System.err.println("HttpSOAPConnection: " + s);
757:            }
758:
759:            private java.net.HttpURLConnection createConnection(URL endpoint)
760:                    throws IOException {
761:                return (HttpURLConnection) endpoint.openConnection();
762:            }
763:
764:        }
ww__w_.___j__ava__2___s._c___om___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.