0

I am using the GeoServer REST API(gsrcc) to add the following SLD XML string to GeoServer 2.13.0:

<StyledLayerDescriptor
    xmlns="http://www.opengis.net/sld"
    xmlns:ogc="http://www.opengis.net/ogc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1.0"
    xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd"
    xmlns:se="http://www.opengis.net/se">
    <NamedLayer>
        <se:Name>FDCWell</se:Name>
        <UserStyle>
            <se:Name>FDCWell</se:Name>
            <se:FeatureTypeStyle>
                <se:Rule>
                    <se:Name>Well</se:Name>
                    <se:Description>
                        <se:Title>Well</se:Title>
                    </se:Description>
                    <ogc:Filter
                        xmlns:ogc="http://www.opengis.net/ogc">
                        <ogc:PropertyIsEqualTo>
                            <ogc:PropertyName>Well</ogc:PropertyName>
                            <ogc:Literal>Well</ogc:Literal>
                        </ogc:PropertyIsEqualTo>
                    </ogc:Filter>
                    <se:PointSymbolizer>
                        <se:Graphic>
                            <se:ExternalGraphic>
                                <se:OnlineResource xlink:type="simple" xlink:href="https://well.png"/>
                                <se:Format>image/svg+xml</se:Format>
                            </se:ExternalGraphic>
                            <se:Size>20</se:Size>
                        </se:Graphic>
                    </se:PointSymbolizer>
                </se:Rule>
            </se:FeatureTypeStyle>
        </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>

SLD Location url is:

http://localhost:8080/geoserver/rest/styles/?name=FDCWell

RestClientCode:

  /**
 * Uploads an SLD to the Geoserver
 * 
 * @param stylename
 * @param sldString SLD-XML as String
 * @return <code>true</code> successfully uploaded
 */
public Boolean createSld(String stylename, String sldString) {
    return null != createSld_location(stylename, sldString);
}
/**
 * @param stylename
 * @param sldString
 * @return REST location URL string to the new style
 * @throws IOException
 */
public String createSld_location(String stylename, String sldString) {
    String location = sendRESTlocation(METHOD_POST, "/styles/" + "?name=" + stylename, sldString, "application/vnd.ogc.sld+xml", "application/vnd.ogc.sld+xml");
    return location;
}
/**
 * @param method e.g. 'POST', 'GET', 'PUT' or 'DELETE'
 * @param urlEncoded e.g. '/workspaces' or '/workspaces.xml'
 * @param contentType format of postData, e.g. null or 'text/xml'
 * @param accept format of response, e.g. null or 'text/xml'
 * @param postData e.g. xml data
 * @return null, or location field of the response header
 */
public String sendRESTlocation(String method, String urlEncoded, byte[] postData, String contentType, String accept) {
    HttpWebResponse resp = sendREST(method, urlEncoded, postData, contentType, accept);
    return resp.Headers["Location"];
}

public String sendRESTlocation(String method, String urlEncoded, String postData, String contentType, String accept) {
    byte[] bPostData = new byte[0];
    if (postData != null)
    {
        bPostData = UTF8Encoding.UTF8.GetBytes(postData);
    }
    return sendRESTlocation(method, urlEncoded, bPostData, contentType, accept);
}
private HttpWebResponse sendREST(String method, String urlAppend, byte[] postData, String contentType, String accept) {
    Boolean doOut = METHOD_DELETE != method;
    // boolean doIn = true; // !doOut

    HttpWebRequest req = GetGeoserverRestRequest(urlAppend);

    //WebRequest.ReadWriteTimeout, WebRequest.Timeout, 
    //req.ReadWriteTimeout = 50000;
    //req.Timeout = 50000;



    // uc.setDoInput(false);
    if (contentType != null && "" != contentType) {
        req.ContentType = contentType;
    }
    if (accept != null && "" != accept) {
        req.Accept = accept;
    }
    req.Method = method;

    if (hasAuthorization()) {
        String userPasswordEncoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
        req.Headers["Authorization"] = "Basic " + userPasswordEncoded;
    }

    req.ContentLength = postData.Length;
    HttpWebResponse resp = null;
    try
    {
        if (postData.Length > 0)
        {
            // Send the request:
            using (Stream post = req.GetRequestStream())
            {

                //RequestStream.WriteTimeout, and RequestStream.ReadTimeout
                //post.WriteTimeout = 50000;
                //post.ReadTimeout = 50000;

                post.Write(postData, 0, postData.Length);
            }
        }



        resp = (HttpWebResponse) req.GetResponse();
    }
    catch (WebException we)
    {
        HttpWebResponse errorResponse = we.Response as HttpWebResponse;
        if (errorResponse.StatusCode == HttpStatusCode.NotFound)
        {

        }
    }

    return resp;
    }

Inside sendREST() method at while getting HttpWebResponse getting the following Exception:

{System.Net.WebException: The remote server returned an error: (406) Not Acceptable. at System.Net.HttpWebRequest.GetResponse() at GeoServerRest.GeoServer.RestClient.sendREST(String method, String urlAppend, Byte[] postData, String contentType, String accept) in C:\Users\abc\Desktop\geo\GeoserverConsole2\GeoserverConsole\GeoserverConsole\GeoServer\RestClient.cs:line 151}

I am debugging from last 2 hours but still not getting where i committing mistake. Please any help to solve the Exception.

1

You are asking for a response format or language that is not supported:

From here:

The HyperText Transfer Protocol (HTTP) 406 Not Acceptable client error response code indicates that a response matching the list of acceptable values defined in Accept-Charset and Accept-Language cannot be served.

In reality, this error is very rarely used: instead of responding using this error code, which would be cryptic for the end user and difficult to fix, servers ignore the relevant header and serve an actual page to the user. It is assumed that even if the user won't be completely happy, they will prefer this to an error code.

If a server returns such an error status, the body of the message should contain the list of the available representations of the resources, allowing the user to manually choose among them.

I can't see where you set your file type but it should be Content-type: application/vnd.ogc.sld+xml and your end point should contain the name of the style you want to create, http://localhost:8080/geoserver/rest/styles/my_style.

The documentation has examples too.

If you are looking for a slightly less abandoned REST client look at GeoServer-Manager which at least post dates the Google code close down.

  • Above is not provided complete code. If you see @Ian above RestClient code comment I'm calling createSld() method from there inside createSld_location() method setting Content-type as application/vnd.ogc.sld+xml.But i'm not sure about Url is correct or not.Earlier it was working with Geoserver2.11. – Ram Jun 7 '18 at 20:07
  • I assume you've looked in the log file at GeoTools dev level – Ian Turton Jun 8 '18 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.