示例#1
0
            /// <exception cref="System.IO.IOException"/>
            protected internal override void ProcessOptions(List <string> args)
            {
                name = StringUtils.PopOptionWithArgument("-n", args);
                string v = StringUtils.PopOptionWithArgument("-v", args);

                if (v != null)
                {
                    value = XAttrCodec.DecodeValue(v);
                }
                xname = StringUtils.PopOptionWithArgument("-x", args);
                if (name != null && xname != null)
                {
                    throw new HadoopIllegalArgumentException("Can not specify both '-n name' and '-x name' option."
                                                             );
                }
                if (name == null && xname == null)
                {
                    throw new HadoopIllegalArgumentException("Must specify '-n name' or '-x name' option."
                                                             );
                }
                if (args.IsEmpty())
                {
                    throw new HadoopIllegalArgumentException("<path> is missing.");
                }
                if (args.Count > 1)
                {
                    throw new HadoopIllegalArgumentException("Too many arguments.");
                }
            }
示例#2
0
 /// <exception cref="System.IO.IOException"/>
 public FSSetXAttr(string path, string name, string encodedValue, EnumSet <XAttrSetFlag
                                                                           > flag)
 {
     this.path  = new Path(path);
     this.name  = name;
     this.value = XAttrCodec.DecodeValue(encodedValue);
     this.flag  = flag;
 }
示例#3
0
        /// <exception cref="System.IO.IOException"/>
        public static string ToJsonString(IList <XAttr> xAttrs, XAttrCodec encoding)
        {
            IDictionary <string, object> finalMap = new SortedDictionary <string, object>();

            finalMap["XAttrs"] = ToJsonArray(xAttrs, encoding);
            ObjectMapper mapper = new ObjectMapper();

            return(mapper.WriteValueAsString(finalMap));
        }
示例#4
0
 /// <summary>Decode xattr value from string</summary>
 /// <exception cref="System.IO.IOException"/>
 private byte[] DecodeXAttrValue(string value)
 {
     if (value != null)
     {
         return(XAttrCodec.DecodeValue(value));
     }
     else
     {
         return(new byte[0]);
     }
 }
示例#5
0
        public virtual void TestGetXAttrFromJson()
        {
            string jsonString = "{\"XAttrs\":[{\"name\":\"user.a1\",\"value\":\"0x313233\"},"
                                + "{\"name\":\"user.a2\",\"value\":\"0x313131\"}]}";
            ObjectReader reader = new ObjectMapper().Reader(typeof(IDictionary));
            IDictionary <object, object> json = reader.ReadValue(jsonString);

            // Get xattr: user.a2
            byte[] value = JsonUtil.GetXAttr(json, "user.a2");
            Assert.AssertArrayEquals(XAttrCodec.DecodeValue("0x313131"), value);
        }
示例#6
0
 /// <exception cref="System.IO.IOException"/>
 private void PrintXAttr(string name, byte[] value)
 {
     if (value != null)
     {
         if (value.Length != 0)
         {
             @out.WriteLine(name + "=" + XAttrCodec.EncodeValue(value, encoding));
         }
         else
         {
             @out.WriteLine(name);
         }
     }
 }
示例#7
0
        /// <exception cref="System.IO.IOException"/>
        private static IDictionary <string, object> ToJsonMap(XAttr xAttr, XAttrCodec encoding
                                                              )
        {
            if (xAttr == null)
            {
                return(null);
            }
            IDictionary <string, object> m = new SortedDictionary <string, object>();

            m["name"]  = XAttrHelper.GetPrefixName(xAttr);
            m["value"] = xAttr.GetValue() != null?XAttrCodec.EncodeValue(xAttr.GetValue(),
                                                                         encoding) : null;

            return(m);
        }
