返回

提问 添加收藏

c#的这段代码怎么转成java的代码?

上帝门口的排队 发布于 2019年05月14日 (共有6个回帖)

using System;
using System.Net;
using System.Threading.Tasks;

using ININ.ICWS.Common;
using ININ.ICWS.Connection;
using ININ.ICWS.Messaging;
using ININ.WebServices.Core;

namespace ININ.ICWS.Examples
{
    public class ExampleApp
    {
        private readonly WebServiceUtility _webServiceUtility;

        public ExampleApp()
        {
            // Create the WebServiceUtility using correct server information.
            // DocumentationExample is your application's user agent used during web requests.
            _webServiceUtility = new WebServiceUtility("DocumentationExample")
            {
                Port = 8019,
                Server = "HostServerName",
                IsHttps = true
            };
        }

        public void PerformConnection()
        {
            // Connect to the IC server.
            var connectionResource = new ConnectionResource(_webServiceUtility);
            var requestParameters = new ConnectionResource.CreateConnectionRequestParameters();
            requestParameters.Accept_Language = "en-US";
            // Request that the connection response return the supported feature versions.
            requestParameters.Include = "features";
            var dataContract = new IcAuthConnectionRequestSettingsDataContract();
            dataContract.ApplicationName = "Example";
            dataContract.UserID = "username";
            dataContract.Password = "password";

            Task<ConnectionResource.CreateConnectionResponses> createConnectionTask 
            = connectionResource.CreateConnection(requestParameters, dataContract);
            createConnectionTask.ContinueWith(t =>
                {
                    ConnectionResource.CreateConnectionResponses createConnectionResponses = t.Result;

                    createConnectionResponses.PerformIfResponseIs201(response => HandleConnection201(response, createConnectionResponses.Set_Cookie));
                    createConnectionResponses.PerformOnFailureResponse(HandleError);
                    createConnectionResponses.PerformDefault(HandleDefault);
                }, TaskContinuationOptions.OnlyOnRanToCompletion);
            createConnectionTask.ContinueWith(t => HandleError(t.Exception), 
                    TaskContinuationOptions.OnlyOnFaulted);
        }

        private void HandleConnection201(ConnectionResponseDataContract response, string cookie)
        {
            _webServiceUtility.SessionParameters =
                new AuthenticationParameters
                    {
                        SessionId = response.SessionId,
                        Cookie = cookie,
                        ININ_ICWS_CSRF_Token = response.CsrfToken
                    };

            // A 201 represents a successful connection, response.alternateHostList are the paths
            // to alternate servers.
            // We can now set our station.
            ChangeStation();
            
            var messagingFeatureVersion = 0;
            
            // Get the messaging version, for use in determining if short-polling or 
            // server-sent events should be used.
            if (response.FeaturesHasValue)
            {
                foreach (FeatureInfoDataContract feature in response.Features)
                {
                    if (!feature.FeatureIdHasValue || !feature.VersionHasValue) continue;
                    if (feature.FeatureId != "messaging") continue;

                    messagingFeatureVersion = feature.Version;
                    break;
                }
            }
            
            // Start processing messages.
            StartMessageProcessing(messagingFeatureVersion);
        }

        private void ChangeStation()
        {
            var stationResource = new StationResource(_webServiceUtility);
            var requestParameters 
            = new StationResource.ChangeStationConnectionRequestParameters();
            var dataContract = new WorkstationSettingsDataContract();
            dataContract.Workstation = "ExampleStation";

            Task<StationResource.ChangeStationConnectionResponses> changeStationConnectionTask 
            = stationResource.ChangeStationConnection(requestParameters, dataContract);
            changeStationConnectionTask.ContinueWith(t => HandleError(t.Exception), 
            TaskContinuationOptions.OnlyOnFaulted);
        }
        
        private void StartMessageProcessing(int messagingFeatureVersion)
        {
            // Here we check the messaging feature version and start the appropriate message handler.
            if (messagingFeatureVersion < 2)
            {
                StartShortPollingMessageProcessing();
            }
            else
            {
                StartServerSentEventsMessageProcessing();
            }
        }

        private void StartShortPollingMessageProcessing()
        {
            // Poll the server of any events that have occurred.  This should be expected
            // on a regular interval.
            
            // TODO:  This example does not show putting this call in an interval.
        
            var messagesResource = new MessagesResource(_webServiceUtility);
            var requestParameters = new MessagesResource.GetMessagesRequestParameters();
            Task<MessagesResource.GetMessagesResponses> messagesTask
            = messagesResource.GetMessages(requestParameters);
            messagesTask.ContinueWith(t =>
                {
                    var result = t.Result;
                    result.PerformIfResponseIs200(messages =>
                        {
                            // messages contains an array of server side events that were retrieved
                            foreach (MessageDataContract dataContract in messages)
                            {
                                HandleMessage(dataContract);
                            }
                        });
                    result.PerformOnFailureResponse(HandleError);
                    result.PerformDefault(HandleDefault);
                }, TaskContinuationOptions.OnlyOnRanToCompletion);
            messagesTask.ContinueWith(t => HandleError(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
        }

        private void StartServerSentEventsMessageProcessing()
        {
            // TODO:  The following code assumes a private variable exists for an appropriate
            // EventSource object, and hooks into a MessageReceived event.  The MessageReceived
            // event assumes a string JSON parameter that is then converted to an appropriate
            // MessageDataContract that can then be processed.

            /*
            if (_EventSource == null)
            {
                // TODO:  This URI will need to be updated appropriately.
                _EventSource = new EventSource("https://HostServerName:8019/icws/{sessionId}/messaging/messages");
            }

            _EventSource.MessageReceived += (json) =>
            {
                var dataContract = MessageDataContract.CreateFromJson(json);

                HandleMessage(dataContract);
            };
            */
        }
        
        private void HandleMessage(MessageDataContract dataContract)
        {
            // Omitted:  TODO, handle dataContract.
        }

        private void HandleDefault(HttpStatusCode statusCode)
        {
            // Omitted:  TODO, handle statusCode.
        }

        private void HandleError(HttpStatusCode statusCode)
        {
            // Omitted:  TODO, handle statusCode.
        }

        private void HandleError(Exception exception)
        {
            // Omitted:  TODO, handle exception.
        }
    }
}

 

请教下。上面的代码怎么转成java的代码。。。

Java
文明上网,理性发言
客户端 Android iPhone WP7