1 package com.dotmarketing.util.ups; 2 3 import java.io.BufferedReader ; 4 import java.io.IOException ; 5 import java.io.InputStreamReader ; 6 import java.io.OutputStream ; 7 import java.net.HttpURLConnection ; 8 import java.net.URL ; 9 import java.net.URLConnection ; 10 import java.util.Date ; 11 12 import com.dotmarketing.util.Config; 13 import com.dotmarketing.util.Logger; 14 import com.sun.net.ssl.HttpsURLConnection; 15 16 21 22 27 28 public class UPSConnections { 29 30 private String protocol = Config.getStringProperty("UPS_PROTOCOL").trim(); 31 private String hostname = Config.getStringProperty("UPS_HOSTNAME").trim(); 32 private String URLPrefix = Config.getStringProperty("UPS_PREFIX").trim(); 33 34 35 public UPSConnections() { 36 37 } 38 39 46 public String contactService(String service, StringBuffer xmlRequest) throws Exception 47 { 48 Logger.debug(UPSConnections.class,"UPS CONNECTIONS ***** Started " + service + " " + new Date ().toString() + " *****"); 50 HttpURLConnection connection; 51 URL url; 52 String response = ""; 53 54 try 55 { 56 Logger.debug(UPSConnections.class,"connect to " + protocol + "://" + hostname + "/" + URLPrefix + "/" + service); 58 if (protocol.equalsIgnoreCase("https")) 60 { 61 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 (protocol + "://" + hostname + "/" + URLPrefix + "/" + service); 65 connection = (HttpsURLConnection) url.openConnection(); 66 67 } 68 else 69 { 70 url = new URL (protocol + "://" + hostname + "/" + URLPrefix + "/" + service); 71 connection = (HttpURLConnection ) url.openConnection(); 72 } 73 Logger.debug(UPSConnections.class,"Establishing connection with " + url.toString()); 75 connection.setDoOutput(true); 77 connection.setDoInput(true); 78 connection.setUseCaches(false); 79 80 81 OutputStream out = connection.getOutputStream(); 83 84 StringBuffer request = new StringBuffer (); 85 request.append(accessXMLRequest()); 86 request.append(xmlRequest); 87 88 out.write((request.toString()).getBytes()); 89 Logger.debug(UPSConnections.class,"Transmission sent to " + url.toString() + ":\n" + xmlRequest); 91 out.close(); 92 93 95 try 96 { 97 response = readURLConnection(connection); 98 }catch (Exception e) 99 { 100 Logger.debug(UPSConnections.class,"Error in reading URL Connection" + e.getMessage()); 102 throw e; 103 } 104 Logger.debug(UPSConnections.class,"Response = " + response); 106 107 } catch (Exception 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 ().toString() + " *********"); 114 Logger.debug(UPSConnections.class,"****** Transmission Finished " + service + " " + new Date ().toString() + " *********"); 115 } 116 117 return response; 118 } 119 120 123 124 private static String readURLConnection(URLConnection uc) throws Exception 125 { 126 StringBuffer buffer = new StringBuffer (); 127 BufferedReader reader = null; 128 try 129 { 130 reader = new BufferedReader (new InputStreamReader (uc.getInputStream())); 131 int letter = 0; 132 while ((letter = reader.read()) != -1) 133 buffer.append((char) letter); 134 } catch (Exception e) 135 { 136 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 io) 145 { 146 Logger.debug(UPSConnections.class,"Error closing URLReader!"); 148 throw io; 149 } 150 } 151 return buffer.toString(); 152 } 153 154 158 public static StringBuffer accessXMLRequest(){ 159 160 StringBuffer xml = new StringBuffer (); 161 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 |