예제 #1
0
        /// <summary>Create a redirection URL</summary>
        /// <exception cref="System.IO.IOException"/>
        private Uri CreateRedirectURL(string path, string encodedPath, HdfsFileStatus status
                                      , UserGroupInformation ugi, ClientProtocol nnproxy, HttpServletRequest request,
                                      string dt)
        {
            string        scheme = request.GetScheme();
            LocatedBlocks blks   = nnproxy.GetBlockLocations(status.GetFullPath(new Path(path))
                                                             .ToUri().GetPath(), 0, 1);
            Configuration conf = NameNodeHttpServer.GetConfFromContext(GetServletContext());
            DatanodeID    host = PickSrcDatanode(blks, status, conf);
            string        hostname;

            if (host is DatanodeInfo)
            {
                hostname = host.GetHostName();
            }
            else
            {
                hostname = host.GetIpAddr();
            }
            int    port    = "https".Equals(scheme) ? host.GetInfoSecurePort() : host.GetInfoPort();
            string dtParam = string.Empty;

            if (dt != null)
            {
                dtParam = JspHelper.GetDelegationTokenUrlParam(dt);
            }
            // Add namenode address to the url params
            NameNode nn        = NameNodeHttpServer.GetNameNodeFromContext(GetServletContext());
            string   addr      = nn.GetNameNodeAddressHostPortString();
            string   addrParam = JspHelper.GetUrlParam(JspHelper.NamenodeAddress, addr);

            return(new Uri(scheme, hostname, port, "/streamFile" + encodedPath + '?' + "ugi="
                           + ServletUtil.EncodeQueryValue(ugi.GetShortUserName()) + dtParam + addrParam));
        }
예제 #2
0
            /// <summary>Create a redirection URL</summary>
            /// <exception cref="System.IO.IOException"/>
            private Uri CreateRedirectURL(UserGroupInformation ugi, DatanodeID host, HttpServletRequest
                                          request, NameNode nn)
            {
                string hostname = host is DatanodeInfo?host.GetHostName() : host.GetIpAddr();

                string scheme = request.GetScheme();
                int    port   = host.GetInfoPort();

                if ("https".Equals(scheme))
                {
                    int portObject = (int)GetServletContext().GetAttribute(DFSConfigKeys.DfsDatanodeHttpsPortKey
                                                                           );
                    if (portObject != null)
                    {
                        port = portObject;
                    }
                }
                string encodedPath = ServletUtil.GetRawPath(request, "/fileChecksum");
                string dtParam     = string.Empty;

                if (UserGroupInformation.IsSecurityEnabled())
                {
                    string tokenString = ugi.GetTokens().GetEnumerator().Next().EncodeToUrlString();
                    dtParam = JspHelper.GetDelegationTokenUrlParam(tokenString);
                }
                string addr      = nn.GetNameNodeAddressHostPortString();
                string addrParam = JspHelper.GetUrlParam(JspHelper.NamenodeAddress, addr);

                return(new Uri(scheme, hostname, port, "/getFileChecksum" + encodedPath + '?' + "ugi="
                               + ServletUtil.EncodeQueryValue(ugi.GetShortUserName()) + dtParam + addrParam));
            }
예제 #3
0
 /// <summary>Select a datanode to service this request.</summary>
 /// <remarks>
 /// Select a datanode to service this request.
 /// Currently, this looks at no more than the first five blocks of a file,
 /// selecting a datanode randomly from the most represented.
 /// </remarks>
 /// <param name="conf"></param>
 /// <exception cref="System.IO.IOException"/>
 private DatanodeID PickSrcDatanode(LocatedBlocks blks, HdfsFileStatus i, Configuration
                                    conf)
 {
     if (i.GetLen() == 0 || blks.GetLocatedBlocks().Count <= 0)
     {
         // pick a random datanode
         NameNode nn = NameNodeHttpServer.GetNameNodeFromContext(GetServletContext());
         return(NamenodeJspHelper.GetRandomDatanode(nn));
     }
     return(JspHelper.BestNode(blks, conf));
 }
예제 #4
0
        public override UserGroupInformation GetValue(HttpContext context)
        {
            Configuration conf = (Configuration)servletcontext.GetAttribute(JspHelper.CurrentConf
                                                                            );

            try
            {
                return(JspHelper.GetUGI(servletcontext, request, conf, UserGroupInformation.AuthenticationMethod
                                        .Kerberos, false));
            }
            catch (IOException e)
            {
                throw new SecurityException(SecurityUtil.FailedToGetUgiMsgHeader + " " + e, e);
            }
        }
