KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > util > ups > UPSConnections


1 package com.dotmarketing.util.ups;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStreamReader JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.net.HttpURLConnection JavaDoc;
8 import java.net.URL JavaDoc;
9 import java.net.URLConnection JavaDoc;
10 import java.util.Date JavaDoc;
11
12 import com.dotmarketing.util.Config;
13 import com.dotmarketing.util.Logger;
14 import com.sun.net.ssl.HttpsURLConnection;
15
16 /**
17  *
18  * @author Oswaldo Galango
19  *
20  */

21
22 /**
23  * The UPSConnections will transmit an HTTP/HTTPS post with the StringBuffer provided as
24  * the data of the post message. The UPSConnections must be constructed with a URL or
25  * IP address and a protocol to use for transmitting the message.
26  */

27
28 public class UPSConnections {
29     
30     private String JavaDoc protocol = Config.getStringProperty("UPS_PROTOCOL").trim();
31     private String JavaDoc hostname = Config.getStringProperty("UPS_HOSTNAME").trim();
32     private String JavaDoc URLPrefix = Config.getStringProperty("UPS_PREFIX").trim();
33     
34     
35     public UPSConnections() {
36         
37     }
38     
39     /**
40      *
41      * @param service This field indicate the url page tools to acces in this case "Rate" is the value to use
42      * @param xmlRequest is the XML Request
43      * @return the XML Response String of the UPS SERVER
44      * @throws Exception
45      */

46     public String JavaDoc contactService(String JavaDoc service, StringBuffer JavaDoc xmlRequest) throws Exception JavaDoc
47     {
48         //System.out.println("UPS CONNECTIONS ***** Started " + service + " " + new Date().toString() + " *****");
49
Logger.debug(UPSConnections.class,"UPS CONNECTIONS ***** Started " + service + " " + new Date JavaDoc().toString() + " *****");
50         HttpURLConnection JavaDoc connection;
51         URL JavaDoc url;
52         String JavaDoc response = "";
53         
54         try
55         {
56             //System.out.println("connect to " + protocol + "://" + hostname + "/" + URLPrefix + "/" + service);
57
Logger.debug(UPSConnections.class,"connect to " + protocol + "://" + hostname + "/" + URLPrefix + "/" + service);
58             // Create new URL and connect
59
if (protocol.equalsIgnoreCase("https"))
60             {
61                 //use Sun's JSSE to deal with SSL
62
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
63                 System.getProperties().put("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
64                 url = new URL JavaDoc(protocol + "://" + hostname + "/" + URLPrefix + "/" + service);
65                 connection = (HttpsURLConnection) url.openConnection();
66                 
67             }
68             else
69             {
70                 url = new URL JavaDoc(protocol + "://" + hostname + "/" + URLPrefix + "/" + service);
71                 connection = (HttpURLConnection JavaDoc) url.openConnection();
72             }
73             //System.out.println("Establishing connection with " + url.toString());
74
Logger.debug(UPSConnections.class,"Establishing connection with " + url.toString());
75             // Setup HTTP POST parameters
76
connection.setDoOutput(true);
77             connection.setDoInput(true);
78             connection.setUseCaches(false);
79             
80             
81             // POST data
82
OutputStream JavaDoc out = connection.getOutputStream();
83             
84             StringBuffer JavaDoc request = new StringBuffer JavaDoc();
85             request.append(accessXMLRequest());
86             request.append(xmlRequest);
87             
88             out.write((request.toString()).getBytes());
89             //System.out.println("Transmission sent to " + url.toString() + ":\n" + xmlRequest);
90
Logger.debug(UPSConnections.class,"Transmission sent to " + url.toString() + ":\n" + xmlRequest);
91             out.close();
92             
93             // get data from URL connection and return the XML document as a StringBuffer
94

95             try
96             {
97                 response = readURLConnection(connection);
98             }catch (Exception JavaDoc e)
99             {
100                 //System.out.println("Error in reading URL Connection" + e.getMessage());
101
Logger.debug(UPSConnections.class,"Error in reading URL Connection" + e.getMessage());
102                 throw e;
103             }
104             //System.out.println("Response = " + response);
105
Logger.debug(UPSConnections.class,"Response = " + response);
106             
107         } catch (Exception JavaDoc e1)
108         {
109             System.out.println("Error sending data to server" + e1.toString());
110             Logger.debug(UPSConnections.class,"Error sending data to server" + e1.toString());
111         } finally
112         {
113             System.out.println("****** Transmission Finished " + service + " " + new Date JavaDoc().toString() + " *********");
114             Logger.debug(UPSConnections.class,"****** Transmission Finished " + service + " " + new Date JavaDoc().toString() + " *********");
115         }
116         
117         return response;
118     }
119     
120     /**
121      * This method read all of the data from a URL conection to a String
122      */

123     
124     private static String JavaDoc readURLConnection(URLConnection JavaDoc uc) throws Exception JavaDoc
125     {
126         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
127         BufferedReader JavaDoc reader = null;
128         try
129         {
130             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(uc.getInputStream()));
131             int letter = 0;
132             while ((letter = reader.read()) != -1)
133                 buffer.append((char) letter);
134         } catch (Exception JavaDoc e)
135         {
136             //System.out.println("Cannot read from URL" + e.toString());
137
Logger.debug(UPSConnections.class,"Cannot read from URL" + e.toString());
138             throw e;
139         } finally
140         {
141             try
142             {
143                 reader.close();
144             } catch (IOException JavaDoc io)
145             {
146                 //System.out.println("Error closing URLReader!");
147
Logger.debug(UPSConnections.class,"Error closing URLReader!");
148                 throw io;
149             }
150         }
151         return buffer.toString();
152     }
153     
154     /**
155      * This xml is obligatory to use. Appended with the xml request to get a result
156      * @return
157      */

158     public static StringBuffer JavaDoc accessXMLRequest(){
159         
160         StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
161         /*Access request*/
162         xml.append("<?xml version=\"1.0\"?>");
163         xml.append("<AccessRequest xml:lang=\"en-US\">");
164         xml.append("<AccessLicenseNumber>"+Config.getStringProperty("UPS_ACCESSLICENSENUMBER")+"</AccessLicenseNumber>");
165         xml.append("<UserId>"+Config.getStringProperty("UPS_USERID")+"</UserId>");
166         xml.append("<Password>"+Config.getStringProperty("UPS_PASSWORD")+"</Password>");
167         xml.append("</AccessRequest>");
168         
169         return xml;
170     }
171     
172 }
173
Popular Tags