Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactoring of the cookie store

  • Loading branch information...
commit 559109b41632b2bf61a6a666ecaedf2fa6badd83 1 parent fe83939
@jbonofre authored
View
17 processor/src/main/java/org/apache/camel/processor/mashup/core/CookieStore.java
@@ -1,16 +1,16 @@
package org.apache.camel.processor.mashup.core;
+import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
/**
* Singleton to share a cookie store.
*/
public class CookieStore {
- private Map<String, org.apache.http.cookie.Cookie> map;
+ private Map<String, List<Cookie>> map;
private static CookieStore _singleton = null;
@@ -22,14 +22,19 @@ public static CookieStore getInstance() {
}
private CookieStore() {
- map = new HashMap<String, org.apache.http.cookie.Cookie>();
+ map = new HashMap<String, List<org.apache.http.cookie.Cookie>>();
}
public void addCookie(String key, org.apache.http.cookie.Cookie cookie) {
- map.put(key, cookie);
+ List<org.apache.http.cookie.Cookie> cookies = this.getCookies(key);
+ if (cookies == null) {
+ cookies = new LinkedList<org.apache.http.cookie.Cookie>();
+ }
+ cookies.add(cookie);
+ map.put(key, cookies);
}
- public org.apache.http.cookie.Cookie getCookie(String key) {
+ public List<org.apache.http.cookie.Cookie> getCookies(String key) {
return map.get(key);
}
View
96 processor/src/main/java/org/apache/camel/processor/mashup/core/MashupProcessor.java
@@ -5,30 +5,24 @@
import org.apache.camel.Processor;
import org.apache.camel.processor.mashup.api.IExtractor;
import org.apache.camel.processor.mashup.model.*;
-import org.apache.commons.beanutils.*;
-import org.apache.http.*;
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.auth.params.AuthPNames;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.client.params.AuthPolicy;
-import org.apache.http.client.utils.URIUtils;
-import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
-import org.apache.http.conn.params.ConnRouteParamBean;
import org.apache.http.impl.auth.BasicSchemeFactory;
import org.apache.http.impl.auth.NTLMSchemeFactory;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
-import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
@@ -42,39 +36,39 @@
* Camel processor loading the navigation file and extract data
*/
public class MashupProcessor implements Processor {
-
+
private final static transient Logger LOGGER = LoggerFactory.getLogger(MashupProcessor.class);
-
+
public final static String DEFAULT_STORE = "data/mashup";
public final static String MASHUP_ID_HEADER = "MASHUP_ID";
public final static String MASHUP_STORE_HEADER = "MASHUP_STORE";
-
+
public void process(Exchange exchange) throws Exception {
LOGGER.trace("Get the Camel in message");
Message in = exchange.getIn();
-
+
LOGGER.trace("Get the Camel out message");
Message out = exchange.getOut();
-
+
LOGGER.trace("Get the {} header", MASHUP_ID_HEADER);
String mashupId = (String) in.getHeader(MASHUP_ID_HEADER);
LOGGER.debug("Mashup ID: {}", mashupId);
-
+
LOGGER.trace("Get the {} header", MASHUP_STORE_HEADER);
String store = (String) in.getHeader(MASHUP_STORE_HEADER);
LOGGER.debug("Mashup Store: {}", store);
-
+
LOGGER.debug("Digesting the navigation file {}/{}.xml", store, mashupId);
FileInputStream fileInputStream = new FileInputStream(store + "/" + mashupId + ".xml");
Mashup mashup = new Mashup();
mashup.digeste(fileInputStream);
-
+
LOGGER.trace("Create the HTTP client");
DefaultHttpClient httpClient = new DefaultHttpClient();
// ClientConnectionManager mgr = httpClient.getConnectionManager();
// HttpParams params = httpClient.getParams();
// httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);
-
+
if (mashup.getProxy() != null) {
LOGGER.trace("Registering the HTTP client proxy");
@@ -150,21 +144,21 @@ public void process(Exchange exchange) throws Exception {
request = new HttpGet(url);
}
- if (mashup.getCookie() != null) {
- LOGGER.trace("Looking for an existing cookie");
- String cookieKey = (String) in.getHeader(mashup.getCookie().getKey());
+ if (mashup.getCookieStore() != null) {
+ LOGGER.trace("Looking cookies in the cookie store");
+ String cookieKey = mashup.getCookieStore().getKey();
if (cookieKey == null) {
- LOGGER.warn("Cookie key " + mashup.getCookie().getKey() + " is not found in the Camel \"in\" header");
- } else {
- org.apache.http.cookie.Cookie storedCookie = cookieStore.getCookie(cookieKey);
- if (storedCookie == null) {
- LOGGER.debug("No cookie yet exist for " + cookieKey);
- } else {
- LOGGER.debug("A cookie exists for " + cookieKey + " use it for the request");
- BasicCookieStore basicCookieStore = new BasicCookieStore();
- basicCookieStore.addCookie(storedCookie);
- httpClient.setCookieStore(basicCookieStore);
- }
+ throw new IllegalArgumentException("CookieStore key is mandatory");
+ }
+ for (String header : in.getHeaders().keySet()) {
+ cookieKey = cookieKey.replace("%" + header + "%", in.getHeader(header).toString());
+ }
+ List<org.apache.http.cookie.Cookie> storedCookies = cookieStore.getCookies(cookieKey);
+ for (org.apache.http.cookie.Cookie cookie : storedCookies) {
+ LOGGER.trace("Adding cookie " + cookie.getName() + " in the HTTP client");
+ BasicCookieStore basicCookieStore = new BasicCookieStore();
+ basicCookieStore.addCookie(cookie);
+ httpClient.setCookieStore(basicCookieStore);
}
} else {
LOGGER.warn("No cookie configuration defined");
@@ -172,22 +166,21 @@ public void process(Exchange exchange) throws Exception {
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
-
- if (mashup.getCookie() != null) {
- String cookieKey = (String) in.getHeader(mashup.getCookie().getKey());
+
+ if (mashup.getCookieStore() != null) {
+ String cookieKey = mashup.getCookieStore().getKey();
if (cookieKey == null) {
- LOGGER.warn("Cookie key " + mashup.getCookie().getKey() + " is not found in the Camel \"in\" header");
- } else {
- LOGGER.trace("Populating the cookie store");
- List<org.apache.http.cookie.Cookie> cookies = httpClient.getCookieStore().getCookies();
- for (org.apache.http.cookie.Cookie cookie : cookies) {
- if (cookie.getName().equals(mashup.getCookie().getName())
- && cookie.getDomain().equals(mashup.getCookie().getDomain())
- && cookie.getPath().equals(mashup.getCookie().getPath())) {
- LOGGER.debug("Storing cookie " + cookie.getName() + " = " + cookie.getValue());
- cookieStore.addCookie(cookieKey, cookie);
- }
- }
+ throw new IllegalArgumentException("CookieStore key is mandatory");
+ }
+ for (String header : in.getHeaders().keySet()) {
+ cookieKey = cookieKey.replace("%" + header + "%", in.getHeader(header).toString());
+ }
+ LOGGER.trace("Populating the cookie store");
+ List<org.apache.http.cookie.Cookie> cookies = httpClient.getCookieStore().getCookies();
+
+ for (org.apache.http.cookie.Cookie cookie : cookies) {
+ LOGGER.trace("Storing cookie " + cookie.getName());
+ cookieStore.addCookie(cookieKey, cookie);
}
}
@@ -211,7 +204,7 @@ public void process(Exchange exchange) throws Exception {
}
} catch (Exception e) {
e.printStackTrace();
- LOGGER.warn("An exception occurs during the extraction",e);
+ LOGGER.warn("An exception occurs during the extraction", e);
LOGGER.warn("Calling the error handler");
exchange.setException(e);
out.setFault(true);
@@ -238,11 +231,12 @@ public void process(Exchange exchange) throws Exception {
}
}
+
}
/**
* Create a new instance of a extractor
- *
+ *
* @param extractor the extractor model object.
* @return the IExtractor object.
*/
View
42 processor/src/main/java/org/apache/camel/processor/mashup/model/Cookie.java
@@ -1,42 +0,0 @@
-package org.apache.camel.processor.mashup.model;
-
-public class Cookie {
-
- private String key;
- private String name;
- private String domain;
- private String path;
-
- public String getKey() {
- return key;
- }
-
- public void setKey(String key) {
- this.key = key;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getDomain() {
- return domain;
- }
-
- public void setDomain(String domain) {
- this.domain = domain;
- }
-
- public String getPath() {
- return path;
- }
-
- public void setPath(String path) {
- this.path = path;
- }
-
-}
View
15 processor/src/main/java/org/apache/camel/processor/mashup/model/CookieStore.java
@@ -0,0 +1,15 @@
+package org.apache.camel.processor.mashup.model;
+
+public class CookieStore {
+
+ private String key;
+
+ public String getKey() {
+ return key;
+ }
+
+ public void setKey(String key) {
+ this.key = key;
+ }
+
+}
View
16 processor/src/main/java/org/apache/camel/processor/mashup/model/Mashup.java
@@ -9,7 +9,7 @@
public class Mashup {
private String id;
- private Cookie cookie;
+ private CookieStore cookieStore;
private Proxy proxy;
private List<Page> pages = new LinkedList<Page>();
@@ -21,12 +21,12 @@ public void setId(String id) {
this.id = id;
}
- public Cookie getCookie() {
- return cookie;
+ public CookieStore getCookieStore() {
+ return cookieStore;
}
- public void setCookie(Cookie cookie) {
- this.cookie = cookie;
+ public void setCookieStore(CookieStore cookieStore) {
+ this.cookieStore = cookieStore;
}
public Proxy getProxy() {
@@ -56,9 +56,9 @@ public void digeste(InputStream inputStream) throws Exception {
digester.addSetProperties("mashup");
- digester.addObjectCreate("mashup/cookie", Cookie.class);
- digester.addSetProperties("mashup/cookie");
- digester.addSetNext("mashup/cookie", "setCookie");
+ digester.addObjectCreate("mashup/cookiestore", CookieStore.class);
+ digester.addSetProperties("mashup/cookiestore");
+ digester.addSetNext("mashup/cookiestore", "setCookieStore");
digester.addObjectCreate("mashup/proxy", Proxy.class);
digester.addSetProperties("mashup/proxy");
View
5 processor/src/test/java/org/apache/camel/processor/mashup/model/TestDigester.java
@@ -22,9 +22,8 @@ public void digeste() throws Exception {
assertEquals("test", mashup.getId());
// get the cookies
- Cookie cookie = mashup.getCookie();
- assertNotNull(cookie);
- assertEquals("JSESSIONID", cookie.getName());
+ CookieStore cookieStore = mashup.getCookieStore();
+ assertNotNull(cookieStore);
// get the proxy
Proxy proxy = mashup.getProxy();
View
2  processor/src/test/resources/model/valid.xml
@@ -3,7 +3,7 @@
<proxy host="192.168.1.1" port="80" username="user" password="password" scheme="NTLM"/>
- <cookie key="LOGIN" name="JSESSIONID" domain="mydomain.com" path="/"/>
+ <cookiestore key="LOGIN"/>
<page url="http://www.example.org/page1" method="POST">
View
7 src/site/xdoc/configuration.xml
@@ -15,7 +15,7 @@
&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br/>
&lt;mashup id="test"&gt;<br/>
-&nbsp; &lt;cookie key="LOGIN" name="JSESSIONID" domain="mydomain.com" path="/"/&gt;<br/>
+&nbsp; &lt;cookiestore key="%LOGIN%"/&gt;<br/>
&nbsp; &lt;proxy scheme="NTLM" host="ip" port="80" username="user" password="password" workstation="workstation" domain="domain"/&gt;<br/>
@@ -47,8 +47,9 @@
</code>
</p>
<p>
- The cookie tag allows you to configure the cookie store. Camel Mashup is able to store cookies and map
- a cookie to a Camel header.
+ The cookiestore tag allows you to configure a cookie store. Camel Mashup is able to store cookies and map
+ a cookie to a Camel header (using %HEADER% notation). All HTTP client cookies are stored in the store and
+ injected in the next iteration.
</p>
<p>
The proxy tag allows you to configure a HTTP proxy. It supports both Basic and NTLM authentication.
Please sign in to comment.
Something went wrong with that request. Please try again.