Beispiel #1
0
 /// <summary>
 /// Initializes the SecurityLookup instance by creating a session and authorizing it so that its
 /// ready to conduct searches. Once initialized multiple searches may be conducted with the same instance.
 /// </summary>
 public void Init()
 {
     try
     {
         SessionOptions sessionOptions = new SessionOptions();
         sessionOptions.ServerHost            = d_host;
         sessionOptions.ServerPort            = d_port;
         sessionOptions.AuthenticationOptions = d_authOptions;
         Console.WriteLine("Connecting to {0}:{1}", d_host, d_port);
         session = new Session(sessionOptions);
         if (!session.Start())
         {
             throw new Exception("Failed to start session");
         }
         identity = null;
         if (d_authOptions != null)
         {
             Authorize(out identity, session);
         }
         if (!session.OpenService(INSTRUMENT_SERVICE))
         {
             throw new Exception(
                       string.Format("Failed to open: {0}", INSTRUMENT_SERVICE));
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(string.Format("Exception: {0}", e.Message));
         Console.WriteLine();
     }
 }
Beispiel #2
0
        /// <summary>
        /// Send a request to the BLP API. Called by Query().
        /// </summary>
        /// <param name="session"></param>
        /// <param name="identity"></param>
        private void SendRequest(Session session, Identity identity)
        {
            Console.WriteLine("Sending Request: {0}", d_requestType.ToString());
            Service instrumentService = session.GetService(INSTRUMENT_SERVICE);
            Request request;

            try
            {
                request = instrumentService.CreateRequest(d_requestType.ToString());
            }
            catch (NotFoundException e)
            {
                throw new Exception(
                          string.Format("Request Type not found: {0}", d_requestType),
                          e);
            }
            request.Set(QUERY_ELEMENT, d_queryString);
            request.Set(MAX_RESULTS_ELEMENT, d_maxResults);

            foreach (KeyValuePair <string, string> entry in d_filters)
            {
                try
                {
                    request.Set(entry.Key, entry.Value);
                }
                catch (NotFoundException e)
                {
                    throw new Exception(string.Format("Filter not found: {0}", entry.Key), e);
                }
                catch (InvalidConversionException e)
                {
                    throw new Exception(
                              string.Format(
                                  "Invalid value: {0} for filter: {1}",
                                  entry.Value,
                                  entry.Key),
                              e);
                }
            }
            request.Print(Console.Out);
            Console.WriteLine();
            session.SendRequest(request, identity, null);
        }
Beispiel #3
0
        /// <summary>
        /// Authorize should be called before any requests are sent. Called by init.
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="session"></param>
        private static void Authorize(out Identity identity, Session session)
        {
            identity = session.CreateIdentity();
            if (!session.OpenService(AUTH_SERVICE))
            {
                throw new Exception(
                          string.Format("Failed to open auth service: {0}",
                                        AUTH_SERVICE));
            }
            Service authService = session.GetService(AUTH_SERVICE);

            EventQueue tokenEventQueue = new EventQueue();

            session.GenerateToken(new CorrelationID(tokenEventQueue), tokenEventQueue);
            string token = null;
            // Generate token responses will come on the dedicated queue. There would be no other
            // messages on that queue.
            Event eventObj = tokenEventQueue.NextEvent(
                Convert.ToInt32(WAIT_TIME.TotalMilliseconds));

            if (eventObj.Type == Event.EventType.TOKEN_STATUS ||
                eventObj.Type == Event.EventType.REQUEST_STATUS)
            {
                foreach (Message msg in eventObj)
                {
                    System.Console.WriteLine(msg);
                    if (msg.MessageType == TOKEN_SUCCESS)
                    {
                        token = msg.GetElementAsString(TOKEN_ELEMENT);
                    }
                }
            }
            if (token == null)
            {
                throw new Exception("Failed to get token");
            }

            Request authRequest = authService.CreateAuthorizationRequest();

            authRequest.Set(TOKEN_ELEMENT, token);

            session.SendAuthorizationRequest(authRequest, identity, null);

            TimeSpan ts = WAIT_TIME;

            for (DateTime startTime = DateTime.UtcNow;
                 ts.TotalMilliseconds > 0;
                 ts = ts - (DateTime.UtcNow - startTime))
            {
                eventObj = session.NextEvent(Convert.ToInt32(ts.TotalMilliseconds));
                // Since no other requests were sent using the session queue, the response can
                // only be for the Authorization request
                if (eventObj.Type != Event.EventType.RESPONSE &&
                    eventObj.Type != Event.EventType.PARTIAL_RESPONSE &&
                    eventObj.Type != Event.EventType.REQUEST_STATUS)
                {
                    continue;
                }

                foreach (Message msg in eventObj)
                {
                    System.Console.WriteLine(msg);
                    if (msg.MessageType != AUTHORIZATION_SUCCESS)
                    {
                        throw new Exception("Authorization Failed");
                    }
                }
                return;
            }
            throw new Exception("Authorization Failed");
        }