Пример #1
0
			protected internal virtual void SendError(ChannelHandlerContext ctx, string message
				, HttpResponseStatus status)
			{
				HttpResponse response = new DefaultHttpResponse(HttpVersion.Http11, status);
				response.SetHeader(HttpHeaders.Names.ContentType, "text/plain; charset=UTF-8");
				// Put shuffle version into http header
				response.SetHeader(ShuffleHeader.HttpHeaderName, ShuffleHeader.DefaultHttpHeaderName
					);
				response.SetHeader(ShuffleHeader.HttpHeaderVersion, ShuffleHeader.DefaultHttpHeaderVersion
					);
				response.SetContent(ChannelBuffers.CopiedBuffer(message, CharsetUtil.Utf8));
				// Close the connection as soon as the error message is sent.
				ctx.GetChannel().Write(response).AddListener(ChannelFutureListener.Close);
			}
            /// <exception cref="System.IO.IOException"/>
            public virtual void Handle(Org.Jboss.Netty.Channel.Channel channel, Org.Apache.Hadoop.Security.Token.Token
                                       <DelegationTokenIdentifier> token, string serviceUrl)
            {
                NUnit.Framework.Assert.AreEqual(this._enclosing.testToken, token);
                byte[] bytes = Sharpen.Runtime.GetBytesForString(TestDelegationTokenRemoteFetcher
                                                                 .ExpDate);
                ChannelBuffer cbuffer = ChannelBuffers.Buffer(bytes.Length);

                cbuffer.WriteBytes(bytes);
                HttpResponse response = new DefaultHttpResponse(HttpVersion.Http11, HttpResponseStatus
                                                                .Ok);

                response.SetHeader(HttpHeaders.Names.ContentLength, bytes.Length.ToString());
                response.SetContent(cbuffer);
                channel.Write(response).AddListener(ChannelFutureListener.Close);
            }
            /// <exception cref="System.Exception"/>
            public override void MessageReceived(ChannelHandlerContext ctx, MessageEvent e)
            {
                HttpRequest request = (HttpRequest)e.GetMessage();

                if (request.GetMethod() == HttpMethod.Options)
                {
                    // Mimic SPNEGO authentication
                    HttpResponse response = new DefaultHttpResponse(HttpVersion.Http11, HttpResponseStatus
                                                                    .Ok);
                    response.AddHeader("Set-Cookie", "hadoop-auth=1234");
                    e.GetChannel().Write(response).AddListener(ChannelFutureListener.Close);
                }
                else
                {
                    if (request.GetMethod() != HttpMethod.Get)
                    {
                        e.GetChannel().Close();
                    }
                }
                UnmodifiableIterator <KeyValuePair <string, TestDelegationTokenRemoteFetcher.Handler
                                                    > > iter = this.routes.GetEnumerator();

                while (iter.HasNext())
                {
                    KeyValuePair <string, TestDelegationTokenRemoteFetcher.Handler> entry = iter.Next(
                        );
                    if (request.GetUri().Contains(entry.Key))
                    {
                        TestDelegationTokenRemoteFetcher.Handler handler = entry.Value;
                        try
                        {
                            handler.Handle(e.GetChannel(), this.token, this.serviceUrl);
                        }
                        catch (Exception ee)
                        {
                            this._enclosing.assertionError = ee;
                            HttpResponse response = new DefaultHttpResponse(HttpVersion.Http11, HttpResponseStatus
                                                                            .BadRequest);
                            response.SetContent(ChannelBuffers.CopiedBuffer(ee.Message, Encoding.Default));
                            e.GetChannel().Write(response).AddListener(ChannelFutureListener.Close);
                        }
                        return;
                    }
                }
            }
            /// <exception cref="System.IO.IOException"/>
            public virtual void Handle(Org.Jboss.Netty.Channel.Channel channel, Org.Apache.Hadoop.Security.Token.Token
                                       <DelegationTokenIdentifier> token, string serviceUrl)
            {
                NUnit.Framework.Assert.AreEqual(this._enclosing.testToken, token);
                Credentials creds = new Credentials();

                creds.AddToken(new Text(serviceUrl), token);
                DataOutputBuffer @out = new DataOutputBuffer();

                creds.Write(@out);
                int           fileLength = @out.GetData().Length;
                ChannelBuffer cbuffer    = ChannelBuffers.Buffer(fileLength);

                cbuffer.WriteBytes(@out.GetData());
                HttpResponse response = new DefaultHttpResponse(HttpVersion.Http11, HttpResponseStatus
                                                                .Ok);

                response.SetHeader(HttpHeaders.Names.ContentLength, fileLength.ToString());
                response.SetContent(cbuffer);
                channel.Write(response).AddListener(ChannelFutureListener.Close);
            }