public byte[] HandleRESTRequest(string Capabilities, string resourceName, string operationName,
                                        string operationInput, string outputFormat, string requestProperties, out string responseProperties)
        {
            try
            {
                responseProperties = null;
                _serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".HandleRESTRequest()",
                                      200, "Request received in Sample Object Interceptor for handleRESTRequest");

                /*
                 * Add code to manipulate REST requests here
                 */

                // Find the correct delegate to forward the request too
                IRESTRequestHandler restRequestHandler = _restSOIHelper.FindRequestHandlerDelegate <IRESTRequestHandler>();
                if (restRequestHandler == null)
                {
                    throw new RestErrorException("Service handler not found");
                }

                var response = restRequestHandler.HandleRESTRequest(
                    Capabilities, resourceName, operationName, operationInput,
                    outputFormat, requestProperties, out responseProperties);

                /*
                 * Manipulate the response.
                 *
                 * Add watermark
                 */

                if (operationName.Equals("export", StringComparison.CurrentCultureIgnoreCase))
                {
                    Image sourceImage = null;
                    if (outputFormat.Equals("image", StringComparison.CurrentCultureIgnoreCase))
                    {
                        sourceImage = Image.FromStream(new System.IO.MemoryStream(response));

                        var watermarker = new ApplyWatermark();

                        var watermarkedImage = watermarker.Mark(sourceImage, "(c) ESRI Inc.");
                        var newResponse      = new System.IO.MemoryStream();
                        watermarkedImage.Save(newResponse, sourceImage.RawFormat);

                        return(newResponse.GetBuffer());
                    }
                    else if (outputFormat.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                    {
                        var    responseString = System.Text.Encoding.UTF8.GetString(response);
                        var    jo             = new JsonObject(responseString);
                        string hrefString     = null;
                        if (!jo.TryGetString("href", out hrefString))
                        {
                            throw new RestErrorException("Export operation returned invalid response");
                        }

                        if (string.IsNullOrEmpty(hrefString))
                        {
                            throw new RestErrorException("Export operation returned invalid response");
                        }

                        // Generate output file location
                        var outputImageFileLocation = GetOutputImageFileLocation(hrefString);

                        // debug logging
                        //_serverLog.LogMessage(ServerLogger.msgType.error, "debug", 0, "output is " + outputImageFileLocation);

                        var   watermarker = new ApplyWatermark();
                        Image watermarkedImage;

                        System.Drawing.Imaging.ImageFormat sourceImageFormat;
                        using (sourceImage = Image.FromFile(outputImageFileLocation))
                        {
                            sourceImageFormat = sourceImage.RawFormat;
                            watermarkedImage  = watermarker.Mark(sourceImage, "(c) ESRI Inc.");
                        }
                        // make sure we dispose sourceImage handles before saving to it

                        watermarkedImage.Save(outputImageFileLocation, sourceImageFormat);
                        watermarkedImage.Dispose();

                        // return response as is because we have modified the file its pointing to
                        return(response);
                    }
                    else if (outputFormat.Equals("kmz", StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Note: Watermark can be added for the kmz format too. In this example we didn't
                        // implement it.
                        throw new RestErrorException("Kmz format is not supported");
                    }
                    else
                    {
                        throw new RestErrorException("Invalid operation parameters");
                    }
                }//if operationName==export
                return(response);
            }
            catch (RestErrorException restException)
            {
                responseProperties = "{\"Content-Type\":\"text/plain;charset=utf-8\"}";
                return(System.Text.Encoding.UTF8.GetBytes(restException.Message));
            }
            catch (Exception e)
            {
                _serverLog.LogMessage(ServerLogger.msgType.error, _soiName + ".HandleRESTRequest()", 500, "Exception " + e.GetType().Name + " " + e.Message + " " + e.StackTrace);
                throw;
            }
        }
        public byte[] HandleRESTRequest ( string Capabilities, string resourceName, string operationName,
            string operationInput, string outputFormat, string requestProperties, out string responseProperties )
        {
            try
            {
                responseProperties = null;
                _serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".HandleRESTRequest()",
                    200, "Request received in Sample Object Interceptor for handleRESTRequest");

                /*
                * Add code to manipulate REST requests here
                */

                // Find the correct delegate to forward the request too
                IRESTRequestHandler restRequestHandler = _restSOIHelper.FindRequestHandlerDelegate<IRESTRequestHandler>();
                if (restRequestHandler == null)
                {
                    throw new RestErrorException("Service handler not found");
                }

                var response = restRequestHandler.HandleRESTRequest(
                        Capabilities, resourceName, operationName, operationInput,
                        outputFormat, requestProperties, out responseProperties);

                /*
                 * Manipulate the response.
                 * 
                 * Add watermark
                 */

                if (operationName.Equals("export", StringComparison.CurrentCultureIgnoreCase))
                {
                    Image sourceImage = null;
                    if (outputFormat.Equals("image", StringComparison.CurrentCultureIgnoreCase))
                    {
                        sourceImage = Image.FromStream(new System.IO.MemoryStream(response));

                        var watermarker = new ApplyWatermark();

                        var watermarkedImage = watermarker.Mark(sourceImage, "(c) ESRI Inc.");
                        var newResponse = new System.IO.MemoryStream();
                        watermarkedImage.Save(newResponse, sourceImage.RawFormat);

                        return newResponse.GetBuffer();
                    }
                    else if (outputFormat.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                    {

                        var responseString = System.Text.Encoding.UTF8.GetString(response);
                        var jo = new JsonObject(responseString);
                        string hrefString = null;
                        if (!jo.TryGetString("href", out hrefString))
                            throw new RestErrorException("Export operation returned invalid response");

                        if (string.IsNullOrEmpty(hrefString))
                            throw new RestErrorException("Export operation returned invalid response");

                        // Generate output file location
                        var outputImageFileLocation = GetOutputImageFileLocation(hrefString);
                        
                        // debug logging
                        //_serverLog.LogMessage(ServerLogger.msgType.error, "debug", 0, "output is " + outputImageFileLocation);

                        var watermarker = new ApplyWatermark();
                        Image watermarkedImage;

                        System.Drawing.Imaging.ImageFormat sourceImageFormat;
                        using( sourceImage = Image.FromFile(outputImageFileLocation))
                        {
                            sourceImageFormat = sourceImage.RawFormat;
                            watermarkedImage = watermarker.Mark(sourceImage, "(c) ESRI Inc.");
                        }
                        // make sure we dispose sourceImage handles before saving to it

                        watermarkedImage.Save(outputImageFileLocation, sourceImageFormat);
                        watermarkedImage.Dispose();

                        // return response as is because we have modified the file its pointing to
                        return response;
                    }
                    else if (outputFormat.Equals("kmz", StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Note: Watermark can be added for the kmz format too. In this example we didn't
                        // implement it.
                        throw new RestErrorException("Kmz format is not supported");
                    }
                    else
                    {
                        throw new RestErrorException("Invalid operation parameters");
                    }
                }//if operationName==export
                return response;
            }
            catch (RestErrorException restException)
            {
                responseProperties = "{\"Content-Type\":\"text/plain;charset=utf-8\"}";
                return System.Text.Encoding.UTF8.GetBytes(restException.Message);
            }
            catch (Exception e)
            {
                _serverLog.LogMessage(ServerLogger.msgType.error, _soiName + ".HandleRESTRequest()", 500, "Exception " + e.GetType().Name + " " + e.Message + " " + e.StackTrace);
                throw;
            }
        }