简体   繁体   English

在Restlet Servlet端删除了Restlet Client在Request中设置的Cookie

[英]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 { 我有一个测试客户端,它发出如下的Restlet请求: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. 在服务器端,我想从请求中读取这些cookie,然后将它们发送回调用客户端,以基于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. 但是我无法检索cookie:我在org.restlet.ext.servlet.ServerServlet的服务方法中设置了调试器,并且该请求没有cookie。

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.? 这是在请求中设置Cookie的正确方法吗?

After some hit and trials by using the following code I am able to retrieve cookies at server side. 通过使用以下代码进行一些尝试后,我能够在服务器端检索cookie。 The documentation of Restlet says ClientResource internally calls Client. Restlet的文档说ClientResource内部调用Client。 Is there a way the same can be achieved using ClientResource by setting some options ?. 有没有一种方法可以通过设置一些选项使用ClientResource来实现? 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. 我想将ClientResource用作我计划引入更改的大多数代码,并使用ClientResource,所有示例源代码也都使用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());
}

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. 我使用以下Maven配置对Restlet 2.3.1进行了一些测试,并且您的用例在使用独立Restlet引擎的情况下可以正常工作。 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. 您会注意到,您需要显式添加cookie来进行响应,以将ml发送回客户端。

Here is the Maven configuration file: 这是Maven配置文件:

<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. 使用命令mvn eclipse:eclipse将其转换为Eclipse Java项目或mvn package以构建相应的jar文件。

You can find a sample project here: https://github.com/templth/restlet-stackoverflow/tree/master/restlet/test-restlet-cookies . 您可以在此处找到示例项目: https : //github.com/templth/restlet-stackoverflow/tree/master/restlet/test-restlet-cookies

It seems that you use the servlet extension of Restlet. 似乎您使用了Restlet的servlet扩展。 I also did a test for a Restlet application embedded in a servlet container and it works for me. 我还对嵌入在servlet容器中的Restlet应用程序进行了测试,它对我有用。 I use the following Maven file: 我使用以下Maven文件:

<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. 使用命令mvn eclipse:eclipse将其转换为Eclipse Java项目或mvn package以构建相应的war文件。 You can notice that the Eclipse project is WTP compliant so you can attach to a server within Eclipse. 您会注意到Eclipse项目是WTP兼容的,因此您可以将其连接到Eclipse中的服务器。

You can find a sample project here: https://github.com/templth/restlet-stackoverflow/tree/master/restlet/test-restlet-cookies-servlet . 您可以在此处找到示例项目: https : //github.com/templth/restlet-stackoverflow/tree/master/restlet/test-restlet-cookies-servlet

Hope it will help you, Thierry 希望对您有帮助,蒂埃里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM