KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > oreilly > servlet > HttpsMessage


1 // Copyright (C) 2000 by Matt Towers <eBozo_AT_hotmail_DOT_com>.
2
// All rights reserved. Use of this class is limited.
3
// Please see the LICENSE for more information.
4

5 /*
6  The source code and object code, of the HttpsMessage class is copyright and
7  owned by Matt Towers.
8
9  Feel free to use the HttpsMessage class in the development of any development
10  project. For this use you are granted a non-exclusive, non-transferable
11  limited license at no cost.
12
13  Redistribution of the HttpsMessage source code is permitted provided that the
14  following conditions are met:
15
16    1.You maintain the original copyright notice in the source code.
17    2.This license file is redistributed with the source code.
18    3.You acknowledge that the parent HttpMessage class, with which this class
19      is intended to be used, is owned and copyrighted by Jason Hunter
20      <jhunter_AT_acm_DOT_org>. (For more information see
21      http://www.servlets.com)
22
23  To clarify, you may use the HttpsMessage class to build new software and may
24  distribute the object code as you see fit. You may NOT distribute the source
25  code as part of a software development kit, other library, or development tool
26  without consent of the copyright holder. Any modified form of the HttpsMessage
27  class is bound by these same restrictions.
28
29  Note that the HttpsMessage class is provided "as is" and the author will not
30  be liable for any damages suffered as a result of your use.
31  Furthermore, you understand the source code comes without any technical
32  support.
33
34  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
35  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
38  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  SUCH DAMAGE.
45
46  Matt Towers <eBozo_AT_hotmail_DOT_com>
47 */

48
49 package com.oreilly.servlet;
50
51 import java.io.*;
52 import java.net.*;
53 import java.util.*;
54 import java.security.Security JavaDoc;
55 import java.security.Provider JavaDoc;
56
57 /**
58  * A class to simplify HTTP/HTTPS client-server communication. It abstracts
59  * the communication into messages, which can be either GET or POST.
60  * <p>
61  * It can be used like this:
62  * <blockquote><pre>
63  * &nbsp;
64  * HttpsMessage msg = new HttpsMessage("https://[some server]");
65  * &nbsp;
66  * Properties props = new Properties();
67  * props.put("name", "value");
68  * &nbsp;
69  * InputStream in = msg.sendGetMessage(props);
70  * </pre></blockquote>
71  * This class extends the HttpMessage class
72  * written by Jason Hunter at servlets.com.
73  * The HttpMessage class can be found in the com.oreilly.servlet
74  * package found at www.servlets.com
75  * <p>
76  * For information see http://www.javaworld.com/javatips/jw-javatip96.html
77  * Note this class works with JDK 1.2 or later only.
78  * <p>
79  * @author <b>Matt Towers</b>
80  * @author Copyright &#169; 2000
81  * @version 1.0, 2000/05/05
82  */

83 public class HttpsMessage extends com.oreilly.servlet.HttpMessage
84 {
85
86   //A flag to indicate weather or not the stream handler has been set
87
static boolean m_bStreamHandlerSet = false;
88
89   /**
90    * Constructs a new HttpsMessage that can be used to communicate with the
91    * servlet at the specified URL using HTTPS.
92    *
93    * @param szURL the server resource (typically a servlet) with which
94    * to communicate
95    */

96   public HttpsMessage(String JavaDoc szURL) throws Exception JavaDoc
97   {
98     super(null);
99     //If the stream handler has already been set
100
//there is no need to do anything
101
if( !m_bStreamHandlerSet )
102     {
103         String JavaDoc szVendor = System.getProperty("java.vendor");
104         String JavaDoc szVersion = System.getProperty("java.version");
105         //Assumes a system version string of the form [major].[minor].[release] (eg. 1.2.2)
106
Double JavaDoc dVersion = new Double JavaDoc(szVersion.substring(0, 3));
107
108         //Otherwise, if we are running in a MS environment, use the MS stream handler.
109
if( -1 < szVendor.indexOf("Microsoft") )
110         {
111             try
112             {
113                 Class JavaDoc clsFactory = Class.forName("com.ms.net.wininet.WininetStreamHandlerFactory" );
114                 if ( null != clsFactory)
115                     URL.setURLStreamHandlerFactory((URLStreamHandlerFactory)clsFactory.newInstance());
116             }
117             catch( ClassNotFoundException JavaDoc cfe )
118             {
119                 throw new Exception JavaDoc("Unable to load the Microsoft SSL stream handler. Check classpath." + cfe.toString());
120             }
121             //If the stream handler factory has already been successfuly set
122
//make sure our flag is set and eat the error
123
catch( Error JavaDoc err ){m_bStreamHandlerSet = true;}
124         }
125         //If we are in a normal Java environment, try to use the JSSE handler.
126
else if( 1.2 <= dVersion.doubleValue() )
127         {
128             System.getProperties().put("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
129             try
130             {
131                 //if we have the JSSE provider available, and it has not already been
132
//set, add it as a new provide to the Security class.
133
Class JavaDoc clsFactory = Class.forName("com.sun.net.ssl.internal.ssl.Provider");
134                 if( (null != clsFactory) && (null == Security.getProvider("SunJSSE")) )
135                         Security.addProvider((Provider JavaDoc)clsFactory.newInstance());
136             }
137             catch( ClassNotFoundException JavaDoc cfe )
138             {
139                 throw new Exception JavaDoc("Unable to load the JSSE SSL stream handler. Check classpath." + cfe.toString());
140             }
141         }
142                                         
143         m_bStreamHandlerSet = true;
144     }
145     
146     super.servlet = new URL(szURL);
147   }
148 }
149
Popular Tags