private byte[] getLayerCountByType(System.Collections.Specialized.NameValueCollection boundVariables,
                                           ESRI.ArcGIS.SOESupport.JsonObject operationInput,
                                           string outputFormat,
                                           string requestProperties,
                                           out string responseProperties)
        {
            responseProperties = null;

            String layerType = "";

            operationInput.TryGetString("type", out layerType);

            JSONObject json = new JSONObject();

            int count = 0;

            if (layerType != null && layerType.Length > 0)
            {
                String aoType = "";
                if (layerType.Equals("all"))
                {
                    count = layerInfos.Count;
                }
                else if (layerType.Equals("feature"))
                {
                    aoType = "Feature Layer";
                }
                else if (layerType.Equals("raster"))
                {
                    aoType = "Raster Layer";
                }
                else if (layerType.Equals("dataset"))
                {
                    aoType = "Network Dataset Layer";
                }

                for (int i = 0; i < layerInfos.Count; i++)
                {
                    if (layerInfos.get_Element(i).Type.Equals(aoType))
                    {
                        count++;
                    }
                }

                json.AddLong("count", count);

                return(Encoding.UTF8.GetBytes(json.ToJSONString(null)));
            }
            else
            {
                return(createErrorObject(500, "Invalid layer type provided. Available types are: \"all\", \"feature\", \"raster\", \"dataset\".",
                                         new String[1] {
                    "No details specified."
                }));
            }
        }
        private byte[] getLayerCountByType(System.Collections.Specialized.NameValueCollection boundVariables,
                                           ESRI.ArcGIS.SOESupport.JsonObject operationInput,
                                           string outputFormat,
                                           string requestProperties,
                                           out string responseProperties)
        {
            IMapImage mapImage = null;

            bool?shouldAdd = null;

            operationInput.TryGetAsBoolean("addlayer", out shouldAdd);

            if (shouldAdd.HasValue)
            {
                if ((bool)shouldAdd)
                {
                    if (((IMapServerInfo4)mapServerInfo).SupportsDynamicLayers)
                    {
                        IRgbColor color = new RgbColor()
                        {
                            Blue = 255
                        };

                        ISimpleLineSymbol outline = new SimpleLineSymbol()
                        {
                            Color = color,
                            Width = 1,
                            Style = esriSimpleLineStyle.esriSLSSolid
                        };

                        ISimpleFillSymbol sfs = new SimpleFillSymbol()
                        {
                            Color   = color,
                            Outline = outline,
                            Style   = esriSimpleFillStyle.esriSFSSolid
                        };

                        ISimpleRenderer sr = new SimpleRenderer()
                        {
                            Symbol = (ISymbol)sfs
                        };

                        IFeatureLayerDrawingDescription featureLayerDrawingDesc = new FeatureLayerDrawingDescription()
                        {
                            FeatureRenderer = (IFeatureRenderer)sr
                        };

                        IMapServerSourceDescription mapServerSourceDesc = new TableDataSourceDescriptionClass();
                        ((IDataSourceDescription)mapServerSourceDesc).WorkspaceID    = "MyFGDB";
                        ((ITableDataSourceDescription)mapServerSourceDesc).TableName = "B";

                        IDynamicLayerDescription dynamicLayerDesc = new LayerDescriptionClass()
                        {
                            ID                 = 3,
                            Visible            = true,
                            DrawingDescription = (ILayerDrawingDescription)featureLayerDrawingDesc,
                            Source             = mapServerSourceDesc
                        };

                        mapDesc.HonorLayerReordering = true;
                        mapDesc.LayerDescriptions.Insert(0, (ILayerDescription)dynamicLayerDesc);

                        mapImage = exportMap();
                    }
                }
                else
                {
                    mapImage = exportMap();
                }
            }
            responseProperties = null;

            JSONObject json = new JSONObject();

            json.AddString("URL", mapImage.URL);
            return(Encoding.UTF8.GetBytes(json.ToJSONString(null)));
        }
        /// <summary>
        ///  Read permission information from disk
        /// </summary>
        /// <param name="fileName">path and name of the file to read permissions from</param>
        /// <returns></returns>
        private Dictionary<String, String> ReadPermissionFile ( String fileName )
        {
            // read the permissions file
            Dictionary<String, String> permissionMap = new Dictionary<String, String>();
            try
            {

                if (!File.Exists(fileName))
                    return permissionMap;

                String jsonStr = File.ReadAllText(fileName);


                var json = new ESRI.ArcGIS.SOESupport.JsonObject(jsonStr);
                System.Object[] permissions = null;
                // create a map of permissions
                // read the permissions array
                if (!json.TryGetArray("permissions", out permissions) || permissions == null)
                    return permissionMap;


                // add to map
                foreach (var permsObj in permissions)
                {
                    ESRI.ArcGIS.SOESupport.JsonObject permsJO = permsObj as ESRI.ArcGIS.SOESupport.JsonObject;
                    if (null == permsJO) continue;

                    // get the fqsn or service name
                    String fqsn = string.Empty;
                    permsJO.TryGetString("fqsn", out fqsn);
                    // read the permission for that service
                    System.Object[] permArray = null;
                    if (!permsJO.TryGetArray("permission", out permArray) || permArray == null)
                        continue;

                    foreach (var permObj in permArray)
                    {
                        ESRI.ArcGIS.SOESupport.JsonObject permJO = permObj as ESRI.ArcGIS.SOESupport.JsonObject;
                        String role = string.Empty;
                        if (!permJO.TryGetString("role", out role))
                            continue;

                        String authorizedLayers = string.Empty;
                        permJO.TryGetString("authorizedLayers", out authorizedLayers); //may be empty or null
                        permissionMap.Add(fqsn + "." + role, authorizedLayers);
                    }
                }
            }
            catch (Exception ignore)
            {
                //TODO error handling
            }
            return permissionMap;
        }