public object Execute(HttpWebRequest executeHttpRequest)
        {
            //We first validate that the user can use the service
            if (!CanUse)
            {
                throw new Exception("The current user is not allowed to Execute on the service " + Name);
            }

            OpenGis.Wps.ExecuteResponse execResponse = null;
            MemoryStream memStream = new MemoryStream();

            try {
                using (var executeResponse = (HttpWebResponse)executeHttpRequest.GetResponse()) {
                    using (var stream = executeResponse.GetResponseStream()) {
                        stream.CopyTo(memStream);
                    }
                    memStream.Seek(0, SeekOrigin.Begin);
                    var    reader       = new StreamReader(memStream);
                    string textResponse = reader.ReadToEnd();
                    log.Debug("Execute response : " + textResponse);

                    if (executeResponse.StatusCode != HttpStatusCode.OK)
                    {
                        log.Debug("Execute response code : " + executeResponse.StatusCode);
                        return(ExecuteError(memStream));
                    }
                }

                memStream.Seek(0, SeekOrigin.Begin);
                execResponse = (ExecuteResponse) new System.Xml.Serialization.XmlSerializer(typeof(ExecuteResponse)).Deserialize(memStream);
                return(execResponse);
            } catch (WebException we) {
                if (we.Response == null)
                {
                    throw new Exception(we.Message);
                }
                using (WebResponse response = we.Response) {
                    using (var httpResponse = (HttpWebResponse)response) {
                        using (var stream = httpResponse.GetResponseStream()) {
                            stream.CopyTo(memStream);
                        }
                        log.Debug("Execute response code : " + httpResponse.StatusCode);
                    }
                }
                return(ExecuteError(memStream));
            } catch (InvalidOperationException ioe) {
                log.Error("InvalidOperationException : " + ioe.Message + " - " + ioe.StackTrace);
                //bug 52 NORTH - to be removed once AIR updated
                return(ExecuteError(memStream));
            } catch (Exception e) {
                log.Error("Execute request failed");
                throw e;
            } finally {
                memStream.Close();
            }
        }
