Following information at this link in the section on creation of layer groups, I've created the method(C#):
public bool AddToLayerGroup(string layerGroupName, string layerName)
{
try
{
string gUrl = GEOSERVER_HOST + "/rest/layergroups";
WebRequest request = WebRequest.Create(gUrl);
request.ContentType = "application/xml";
request.Method = "POST";
request.Credentials = new NetworkCredential(GEOSERVER_USER, GEOSERVER_PASSWD);
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(GetAddToLayerGroupXml(layerGroupName, layerName));
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
WebResponse response = request.GetResponse();
return true;
}
catch (Exception)
{
return false;
}
}
When I run a unit test against this method, I get an Internal Server Error
reported from GeoServer if I use POST
HTTP method.
Now the layerGroupName
and layerName
used in the above method are already existing in GeoServer. What I want to achieve is provide a way to create a layer group if it doesn't exist, or just use an existing one. The layerName
always already exists in GeoServer, as I'm using a separate method to create them.
Can anyone spot any problem with my code above. Or, suggest an alternative approach. cURL or Java or C# examples are welcome. Thanks in advance.
UPDATE: From Tomcat logs, GeoServer complains that the layer group I've specified already exists. Is there away to add a layer to an existing layer group using the REST API?