예제 #5
0
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual string AddDelegationTokenParam(string query)
        {
            string tokenString = null;

            if (UserGroupInformation.IsSecurityEnabled())
            {
                lock (this)
                {
                    tokenAspect.EnsureTokenInitialized();
                    if (delegationToken != null)
                    {
                        tokenString = delegationToken.EncodeToUrlString();
                        return(query + JspHelper.GetDelegationTokenUrlParam(tokenString));
                    }
                }
            }
            return(query);
        }
예제 #6
0
        /// <exception cref="System.IO.IOException"/>
        internal virtual UserGroupInformation Ugi()
        {
            if (UserGroupInformation.IsSecurityEnabled())
            {
                return(TokenUGI());
            }
            string usernameFromQuery = @params.UserName();
            string doAsUserFromQuery = @params.DoAsUser();
            string remoteUser        = usernameFromQuery == null?JspHelper.GetDefaultWebUserName(@params
                                                                                                 .Conf()) : usernameFromQuery;

            // not specified in
            // request
            UserGroupInformation ugi = UserGroupInformation.CreateRemoteUser(remoteUser);

            JspHelper.CheckUsername(ugi.GetShortUserName(), usernameFromQuery);
            if (doAsUserFromQuery != null)
            {
                // create and attempt to authorize a proxy user
                ugi = UserGroupInformation.CreateProxyUser(doAsUserFromQuery, ugi);
            }
            return(ugi);
        }
예제 #7
0
 /// <exception cref="System.IO.IOException"/>
 protected internal virtual UserGroupInformation GetUGI(HttpServletRequest request
                                                        , Configuration conf)
 {
     return(JspHelper.GetUGI(GetServletContext(), request, conf));
 }
예제 #8
0
        /// <exception cref="Javax.Servlet.ServletException"/>
        /// <exception cref="System.IO.IOException"/>
        protected override void DoGet(HttpServletRequest request, HttpServletResponse response
                                      )
        {
            string path        = ServletUtil.GetDecodedPath(request, "/streamFile");
            string rawPath     = ServletUtil.GetRawPath(request, "/streamFile");
            string filename    = JspHelper.ValidatePath(path);
            string rawFilename = JspHelper.ValidatePath(rawPath);

            if (filename == null)
            {
                response.SetContentType("text/plain");
                PrintWriter @out = response.GetWriter();
                @out.Write("Invalid input");
                return;
            }
            Enumeration <string> reqRanges = request.GetHeaders("Range");

            if (reqRanges != null && !reqRanges.MoveNext())
            {
                reqRanges = null;
            }
            DFSClient dfs;

            try
            {
                dfs = GetDFSClient(request);
            }
            catch (Exception e)
            {
                response.SendError(400, e.Message);
                return;
            }
            HdfsDataInputStream @in   = null;
            OutputStream        out_1 = null;

            try
            {
                @in   = dfs.CreateWrappedInputStream(dfs.Open(filename));
                out_1 = response.GetOutputStream();
                long fileLen = @in.GetVisibleLength();
                if (reqRanges != null)
                {
                    IList <InclusiveByteRange> ranges = InclusiveByteRange.SatisfiableRanges(reqRanges
                                                                                             , fileLen);
                    StreamFile.SendPartialData(@in, out_1, response, fileLen, ranges);
                }
                else
                {
                    // No ranges, so send entire file
                    response.SetHeader("Content-Disposition", "attachment; filename=\"" + rawFilename
                                       + "\"");
                    response.SetContentType("application/octet-stream");
                    response.SetHeader(ContentLength, string.Empty + fileLen);
                    StreamFile.CopyFromOffset(@in, out_1, 0L, fileLen);
                }
                @in.Close();
                @in = null;
                out_1.Close();
                out_1 = null;
                dfs.Close();
                dfs = null;
            }
            catch (IOException ioe)
            {
                if (Log.IsDebugEnabled())
                {
                    Log.Debug("response.isCommitted()=" + response.IsCommitted(), ioe);
                }
                throw;
            }
            finally
            {
                IOUtils.Cleanup(Log, @in);
                IOUtils.Cleanup(Log, out_1);
                IOUtils.Cleanup(Log, dfs);
            }
        }