gt:                 while (iter.hasNext()) {
context: /*
   * $Id$
   *
   * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
   * Santa Clara, California 95054, U.S.A. All rights reserved.
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   * 
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   * 
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   */
  package org.jdesktop.swingx.action;
  
  import java.awt.event.ActionEvent;
  import java.io.BufferedReader;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.UnknownHostException;
  import java.security.AccessControlException;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  import javax.swing.AbstractAction;
  import javax.swing.Action;
  import javax.swing.Icon;
  
  //import org.jdesktop.swing.Application;
  
  
  /**
   * An action which will invoke an http POST operation.
   *
   * @author Mark Davidson
   */
  public class ServerAction extends AbstractAction {
      // Server action support
      private static final Logger LOG = Logger.getLogger(ServerAction.class
              .getName());
      private static final String PARAMS = "action-params";
      private static final String HEADERS = "action-headers";
      private static final String URL = "action-url";
  
      private static final String URL_CACHE = "_URL-CACHE__";
  
      public ServerAction() {
          this("action");
      }
  
      public ServerAction(String name) {
          super(name);
      }
  
      /**
       * @param name display name of the action
       * @param command the value of the action command key
       */
      public ServerAction(String name, String command) {
          this(name, command, null);
      }
  
      public ServerAction(String name, Icon icon) {
          super(name, icon);
      }
  
      /**
       * @param name display name of the action
       * @param command the value of the action command key
       * @param icon icon to display
       */
      public ServerAction(String name, String command, Icon icon) {
          super(name, icon);
          putValue(Action.ACTION_COMMAND_KEY, command);
      }
  
      /**
       * Set the url for the action.
       * <p>
       * @param url a string representation of the url
       */
      public void setURL(String url) {
          putValue(URL, url);
          putValue(URL_CACHE, null);
      }
  
      public String getURL() {
          return (String)getValue(URL);
      }
  
      @SuppressWarnings("unchecked")
      private Map<String, String> getParams() {
          return (Map)getValue(PARAMS);
      }
  
      private void setParams(Map<String, String> params) {
          putValue(PARAMS, params);
      }
  
      /**
       * Adds a name value pair which represents a url parameter in an http
       * POST request.
       */
      public void addParam(String name, String value) {
          Map<String, String> params = getParams();
          if (params == null) {
              params = new HashMap<String, String>();
              setParams(params);
          }
          params.put(name, value);
      }
  
      /**
       * Return a parameter value corresponding to name or null if it doesn't exist.
       */
      public String getParamValue(String name) {
          Map<String, String> params = getParams();
          return params == null ? null : params.get(name);
      }
  
      /**
       * Return a set of parameter names or null if there are no params
       */
      public Set<String> getParamNames() {
          Map<String, String> params = getParams();
          return params == null ? null : params.keySet();
      }
  
      @SuppressWarnings("unchecked")
      private Map<String, String> getHeaders() {
          return (Map)getValue(HEADERS);
      }
  
      private void setHeaders(Map<String, String> headers) {
          putValue(HEADERS, headers);
      }
  
      /**
       * Adds a name value pair which represents a url connection request property.
       * For example, name could be "Content-Type" and the value could be
       * "application/x-www-form-urlencoded"
       */
      public void addHeader(String name, String value) {
          Map<String, String> map = getHeaders();
          if (map == null) {
              map = new HashMap<String, String>();
              setHeaders(map);
          }
          map.put(name, value);
      }
  
      /**
       * Return a header value corresponding to name or null if it doesn't exist.
       */
      public String getHeaderValue(String name) {
          Map<String, String> headers = getHeaders();
          return headers == null ? null : headers.get(name);
      }
  
      /**
       * Return a set of parameter names or null if there are no params
       */
      public Set<String> getHeaderNames() {
          Map<String, String> headers = getHeaders();
          return headers == null ? null : headers.keySet();
      }
  
      /**
       * Invokes the server operation when the action has been invoked.
       */
      @Override
      public void actionPerformed(ActionEvent evt) {
          URL execURL = (URL)getValue(URL_CACHE);
          if (execURL == null && !"".equals(getURL())) {
              try {
                  String url = getURL();
                  if (url.startsWith("http")) {
                      execURL = new URL(url);
                  } else {
                  }
                  if (execURL == null) {
                      // XXX TODO: send a message
                      return;
                  } else {
                      // Cache this value.
                      putValue(URL_CACHE, execURL);
                  }
  
              } catch (MalformedURLException ex) {
                  LOG.log(Level.WARNING, "something went wrong...", ex);
              }
          }
  
          try {
              URLConnection uc = execURL.openConnection();
  
              // Get all the header name/value pairs ans set the request headers
              Set<String> headerNames = getHeaderNames();
              if (headerNames != null && !headerNames.isEmpty()) {
                  Iterator<String> iter = headerNames.iterator();
  