1 55 package org.lateralnz.common.util; 56 57 import java.io.BufferedReader ; 58 import java.io.InputStream ; 59 import java.io.InputStreamReader ; 60 import java.io.OutputStream ; 61 import java.net.URL ; 62 import java.net.HttpURLConnection ; 63 64 import org.apache.log4j.Logger; 65 66 import org.lateralnz.common.util.Constants; 67 68 73 public class HttpUtils implements Constants { 74 75 private static final Logger log = Logger.getLogger(HttpUtils.class.getName()); 76 77 public static final int GET_METHOD = 0; 78 public static final int POST_METHOD = 1; 79 public static final String POST = "POST"; 80 public static final String GET = "GET"; 81 private static final String CLOSE = "Close"; 82 private static final String CONNECTION = "Connection"; 83 private static final String CONTENT_TYPE = "Content-Type"; 84 private static final String CONTENT_LEN = "Content-Length"; 86 private static final String SENT_TEXT1 = "Sent :: "; 87 private static final String SENT_TEXT2 = " ("; 88 private static final String SENT_TEXT3 = ")"; 89 90 private HttpUtils() { 91 } 92 93 public static final String send(String url, int method, String contentType, String body) throws Exception { 94 return send(url, method, null, contentType, body); 95 } 96 97 104 public static final String send(String url, int method, String [][] extraHeaders, String contentType, String body) throws Exception { 105 106 if (!StringUtils.isEmpty(url)) { 108 url = StringUtils.replace(url, SPACE, PLUS); 109 } 110 111 String sentMethod; 112 URL messageURL = new URL (url); 113 HttpURLConnection con = null; 114 OutputStream out = null; 115 InputStream in = null; 116 BufferedReader r = null; 117 118 try { 119 con = (HttpURLConnection )messageURL.openConnection(); 120 121 con.setRequestProperty(CONNECTION, CLOSE); 122 con.setDoInput(true); 123 124 if (extraHeaders != null) { 125 for (int i = 0; i < extraHeaders.length; i++) { 126 con.setRequestProperty(extraHeaders[i][0], extraHeaders[i][1]); 127 } 128 } 129 130 if (method == GET_METHOD) { 131 con.setRequestMethod(GET); 132 con.setDoOutput(false); 133 sentMethod = GET; 134 } 135 else { 136 con.setRequestMethod(POST); 137 con.setDoOutput(true); 138 con.setRequestProperty(CONTENT_TYPE, contentType); 139 140 if(!StringUtils.isEmpty(body)) { 142 int length = body.length(); 143 con.setRequestProperty(CONTENT_LEN, EMPTY+length); 144 } 145 else { 146 con.setRequestProperty(CONTENT_LEN, ZERO); 147 } 148 149 out = con.getOutputStream(); 150 out.write(body.getBytes()); 151 sentMethod = POST; 152 } 153 154 in = con.getInputStream(); 155 r = new BufferedReader (new InputStreamReader (in)); 156 String line; 157 158 StringBuffer sb = new StringBuffer (); 159 char[] buf = new char[1024]; 160 int len; 161 while ((len = r.read(buf)) != -1) { 162 sb.append(buf, 0, len); 163 } 164 165 log.info("Sent: " + url); 169 log.info("Method: " + sentMethod); 170 172 return sb.toString(); 173 } 174 finally { 175 IOUtils.close(r); 176 IOUtils.close(in); 177 IOUtils.close(out); 178 IOUtils.disconnect(con); 179 } 180 } 181 182 private static final int getMethod(String method) throws Exception { 183 if (method != null) { 184 if (method.equals(GET)) { 185 return GET_METHOD; 186 } 187 else if (method.equals(POST)) { 188 return POST_METHOD; 189 } 190 } 191 throw new Exception ("invalid method : " + method); 192 } 193 194 public static final void main(String [] args) throws Exception { 196 String url = args[0]; 197 int method = getMethod(args[1]); 198 String contentType = args[2]; 199 String body = StringUtils.readFromFile(args[3]); 200 201 if (StringUtils.isEmpty(body)) { 202 throw new Exception ("invalid file " + args[3]); 203 } 204 205 System.out.println(HttpUtils.send(url, method, contentType, body)); 206 } 207 } | Popular Tags |