public @Nullable <T> T get(Class<T> classType, @Nonnull String resource) throws CloudException, InternalException { try { return this.<T>get( classType, new URI(endpoint + provider.getContext().getAccountNumber() + resource)); } catch (URISyntaxException e) { throw new InternalException( "Endpoint misconfiguration (" + endpoint + provider.getContext().getAccountNumber() + resource + "): " + e.getMessage()); } }
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 <T> String post(String resource, T object) throws JAXBException, CloudException, InternalException { StringWriter stringWriter = new StringWriter(); JAXBContext jc = JAXBContext.newInstance(object.getClass()); Marshaller m = jc.createMarshaller(); m.marshal(object, stringWriter); return post(provider.getContext().getAccountNumber(), resource, stringWriter.toString()); }
public @Nullable <T> T get(Class<T> classType, @Nonnull URI uri) throws CloudException, InternalException { InputStream responseAsStream = getAsStream(provider.getContext().getAccountNumber(), uri); if (responseAsStream == null) { logger.info("Unable to perform HTTP GET at following resource: " + uri.toString()); return null; } try { JAXBContext context = JAXBContext.newInstance(classType); Unmarshaller u = context.createUnmarshaller(); return (T) u.unmarshal(responseAsStream); } catch (Exception ex) { logger.error(ex.getMessage()); throw new InternalException(ex); } }
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 HttpProxyConfig getHttpProxyConfigData() { Properties p = provider.getContext().getCustomProperties(); HttpProxyConfig httpProxyConfig = null; if (p != null && p.getProperty("proxyHost") != null && p.getProperty("proxyPort") != null) { if (p.getProperty("proxyPort").length() > 0) { httpProxyConfig = new HttpProxyConfig( p.getProperty("proxyHost"), Integer.parseInt(p.getProperty("proxyPort"))); } } else { p = System.getProperties(); if (p != null && p.getProperty("proxyHost") != null && p.getProperty("proxyPort") != null) { if (p.getProperty("proxyPort").length() > 0) { httpProxyConfig = new HttpProxyConfig( p.getProperty("proxyHost"), Integer.parseInt(p.getProperty("proxyPort"))); } } } return httpProxyConfig; }
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); }