示例#8
0
        /// <summary>Converts xAttrs to a JSON object.</summary>
        /// <param name="xAttrs">file xAttrs.</param>
        /// <param name="encoding">format of xattr values.</param>
        /// <returns>The JSON representation of the xAttrs.</returns>
        /// <exception cref="System.IO.IOException"></exception>
        private static IDictionary XAttrsToJSON(IDictionary <string, byte[]> xAttrs, XAttrCodec
                                                encoding)
        {
            IDictionary jsonMap   = new LinkedHashMap();
            JSONArray   jsonArray = new JSONArray();

            if (xAttrs != null)
            {
                foreach (KeyValuePair <string, byte[]> e in xAttrs)
                {
                    IDictionary json = new LinkedHashMap();
                    json[HttpFSFileSystem.XattrNameJson] = e.Key;
                    if (e.Value != null)
                    {
                        json[HttpFSFileSystem.XattrValueJson] = XAttrCodec.EncodeValue(e.Value, encoding);
                    }
                    jsonArray.AddItem(json);
                }
            }
            jsonMap[HttpFSFileSystem.XattrsJson] = jsonArray;
            return(jsonMap);
        }
示例#9
0
 /// <exception cref="System.IO.IOException"/>
 private static object[] ToJsonArray(IList <XAttr> array, XAttrCodec encoding)
 {
     if (array == null)
     {
         return(null);
     }
     else
     {
         if (array.Count == 0)
         {
             return(EmptyObjectArray);
         }
         else
         {
             object[] a = new object[array.Count];
             for (int i = 0; i < array.Count; i++)
             {
                 a[i] = ToJsonMap(array[i], encoding);
             }
             return(a);
         }
     }
 }
示例#10
0
            /// <exception cref="System.IO.IOException"/>
            protected internal override void ProcessOptions(List <string> args)
            {
                name = StringUtils.PopOptionWithArgument("-n", args);
                string en = StringUtils.PopOptionWithArgument("-e", args);

                if (en != null)
                {
                    try
                    {
                        encoding = enValueOfFunc.Apply(StringUtils.ToUpperCase(en));
                    }
                    catch (ArgumentException)
                    {
                        throw new ArgumentException("Invalid/unsupported encoding option specified: " + en
                                                    );
                    }
                    Preconditions.CheckArgument(encoding != null, "Invalid/unsupported encoding option specified: "
                                                + en);
                }
                bool r = StringUtils.PopOption("-R", args);

                SetRecursive(r);
                dump = StringUtils.PopOption("-d", args);
                if (!dump && name == null)
                {
                    throw new HadoopIllegalArgumentException("Must specify '-n name' or '-d' option."
                                                             );
                }
                if (args.IsEmpty())
                {
                    throw new HadoopIllegalArgumentException("<path> is missing.");
                }
                if (args.Count > 1)
                {
                    throw new HadoopIllegalArgumentException("Too many arguments.");
                }
            }
示例#11
0
        public virtual void TestXAttrValueParam()
        {
            XAttrValueParam p = new XAttrValueParam("0x313233");

            Assert.AssertArrayEquals(p.GetXAttrValue(), XAttrCodec.DecodeValue("0x313233"));
        }
示例#12
0
        public virtual void TestToXAttrMap()
        {
            string jsonString = "{\"XAttrs\":[{\"name\":\"user.a1\",\"value\":\"0x313233\"},"
                                + "{\"name\":\"user.a2\",\"value\":\"0x313131\"}]}";
            ObjectReader reader = new ObjectMapper().Reader(typeof(IDictionary));
            IDictionary <object, object> json = reader.ReadValue(jsonString);
            XAttr xAttr1 = (new XAttr.Builder()).SetNameSpace(XAttr.NameSpace.User).SetName("a1"
                                                                                            ).SetValue(XAttrCodec.DecodeValue("0x313233")).Build();
            XAttr xAttr2 = (new XAttr.Builder()).SetNameSpace(XAttr.NameSpace.User).SetName("a2"
                                                                                            ).SetValue(XAttrCodec.DecodeValue("0x313131")).Build();
            IList <XAttr> xAttrs = Lists.NewArrayList();

            xAttrs.AddItem(xAttr1);
            xAttrs.AddItem(xAttr2);
            IDictionary <string, byte[]> xAttrMap       = XAttrHelper.BuildXAttrMap(xAttrs);
            IDictionary <string, byte[]> parsedXAttrMap = JsonUtil.ToXAttrs(json);

            NUnit.Framework.Assert.AreEqual(xAttrMap.Count, parsedXAttrMap.Count);
            IEnumerator <KeyValuePair <string, byte[]> > iter = xAttrMap.GetEnumerator();

            while (iter.HasNext())
            {
                KeyValuePair <string, byte[]> entry = iter.Next();
                Assert.AssertArrayEquals(entry.Value, parsedXAttrMap[entry.Key]);
            }
        }
