#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Security; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Xml.Linq; namespace HeuristicLab.Clients.Hive.CloudManager.Azure { public class ServiceWebRequest { private string thumbprint; private string versionId; #region Constructors public ServiceWebRequest(string thumbprint) : this(thumbprint, Constants.APIVersionDefault) { } public ServiceWebRequest(string thumbprint, string versionId) { this.thumbprint = thumbprint; this.versionId = versionId; } #endregion #region Public Methods public XDocument Invoke(string uri) { XDocument responsePayload = null; ; Uri operationUri = new Uri(uri); HttpWebRequest httpWebRequest = CreateHttpWebRequest(operationUri, Constants.HttpMethodGET); try { using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse()) { Stream responseStream = response.GetResponseStream(); responsePayload = XDocument.Load(responseStream); } } catch (WebException ex) { Debug.WriteLine("Response status code: " + ex.Status); string response = ParseResponse(ex.Response); WriteRespone(response); string errorMsg = GetErrorMessageFromWebExceptionResponse(response); throw new SystemException(errorMsg); } return responsePayload; } public string Invoke(string uri, XDocument payload) { string requestId = Invoke(uri, Constants.HttpMethodPOST, payload); return requestId; } public string Invoke(string uri, string httpWebRequestMethod) { Uri operationUri = new Uri(uri); HttpWebRequest httpWebRequest = CreateHttpWebRequest(operationUri, httpWebRequestMethod); String requestId = String.Empty; try { using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse()) { requestId = response.Headers[Constants.HeaderRequestId]; } } catch (WebException ex) { Debug.WriteLine("Response status code: " + ex.Status); string response = ParseResponse(ex.Response); WriteRespone(response); string errorMsg = GetErrorMessageFromWebExceptionResponse(response); throw new SystemException(errorMsg); } return requestId; } public string Invoke(string uri, string httpWebRequestMethod, XDocument payload) { Uri operationUri = new Uri(uri); HttpWebRequest httpWebRequest = CreateHttpWebRequest(operationUri, httpWebRequestMethod); using (Stream requestStream = httpWebRequest.GetRequestStream()) { using (StreamWriter streamWriter = new StreamWriter(requestStream, UTF8Encoding.UTF8)) { payload.Save(streamWriter, SaveOptions.DisableFormatting); } } string requestId = String.Empty; try { using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse()) { requestId = response.Headers[Constants.HeaderRequestId]; } } catch (WebException ex) { Debug.WriteLine("Response status code: " + ex.Status); string response = ParseResponse(ex.Response); WriteRespone(response); string errorMsg = GetErrorMessageFromWebExceptionResponse(response); throw new SystemException(errorMsg); } return requestId; } #endregion #region Private Methods private string ParseResponse(WebResponse webResponse) { string response = string.Empty; using (Stream responseStream = webResponse.GetResponseStream()) { if (responseStream == null) return response; using (StreamReader reader = new StreamReader(responseStream)) { response = reader.ReadToEnd(); } } return response; } private void WriteRespone(string response) { Debug.WriteLine("Response output:"); Debug.WriteLine(response); } private string GetErrorMessageFromWebExceptionResponse(string response) { XNamespace wa = Constants.NSWindowsAzure; XDocument doc = XDocument.Parse(response); string errorCode = doc.Descendants(wa + "Code").Single().Value; string errorMessage = doc.Descendants(wa + "Message").Single().Value; return errorCode + ": " + errorMessage; } private X509Certificate2 GetCertificate(string certThumbprint) { X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); X509Certificate2 cert = null; try { certStore.Open(OpenFlags.ReadOnly); X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, certThumbprint, false); if (certCollection.Count == 0) { throw new Exception("Error: No certificate found containing thumbprint" + certThumbprint); } cert = certCollection[0]; } catch (Exception e) { if (e is CryptographicException) throw new SystemException("Error: The certificate store is unreadable."); else if (e is SecurityException) throw new SystemException("Error: You don't have the required permission."); else if (e is ArgumentException) throw new SystemException("Error: Invalid values in the certificate store."); else throw new SystemException(e.Message, e); } finally { certStore.Close(); } return cert; } private HttpWebRequest CreateHttpWebRequest(Uri uri, string httpWebRequestMethod) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.Headers.Add(Constants.HeaderVersionName, versionId); request.Method = httpWebRequestMethod; request.ContentType = Constants.ContentTypeAppXml; X509Certificate2 cert = GetCertificate(thumbprint); request.ClientCertificates.Add(cert); return request; } #endregion } }