hims
hims

Reputation: 11

Cookies set in Request by Restlet Client removed at Restlet Servlet end

I have a Test Client which makes Restlet Request as below : public class TestRestlet {

public static void main(String[] args) throws IOException {
    CookieHandler.setDefault(new CookieManager( null, CookiePolicy.ACCEPT_ALL ));
    ClientResource resource = getClientResource();
    Representation rep = resource.get();
    System.out.println(rep.getText());

}

private static ClientResource getClientResource() {
    String resouceURL = "http://localhost:8080/ActivitiSampleProject/service/process-definitions?suspended=false";
    CookieSetting cookie1 = new CookieSetting("USER", "qdny6HjWY0HONvWoyufBWemrDE+5IcdsssssK0E8UGmu5RKPF7h0BWKvBPSn+Kucb82Aq");
    cookie1.setDomain(".abc.com");
    cookie1.setPath("/");
    cookie1.setMaxAge(-1);
    ClientResource resource = new ClientResource(resouceURL);
    resource.getRequest().getCookies().add(cookie1);
    return resource;
}

}

At server side I want to read these cookies from request and send them back to calling client to fetch some information based on the cookie.

But I am unable to retrieve the cookies : I have set my debugger at service method of org.restlet.ext.servlet.ServerServlet and the request has no cookies.

public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    .......
}

Is this the correct way to set cookies in request what am i doing wrong.?

After some hit and trials by using the following code I am able to retrieve cookies at server side. The documentation of Restlet says ClientResource internally calls Client. Is there a way the same can be achieved using ClientResource by setting some options ?. I want to use ClientResource as most of the code I plan to introduce a change to, uses ClientResource, also all example source code uses ClientResource.

public static void main(String[] args) throws IOException {
    Client c = new Client(Protocol.HTTP);
    Request request = new Request(Method.GET,
            "http://localhost:8080/ActivitiSampleProjectNonSpring/service/hello");
    request.getCookies().add("USER", "TESTCOOKIE");

    Response response = c.handle(request);
    Representation rep =  response.getEntity();
    System.out.println(rep.getText());
}

Upvotes: 1

Views: 1622

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202176

I did some tests with Restlet 2.3.1 with the following Maven configuration and your use case works on my side using standalone Restlet engine. I used the code you provided in your question. You can notice that you need to explicitly add cookies to response to send theml back to the client.

Here is the Maven configuration file:

<project (...)>
    <modelVersion>4.0.0</modelVersion>
    <groupId>stackoverflow</groupId>
    <artifactId>test-restlet-cookies</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java-version>1.7</java-version>
        <restlet-version>2.3.1</restlet-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Public online Restlet repository</name>
            <url>http://maven.restlet.com</url>
        </repository>
    </repositories>
</project>

Use the command mvn eclipse:eclipse to convert it into an Eclipse Java project or mvn package to build the corresponding jar file.

You can find a sample project here: https://github.com/templth/restlet-stackoverflow/tree/master/restlet/test-restlet-cookies.

It seems that you use the servlet extension of Restlet. I also did a test for a Restlet application embedded in a servlet container and it works for me. I use the following Maven file:

<project (...)>
    <modelVersion>4.0.0</modelVersion>
    <groupId>stackoverflow</groupId>
    <artifactId>test-restlet-cookies-servlet</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java-version>1.7</java-version>
        <restlet-version>2.3.1</restlet-version>
        <wtp-version>2.0</wtp-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>

        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.servlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Public online Restlet repository</name>
            <url>http://maven.restlet.com</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <wtpapplicationxml>true</wtpapplicationxml>
                    <wtpversion>${wtp-version}</wtpversion>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Use the command mvn eclipse:eclipse to convert it into an Eclipse Java project or mvn package to build the corresponding war file. You can notice that the Eclipse project is WTP compliant so you can attach to a server within Eclipse.

You can find a sample project here: https://github.com/templth/restlet-stackoverflow/tree/master/restlet/test-restlet-cookies-servlet.

Hope it will help you, Thierry

Upvotes: 1

Related Questions