Reputation: 5
I'm working on a simple Jersey client app that does a GET request, stores the cookies, then opens the URL in WebView (JavaFX). I have this working when I don't set my connector which is ApacheConnectorProvider(). When I comment that one line out the CookieManager stores my cookies but when I uncomment the line I get no cookies in my CookieManager. Does anyone know why this is?
My code is as follows >
import java.io.IOException;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.ProtocolException;
import java.net.URI;
import java.net.URL;
import java.util.List;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import com.ftl.destroyer.ClientHelperJersey;
public class TestCookiesJersey extends Application {
private URL url = null;
private ClientHelperJersey clientHelper;
final URI proxySettings = null;
public TestCookiesJersey() {
this.clientHelper = new ClientHelperJersey(proxySettings, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36");
}
public void startProcess(String query) throws IOException {
url = new URL("http://www.pacsun.com/");
// GET request to server. goto first page.
jerseyGETRequest(url);
// display cookies.
for (HttpCookie cookie : this.clientHelper.getCm().getCookieStore().getCookies()) {
System.out.println(cookie.getName() + " " + cookie.getValue());
}
//
CookieStore cookieJar = this.clientHelper.getCm().getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
for (HttpCookie cookie : cookies) {
System.out.println("CookieHandler retrieved cookie: " + cookie);
}
launch();
}
public void jerseyGETRequest(URL url) throws IOException, ProtocolException {
// build the GET request invocation.
final Invocation invocation = this.clientHelper.addHeaders(this.clientHelper.getClient().target(url.toString()).request(), false).header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8").header("Accept-Language", "en-US,en;q=0.8,da;q=0.6").header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8").property("jersey.config.client.readTimeout", 15000).buildGet();
final Response response = invocation.invoke();
response.getCookies();
}
public void start(Stage stage) {
stage.setTitle("TestCookiesJersey");
BorderPane borderPane = new BorderPane();
WebView webviewBrowser = new WebView();
borderPane.setCenter(webviewBrowser);
final WebEngine engine = webviewBrowser.getEngine();
engine.load("http://www.pacsun.com/");
engine.setJavaScriptEnabled(true);
stage.setScene(new Scene(borderPane, 850, 600));
stage.show();
}
public static void main(String[] args) {
try {
TestCookiesJersey destroyer = new TestCookiesJersey();
destroyer.startProcess("");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
That's the main class, and then I use a helper class that sets up the Jersey Client and sets the ApacheConnector >
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Configuration;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.filter.LoggingFilter;
public class ClientHelperJersey {
public CookieManager cm = new CookieManager();
private final String userAgent;
private final Client client;
public ClientHelperJersey(URI proxySettings, String userAgent) {
CookieHandler.setDefault(cm);
this.cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
// below works, I can see the 1 cookie being set if I uncomment.
//HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
//cookie.setDomain("foo.com");
//this.cm.getCookieStore().add(null, cookie);
this.userAgent = userAgent;
ClientConfig config = new ClientConfig();
config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
config.property(ApacheClientProperties.DISABLE_COOKIES, false);
config.connectorProvider(new ApacheConnectorProvider());
config.register(new LoggingFilter());
config.register(new CookieFilter(this.cm));
ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig((Configuration) config);
this.client = clientBuilder.build();
}
public Client getClient() {
return this.client;
}
public CookieManager getCm() {
return this.cm;
}
public Invocation.Builder addHeaders(Invocation.Builder requestBuilder, boolean ajaxRequest) {
requestBuilder.header("User-Agent", this.userAgent);
return requestBuilder;
}
}
If I comment this one line out in ClientHelperJersey the cookies are saved by the CookieManager >
config.connectorProvider(new ApacheConnectorProvider());
Does anyone know how to use the ApacheConnectorProvider() so that I can save my cookies and load a URL in WebView that contains the cookies?
Upvotes: 0
Views: 2314
Reputation: 5
After some research I have a working solution. Essentially what I was trying to do was use the java.net.CookieManager
from my Jersey client (using an Apache connector) and load my cookies into a JavaFX WebView using the Jersey client'sjava.net.CookieManager
and java.net.CookieStore
.
Thanks to this article I learned that WebView will install it's own implementation of CookieHandler
and you shouldn't:
waste your time trying to use java.net.CookieManager, and java.net.CookieStore. They are likely to cause problems with many sites because they implement the wrong standard.
What I did was created a list of cookies and made sure to set them before I loaded my WebEngine
, but after I instantiated the WebView
.
Here's my solution:
public void start(Stage stage) throws IOException {
stage.setTitle("CookieManagerTest.");
BorderPane borderPane = new BorderPane();
WebView webviewBrowser = new WebView();
borderPane.setCenter(webviewBrowser);
URI uri = URI.create("http://www.pacsun.com/");
Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();
headers.put("Set-Cookie", Arrays.asList("cookie1=value1","cookie2=value2"));
java.net.CookieHandler.getDefault().put(uri, headers);
final WebEngine engine = webviewBrowser.getEngine();
engine.setJavaScriptEnabled(true);
engine.load(uri.toString());
stage.setScene(new Scene(borderPane, 850, 600));
stage.show();
Upvotes: 0