protected @Nonnull HttpClient getClient(String url, boolean multipart) throws InternalException {
    ProviderContext ctx = provider.getContext();

    if (ctx == null) {
      throw new InternalException("No context was specified for this request");
    }
    boolean ssl = url.startsWith("https");
    HttpParams params = new BasicHttpParams();

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    if (!multipart) {
      HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    }
    HttpProtocolParams.setUserAgent(params, "Dasein Cloud");

    Properties p = ctx.getCustomProperties();

    if (p != null) {
      String proxyHost = p.getProperty("proxyHost");
      String proxyPort = p.getProperty("proxyPort");

      if (proxyHost != null) {
        int port = 0;

        if (proxyPort != null && proxyPort.length() > 0) {
          port = Integer.parseInt(proxyPort);
        }
        params.setParameter(
            ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, port, ssl ? "https" : "http"));
      }
    }
    return new DefaultHttpClient(params);
  }
  public AzureMethod(Azure azure) throws CloudException {
    provider = azure;
    ProviderContext ctx = provider.getContext();

    if (ctx == null) {
      throw new AzureConfigException("No context was provided for this request");
    }
    endpoint = ctx.getEndpoint();
    if (endpoint == null) {
      throw new AzureConfigException("No endpoint was provided for this request");
    }
    if (!endpoint.endsWith("/")) {
      endpoint = endpoint + "/";
    }

    this.strategy = provider.getContext().getRequestTrackingStrategy();
  }
  public @Nonnull int getOperationStatus(String requestID)
      throws CloudException, InternalException {
    ProviderContext ctx = provider.getContext();
    Document doc = getAsXML(ctx.getAccountNumber(), "/operations/" + requestID);

    if (doc == null) {
      return -2;
    }

    NodeList entries = doc.getElementsByTagName("Operation");
    Node entry = entries.item(0);

    NodeList s = entry.getChildNodes();

    String status = "";
    String httpCode = "";

    for (int i = 0; i < s.getLength(); i++) {
      Node attribute = s.item(i);
      if (attribute.getNodeType() == Node.TEXT_NODE) {
        continue;
      }
      if (attribute.getNodeName().equalsIgnoreCase("status") && attribute.hasChildNodes()) {
        status = attribute.getFirstChild().getNodeValue().trim();
        continue;
      }
      if (status.length() > 0 && !status.equalsIgnoreCase("inProgress")) {
        if (attribute.getNodeName().equalsIgnoreCase("httpstatuscode")
            && attribute.hasChildNodes()) {
          httpCode = attribute.getFirstChild().getNodeValue().trim();
        }
      }
    }

    if (status.equalsIgnoreCase("succeeded")) {
      return HttpServletResponse.SC_OK;
    } else if (status.equalsIgnoreCase("failed")) {
      String errMsg = checkError(s, httpCode);
      throw new CloudException(errMsg);
    }
    return -1;
  }
  private @Nullable VPN toVPN(@Nonnull ProviderContext ctx, @Nullable Node node)
      throws CloudException, InternalException {
    if (node == null) {
      return null;
    }

    NodeList attributes = node.getChildNodes();
    String name = null, description = null;
    VPN vpn = new VPN();

    vpn.setCurrentState(VPNState.PENDING);
    vpn.setProviderRegionId(ctx.getRegionId());
    for (int i = 0; i < attributes.getLength(); i++) {
      Node attr = attributes.item(i);
      String nodeName = attr.getNodeName();

      if (nodeName.equalsIgnoreCase("vpnGatewayId") && attr.hasChildNodes()) {
        vpn.setProviderVpnId(attr.getFirstChild().getNodeValue().trim());
      } else if (nodeName.equalsIgnoreCase("state")) {
        vpn.setCurrentState(toVPNState(attr.getFirstChild().getNodeValue().trim()));
      } else if (nodeName.equalsIgnoreCase("type") && attr.hasChildNodes()) {
        String t = attr.getFirstChild().getNodeValue().trim();

        if (t.equalsIgnoreCase("ipsec.1")) {
          vpn.setProtocol(VPNProtocol.IPSEC1);
        } else if (t.equalsIgnoreCase("openvpn")) {
          vpn.setProtocol(VPNProtocol.OPEN_VPN);
        } else {
          logger.warn("DEBUG: Unknown VPN gateway type: " + t);
          vpn.setProtocol(VPNProtocol.IPSEC1);
        }
      } else if (nodeName.equalsIgnoreCase("attachments") && attr.hasChildNodes()) {
        TreeSet<String> vlans = new TreeSet<String>();
        NodeList list = attr.getChildNodes();

        for (int j = 0; j < list.getLength(); j++) {
          Node att = list.item(j);

          if (att.getNodeName().equalsIgnoreCase("item") && att.hasChildNodes()) {
            NodeList aaList = attr.getChildNodes();
            String id = null;

            for (int k = 0; k < aaList.getLength(); k++) {
              Node aa = aaList.item(k);

              if (aa.getNodeName().equalsIgnoreCase("vpcId") && aa.hasChildNodes()) {
                id = aa.getFirstChild().getNodeValue().trim();
                break;
              }
            }
            if (id != null) {
              vlans.add(id);
            }
          }
        }
        vpn.setProviderVlanIds(vlans.toArray(new String[vlans.size()]));
      } else if (nodeName.equalsIgnoreCase("tagSet") && attr.hasChildNodes()) {
        provider.setTags(attr, vpn);
        if (vpn.getTags().get("name") != null) {
          name = vpn.getTags().get("name");
        }
        if (vpn.getTags().get("description") != null) {
          description = vpn.getTags().get("description");
        }
      }
    }
    if (vpn.getProviderVpnId() == null) {
      return null;
    }
    if (vpn.getName() == null) {
      vpn.setName(name == null ? vpn.getProviderVpnId() : name);
    }
    if (vpn.getDescription() == null) {
      vpn.setDescription(description == null ? vpn.getName() : description);
    }
    return vpn;
  }
  private @Nullable VPNGateway toGateway(@Nonnull ProviderContext ctx, @Nullable Node node)
      throws CloudException, InternalException {
    if (node == null) {
      return null;
    }

    NodeList attributes = node.getChildNodes();
    VPNGateway gateway = new VPNGateway();

    gateway.setProviderOwnerId(ctx.getAccountNumber());
    gateway.setProviderRegionId(ctx.getRegionId());
    gateway.setCurrentState(VPNGatewayState.PENDING);
    for (int i = 0; i < attributes.getLength(); i++) {
      Node attr = attributes.item(i);
      String nodeName = attr.getNodeName();

      if (nodeName.equalsIgnoreCase("customerGatewayId") && attr.hasChildNodes()) {
        gateway.setProviderVpnGatewayId(attr.getFirstChild().getNodeValue().trim());
      } else if (nodeName.equalsIgnoreCase("state") && attr.hasChildNodes()) {
        String state = attr.getFirstChild().getNodeValue().trim();

        if (state.equalsIgnoreCase("available")) {
          gateway.setCurrentState(VPNGatewayState.AVAILABLE);
        } else if (state.equalsIgnoreCase("deleting")) {
          gateway.setCurrentState(VPNGatewayState.DELETING);
        } else if (state.equalsIgnoreCase("deleted")) {
          gateway.setCurrentState(VPNGatewayState.DELETED);
        } else if (state.equalsIgnoreCase("pending")) {
          gateway.setCurrentState(VPNGatewayState.PENDING);
        } else {
          logger.warn("DEBUG: Unknown VPN gateway state: " + state);
        }
      } else if (nodeName.equalsIgnoreCase("type") && attr.hasChildNodes()) {
        String t = attr.getFirstChild().getNodeValue().trim();

        if (t.equalsIgnoreCase("ipsec.1")) {
          gateway.setProtocol(VPNProtocol.IPSEC1);
        } else if (t.equalsIgnoreCase("openvpn")) {
          gateway.setProtocol(VPNProtocol.OPEN_VPN);
        } else {
          logger.warn("DEBUG: Unknown VPN gateway type: " + t);
          gateway.setProtocol(VPNProtocol.IPSEC1);
        }
      } else if (nodeName.equalsIgnoreCase("ipAddress") && attr.hasChildNodes()) {
        gateway.setEndpoint(attr.getFirstChild().getNodeValue().trim());
      } else if (nodeName.equalsIgnoreCase("bgpAsn") && attr.hasChildNodes()) {
        gateway.setBgpAsn(attr.getFirstChild().getNodeValue().trim());
      } else if (nodeName.equalsIgnoreCase("tagSet") && attr.hasChildNodes()) {
        provider.setTags(attr, gateway);
      }
    }
    if (gateway.getProviderVpnGatewayId() == null) {
      return null;
    }
    if (gateway.getName() == null) {
      gateway.setName(gateway.getProviderVpnGatewayId() + " [" + gateway.getEndpoint() + "]");
    }
    if (gateway.getDescription() == null) {
      gateway.setDescription(gateway.getName());
    }
    return gateway;
  }
 public static String getAccountNumber(@Nullable ProviderContext ctx) {
   return ((ctx == null || ctx.getAccountNumber() == null) ? "---" : ctx.getAccountNumber());
 }
  protected @Nonnull HttpClient getClient() throws CloudException, InternalException {
    ProviderContext ctx = provider.getContext();

    if (ctx == null) {
      throw new AzureConfigException("No context was defined for this request");
    }
    String endpoint = ctx.getEndpoint();

    if (endpoint == null) {
      throw new AzureConfigException("No cloud endpoint was defined");
    }
    boolean ssl = endpoint.startsWith("https");
    int targetPort;

    try {
      URI uri = new URI(endpoint);

      targetPort = uri.getPort();
      if (targetPort < 1) {
        targetPort = (ssl ? 443 : 80);
      }
    } catch (URISyntaxException e) {
      throw new AzureConfigException(e);
    }
    HttpParams params = new BasicHttpParams();
    SchemeRegistry registry = new SchemeRegistry();

    try {
      registry.register(
          new Scheme(
              ssl ? "https" : "http",
              targetPort,
              new AzureSSLSocketFactory(new AzureX509(provider))));
    } catch (KeyManagementException e) {
      e.printStackTrace();
      throw new InternalException(e);
    } catch (UnrecoverableKeyException e) {
      e.printStackTrace();
      throw new InternalException(e);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      throw new InternalException(e);
    } catch (KeyStoreException e) {
      e.printStackTrace();
      throw new InternalException(e);
    }

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    HttpProtocolParams.setUserAgent(params, "Dasein Cloud");
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 300000);

    HttpProxyConfig httpProxyConfig = getHttpProxyConfigData();
    if (httpProxyConfig != null) {
      params.setParameter(
          ConnRoutePNames.DEFAULT_PROXY,
          new HttpHost(httpProxyConfig.getHost(), httpProxyConfig.getPort()));
      registry.register(new Scheme("http", httpProxyConfig.getPort(), new PlainSocketFactory()));
    }

    ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry);

    return new DefaultHttpClient(ccm, params);
  }