1 64 65 package com.jcorporate.expresso.core.utility; 66 67 72 73 import com.jcorporate.expresso.core.db.DBException; 74 75 import java.io.BufferedReader ; 76 import java.io.InputStream ; 77 import java.io.InputStreamReader ; 78 import java.net.MalformedURLException ; 79 import java.net.URL ; 80 import java.net.URLConnection ; 81 82 83 90 public class RunURL { 91 private static final String thisClass = RunURL.class.getName() + "."; 92 93 96 public RunURL() { 97 super(); 98 } 99 100 106 public RunURL(String URLString) 107 throws DBException { 108 String myName = (thisClass + "RunURL(String[])"); 109 URL myUrl = null; 110 URLConnection c; 111 System.out.println(myName + ":Executing URL:" + URLString); 112 113 try { 114 myUrl = new URL (URLString); 115 } catch (MalformedURLException m) { 116 throw new DBException(myName + ":Bad URL:" + URLString); 117 } 118 try { 119 c = myUrl.openConnection(); 120 c.setDoOutput(true); 121 c.setAllowUserInteraction(true); 122 123 Object o2 = c.getContent(); 124 125 if (o2 == null) { 126 throw new DBException(myName + ":Null content reading from " + 127 "server"); 128 } 129 if (o2 instanceof InputStream ) { 130 InputStream is = (InputStream ) o2; 131 BufferedReader dis = new BufferedReader (new InputStreamReader (is)); 132 133 if (dis == null) { 134 throw new DBException(myName + ":No connection to " + 135 "server:DataInputStream dis is null"); 136 } 137 138 int linesRead = 0; 139 String oneLine = dis.readLine(); 140 141 if (oneLine == null) { 142 throw new DBException(myName + ":Server never sent EOF"); 143 } 144 145 System.out.println(oneLine); 146 147 while (oneLine != null) { 148 if (!oneLine.equals("")) { 149 linesRead++; 150 System.out.println(oneLine); 151 } 152 153 154 oneLine = dis.readLine(); 155 } 156 157 dis.close(); 158 } else { 159 throw new DBException(myName + ":Could not get input stream " + 160 "from servlet"); 161 } 162 } catch (Exception io) { 163 io.printStackTrace(); 164 throw new DBException(myName + ":I/O Error communicating with " + 165 "server with URL '" + URLString + 166 "'. Logged in as user " + 167 System.getProperty("user.name") + ":" + 168 io.getMessage()); 169 } 170 171 System.out.println("URL executed successfully"); 172 } 173 174 179 public static void main(String [] args) { 180 if (args.length < 1) { 181 System.out.println("You must specify a URL as an argument. " + 182 "Enclose in quotes"); 183 System.exit(1); 184 } 185 try { 186 new RunURL(args[0]); 187 } catch (DBException de) { 188 System.out.println(de.getMessage()); 189 System.exit(1); 190 } 191 } 192 193 } 194 195 | Popular Tags |