来源 目录
简单日志
// Copyright 2015 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See the use restrictions at <your ArcGIS install location>/DeveloperKit10.3/userestrictions.txt.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.SOESupport;
namespace NetSimpleLoggerSOI
{
//Used to pass error JSON as REST response
internal class RESTErrorException : Exception
{
public RESTErrorException ( string json ) : base(json) { }
}
[ComVisible(true)]
[Guid("E911E5BE-9B35-481B-A8CA-3D29CC431817")]
[ClassInterface(ClassInterfaceType.None)]
[ServerObjectInterceptor("MapServer",
Description = "DotNet Simple Logger REST SOI Example",
DisplayName = "DotNet Simple Logger REST SOI Example",
Properties = "")]
public class NetSimpleLoggerSOI : IServerObjectExtension, IRESTRequestHandler, IWebRequestHandler, IRequestHandler2, IRequestHandler
{
private string _soiName;
private IServerObjectHelper _soHelper;
private ServerLogger _serverLog;
private Dictionary<String, IServerObjectExtension> _extensionCache = new Dictionary<String, IServerObjectExtension>();
IServerEnvironment2 _serverEnvironment;
private IServerEnvironment2 ServerEnvironment
{
get
{
if (_serverEnvironment == null)
{
UID uid = new UIDClass();
uid.Value = "{32D4C328-E473-4615-922C-63C108F55E60}";
// CoCreate an EnvironmentManager and retrieve the IServerEnvironment
IEnvironmentManager environmentManager = new EnvironmentManager() as IEnvironmentManager;
_serverEnvironment = environmentManager.GetEnvironment(uid) as IServerEnvironment2;
}
return _serverEnvironment;
}
}
public NetSimpleLoggerSOI()
{
_soiName = this.GetType().Name;
}
public void Init(IServerObjectHelper pSOH)
{
_soHelper = pSOH;
_serverLog = new ServerLogger();
_serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".init()", 200, "Initialized " + _soiName + " SOI.");
}
public void Shutdown()
{
_serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".init()", 200, "Shutting down " + _soiName + " SOI.");
}
public string GetSchema()
{
IRESTRequestHandler restRequestHandler = FindRequestHandlerDelegate<IRESTRequestHandler>();
if (restRequestHandler == null)
throw new RESTErrorException("Service handler not found");
return restRequestHandler.GetSchema();
}
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 Server Object Interceptor for handleRESTRequest");
/*
* Add code to manipulate REST requests here
*/
// Find the correct delegate to forward the request too
IRESTRequestHandler restRequestHandler = FindRequestHandlerDelegate<IRESTRequestHandler>();
if (restRequestHandler == null)
throw new RESTErrorException("Service handler not found");
return restRequestHandler.HandleRESTRequest(
Capabilities, resourceName, operationName, operationInput,
outputFormat, requestProperties, out responseProperties);
}
catch (RESTErrorException e)
{
responseProperties = "{\"Content-Type\":\"text/plain;charset=utf-8\"}";
return System.Text.Encoding.UTF8.GetBytes(e.Message);
}
}
public byte[] HandleStringWebRequest(esriHttpMethod httpMethod, string requestURL,
string queryString, string Capabilities, string requestData,
out string responseContentType, out esriWebResponseDataType respDataType)
{
_serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".HandleStringWebRequest()",
200, "Request received in Server Object Interceptor for HandleStringWebRequest");
/*
* Add code to manipulate requests here
*/
IWebRequestHandler webRequestHandler = FindRequestHandlerDelegate<IWebRequestHandler>();
if (webRequestHandler != null)
{
return webRequestHandler.HandleStringWebRequest(
httpMethod, requestURL, queryString, Capabilities, requestData, out responseContentType, out respDataType);
}
responseContentType = null;
respDataType = esriWebResponseDataType.esriWRDTPayload;
//Insert error response here.
return null;
}
public byte[] HandleBinaryRequest(ref byte[] request)
{
_serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".HandleBinaryRequest()",
200, "Request received in Server Object Interceptor for HandleBinaryRequest");
/*
* Add code to manipulate requests here
*/
IRequestHandler requestHandler = FindRequestHandlerDelegate<IRequestHandler >();
if (requestHandler != null)
{
return requestHandler.HandleBinaryRequest(request);
}
//Insert error response here.
return null;
}
public byte[] HandleBinaryRequest2(string Capabilities, ref byte[] request)
{
_serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".HandleBinaryRequest2()",
200, "Request received in Server Object Interceptor for HandleBinaryRequest2");
/*
* Add code to manipulate requests here
*/
IRequestHandler2 requestHandler = FindRequestHandlerDelegate<IRequestHandler2>();
if (requestHandler != null)
{
return requestHandler.HandleBinaryRequest2(Capabilities, request);
}
//Insert error response here.
return null;
}
public string HandleStringRequest(string Capabilities, string request)
{
_serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".HandleStringRequest()",
200, "Request received in Server Object Interceptor for HandleStringRequest");
/*
* Add code to manipulate requests here
*/
IRequestHandler requestHandler = FindRequestHandlerDelegate<IRequestHandler>();
if (requestHandler != null)
{
return requestHandler.HandleStringRequest(Capabilities, request);
}
//Insert error response here.
return null;
}
private THandlerInterface FindRequestHandlerDelegate<THandlerInterface>() where THandlerInterface : class
{
try
{
IPropertySet props = ServerEnvironment.Properties;
String extensionName;
try
{
extensionName = (String)props.GetProperty("ExtensionName");
}
catch (Exception /*e*/)
{
extensionName = null;
}
if (String.IsNullOrEmpty(extensionName))
{
return (_soHelper.ServerObject as THandlerInterface);
}
// Get the extension reference from cache if available
if (_extensionCache.ContainsKey(extensionName))
{
return (_extensionCache[extensionName] as THandlerInterface);
}
// This request is to be made on a specific extension
// so we find the extension from the extension manager
IServerObjectExtensionManager extnMgr = _soHelper.ServerObject as IServerObjectExtensionManager;
IServerObjectExtension soe = extnMgr.FindExtensionByTypeName(extensionName);
return (soe as THandlerInterface);
}
catch (Exception e)
{
_serverLog.LogMessage(ServerLogger.msgType.error,
_soiName + ".FindRequestHandlerDelegate()", 500, e.ToString());
throw;
}
}
}
}