Search code examples
httphttp-headersrestlethttp-getrestlet-2.0

how to change standard header value in restlets?


I have the following get:

@Get
public String represent(Variant variant) throws ResourceException
{
    String text = "returntext";

    text+="\r\n";
    return text;
}

The response from invoking this service:

CFG - HTTP/1.1 200 OK Accept-Ranges: bytes Content-Type: text/plain;charset=UTF-8 Date: Mon, 29 Jul 2013 19:59:37 GMT Server: Restlet-Framework/2.0.9 Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept Content-Length: 118 Connection: keep-alive

How do I change the connection header value to close ?

I think this maybe a restlet bug.


Solution

  • Whether the server closes the connection or not, depends on whether the client request asks for the connection to close or not.

    Here is a sample Server code:

    import org.restlet.data.Form;
    import org.restlet.data.MediaType;
    import org.restlet.data.Parameter;
    import org.restlet.resource.Get;
    import org.restlet.resource.ServerResource;
    import org.restlet.util.Series;
    
    public class TestRestlet extends ServerResource {
        @Get
        public String getImpl(){
            return "Sample Response Text\r\n";
        }
    }
    

    And here is what i got on linux commmand line (using only telnet): [Please note that the last line in request-header in each request is followed by 2 newlines] [To avoid any confusion, some of the requests do not contain request-body.]

    [root@mylinuxserver]# telnet 172.16.101.34 6060
    Trying 172.16.101.34...
    Connected to win7comp01 (172.16.101.34).
    Escape character is '^]'.
    GET /TestRestlet HTTP/1.1
    Host: 172.16.101.34:6060
    
    HTTP/1.1 200 OK
    Set-Cookie: JSESSIONID=C2E77F4D0437E525A0FC66498EF09E8B; Path=/hotelSoft
    Date: Wed, 31 Jul 2013 08:25:44 GMT
    Accept-Ranges: bytes
    Server: Restlet-Framework/2.0.15
    Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
    Content-Type: application/json;charset=UTF-8
    Content-Length: 22
    
    Sample Response Text
    
    GET /TestRestlet HTTP/1.1
    Host: 172.16.101.34:6060
    Connection: Keep-Alive
    
    HTTP/1.1 200 OK
    Set-Cookie: JSESSIONID=1873DE26443F5DF62379B895AEA0F004; Path=/hotelSoft
    Date: Wed, 31 Jul 2013 08:25:48 GMT
    Accept-Ranges: bytes
    Server: Restlet-Framework/2.0.15
    Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
    Content-Type: application/json;charset=UTF-8
    Content-Length: 22
    
    Sample Response Text
    
    GET /TestRestlet HTTP/1.1
    Host: 172.16.101.34:6060
    Connection: close
    
    HTTP/1.1 200 OK
    Set-Cookie: JSESSIONID=43EC7C9AACC6C0CEF6FAC8F608B1D79C; Path=/hotelSoft
    Date: Wed, 31 Jul 2013 08:25:57 GMT
    Accept-Ranges: bytes
    Server: Restlet-Framework/2.0.15
    Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
    Content-Type: application/json;charset=UTF-8
    Content-Length: 22
    Connection: close
    
    Sample Response Text
    Connection closed by foreign host.
    [root@mylinuxserver]# telnet 172.16.101.34 6060
    Trying 172.16.101.34...
    Connected to win7comp01 (172.16.101.34).
    Escape character is '^]'.
    GET /TestRestlet HTTP/1.0
    
    HTTP/1.1 200 OK
    Set-Cookie: JSESSIONID=2C879A91F2501DD9D3B39EF50C3F46CA; Path=/hotelSoft
    Date: Wed, 31 Jul 2013 08:26:09 GMT
    Accept-Ranges: bytes
    Server: Restlet-Framework/2.0.15
    Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
    Content-Type: application/json;charset=UTF-8
    Content-Length: 22
    Connection: close
    
    Sample Response Text
    Connection closed by foreign host.
    [root@mylinuxserver]# telnet 172.16.101.34 6060
    Trying 172.16.101.34...
    Connected to win7comp01 (172.16.101.34).
    Escape character is '^]'.
    GET /TestRestlet
    
    Sample Response Text
    Connection closed by foreign host.
    [root@mylinuxserver]#
    

    In the above examples, several types of HTTP connections are made.

    The response to the 1st request:

    GET /TestRestlet HTTP/1.1
    Host: 172.16.101.34:6060
    

    [Note: the line Host: 172.16.101.34:6060 is followed by 2 \r\n: \r\n\r\n]

    is:

    HTTP/1.1 200 OK
    Set-Cookie: JSESSIONID=C2E77F4D0437E525A0FC66498EF09E8B; Path=/hotelSoft
    Date: Wed, 31 Jul 2013 08:25:44 GMT
    Accept-Ranges: bytes
    Server: Restlet-Framework/2.0.15
    Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
    Content-Type: application/json;charset=UTF-8
    Content-Length: 22
    
    Sample Response Text
    

    The connection is not closed yet, and we send another request:

    GET /TestRestlet HTTP/1.1
    Host: 172.16.101.34:6060
    Connection: Keep-Alive
    

    That gets the response:

    HTTP/1.1 200 OK
    Set-Cookie: JSESSIONID=1873DE26443F5DF62379B895AEA0F004; Path=/hotelSoft
    Date: Wed, 31 Jul 2013 08:25:48 GMT
    Accept-Ranges: bytes
    Server: Restlet-Framework/2.0.15
    Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
    Content-Type: application/json;charset=UTF-8
    Content-Length: 22
    

    Still the connection is not closed.

    However on the 3rd request:

    GET /TestRestlet HTTP/1.1
    Host: 172.16.101.34:6060
    Connection: close
    

    The connection is closed, because the request contains Connection: close header. You can see the telnet exits after displaying a message: Connection closed by foreign host.

    There are 2 more sample request-response in the above given examples:

    1.An HTTP 1.0 request:

    GET /TestRestlet HTTP/1.0
    

    With response:

    HTTP/1.1 200 OK
    Set-Cookie: JSESSIONID=2C879A91F2501DD9D3B39EF50C3F46CA; Path=/hotelSoft
    Date: Wed, 31 Jul 2013 08:26:09 GMT
    Accept-Ranges: bytes
    Server: Restlet-Framework/2.0.15
    Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
    Content-Type: application/json;charset=UTF-8
    Content-Length: 22
    Connection: close
    
    Sample Response Text
    

    And the telnet exits after displaying: Connection closed by foreign host.

    2.A request without HTTP version mentioned:

    GET /TestRestlet
    

    And response is: (Without headers)

    Sample Response Text
    

    And the telnet exits with a message: Connection closed by foreign host.

    Conclusion:

    Whatever is your client / client-program , make it send an HTTP-1.0 request , or a HTTP-1.1 request with Connection: close header.

    In Java, you achieve this by:

    import java.net.HttpURLConnection;
    import java.net.URL;
    .
    .
    .
    HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://....").openConnection();
    httpURLConnection.setRequestProperty("Connection","close");
    // rest of the code here....
    

    Also check if a statement like this:

    httpURLConnection.disconnect();
    

    can help you disconnect the connection.