示例#13
0
        public virtual void TestToJsonFromXAttrs()
        {
            string jsonString = "{\"XAttrs\":[{\"name\":\"user.a1\",\"value\":\"0x313233\"},"
                                + "{\"name\":\"user.a2\",\"value\":\"0x313131\"}]}";
            XAttr xAttr1 = (new XAttr.Builder()).SetNameSpace(XAttr.NameSpace.User).SetName("a1"
                                                                                            ).SetValue(XAttrCodec.DecodeValue("0x313233")).Build();
            XAttr xAttr2 = (new XAttr.Builder()).SetNameSpace(XAttr.NameSpace.User).SetName("a2"
                                                                                            ).SetValue(XAttrCodec.DecodeValue("0x313131")).Build();
            IList <XAttr> xAttrs = Lists.NewArrayList();

            xAttrs.AddItem(xAttr1);
            xAttrs.AddItem(xAttr2);
            NUnit.Framework.Assert.AreEqual(jsonString, JsonUtil.ToJsonString(xAttrs, XAttrCodec
                                                                              .Hex));
        }
示例#14
0
 /// <summary>Params for setting an xAttr</summary>
 /// <exception cref="System.IO.IOException"/>
 public static string SetXAttrParam(string name, byte[] value)
 {
     return("xattr.name=" + name + "&xattr.value=" + XAttrCodec.EncodeValue(value, XAttrCodec
                                                                            .Hex) + "&encoding=hex&flag=create");
 }
