1 7 8 46 package org.jdesktop.swing.actions; 47 48 import java.awt.event.ActionEvent ; 49 50 import java.io.BufferedReader ; 51 import java.io.ByteArrayOutputStream ; 52 import java.io.InputStream ; 53 import java.io.InputStreamReader ; 54 import java.io.IOException ; 55 import java.io.PrintWriter ; 56 import java.io.UnsupportedEncodingException ; 57 58 import java.net.MalformedURLException ; 59 import java.net.HttpURLConnection ; 60 import java.net.UnknownHostException ; 61 import java.net.URL ; 62 import java.net.URLConnection ; 63 import java.net.URLEncoder ; 64 65 import java.security.AccessControlException ; 66 67 import java.util.Map ; 68 import java.util.HashMap ; 69 import java.util.Iterator ; 70 import java.util.Set ; 71 72 import javax.swing.AbstractAction ; 73 import javax.swing.Action ; 74 import javax.swing.Icon ; 75 76 import org.jdesktop.swing.Application; 77 78 79 84 public class ServerAction extends AbstractAction { 85 87 private static final String PARAMS = "action-params"; 88 private static final String HEADERS = "action-headers"; 89 private static final String URL = "action-url"; 90 91 private static final String URL_CACHE = "_URL-CACHE__"; 92 93 public ServerAction() { 94 this("action"); 95 } 96 97 public ServerAction(String name) { 98 super(name); 99 } 100 101 105 public ServerAction(String name, String command) { 106 this(name, command, null); 107 } 108 109 public ServerAction(String name, Icon icon) { 110 super(name, icon); 111 } 112 113 118 public ServerAction(String name, String command, Icon icon) { 119 super(name, icon); 120 putValue(Action.ACTION_COMMAND_KEY, command); 121 } 122 123 128 public void setURL(String url) { 129 putValue(URL, url); 130 putValue(URL_CACHE, null); 131 } 132 133 public String getURL() { 134 return (String )getValue(URL); 135 } 136 137 private Map getParams() { 138 return (Map )getValue(PARAMS); 139 } 140 141 private void setParams(Map params) { 142 putValue(PARAMS, params); 143 } 144 145 149 public void addParam(String name, String value) { 150 Map params = getParams(); 151 if (params == null) { 152 params = new HashMap (); 153 setParams(params); 154 } 155 params.put(name, value); 156 } 157 158 161 public String getParamValue(String name) { 162 Map params = getParams(); 163 return params == null ? null : (String )params.get(name); 164 } 165 166 169 public Set getParamNames() { 170 Map params = getParams(); 171 return params == null ? null : params.keySet(); 172 } 173 174 private Map getHeaders() { 175 return (Map )getValue(HEADERS); 176 } 177 178 private void setHeaders(Map headers) { 179 putValue(HEADERS, headers); 180 } 181 182 187 public void addHeader(String name, String value) { 188 Map map = getHeaders(); 189 if (map != null) { 190 map = new HashMap (); 191 setHeaders(map); 192 } 193 map.put(name, value); 194 } 195 196 199 public String getHeaderValue(String name) { 200 Map headers = getHeaders(); 201 return headers == null ? null : (String )headers.get(name); 202 } 203 204 207 public Set getHeaderNames() { 208 Map headers = getHeaders(); 209 return headers == null ? null : headers.keySet(); 210 } 211 212 215 public void actionPerformed(ActionEvent evt) { 216 URL execURL = (URL )getValue(URL_CACHE); 217 if (execURL == null && !"".equals(getURL())) { 218 try { 219 String url = getURL(); 220 if (url.startsWith("http")) { 221 execURL = new URL (url); 222 } else { 223 execURL = Application.getURL(url, this); 225 } 226 if (execURL == null) { 227 return; 229 } else { 230 putValue(URL_CACHE, execURL); 232 } 233 234 239 } catch (MalformedURLException ex) { 240 ex.printStackTrace(); 241 } 242 } 243 244 try { 245 URLConnection uc = execURL.openConnection(); 246 247 Set headerNames = getHeaderNames(); 249 if (headerNames != null && !headerNames.isEmpty()) { 250 Iterator iter = headerNames.iterator(); 251 while (iter.hasNext()) { 252 String name = (String )iter.next(); 253 uc.setRequestProperty(name, getHeaderValue(name)); 254 } 255 } 256 uc.setUseCaches(false); 257 uc.setDoOutput(true); 258 259 ByteArrayOutputStream byteStream = new ByteArrayOutputStream (512); 260 PrintWriter out = new PrintWriter (byteStream, true); 261 out.print(getPostData()); 262 out.flush(); 263 264 String length = String.valueOf(byteStream.size()); 266 uc.setRequestProperty("Content-length", length); 267 268 byteStream.writeTo(uc.getOutputStream()); 270 271 BufferedReader buf = null; 272 if (uc instanceof HttpURLConnection ) { 273 HttpURLConnection huc = (HttpURLConnection )uc; 274 int code = huc.getResponseCode(); 275 String message = huc.getResponseMessage(); 276 277 if (code < 400) { 279 buf = new BufferedReader (new InputStreamReader (uc.getInputStream())); 284 285 } else { 286 buf = new BufferedReader (new InputStreamReader (huc.getErrorStream())); 289 } 290 String line; 291 292 StringBuffer buffer = new StringBuffer (); 293 while ((line = buf.readLine()) != null) { 294 buffer.append(line); 297 buffer.append('\n'); 298 } 299 if (Debug.debug) { 300 System.out.println(buffer.toString()); 303 } 304 } 305 } catch (UnknownHostException uhe) { 306 Debug.printException("UnknownHostException detected. Could it be a proxy issue?\n" + 307 uhe.getMessage(), uhe); 308 } catch (AccessControlException aex) { 309 Debug.printException("AccessControlException detected\n" + 310 aex.getMessage(), aex); 311 } catch (IOException ex) { 312 Debug.printException("IOException detected\n" + 313 ex.getMessage(), ex); 314 } 315 } 316 317 321 private String getPostData() { 322 StringBuffer postData = new StringBuffer (); 324 325 327 Set paramNames = getParamNames(); 329 if (paramNames != null && !paramNames.isEmpty()) { 330 Iterator iter = paramNames.iterator(); 331 try { 332 while (iter.hasNext()) { 333 String name = (String ) iter.next(); 334 postData.append('&').append(name).append('='); 335 postData.append(getParamValue(name)); 336 } 337 } 338 catch (Exception ex) { 340 } 341 postData.setCharAt(0, '?'); 343 } 344 if (Debug.debug) { 345 System.out.println("ServerAction: POST data: " + postData.toString()); 346 } 347 return postData.toString(); 348 } 349 350 354 private StringBuffer getDataBuffer() throws UnsupportedEncodingException { 355 StringBuffer buffer = new StringBuffer ("content="); 356 362 return buffer; 363 } 364 365 370 private String createMessage(int code, String msg) { 371 StringBuffer buffer = new StringBuffer ("The action \""); 372 buffer.append(getValue(NAME)); 373 374 if (code < 400) { 375 buffer.append("\" has succeeded "); 376 } else { 377 buffer.append("\" has failed\nPlease check the Java console for more details.\n"); 378 } 379 buffer.append("\nServer response:\nCode: "); 382 buffer.append(code); 383 buffer.append(" Message: "); 384 buffer.append(msg); 385 386 return buffer.toString(); 387 } 388 } 389 | Popular Tags |