0

I got a request from customer to create a service for traffic control of WMS service. The idea is:

  1. a company buys some amount of data from WMS provider;
  2. the company shares this data among its clients ;
  3. the service should track how much traffic and requests have been consumed.

So I need some kind of proxy which passes requests to a target WMS services and meanwhile logs passed traffic.

The problem is that I have no clue how to start and where to start from. I will appreciate any idea, concept or so.

Thanks in advance!

Eduard Lepner
  • 639
  • 6
  • 17

1 Answers1

2

Here is how I implemented proxy that passes requests to GeoServer WMS service and returns the map image. I use Web API, NET 4.5. You can customize my example with logging and authorization.

    public async Task<HttpResponseMessage> GetMapAsync()
    {
        HttpClient client = new HttpClient();

        WmsLayer layer = _unitOfWork.LayerRepository.GetById(model.LayerId) as WmsLayer;

        // This is typical WMS request url pointing to server behind the firewall, 
        // something like http://localhost:8081/geoserver/data/wms?service=WMS&version=1.1.0&request=GetMap&layers=data:layer1&styles=&bbox=337098.1084,4972868.3433,368833.6,5014800.3417&width=387&height=512&srs=EPSG:3765&format=image%2Fpng%3B+mode%3D8bit
        string url = layer.GetWmsRequestUrl(layer.Url, model.SRS, model.Width, model.Height, model.BBox);

        // See http://developer.greenbutton.com/downloading-large-files-with-the-net-httpclient/
        var responseMessage = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);

        var response = Request.CreateResponse();

        response.Content = new PushStreamContent(
            async (outputStream, httpContent, transportContext) =>
            {
                var buffer = new byte[65536];
                using (var httpStream = await responseMessage.Content.ReadAsStreamAsync())
                {
                    var bytesRead = 1;
                    while ((bytesRead = httpStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        await outputStream.WriteAsync(buffer, 0, bytesRead);
                    }
                }
                outputStream.Close();
            }
            , new MediaTypeHeaderValue("image/png"));

        return response;
    }
Zeljko Vujaklija
  • 500
  • 4
  • 12