示例#15
0
        public virtual Response Get(string path, HttpFSParametersProvider.OperationParam
                                    op, Parameters @params, HttpServletRequest request)
        {
            UserGroupInformation user = HttpUserGroupInformation.Get();
            Response             response;

            path = MakeAbsolute(path);
            MDC.Put(HttpFSFileSystem.OpParam, op.Value().ToString());
            MDC.Put("hostname", request.GetRemoteAddr());
            switch (op.Value())
            {
            case HttpFSFileSystem.Operation.Open:
            {
                //Invoking the command directly using an unmanaged FileSystem that is
                // released by the FileSystemReleaseFilter
                FSOperations.FSOpen command = new FSOperations.FSOpen(path);
                FileSystem          fs      = CreateFileSystem(user);
                InputStream         @is     = command.Execute(fs);
                long offset = @params.Get <HttpFSParametersProvider.OffsetParam>(HttpFSParametersProvider.OffsetParam
                                                                                 .Name);
                long len = @params.Get <HttpFSParametersProvider.LenParam>(HttpFSParametersProvider.LenParam
                                                                           .Name);
                AuditLog.Info("[{}] offset [{}] len [{}]", new object[] { path, offset, len });
                InputStreamEntity entity = new InputStreamEntity(@is, offset, len);
                response = Response.Ok(entity).Type(MediaType.ApplicationOctetStream).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Getfilestatus:
            {
                FSOperations.FSFileStatus command = new FSOperations.FSFileStatus(path);
                IDictionary json = FsExecute(user, command);
                AuditLog.Info("[{}]", path);
                response = Response.Ok(json).Type(MediaType.ApplicationJson).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Liststatus:
            {
                string filter = @params.Get <HttpFSParametersProvider.FilterParam>(HttpFSParametersProvider.FilterParam
                                                                                   .Name);
                FSOperations.FSListStatus command = new FSOperations.FSListStatus(path, filter);
                IDictionary json = FsExecute(user, command);
                AuditLog.Info("[{}] filter [{}]", path, (filter != null) ? filter : "-");
                response = Response.Ok(json).Type(MediaType.ApplicationJson).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Gethomedirectory:
            {
                EnforceRootPath(op.Value(), path);
                FSOperations.FSHomeDir command = new FSOperations.FSHomeDir();
                JSONObject             json    = FsExecute(user, command);
                AuditLog.Info(string.Empty);
                response = Response.Ok(json).Type(MediaType.ApplicationJson).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Instrumentation:
            {
                EnforceRootPath(op.Value(), path);
                Groups         groups     = HttpFSServerWebApp.Get().Get <Groups>();
                IList <string> userGroups = groups.GetGroups(user.GetShortUserName());
                if (!userGroups.Contains(HttpFSServerWebApp.Get().GetAdminGroup()))
                {
                    throw new AccessControlException("User not in HttpFSServer admin group");
                }
                Instrumentation instrumentation = HttpFSServerWebApp.Get().Get <Instrumentation>();
                IDictionary     snapshot        = instrumentation.GetSnapshot();
                response = Response.Ok(snapshot).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Getcontentsummary:
            {
                FSOperations.FSContentSummary command = new FSOperations.FSContentSummary(path);
                IDictionary json = FsExecute(user, command);
                AuditLog.Info("[{}]", path);
                response = Response.Ok(json).Type(MediaType.ApplicationJson).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Getfilechecksum:
            {
                FSOperations.FSFileChecksum command = new FSOperations.FSFileChecksum(path);
                IDictionary json = FsExecute(user, command);
                AuditLog.Info("[{}]", path);
                response = Response.Ok(json).Type(MediaType.ApplicationJson).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Getfileblocklocations:
            {
                response = Response.Status(Response.Status.BadRequest).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Getaclstatus:
            {
                FSOperations.FSAclStatus command = new FSOperations.FSAclStatus(path);
                IDictionary json = FsExecute(user, command);
                AuditLog.Info("ACL status for [{}]", path);
                response = Response.Ok(json).Type(MediaType.ApplicationJson).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Getxattrs:
            {
                IList <string> xattrNames = @params.GetValues <HttpFSParametersProvider.XAttrNameParam
                                                               >(HttpFSParametersProvider.XAttrNameParam.Name);
                XAttrCodec encoding = @params.Get <HttpFSParametersProvider.XAttrEncodingParam>(HttpFSParametersProvider.XAttrEncodingParam
                                                                                                .Name);
                FSOperations.FSGetXAttrs command = new FSOperations.FSGetXAttrs(path, xattrNames,
                                                                                encoding);
                IDictionary json = FsExecute(user, command);
                AuditLog.Info("XAttrs for [{}]", path);
                response = Response.Ok(json).Type(MediaType.ApplicationJson).Build();
                break;
            }

            case HttpFSFileSystem.Operation.Listxattrs:
            {
                FSOperations.FSListXAttrs command = new FSOperations.FSListXAttrs(path);
                IDictionary json = FsExecute(user, command);
                AuditLog.Info("XAttr names for [{}]", path);
                response = Response.Ok(json).Type(MediaType.ApplicationJson).Build();
                break;
            }

            default:
            {
                throw new IOException(MessageFormat.Format("Invalid HTTP GET operation [{0}]", op
                                                           .Value()));
            }
            }
            return(response);
        }
示例#16
0
 /// <summary>Creates getting xattrs executor.</summary>
 /// <param name="path">the path to retrieve the xattrs.</param>
 public FSGetXAttrs(string path, IList <string> names, XAttrCodec encoding)
 {
     this.path     = new Path(path);
     this.names    = names;
     this.encoding = encoding;
 }
示例#17
0
        /// <exception cref="System.IO.IOException"/>
        public virtual byte[] GetXAttrValue()
        {
            string v = GetValue();

            return(XAttrCodec.DecodeValue(v));
        }