View Javadoc
1 /* 2 * Copyright (c) 2003 3 * Information Desire GmbH 4 * All rights reserved. 5 */ 6 package com.infodesire.infobit.servlet; 7 8 import java.io.BufferedReader; 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.IOException; 12 import java.io.InputStream; 13 import java.io.InputStreamReader; 14 import java.io.OutputStream; 15 import java.io.OutputStreamWriter; 16 import java.io.Reader; 17 import java.io.Writer; 18 19 import java.net.InetAddress; 20 import java.net.MalformedURLException; 21 import java.net.Socket; 22 import java.net.URL; 23 24 /*** 25 * Class ClientTool supports the access to various test servlets. In particular, 26 * it may be used to send PUT requests to the {@link BinaryServlet}. <p> 27 * 28 * <b>Usage:</b> <br> 29 * [--put file | --type mime-type]* resource <p> 30 * 31 * <b>Description</b> <br> 32 * Sends the request specified by the relative URL <code>resource</code>. The 33 * request is specified by the options. <p> 34 * 35 * <b>Options:</b> <br> 36 * 37 * <dl> 38 * <dt> --put file 39 * <dd> Request to PUT <code>file</code> to <code>resource> 40 * 41 * 42 * 43 * 44 * 45 * 46 * 47 * 48 * 49 * 50 * 51 * 52 * 53 * 54 * 55 * 56 * 57 * 58 * 59 * 60 * 61 * 62 * 63 * 64 * 65 * 66 * 67 * 68 * 69 * 70 * 71 * 72 * 73 * 74 * 75 * 76 * 77 * 78 * 79 * 80 * 81 * 82 * 83 * 84 * 85 * 86 * 87 * 88 * 89 * 90 * 91 * 92 * 93 * 94 * 95 * 96 * 97 *<dt> --type type 98 * <dd> Specifies the MIME type of a resource to PUT. Defaults to <code>application/octet</code> 99 * 100 * </dl> 101 * 102 * 103 * @author peter2 104 * @created August 29, 2003 105 * @version $Revision: 1.29 $ 106 */ 107 public class ClientTool { 108 109 /*** 110 * End-of-line marker appropriate for mail header 111 */ 112 private final static String EOL = "\r\n"; 113 114 /*** 115 * The resource to access 116 */ 117 private URL _resource; 118 119 /*** 120 * Content type of the body to PUT 121 */ 122 private String _contentType = "application/octet"; 123 124 /*** 125 * System ID of an entity to PUT to {@link #_resource} 126 */ 127 private File _entity; 128 129 130 /*** 131 * Performs a request. 132 * 133 * @param argv Description of Parameter 134 */ 135 public static void main(String[] argv) { 136 int rc = 0; 137 138 try { 139 ClientTool tool = new ClientTool(); 140 141 if (tool.setup(argv)) { 142 rc = tool.process(); 143 } 144 } catch (Exception e) { 145 e.printStackTrace(System.err); 146 rc = -1; 147 } 148 } 149 150 151 /*** 152 * Sets up the properties from a command line. See the class comment for a 153 * specification of the command line format. 154 * 155 * @param argv The command line arguments 156 * @return The command line arguments are appropriate 157 */ 158 public boolean setup(String[] argv) { 159 boolean success = true; 160 161 try { 162 int i; 163 for (i = 0; success && 164 i < argv.length && argv[i].startsWith("--"); 165 ++i) { 166 167 if (argv[i].equals("--put")) { 168 _entity = new File(argv[++i]); 169 } 170 else if (argv[i].equals("--type")) { 171 _contentType = argv[++i]; 172 } 173 else { 174 success = false; 175 System.err.println("Illegal option: " + argv[i]); 176 } 177 } 178 179 _resource = new URL(argv[i]); 180 } catch (ArrayIndexOutOfBoundsException e) { 181 success = false; 182 System.err.println("missing option argument or resource"); 183 } catch (MalformedURLException e) { 184 success = false; 185 System.err.println("Illegal resource format: " + e.getMessage()); 186 } 187 188 return success; 189 } 190 191 192 /*** 193 * Processes the request specified by the object state. 194 * 195 * @return An error code, whith zero indicating success 196 * @exception IOException Description of Exception 197 */ 198 public int process() throws IOException { 199 int rc = 0; 200 201 if (_resource != null) { 202 InputStream data = new FileInputStream(_entity); 203 204 String host = _resource.getHost(); 205 int port = _resource.getPort(); 206 if (port == -1) { 207 port = _resource.getDefaultPort(); 208 } 209 210 InetAddress addr = InetAddress.getByName(host); 211 Socket conn = new Socket(addr, port); 212 OutputStream binary = conn.getOutputStream(); 213 Writer out = new OutputStreamWriter(binary); 214 215 String res = _resource.getPath() + '?' + _resource.getQuery(); 216 out.write("PUT " + res + " HTTP/1.1" + EOL); 217 out.write("Host: " + host + ":" + port + EOL); 218 out.write("Content-Type: " + _contentType + EOL); 219 out.write("Content-Length: " + _entity.length() + EOL); 220 out.write("Connection: close" + EOL); 221 out.write(EOL); 222 out.flush(); 223 224 byte[] b = new byte[512]; 225 int n; 226 while ((n = data.read(b)) > -1) { 227 binary.write(b, 0, n); 228 } 229 binary.flush(); 230 231 InputStream is = conn.getInputStream(); 232 Reader rd = new InputStreamReader(is); 233 Reader response = new BufferedReader(rd); 234 235 scanResponse(response); 236 response.close(); 237 out.close(); 238 conn.close(); 239 } 240 241 return rc; 242 } 243 244 245 /*** 246 * Scans the server response. 247 * 248 * @param response Where to read the response from. 249 * @exception IOException Description of Exception 250 */ 251 private void scanResponse(Reader response) throws IOException { 252 int state = 0; 253 int d; 254 255 while ((d = response.read()) != -1) { 256 char c = (char) d; 257 258 switch (state) { 259 case 0: 260 // scanning text 261 switch (c) { 262 case '\r': 263 state = 1; 264 break; 265 case '\n': 266 System.out.println(); 267 default: 268 System.out.print(c); 269 } 270 break; 271 case 1: 272 // CR pending 273 switch (c) { 274 case '\n': 275 System.out.println(); 276 state = 0; 277 break; 278 default: 279 System.out.print('\r'); 280 state = 0; 281 } 282 } 283 } 284 } 285 286 }

This page was automatically generated by Maven