Exemple #2
0
        public object Get(ProxyWpsJobSearchRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);

            context.Open();
            context.LogInfo(this, string.Format("/proxy/wps/{{jobid}}/search GET jobid='{0}'", request.jobid));

            WpsJob wpsjob = WpsJob.FromIdentifier(context, request.jobid);

            if (string.IsNullOrEmpty(wpsjob.StatusLocation))
            {
                throw new Exception("Invalid Status Location");
            }

            context.LogDebug(this, string.Format("Wps Proxy search for wpsjob {0}", wpsjob.Identifier));

            HttpWebRequest executeHttpRequest;

            if (wpsjob.Provider != null)
            {
                executeHttpRequest = wpsjob.Provider.CreateWebRequest(wpsjob.StatusLocation);
            }
            else
            {
                NetworkCredential credentials = null;
                var uri = new UriBuilder(wpsjob.StatusLocation);
                if (!string.IsNullOrEmpty(uri.UserName) && !string.IsNullOrEmpty(uri.Password))
                {
                    credentials = new NetworkCredential(uri.UserName, uri.Password);
                }
                executeHttpRequest = WpsProvider.CreateWebRequest(wpsjob.StatusLocation, credentials, context.Username);
            }

            if (wpsjob.StatusLocation.Contains("gpod.eo.esa.int"))
            {
                executeHttpRequest.Headers.Add("X-UserID", context.GetConfigValue("GpodWpsUser"));
            }
            OpenGis.Wps.ExecuteResponse execResponse = null;
            try {
                context.LogDebug(this, string.Format("Wps proxy - exec response requested - {0}", executeHttpRequest.Address.AbsoluteUri));
                using (var response = executeHttpRequest.GetResponse())
                    using (var stream = response.GetResponseStream())
                        execResponse = (OpenGis.Wps.ExecuteResponse) new System.Xml.Serialization.XmlSerializer(typeof(OpenGis.Wps.ExecuteResponse)).Deserialize(stream);
                context.LogDebug(this, string.Format("Wps proxy - exec response OK"));
            } catch (Exception e) {
                context.LogError(this, e.Message, e);
            }
            if (execResponse == null)
            {
                throw new Exception("Unable to get execute response from proxied job");
            }

            execResponse.statusLocation = context.BaseUrl + "/wps/RetrieveResultServlet?id=" + wpsjob.Identifier;
            context.LogDebug(this, string.Format("Proxy WPS - uri: " + execResponse.statusLocation));

            AtomFeed feed = new AtomFeed();

            if (execResponse.ProcessOutputs != null)
            {
                foreach (OutputDataType output in execResponse.ProcessOutputs)
                {
                    if (output.Identifier != null && output.Identifier.Value != null && output.Identifier.Value.Equals("result_metadata"))
                    {
                        context.LogDebug(this, string.Format("Wps proxy - metadata"));
                        if (output.Item is OutputReferenceType)
                        {
                            var            reference = output.Item as OutputReferenceType;
                            HttpWebRequest atomRequest;
                            if (wpsjob.Provider != null)
                            {
                                atomRequest = wpsjob.Provider.CreateWebRequest(reference.href);
                            }
                            else
                            {
                                NetworkCredential credentials = null;
                                var uri = new UriBuilder(reference.href);
                                if (!string.IsNullOrEmpty(uri.UserName) && !string.IsNullOrEmpty(uri.Password))
                                {
                                    credentials = new NetworkCredential(uri.UserName, uri.Password);
                                }
                                atomRequest = WpsProvider.CreateWebRequest(reference.href, credentials, context.Username);
                            }
                            feed = CreateFeedForMetadata(atomRequest);
                        }
                        else if (output.Item is DataType)
                        {
                            var            item      = ((DataType)(output.Item)).Item as ComplexDataType;
                            var            reference = item.Reference as OutputReferenceType;
                            HttpWebRequest atomRequest;
                            if (wpsjob.Provider != null)
                            {
                                atomRequest = wpsjob.Provider.CreateWebRequest(reference.href);
                            }
                            else
                            {
                                NetworkCredential credentials = null;
                                var uri = new UriBuilder(reference.href);
                                if (!string.IsNullOrEmpty(uri.UserName) && !string.IsNullOrEmpty(uri.Password))
                                {
                                    credentials = new NetworkCredential(uri.UserName, uri.Password);
                                }
                                atomRequest = WpsProvider.CreateWebRequest(reference.href, credentials, context.Username);
                            }
                            feed = CreateFeedForMetadata(atomRequest);
                        }
                    }
                    else
                    {
                        if (output.Item is DataType && ((DataType)(output.Item)).Item != null)
                        {
                            var item = ((DataType)(output.Item)).Item as ComplexDataType;
                            if (item.Any != null && item.Any [0].LocalName != null)
                            {
                                if (item.Any [0].LocalName.Equals("RDF"))
                                {
                                    context.LogDebug(this, string.Format("Wps proxy - RDF"));
                                    feed = CreateFeedForRDF(item.Any [0], request.jobid, context.BaseUrl);
                                }
                                else if (item.Any [0].LocalName.Equals("metalink"))
                                {
                                    context.LogDebug(this, string.Format("Wps proxy - metalink"));
                                    feed = CreateFeedForMetalink(item.Any [0], request.jobid, context.BaseUrl, context);
                                }
                            }
                        }
                    }
                }
            }

            /* Proxy id + add self */
            foreach (var item in feed.Items)
            {
                var self   = context.BaseUrl + "/proxy/wps/" + wpsjob.Identifier + "/search?uid=" + HttpUtility.UrlEncode(item.Id);
                var search = context.BaseUrl + "/proxy/wps/" + wpsjob.Identifier + "/description";
                item.Id = self;
                item.Links.Add(Terradue.ServiceModel.Syndication.SyndicationLink.CreateSelfLink(new Uri(self), "application/atom+xml"));
                item.Links.Add(new Terradue.ServiceModel.Syndication.SyndicationLink(new Uri(search), "search", "OpenSearch Description link", "application/opensearchdescription+xml", 0));
            }

            feed.Id = wpsjob.StatusLocation;

            var  ose          = MasterCatalogue.OpenSearchEngine;
            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(HttpContext.Current.Request.QueryString, HttpContext.Current.Request.Headers, ose);
            var  ext          = ose.GetFirstExtensionByTypeAbility(responseType);

            var osfeed = new AtomFeedOpenSearchable(feed);
            IOpenSearchResultCollection osr = ose.Query(osfeed, HttpContext.Current.Request.QueryString, responseType);

            var osrDesc = new Uri(context.BaseUrl + "/proxy/wps/" + wpsjob.Identifier + "/description");

            osr.Links.Add(new Terradue.ServiceModel.Syndication.SyndicationLink(osrDesc, "search", "OpenSearch Description link", "application/opensearchdescription+xml", 0));

            context.Close();
            return(new HttpResult(osr.SerializeToString(), osr.ContentType));
        }